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