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 resolvedCacheDir = resolveCacheDir(
options.globalPatchOptions.cacheDir,
);
final cacheDir = Directory(resolvedCacheDir);
if (!cacheDir.existsSync()) {
stderr.writeln(
'$resolvedCacheDir 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(
resolvedCacheDir,
).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: resolvedCacheDir);
await dotGitDir.delete(recursive: true);
}
await Process.run('git', ['init'], workingDirectory: resolvedCacheDir);
await Process.run('git', ['add', '.'], workingDirectory: resolvedCacheDir);
await Process.run('git', [
'commit',
'-m',
'"Patch initialized"',
], workingDirectory: resolvedCacheDir);
stdout.writeln('Patch initialized');
return 0;
}