|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// @dart = 2.12 |
| 6 | + |
| 7 | +import 'dart:io' as io; |
| 8 | + |
| 9 | +import 'package:args/command_runner.dart'; |
| 10 | +import 'package:path/path.dart' as path; |
| 11 | + |
| 12 | +/// The command that implements the pre-push githook |
| 13 | +class PrePushCommand extends Command<bool> { |
| 14 | + @override |
| 15 | + final String name = 'pre-push'; |
| 16 | + |
| 17 | + @override |
| 18 | + final String description = 'Checks to run before a "git push"'; |
| 19 | + |
| 20 | + @override |
| 21 | + Future<bool> run() async { |
| 22 | + final Stopwatch sw = Stopwatch()..start(); |
| 23 | + final bool verbose = globalResults!['verbose']! as bool; |
| 24 | + final String flutterRoot = globalResults!['flutter']! as String; |
| 25 | + final List<bool> checkResults = await Future.wait<bool>(<Future<bool>>[ |
| 26 | + _runLinter(flutterRoot, verbose), |
| 27 | + _runFormatter(flutterRoot, verbose), |
| 28 | + ]); |
| 29 | + sw.stop(); |
| 30 | + io.stdout.writeln('pre-push checks finished in ${sw.elapsed}'); |
| 31 | + return !checkResults.contains(false); |
| 32 | + } |
| 33 | + |
| 34 | + Future<bool> _runLinter(String flutterRoot, bool verbose) async { |
| 35 | + if (io.Platform.isWindows) { |
| 36 | + return true; |
| 37 | + } |
| 38 | + return _runCheck( |
| 39 | + flutterRoot, |
| 40 | + path.join(flutterRoot, 'ci', 'lint.sh'), |
| 41 | + <String>[], |
| 42 | + 'Linting check', |
| 43 | + verbose: verbose, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + Future<bool> _runFormatter(String flutterRoot, bool verbose) { |
| 48 | + final String ext = io.Platform.isWindows ? '.bat' : '.sh'; |
| 49 | + return _runCheck( |
| 50 | + flutterRoot, |
| 51 | + path.join(flutterRoot, 'ci', 'format$ext'), |
| 52 | + <String>[], |
| 53 | + 'Formatting check', |
| 54 | + verbose: verbose, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + Future<bool> _runCheck( |
| 59 | + String flutterRoot, |
| 60 | + String scriptPath, |
| 61 | + List<String> scriptArgs, |
| 62 | + String checkName, { |
| 63 | + bool verbose = false, |
| 64 | + }) async { |
| 65 | + if (verbose) { |
| 66 | + io.stdout.writeln('Starting "$checkName": $scriptPath'); |
| 67 | + } |
| 68 | + final io.ProcessResult result = await io.Process.run( |
| 69 | + scriptPath, |
| 70 | + scriptArgs, |
| 71 | + workingDirectory: flutterRoot, |
| 72 | + ); |
| 73 | + if (result.exitCode != 0) { |
| 74 | + final StringBuffer message = StringBuffer(); |
| 75 | + message.writeln('Check "$checkName" failed.'); |
| 76 | + message.writeln('command: $scriptPath ${scriptArgs.join(" ")}'); |
| 77 | + message.writeln('working directory: $flutterRoot'); |
| 78 | + message.writeln('exit code: ${result.exitCode}'); |
| 79 | + message.writeln('stdout:'); |
| 80 | + message.writeln(result.stdout); |
| 81 | + message.writeln('stderr:'); |
| 82 | + message.writeln(result.stderr); |
| 83 | + io.stderr.write(message.toString()); |
| 84 | + return false; |
| 85 | + } |
| 86 | + if (verbose) { |
| 87 | + io.stdout.writeln('Check "$checkName" finished successfully.'); |
| 88 | + } |
| 89 | + return true; |
| 90 | + } |
| 91 | +} |
0 commit comments