| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library css; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:path/path.dart' as path; | |
| 10 import 'package:source_span/source_span.dart'; | |
| 11 | |
| 12 import 'parser.dart'; | |
| 13 import 'visitor.dart'; | |
| 14 import 'src/messages.dart'; | |
| 15 import 'src/options.dart'; | |
| 16 | |
| 17 void main(List<String> arguments) { | |
| 18 // TODO(jmesserly): fix this to return a proper exit code | |
| 19 var options = PreprocessorOptions.parse(arguments); | |
| 20 if (options == null) return; | |
| 21 | |
| 22 messages = new Messages(options: options); | |
| 23 | |
| 24 _time('Total time spent on ${options.inputFile}', () { | |
| 25 _compile(options.inputFile, options.verbose); | |
| 26 }, true); | |
| 27 } | |
| 28 | |
| 29 void _compile(String inputPath, bool verbose) { | |
| 30 var ext = path.extension(inputPath); | |
| 31 if (ext != '.css' && ext != '.scss') { | |
| 32 messages.error("Please provide a CSS/Sass file", null); | |
| 33 return; | |
| 34 } | |
| 35 try { | |
| 36 // Read the file. | |
| 37 var filename = path.basename(inputPath); | |
| 38 var contents = new File(inputPath).readAsStringSync(); | |
| 39 var file = new SourceFile(contents, url: path.toUri(inputPath)); | |
| 40 | |
| 41 // Parse the CSS. | |
| 42 var tree = _time('Parse $filename', | |
| 43 () => new Parser(file, contents).parse(), verbose); | |
| 44 | |
| 45 _time('Analyzer $filename', | |
| 46 () => new Analyzer([tree], messages), verbose).run(); | |
| 47 | |
| 48 // Emit the processed CSS. | |
| 49 var emitter = new CssPrinter(); | |
| 50 _time('Codegen $filename', | |
| 51 () => emitter.visitTree(tree, pretty: true), verbose); | |
| 52 | |
| 53 // Write the contents to a file. | |
| 54 var outPath = path.join(path.dirname(inputPath), '_$filename'); | |
| 55 new File(outPath).writeAsStringSync(emitter.toString()); | |
| 56 } catch (e) { | |
| 57 messages.error('error processing $inputPath. Original message:\n $e', null); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 _time(String message, callback(), bool printTime) { | |
| 62 if (!printTime) return callback(); | |
| 63 final watch = new Stopwatch(); | |
| 64 watch.start(); | |
| 65 var result = callback(); | |
| 66 watch.stop(); | |
| 67 final duration = watch.elapsedMilliseconds; | |
| 68 _printMessage(message, duration); | |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 void _printMessage(String message, int duration) { | |
| 73 var buf = new StringBuffer(); | |
| 74 buf.write(message); | |
| 75 for (int i = message.length; i < 60; i++) buf.write(' '); | |
| 76 buf.write(' -- '); | |
| 77 if (duration < 10) buf.write(' '); | |
| 78 if (duration < 100) buf.write(' '); | |
| 79 buf..write(duration)..write(' ms'); | |
| 80 print(buf.toString()); | |
| 81 } | |
| OLD | NEW |