|
| 1 | +// Copyright 2019 The Chromium 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 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:args/command_runner.dart'; |
| 8 | +import 'package:meta/meta.dart'; |
| 9 | + |
| 10 | +const String kOptionResourcesRoot = 'resources-root'; |
| 11 | +const String kOptionTimeLimitMs = 'time-limit-ms'; |
| 12 | +const String kOptionDevice = 'device'; |
| 13 | +const String kOptionProessName = 'process-name'; |
| 14 | +const String kOptionOutJson = 'out-json'; |
| 15 | +const String kFlagVerbose = 'verbose'; |
| 16 | + |
| 17 | +const String kDefaultProccessName = 'Runner'; // Flutter app's default process |
| 18 | + |
| 19 | +abstract class BaseCommand extends Command<void> { |
| 20 | + BaseCommand() { |
| 21 | + argParser.addFlag(kFlagVerbose); |
| 22 | + argParser.addOption( |
| 23 | + kOptionOutJson, |
| 24 | + abbr: 'o', |
| 25 | + help: 'Specifies the json file for result output.', |
| 26 | + defaultsTo: 'result.json', |
| 27 | + ); |
| 28 | + argParser.addOption( |
| 29 | + kOptionResourcesRoot, |
| 30 | + abbr: 'r', |
| 31 | + help: 'Specifies the path to download resources', |
| 32 | + defaultsTo: defaultResourcesRoot, |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + static String get defaultResourcesRoot => |
| 37 | + '${Platform.environment['HOME']}/.measure'; |
| 38 | + |
| 39 | + static Future<void> doEnsureResources(String rootPath, |
| 40 | + {bool isVerbose}) async { |
| 41 | + final Directory root = await Directory(rootPath).create(recursive: true); |
| 42 | + final Directory previous = Directory.current; |
| 43 | + Directory.current = root; |
| 44 | + final File ensureFile = File('ensure_file.txt'); |
| 45 | + ensureFile.writeAsStringSync('flutter/packages/measure/resources latest'); |
| 46 | + if (isVerbose) { |
| 47 | + print('Downloading resources from CIPD...'); |
| 48 | + } |
| 49 | + final ProcessResult result = Process.runSync( |
| 50 | + 'cipd', |
| 51 | + <String>[ |
| 52 | + 'ensure', |
| 53 | + '-ensure-file', |
| 54 | + 'ensure_file.txt', |
| 55 | + '-root', |
| 56 | + '.', |
| 57 | + ], |
| 58 | + ); |
| 59 | + if (result.exitCode != 0) { |
| 60 | + print('cipd ensure stdout:\n${result.stdout}\n'); |
| 61 | + print('cipd ensure stderr:\n${result.stderr}\n'); |
| 62 | + throw Exception('Failed to download the CIPD package.'); |
| 63 | + } |
| 64 | + if (isVerbose) { |
| 65 | + print('Download completes.'); |
| 66 | + } |
| 67 | + Directory.current = previous; |
| 68 | + } |
| 69 | + |
| 70 | + @protected |
| 71 | + Future<void> ensureResources() async { |
| 72 | + doEnsureResources(resourcesRoot, isVerbose: isVerbose); |
| 73 | + } |
| 74 | + |
| 75 | + @protected |
| 76 | + void checkRequiredOption(String option) { |
| 77 | + if (argResults[option] == null) { |
| 78 | + throw Exception('Option $option is required.'); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @protected |
| 83 | + bool get isVerbose => argResults[kFlagVerbose]; |
| 84 | + @protected |
| 85 | + String get outJson => argResults[kOptionOutJson]; |
| 86 | + @protected |
| 87 | + String get resourcesRoot => argResults[kOptionResourcesRoot]; |
| 88 | +} |
| 89 | + |
| 90 | +abstract class IosCpuGpuSubcommand extends BaseCommand { |
| 91 | + IosCpuGpuSubcommand() { |
| 92 | + argParser.addOption( |
| 93 | + kOptionProessName, |
| 94 | + abbr: 'p', |
| 95 | + help: 'Specifies the process name used for filtering the instruments CPU ' |
| 96 | + 'measurements.', |
| 97 | + defaultsTo: kDefaultProccessName, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + @protected |
| 102 | + String get processName => argResults[kOptionProessName]; |
| 103 | + @protected |
| 104 | + String get traceUtilityPath => '$resourcesRoot/resources/TraceUtility'; |
| 105 | +} |
0 commit comments