| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env dart | |
| 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 3 // for details. All rights reserved. Use of this source code is governed by a | |
| 4 // BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 /// Command line tool to write checker errors as inline comments in the source | |
| 7 /// code of the program. This tool requires the info.json file created by | |
| 8 /// running dartdevc.dart passing the arguments | |
| 9 /// --dump-info --dump-info-file info.json | |
| 10 | |
| 11 library dev_compiler.bin.edit_files; | |
| 12 | |
| 13 import 'dart:io'; | |
| 14 import 'dart:convert'; | |
| 15 | |
| 16 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; | |
| 17 import 'package:analyzer/src/generated/source.dart' show Source; | |
| 18 import 'package:args/args.dart'; | |
| 19 import 'package:cli_util/cli_util.dart' show getSdkDir; | |
| 20 import 'package:source_maps/refactor.dart'; | |
| 21 import 'package:source_span/source_span.dart'; | |
| 22 | |
| 23 import 'package:dev_compiler/src/analysis_context.dart'; | |
| 24 import 'package:dev_compiler/src/options.dart'; | |
| 25 import 'package:dev_compiler/src/summary.dart'; | |
| 26 | |
| 27 final ArgParser argParser = new ArgParser() | |
| 28 ..addOption('level', help: 'Minimum error level', defaultsTo: "info") | |
| 29 ..addOption('checkout-files-executable', | |
| 30 help: 'Executable to check out files from source control (e.g. svn)', | |
| 31 defaultsTo: null) | |
| 32 ..addOption('checkout-files-arg', | |
| 33 help: 'Arg to check out files from source control (e.g. checkout)', | |
| 34 defaultsTo: null) | |
| 35 ..addOption('include-pattern', | |
| 36 help: 'regular expression of file names to include', defaultsTo: null) | |
| 37 ..addOption('exclude-pattern', | |
| 38 help: 'regular expression of file names to exclude', defaultsTo: null) | |
| 39 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) | |
| 40 ..addOption('package-root', | |
| 41 abbr: 'p', | |
| 42 help: 'Package root to resolve "package:" imports', | |
| 43 defaultsTo: 'packages/') | |
| 44 ..addFlag('use-multi-package', | |
| 45 help: 'Whether to use the multi-package resolver for "package:" imports', | |
| 46 defaultsTo: false) | |
| 47 ..addOption('package-paths', | |
| 48 help: 'if using the multi-package resolver, ' | |
| 49 'the list of directories where to look for packages.', | |
| 50 defaultsTo: '') | |
| 51 ..addFlag('help', abbr: 'h', help: 'Display this message'); | |
| 52 | |
| 53 void _showUsageAndExit() { | |
| 54 print('usage: edit_files [<options>] <summary.json>\n'); | |
| 55 print('<summary.json> GlobalSummary serialized as json.\n'); | |
| 56 print('<options> include:\n'); | |
| 57 print(argParser.usage); | |
| 58 exit(1); | |
| 59 } | |
| 60 | |
| 61 class EditFileSummaryVisitor extends RecursiveSummaryVisitor { | |
| 62 var _files = new Map<String, TextEditTransaction>(); | |
| 63 AnalysisContext context; | |
| 64 String level; | |
| 65 String checkoutFilesExecutable; | |
| 66 String checkoutFilesArg; | |
| 67 RegExp includePattern; | |
| 68 RegExp excludePattern; | |
| 69 | |
| 70 final Map<Uri, Source> _sources = <Uri, Source>{}; | |
| 71 | |
| 72 EditFileSummaryVisitor(this.context, this.level, this.checkoutFilesExecutable, | |
| 73 this.checkoutFilesArg, this.includePattern, this.excludePattern); | |
| 74 | |
| 75 TextEditTransaction getEdits(String name) => _files.putIfAbsent(name, () { | |
| 76 var fileContents = new File(name).readAsStringSync(); | |
| 77 return new TextEditTransaction( | |
| 78 fileContents, new SourceFile(fileContents)); | |
| 79 }); | |
| 80 | |
| 81 /// Find the corresponding [Source] for [uri]. | |
| 82 Source findSource(Uri uri) { | |
| 83 var source = _sources[uri]; | |
| 84 if (source != null) return source; | |
| 85 return _sources[uri] = context.sourceFactory.forUri('$uri'); | |
| 86 } | |
| 87 | |
| 88 @override | |
| 89 void visitMessage(MessageSummary message) { | |
| 90 var uri = message.span.sourceUrl; | |
| 91 // Ignore dart: libraries. | |
| 92 if (uri.scheme == 'dart') return; | |
| 93 if (level != null) { | |
| 94 // Filter out messages with lower severity. | |
| 95 switch (message.level) { | |
| 96 case "info": | |
| 97 if (level != "info") return; | |
| 98 break; | |
| 99 case "warning": | |
| 100 if (level == "severe") return; | |
| 101 break; | |
| 102 } | |
| 103 } | |
| 104 var fullName = findSource(uri).fullName; | |
| 105 if (includePattern != null && !includePattern.hasMatch(fullName)) return; | |
| 106 if (excludePattern != null && excludePattern.hasMatch(fullName)) return; | |
| 107 var edits = getEdits(fullName); | |
| 108 edits.edit(message.span.start.offset, message.span.start.offset, | |
| 109 " /* DDC:${message.level}: ${message.kind}, ${message.message} */ "); | |
| 110 } | |
| 111 | |
| 112 void build() { | |
| 113 if (checkoutFilesExecutable != null) { | |
| 114 Process.runSync( | |
| 115 checkoutFilesExecutable, [checkoutFilesArg]..addAll(_files.keys)); | |
| 116 } | |
| 117 _files.forEach((name, transaction) { | |
| 118 var nestedPrinter = transaction.commit()..build(name); | |
| 119 new File(name).writeAsStringSync(nestedPrinter.text, flush: true); | |
| 120 }); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 void main(List<String> argv) { | |
| 125 ArgResults args = argParser.parse(argv); | |
| 126 if (args['help']) _showUsageAndExit(); | |
| 127 | |
| 128 if (args.rest.isEmpty) { | |
| 129 print('Expected filename.'); | |
| 130 _showUsageAndExit(); | |
| 131 } | |
| 132 | |
| 133 var sdkDir = getSdkDir(argv); | |
| 134 if (sdkDir == null) { | |
| 135 print('Could not automatically find dart sdk path.'); | |
| 136 print('Please pass in explicitly: --dart-sdk <path>'); | |
| 137 exit(1); | |
| 138 } | |
| 139 | |
| 140 var filename = args.rest.first; | |
| 141 var options = new SourceResolverOptions( | |
| 142 dartSdkPath: sdkDir.path, | |
| 143 useMultiPackage: args['use-multi-package'], | |
| 144 packageRoot: args['package-root'], | |
| 145 packagePaths: args['package-paths'].split(',')); | |
| 146 | |
| 147 Map json = JSON.decode(new File(filename).readAsStringSync()); | |
| 148 var summary = GlobalSummary.parse(json); | |
| 149 var excludePattern = (args['exclude-pattern'] != null) | |
| 150 ? new RegExp(args['exclude-pattern']) | |
| 151 : null; | |
| 152 var includePattern = (args['include-pattern'] != null) | |
| 153 ? new RegExp(args['include-pattern']) | |
| 154 : null; | |
| 155 | |
| 156 var context = createAnalysisContextWithSources(options); | |
| 157 var visitor = new EditFileSummaryVisitor( | |
| 158 context, | |
| 159 args['level'], | |
| 160 args['checkout-files-executable'], | |
| 161 args['checkout-files-arg'], | |
| 162 includePattern, | |
| 163 excludePattern); | |
| 164 summary.accept(visitor); | |
| 165 visitor.build(); | |
| 166 } | |
| OLD | NEW |