|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:intl/intl.dart'; |
| 9 | +import 'package:yaml/yaml.dart'; |
| 10 | + |
| 11 | +import '../core.dart'; |
| 12 | +import '../processes.dart'; |
| 13 | +import '../utils.dart'; |
| 14 | + |
| 15 | +// TODO(devoncarew): have a flag to elide paths (enabled by default) |
| 16 | + |
| 17 | +final NumberFormat _nf = NumberFormat(); |
| 18 | + |
| 19 | +const bool _elideFilePaths = true; |
| 20 | + |
| 21 | +/// Print output useful for diagnosing local issues. |
| 22 | +class BugCommand extends DartdevCommand { |
| 23 | + static const String cmdName = 'bug'; |
| 24 | + |
| 25 | + static const String cmdDescription = |
| 26 | + 'Show diagnostic information about the installed tooling.'; |
| 27 | + |
| 28 | + BugCommand({bool verbose = false}) : super(cmdName, cmdDescription, verbose); |
| 29 | + |
| 30 | + static const String _message = |
| 31 | + 'If providing this information as part of reporting a bug, please review ' |
| 32 | + 'the information below carefully to ensure it only contains things ' |
| 33 | + "you're comfortable posting publicly."; |
| 34 | + |
| 35 | + @override |
| 36 | + FutureOr<int> run() async { |
| 37 | + print(''); |
| 38 | + print(wrapText(_message, width: dartdevUsageLineLength)); |
| 39 | + |
| 40 | + print(''); |
| 41 | + print('#### General info'); |
| 42 | + print(''); |
| 43 | + print('- Dart ${Platform.version}'); |
| 44 | + print('- on ${Platform.operatingSystem} / ' |
| 45 | + '${Platform.operatingSystemVersion}'); |
| 46 | + print('- locale is ${Platform.localeName}'); |
| 47 | + |
| 48 | + // project information |
| 49 | + var projectInfo = getProjectInfo(project, onlySimpleDeps: _elideFilePaths); |
| 50 | + if (projectInfo != null) { |
| 51 | + print(''); |
| 52 | + print('#### Project info'); |
| 53 | + print(''); |
| 54 | + print("- sdk constraint: '${projectInfo.sdkDependency ?? ''}'"); |
| 55 | + print('- dependencies: ${projectInfo.dependencies.join(', ')}'); |
| 56 | + print('- dev_dependencies: ${projectInfo.devDependencies.join(', ')}'); |
| 57 | + if (projectInfo.elidedDependencies > 0) { |
| 58 | + print('- elided dependencies: ${projectInfo.elidedDependencies}'); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // process information |
| 63 | + var processInfo = |
| 64 | + ProcessInfo.getProcessInfo(elideFilePaths: _elideFilePaths); |
| 65 | + if (processInfo != null) { |
| 66 | + print(''); |
| 67 | + print('#### Process info'); |
| 68 | + print(''); |
| 69 | + |
| 70 | + if (processInfo.isEmpty) { |
| 71 | + print('No Dart processes found.'); |
| 72 | + } else { |
| 73 | + var table = MarkdownTable(); |
| 74 | + table.startRow() |
| 75 | + ..cell('Memory', right: true) |
| 76 | + ..cell('CPU', right: true) |
| 77 | + ..cell('Elapsed time', right: true) |
| 78 | + ..cell('Command line'); |
| 79 | + |
| 80 | + for (var process in processInfo) { |
| 81 | + var row = table.startRow(); |
| 82 | + row.cell('${_nf.format(process.memoryMb)} MB', right: true); |
| 83 | + row.cell('${process.cpuPercent.toStringAsFixed(1)}%', right: true); |
| 84 | + row.cell(process.elapsedTime, right: true); |
| 85 | + row.cell(_elideFilePaths |
| 86 | + ? _noMoreThan(process.commandLine, MarkdownTable.defaultMaxWidth) |
| 87 | + : process.commandLine); |
| 88 | + } |
| 89 | + |
| 90 | + print(table.finish().trimRight()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return 0; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +ProjectInfo? getProjectInfo(Project project, {bool onlySimpleDeps = true}) { |
| 99 | + if (!project.hasPubspecFile) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + var pubspec = loadYaml(project.pubspecFile.readAsStringSync()) as Map; |
| 104 | + var elidedDependencies = 0; |
| 105 | + |
| 106 | + List<String> getDeps(String dependencyType) { |
| 107 | + var deps = pubspec[dependencyType] as Map?; |
| 108 | + var results = <String>[]; |
| 109 | + if (deps != null) { |
| 110 | + for (var pkgName in deps.keys) { |
| 111 | + var dep = deps[pkgName]; |
| 112 | + |
| 113 | + // Don't report path: or git: dependencies. |
| 114 | + if (onlySimpleDeps && dep is Map) { |
| 115 | + if (dep.length != 1) { |
| 116 | + elidedDependencies++; |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + final key = dep.keys.first; |
| 121 | + if (key != 'sdk') { |
| 122 | + elidedDependencies++; |
| 123 | + continue; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + results.add(pkgName); |
| 128 | + } |
| 129 | + } |
| 130 | + return results..sort(); |
| 131 | + } |
| 132 | + |
| 133 | + return ProjectInfo( |
| 134 | + sdkDependency: (pubspec['environment'] as Map?)?['sdk'] as String?, |
| 135 | + dependencies: getDeps('dependencies'), |
| 136 | + devDependencies: getDeps('dev_dependencies'), |
| 137 | + elidedDependencies: elidedDependencies, |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | +class ProjectInfo { |
| 142 | + final String? sdkDependency; |
| 143 | + final List<String> dependencies; |
| 144 | + final List<String> devDependencies; |
| 145 | + final int elidedDependencies; |
| 146 | + |
| 147 | + ProjectInfo({ |
| 148 | + required this.sdkDependency, |
| 149 | + required this.dependencies, |
| 150 | + required this.devDependencies, |
| 151 | + required this.elidedDependencies, |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +String _noMoreThan(String value, int length) { |
| 156 | + if (value.length <= length) return value; |
| 157 | + return '${value.substring(0, length - 1)}…'; |
| 158 | +} |
0 commit comments