Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: bin/edit_files.dart

Issue 1143683002: cleanup: simplify creation of AnalysisContext (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/devc.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env dart 1 #!/usr/bin/env dart
2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 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 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. 4 // BSD-style license that can be found in the LICENSE file.
5 5
6 /// Command line tool to write checker errors as inline comments in the source 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 7 /// code of the program. This tool requires the info.json file created by
8 /// running devc.dart passing the arguments 8 /// running devc.dart passing the arguments
9 /// --dump-info --dump-info-file info.json 9 /// --dump-info --dump-info-file info.json
10 10
11 library dev_compiler.bin.edit_files; 11 library dev_compiler.bin.edit_files;
12 12
13 import 'dart:io'; 13 import 'dart:io';
14 import 'dart:convert'; 14 import 'dart:convert';
15 15
16 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext;
16 import 'package:analyzer/src/generated/source.dart' show Source; 17 import 'package:analyzer/src/generated/source.dart' show Source;
17 import 'package:args/args.dart'; 18 import 'package:args/args.dart';
18 import 'package:cli_util/cli_util.dart' show getSdkDir; 19 import 'package:cli_util/cli_util.dart' show getSdkDir;
19 import 'package:source_maps/refactor.dart'; 20 import 'package:source_maps/refactor.dart';
20 import 'package:source_span/source_span.dart'; 21 import 'package:source_span/source_span.dart';
21 22
23 import 'package:dev_compiler/src/analysis_context.dart';
22 import 'package:dev_compiler/src/options.dart'; 24 import 'package:dev_compiler/src/options.dart';
23 import 'package:dev_compiler/src/summary.dart'; 25 import 'package:dev_compiler/src/summary.dart';
24 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver;
25 26
26 final ArgParser argParser = new ArgParser() 27 final ArgParser argParser = new ArgParser()
27 ..addOption('level', help: 'Minimum error level', defaultsTo: "info") 28 ..addOption('level', help: 'Minimum error level', defaultsTo: "info")
28 ..addOption('checkout-files-executable', 29 ..addOption('checkout-files-executable',
29 help: 'Executable to check out files from source control (e.g. svn)', 30 help: 'Executable to check out files from source control (e.g. svn)',
30 defaultsTo: null) 31 defaultsTo: null)
31 ..addOption('checkout-files-arg', 32 ..addOption('checkout-files-arg',
32 help: 'Arg to check out files from source control (e.g. checkout)', 33 help: 'Arg to check out files from source control (e.g. checkout)',
33 defaultsTo: null) 34 defaultsTo: null)
34 ..addOption('include-pattern', 35 ..addOption('include-pattern',
(...skipping 15 matching lines...) Expand all
50 void _showUsageAndExit() { 51 void _showUsageAndExit() {
51 print('usage: edit_files [<options>] <summary.json>\n'); 52 print('usage: edit_files [<options>] <summary.json>\n');
52 print('<summary.json> GlobalSummary serialized as json.\n'); 53 print('<summary.json> GlobalSummary serialized as json.\n');
53 print('<options> include:\n'); 54 print('<options> include:\n');
54 print(argParser.usage); 55 print(argParser.usage);
55 exit(1); 56 exit(1);
56 } 57 }
57 58
58 class EditFileSummaryVisitor extends RecursiveSummaryVisitor { 59 class EditFileSummaryVisitor extends RecursiveSummaryVisitor {
59 var _files = new Map<String, TextEditTransaction>(); 60 var _files = new Map<String, TextEditTransaction>();
60 TypeResolver typeResolver; 61 AnalysisContext context;
61 String level; 62 String level;
62 String checkoutFilesExecutable; 63 String checkoutFilesExecutable;
63 String checkoutFilesArg; 64 String checkoutFilesArg;
64 RegExp includePattern; 65 RegExp includePattern;
65 RegExp excludePattern; 66 RegExp excludePattern;
66 67
67 final Map<Uri, Source> _sources = <Uri, Source>{}; 68 final Map<Uri, Source> _sources = <Uri, Source>{};
68 69
69 EditFileSummaryVisitor(this.typeResolver, this.level, 70 EditFileSummaryVisitor(this.context, this.level,
70 this.checkoutFilesExecutable, this.checkoutFilesArg, this.includePattern, 71 this.checkoutFilesExecutable, this.checkoutFilesArg, this.includePattern,
71 this.excludePattern); 72 this.excludePattern);
72 73
73 TextEditTransaction getEdits(String name) => _files.putIfAbsent(name, () { 74 TextEditTransaction getEdits(String name) => _files.putIfAbsent(name, () {
74 var fileContents = new File(name).readAsStringSync(); 75 var fileContents = new File(name).readAsStringSync();
75 return new TextEditTransaction(fileContents, new SourceFile(fileContents)); 76 return new TextEditTransaction(fileContents, new SourceFile(fileContents));
76 }); 77 });
77 78
78 /// Find the corresponding [Source] for [uri]. 79 /// Find the corresponding [Source] for [uri].
79 Source findSource(Uri uri) { 80 Source findSource(Uri uri) {
80 var source = _sources[uri]; 81 var source = _sources[uri];
81 if (source != null) return source; 82 if (source != null) return source;
82 return _sources[uri] = typeResolver.context.sourceFactory.forUri('$uri'); 83 return _sources[uri] = context.sourceFactory.forUri('$uri');
83 } 84 }
84 85
85 @override 86 @override
86 void visitMessage(MessageSummary message) { 87 void visitMessage(MessageSummary message) {
87 var uri = message.span.sourceUrl; 88 var uri = message.span.sourceUrl;
88 // Ignore dart: libraries. 89 // Ignore dart: libraries.
89 if (uri.scheme == 'dart') return; 90 if (uri.scheme == 'dart') return;
90 if (level != null) { 91 if (level != null) {
91 // Filter out messages with lower severity. 92 // Filter out messages with lower severity.
92 switch (message.level) { 93 switch (message.level) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 } 129 }
129 130
130 var sdkDir = getSdkDir(argv); 131 var sdkDir = getSdkDir(argv);
131 if (sdkDir == null) { 132 if (sdkDir == null) {
132 print('Could not automatically find dart sdk path.'); 133 print('Could not automatically find dart sdk path.');
133 print('Please pass in explicitly: --dart-sdk <path>'); 134 print('Please pass in explicitly: --dart-sdk <path>');
134 exit(1); 135 exit(1);
135 } 136 }
136 137
137 var filename = args.rest.first; 138 var filename = args.rest.first;
138 var options = new ResolverOptions( 139 var options = new CompilerOptions(
140 dartSdkPath: sdkDir.path,
139 useMultiPackage: args['use-multi-package'], 141 useMultiPackage: args['use-multi-package'],
140 packageRoot: args['package-root'], 142 packageRoot: args['package-root'],
141 packagePaths: args['package-paths'].split(',')); 143 packagePaths: args['package-paths'].split(','));
142 144
143 var typeResolver = new TypeResolver.fromDir(sdkDir.path, options);
144
145 Map json = JSON.decode(new File(filename).readAsStringSync()); 145 Map json = JSON.decode(new File(filename).readAsStringSync());
146 var summary = GlobalSummary.parse(json); 146 var summary = GlobalSummary.parse(json);
147 var excludePattern = (args['exclude-pattern'] != null) 147 var excludePattern = (args['exclude-pattern'] != null)
148 ? new RegExp(args['exclude-pattern']) 148 ? new RegExp(args['exclude-pattern'])
149 : null; 149 : null;
150 var includePattern = (args['include-pattern'] != null) 150 var includePattern = (args['include-pattern'] != null)
151 ? new RegExp(args['include-pattern']) 151 ? new RegExp(args['include-pattern'])
152 : null; 152 : null;
153 153
154 var visitor = new EditFileSummaryVisitor(typeResolver, args['level'], 154 var context = createAnalysisContext(options);
155 var visitor = new EditFileSummaryVisitor(context, args['level'],
155 args['checkout-files-executable'], args['checkout-files-arg'], 156 args['checkout-files-executable'], args['checkout-files-arg'],
156 includePattern, excludePattern); 157 includePattern, excludePattern);
157 summary.accept(visitor); 158 summary.accept(visitor);
158 visitor.build(); 159 visitor.build();
159 } 160 }
OLDNEW
« no previous file with comments | « no previous file | lib/devc.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698