|
| 1 | +// Copyright 2026 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:args/command_runner.dart'; |
| 9 | +import 'package:cli_util/cli_logging.dart'; |
| 10 | +import 'package:io/io.dart'; |
| 11 | +import 'package:meta/meta.dart'; |
| 12 | +import 'package:path/path.dart' as path; |
| 13 | + |
| 14 | +import '../model.dart'; |
| 15 | +import '../utils.dart'; |
| 16 | + |
| 17 | +class PresubmitCommand extends Command { |
| 18 | + PresubmitCommand({@visibleForTesting this.processManager}) { |
| 19 | + argParser.addFlag( |
| 20 | + 'fix', |
| 21 | + help: 'Apply dart fixes and formatting.', |
| 22 | + defaultsTo: false, |
| 23 | + negatable: false, |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + ProcessManager? processManager; |
| 28 | + |
| 29 | + @override |
| 30 | + String get name => 'presubmit'; |
| 31 | + |
| 32 | + @override |
| 33 | + String get description => |
| 34 | + 'Run repo checks, analysis, fix, and format on all packages.'; |
| 35 | + |
| 36 | + @override |
| 37 | + Future run() async { |
| 38 | + final log = Logger.standard(); |
| 39 | + final repo = DevToolsRepo.getInstance(); |
| 40 | + final pm = processManager ?? ProcessManager(); |
| 41 | + final fix = argResults!['fix'] as bool; |
| 42 | + |
| 43 | + log.stdout('Running pub get...'); |
| 44 | + final pubGetResult = await runner?.run(['pub-get']); |
| 45 | + if (pubGetResult is int && pubGetResult != 0) { |
| 46 | + log.stderr('Pub get failed. Exiting early.'); |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + final packages = repo.getPackages(includeSubdirectories: false); |
| 51 | + int failureCount = 0; |
| 52 | + |
| 53 | + if (fix) { |
| 54 | + log.stdout('Running Dart Fix and Format...'); |
| 55 | + for (final p in packages) { |
| 56 | + if (!p.hasAnyDartCode) continue; |
| 57 | + |
| 58 | + final progress = log.progress(' ${p.relativePath}'); |
| 59 | + |
| 60 | + final fixProcess = await pm.runProcess( |
| 61 | + CliCommand.dart(['fix', '--apply'], throwOnException: false), |
| 62 | + workingDirectory: p.packagePath, |
| 63 | + ); |
| 64 | + |
| 65 | + final pathsToFormat = _getPathsToFormat(p); |
| 66 | + |
| 67 | + final formatProcess = await pm.runProcess( |
| 68 | + CliCommand.dart(['format', ...pathsToFormat], throwOnException: false), |
| 69 | + workingDirectory: p.packagePath, |
| 70 | + ); |
| 71 | + |
| 72 | + if (fixProcess.exitCode == 0 && formatProcess.exitCode == 0) { |
| 73 | + progress.finish(showTiming: true); |
| 74 | + } else { |
| 75 | + failureCount++; |
| 76 | + progress.finish(message: 'failed'); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (failureCount > 0) { |
| 81 | + log.stderr('Presubmit failed.'); |
| 82 | + log.stderr(' Fix or Format failed on $failureCount packages.'); |
| 83 | + return 1; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + log.stdout('Running Repo Check...'); |
| 88 | + final repoCheckResult = await runner?.run(['repo-check']); |
| 89 | + if (repoCheckResult is int && repoCheckResult != 0) { |
| 90 | + log.stderr('Repo checks failed. Exiting early.'); |
| 91 | + return 1; |
| 92 | + } |
| 93 | + |
| 94 | + log.stdout('Running Analyze...'); |
| 95 | + final analyzeResult = await runner?.run(['analyze']); |
| 96 | + if (analyzeResult is int && analyzeResult != 0) { |
| 97 | + log.stderr('Analysis failed. Exiting early.'); |
| 98 | + return 1; |
| 99 | + } |
| 100 | + |
| 101 | + if (!fix) { |
| 102 | + log.stdout('Running Dart Format Check...'); |
| 103 | + for (final p in packages) { |
| 104 | + if (!p.hasAnyDartCode) continue; |
| 105 | + |
| 106 | + final progress = log.progress(' ${p.relativePath}'); |
| 107 | + |
| 108 | + final pathsToFormat = _getPathsToFormat(p); |
| 109 | + |
| 110 | + final formatProcess = await pm.runProcess( |
| 111 | + CliCommand.dart( |
| 112 | + ['format', '--output=none', '--set-exit-if-changed', ...pathsToFormat], |
| 113 | + throwOnException: false, |
| 114 | + ), |
| 115 | + workingDirectory: p.packagePath, |
| 116 | + ); |
| 117 | + |
| 118 | + if (formatProcess.exitCode == 0) { |
| 119 | + progress.finish(showTiming: true); |
| 120 | + } else { |
| 121 | + failureCount++; |
| 122 | + progress.finish(message: 'failed'); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (failureCount > 0) { |
| 127 | + log.stderr('Presubmit failed.'); |
| 128 | + log.stderr(' Formatting issues found in $failureCount packages.'); |
| 129 | + return 1; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + log.stdout('Presubmit passed!'); |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + List<String> _getPathsToFormat(Package p) { |
| 138 | + final pathsToFormat = <String>[]; |
| 139 | + if (p.relativePath == 'tool') { |
| 140 | + final children = Directory(p.packagePath).listSync(); |
| 141 | + for (final entity in children) { |
| 142 | + final name = path.basename(entity.path); |
| 143 | + if (name.startsWith('.')) continue; |
| 144 | + if (name == 'flutter-sdk') continue; |
| 145 | + if (entity is Directory) { |
| 146 | + pathsToFormat.add(name); |
| 147 | + } else if (entity is File && name.endsWith('.dart')) { |
| 148 | + pathsToFormat.add(name); |
| 149 | + } |
| 150 | + } |
| 151 | + } else { |
| 152 | + pathsToFormat.add('.'); |
| 153 | + } |
| 154 | + return pathsToFormat; |
| 155 | + } |
| 156 | +} |
0 commit comments