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 {
if (!isProjectMode) {
stderr.writeln('Project cache is not supported for global mode');
return 1;
}
final options = PatchOptions.fromArgResults(argResults!);
final cacheDir = Directory(options.globalPatchOptions.cacheDir);
if (!cacheDir.existsSync()) {
stderr.writeln(
'${options.globalPatchOptions.cacheDir} does not exist. Did you run `$kExecutableName pub get`?',
);
return 1;
}
if (!(await gitExists())) {
stderr.writeln('Git is not installed');
return 1;
}
// dart format off
final dotGitDir = Directory(
options.globalPatchOptions.cacheDir,
).listSync().firstWhereOrNull((entity) => basename(entity.path) == '.git')
as Directory?;
// dart format on
final dotGitExists = dotGitDir != null;
if (dotGitExists && !options.force) {
stderr.writeln(
'Patch is already initialized. Run with --force to initialize from scratch',
);
return 1;
}
if (dotGitExists && options.force) {
await Process.run(
'git',
[
'checkout',
'.',
],
workingDirectory: options.globalPatchOptions.cacheDir);
await dotGitDir.delete(recursive: true);
}
await Process.run(
'git',
[
'init',
],
workingDirectory: options.globalPatchOptions.cacheDir);
await Process.run(
'git',
[
'add',
'.',
],
workingDirectory: options.globalPatchOptions.cacheDir);
await Process.run(
'git',
[
'commit',
'-m',
'"Patch initialized"',
],
workingDirectory: options.globalPatchOptions.cacheDir);
stdout.writeln('Patch initialized');
return 0;
}