run method
Runs this command.
The return value is wrapped in a Future if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<int> run() async {
final directory = context.targetDirectory;
if (!File(p.join(directory, 'pubspec.yaml')).existsSync()) {
throw DpkException(
'No pubspec.yaml in ${displayPath(directory)}. Run dpk init in a '
'Dart package or workspace root.',
);
}
final workspace = Workspace.discover(directory);
if (workspace != null && workspace.current != workspace.root) {
throw DpkException(
'${displayPath(directory)} is a workspace package. Run dpk init at '
'the workspace root, ${displayPath(workspace.root.path)}. A workspace '
"package's own dpk.yaml may only add scripts.",
);
}
final constraintText = argResults!.option('version-constraint')!;
try {
VersionConstraint.parse(constraintText);
} on FormatException {
throw DpkException(
'"$constraintText" is not a version constraint. Use one like ^1.0.0.',
);
}
final configFile = File(p.join(directory, kConfigFileName));
if (configFile.existsSync() && !argResults!.flag('force')) {
throw DpkException(
'${displayPath(configFile.path)} already exists. Run with --force to '
'overwrite it.',
);
}
final projectMode = argResults!.option('mode') == 'project';
configFile.writeAsStringSync(
[
'# yaml-language-server: \$schema=$schemaUrl',
'version: $constraintText',
if (projectMode) ...['', 'mode: project'],
'',
'scripts:',
' analyze:',
' command: dart analyze',
' description: Analyze the code.',
' test:',
' command: dart test',
' description: Run the tests.',
'',
].join('\n'),
);
console.info('Created ${displayPath(configFile.path)}');
if (projectMode) {
_ignoreProjectCache(directory);
}
console.info(
'\nNext steps:\n'
' dpk get Get dependencies.\n'
' dpk run List the scripts.\n'
' dpk run test Run a script.',
);
return 0;
}