OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 @Deprecated('Will be removed in v0.15.0') |
5 library css; | 6 library css; |
6 | 7 |
7 import 'dart:io'; | 8 import 'dart:io'; |
8 | 9 |
| 10 import 'package:args/args.dart'; |
9 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
10 import 'package:source_span/source_span.dart'; | 12 import 'package:source_span/source_span.dart'; |
11 | 13 |
12 import 'parser.dart'; | 14 import 'parser.dart'; |
13 import 'src/messages.dart'; | 15 import 'src/messages.dart'; |
14 import 'visitor.dart'; | 16 import 'visitor.dart'; |
15 | 17 |
16 void main(List<String> arguments) { | 18 void main(List<String> arguments) { |
17 // TODO(jmesserly): fix this to return a proper exit code | 19 // TODO(jmesserly): fix this to return a proper exit code |
18 var options = PreprocessorOptions.parse(arguments); | 20 var options = _parseOptions(arguments); |
19 if (options == null) return; | 21 if (options == null) return; |
20 | 22 |
21 messages = new Messages(options: options); | 23 messages = new Messages(options: options); |
22 | 24 |
23 _time('Total time spent on ${options.inputFile}', () { | 25 _time('Total time spent on ${options.inputFile}', () { |
24 _compile(options.inputFile, options.verbose); | 26 _compile(options.inputFile, options.verbose); |
25 }, true); | 27 }, true); |
26 } | 28 } |
27 | 29 |
28 void _compile(String inputPath, bool verbose) { | 30 void _compile(String inputPath, bool verbose) { |
29 var ext = path.extension(inputPath); | 31 var ext = path.extension(inputPath); |
30 if (ext != '.css' && ext != '.scss') { | 32 if (ext != '.css' && ext != '.scss') { |
31 messages.error("Please provide a CSS/Sass file", null); | 33 messages.error("Please provide a CSS/Sass file", null); |
32 return; | 34 return; |
33 } | 35 } |
34 try { | 36 try { |
35 // Read the file. | 37 // Read the file. |
36 var filename = path.basename(inputPath); | 38 var filename = path.basename(inputPath); |
37 var contents = new File(inputPath).readAsStringSync(); | 39 var contents = new File(inputPath).readAsStringSync(); |
38 var file = new SourceFile(contents, url: path.toUri(inputPath)); | 40 var file = new SourceFile.fromString(contents, url: path.toUri(inputPath)); |
39 | 41 |
40 // Parse the CSS. | 42 // Parse the CSS. |
41 StyleSheet tree = _time( | 43 StyleSheet tree = _time( |
42 'Parse $filename', () => new Parser(file, contents).parse(), verbose); | 44 'Parse $filename', () => new Parser(file, contents).parse(), verbose); |
43 | 45 |
44 _time('Analyzer $filename', () => new Analyzer([tree], messages), verbose) | 46 _time('Analyzer $filename', () => new Analyzer([tree], messages), verbose) |
45 .run(); | 47 .run(); |
46 | 48 |
47 // Emit the processed CSS. | 49 // Emit the processed CSS. |
48 var emitter = new CssPrinter(); | 50 var emitter = new CssPrinter(); |
(...skipping 22 matching lines...) Expand all Loading... |
71 void _printMessage(String message, int duration) { | 73 void _printMessage(String message, int duration) { |
72 var buf = new StringBuffer(); | 74 var buf = new StringBuffer(); |
73 buf.write(message); | 75 buf.write(message); |
74 for (int i = message.length; i < 60; i++) buf.write(' '); | 76 for (int i = message.length; i < 60; i++) buf.write(' '); |
75 buf.write(' -- '); | 77 buf.write(' -- '); |
76 if (duration < 10) buf.write(' '); | 78 if (duration < 10) buf.write(' '); |
77 if (duration < 100) buf.write(' '); | 79 if (duration < 100) buf.write(' '); |
78 buf..write(duration)..write(' ms'); | 80 buf..write(duration)..write(' ms'); |
79 print(buf.toString()); | 81 print(buf.toString()); |
80 } | 82 } |
| 83 |
| 84 PreprocessorOptions _fromArgs(ArgResults args) => new PreprocessorOptions( |
| 85 warningsAsErrors: args['warnings_as_errors'], |
| 86 throwOnWarnings: args['throw_on_warnings'], |
| 87 throwOnErrors: args['throw_on_errors'], |
| 88 verbose: args['verbose'], |
| 89 checked: args['checked'], |
| 90 lessSupport: args['less'], |
| 91 useColors: args['colors'], |
| 92 polyfill: args['polyfill'], |
| 93 inputFile: args.rest.length > 0 ? args.rest[0] : null); |
| 94 |
| 95 // tool.dart [options...] <css file> |
| 96 PreprocessorOptions _parseOptions(List<String> arguments) { |
| 97 var parser = new ArgParser() |
| 98 ..addFlag('verbose', |
| 99 abbr: 'v', |
| 100 defaultsTo: false, |
| 101 negatable: false, |
| 102 help: 'Display detail info') |
| 103 ..addFlag('checked', |
| 104 defaultsTo: false, |
| 105 negatable: false, |
| 106 help: 'Validate CSS values invalid value display a warning message') |
| 107 ..addFlag('less', |
| 108 defaultsTo: true, |
| 109 negatable: true, |
| 110 help: 'Supports subset of Less syntax') |
| 111 ..addFlag('suppress_warnings', |
| 112 defaultsTo: true, help: 'Warnings not displayed') |
| 113 ..addFlag('warnings_as_errors', |
| 114 defaultsTo: false, help: 'Warning handled as errors') |
| 115 ..addFlag('throw_on_errors', |
| 116 defaultsTo: false, help: 'Throw on errors encountered') |
| 117 ..addFlag('throw_on_warnings', |
| 118 defaultsTo: false, help: 'Throw on warnings encountered') |
| 119 ..addFlag('colors', |
| 120 defaultsTo: true, help: 'Display errors/warnings in colored text') |
| 121 ..addFlag('polyfill', |
| 122 defaultsTo: false, help: 'Generate polyfill for new CSS features') |
| 123 ..addFlag('help', |
| 124 abbr: 'h', |
| 125 defaultsTo: false, |
| 126 negatable: false, |
| 127 help: 'Displays this help message'); |
| 128 |
| 129 try { |
| 130 var results = parser.parse(arguments); |
| 131 if (results['help'] || results.rest.length == 0) { |
| 132 _showUsage(parser); |
| 133 return null; |
| 134 } |
| 135 return _fromArgs(results); |
| 136 } on FormatException catch (e) { |
| 137 print(e.message); |
| 138 _showUsage(parser); |
| 139 return null; |
| 140 } |
| 141 } |
| 142 |
| 143 void _showUsage(ArgParser parser) { |
| 144 print('Usage: css [options...] input.css'); |
| 145 print(parser.usage); |
| 146 } |
OLD | NEW |