run method

Future<int> run(
  1. List<String> args, {
  2. String? workingDirectory,
})

Executes the CLI with args.

Implementation

Future<int> run(List<String> args, {String? workingDirectory}) async {
  final ArgParser parser = _parser();
  late final ArgResults root;
  try {
    root = parser.parse(args);
  } on FormatException catch (error) {
    _stderr('Invalid usage: ${error.message}');
    _stderr(parser.usage);
    return usageError;
  }

  if ((root['help'] as bool) || root.command == null) {
    _printRootHelp(parser);
    return success;
  }

  final ArgResults command = root.command!;
  if ((command['help'] as bool? ?? false) == true) {
    _stdout(parser.commands[command.name]!.usage);
    return success;
  }

  final String projectRoot = workingDirectory ?? Directory.current.path;
  final ScanService scanService = ScanService(
    configService: ConfigService(_fileSystem),
    pubspecService: PubspecService(_fileSystem),
    depsService: DepsService(_processRunner, _fileSystem),
    graphService: const GraphService(),
    pubApiService: OnlinePubApiService(
      _httpClient,
      CacheService(_fileSystem),
      p.join(projectRoot, '.dep_sherpa', 'cache'),
    ),
    githubService: OnlineGithubService(
      _httpClient,
      CacheService(_fileSystem),
      p.join(projectRoot, '.dep_sherpa', 'cache'),
    ),
    scoringService: const ScoringService(),
    fileSystem: _fileSystem,
  );

  try {
    switch (command.name) {
      case 'scan':
        return await _scan(command, scanService, projectRoot);
      case 'rank':
        return await _rank(command, scanService, projectRoot);
      case 'graph':
        return await _graph(command, scanService, projectRoot);
      case 'diff':
        return _diff(command);
      case 'explain':
        return _explain(command, projectRoot);
      default:
        _stderr('Unknown command: ${command.name}');
        return usageError;
    }
  } catch (error) {
    _stderr(error.toString());
    return analysisFailure;
  }
}