| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 /// Loads a human-readable source map and outputs the source map file for it. |
| 6 |
| 7 library save; |
| 8 |
| 9 import 'dart:convert'; |
| 10 import 'dart:io'; |
| 11 import 'package:source_maps/source_maps.dart'; |
| 12 import 'lax_json.dart' as lazon; |
| 13 |
| 14 void main(List<String> args) { |
| 15 if (args.isEmpty) { |
| 16 print(''' |
| 17 Usage: save <dir-containing 'out.js.map2'> |
| 18 or: save <human-readable-source-map-file> [<source-map-file>]'''); |
| 19 exit(1); |
| 20 } |
| 21 |
| 22 File humanReadableSourceMapFile; |
| 23 File sourceMapFile; |
| 24 if (args.length == 1 && new Directory(args[0]).existsSync()) { |
| 25 humanReadableSourceMapFile = new File('${args[0]}/out.js.map2'); |
| 26 sourceMapFile = new File('${args[0]}/out.js.map'); |
| 27 } else { |
| 28 humanReadableSourceMapFile = new File(args[0]); |
| 29 if (args.length > 1) { |
| 30 sourceMapFile = new File(args[1]); |
| 31 } |
| 32 } |
| 33 |
| 34 String humanReadableSourceMap = humanReadableSourceMapFile.readAsStringSync(); |
| 35 SingleMapping mapping = |
| 36 convertFromHumanReadableSourceMap(humanReadableSourceMap); |
| 37 |
| 38 if (sourceMapFile != null) { |
| 39 sourceMapFile.writeAsStringSync(JSON.encoder.convert(mapping.toJson())); |
| 40 } else { |
| 41 print(new JsonEncoder.withIndent(' ').convert(mapping.toJson())); |
| 42 } |
| 43 } |
| 44 |
| 45 SingleMapping convertFromHumanReadableSourceMap(String json) { |
| 46 Map inputMap = lazon.decode(json); |
| 47 String file = inputMap['file']; |
| 48 Map urls = inputMap['sources']; |
| 49 List<String> sources = new List<String>.filled(urls.length, null); |
| 50 urls.forEach((String index, String url) { |
| 51 int i = int.parse(index); |
| 52 assert(sources[i] == null); |
| 53 sources[i] = url; |
| 54 }); |
| 55 List lines = inputMap['lines']; |
| 56 Map<String, int> names = <String, int>{}; |
| 57 Map<int, TargetLineEntry> lineEntryMap = {}; |
| 58 |
| 59 for (Map line in lines) { |
| 60 String targetString = line['target']; |
| 61 String sourceString = line['source']; |
| 62 String name = line['name']; |
| 63 int nameIndex; |
| 64 if (name != null) { |
| 65 nameIndex = names.putIfAbsent(name, () => names.length); |
| 66 } |
| 67 |
| 68 int lineNo; |
| 69 int columnStart; |
| 70 int columnEnd; |
| 71 if (targetString != null) { |
| 72 int commaPos = targetString.indexOf(','); |
| 73 lineNo = int.parse(targetString.substring(0, commaPos)) - 1; |
| 74 if (lineNo < 0) { |
| 75 throw new ArgumentError('target line must be > 0: $lineNo'); |
| 76 } |
| 77 targetString = targetString.substring(commaPos + 1); |
| 78 int dashPos = targetString.indexOf('-'); |
| 79 columnStart = int.parse(targetString.substring(0, dashPos)) - 1; |
| 80 if (columnStart < 0) { |
| 81 throw new ArgumentError( |
| 82 'target column start must be > 0: $columnStart'); |
| 83 } |
| 84 targetString = targetString.substring(dashPos + 1); |
| 85 if (!targetString.isEmpty) { |
| 86 columnEnd = int.parse(targetString) - 1; |
| 87 if (columnEnd < 0) { |
| 88 throw new ArgumentError('target column end must be > 0: $columnEnd'); |
| 89 } |
| 90 } |
| 91 } |
| 92 int sourceUrlId; |
| 93 int sourceLine; |
| 94 int sourceColumn; |
| 95 if (sourceString != null) { |
| 96 int colonPos = sourceString.indexOf(':'); |
| 97 sourceUrlId = int.parse(sourceString.substring(0, colonPos)); |
| 98 if (sourceUrlId < 0) { |
| 99 throw new ArgumentError('source url id end must be > 0: $sourceUrlId'); |
| 100 } else if (sourceUrlId >= sources.length) { |
| 101 throw new ArgumentError( |
| 102 'source url id end must be < ${sources.length}: $sourceUrlId'); |
| 103 } |
| 104 |
| 105 sourceString = sourceString.substring(colonPos + 1); |
| 106 int commaPos = sourceString.indexOf(','); |
| 107 sourceLine = int.parse(sourceString.substring(0, commaPos)) - 1; |
| 108 if (sourceLine < 0) { |
| 109 throw new ArgumentError('source line end must be > 0: $sourceLine'); |
| 110 } |
| 111 sourceString = sourceString.substring(commaPos + 1); |
| 112 sourceColumn = int.parse(sourceString) - 1; |
| 113 if (sourceColumn < 0) { |
| 114 throw new ArgumentError('source column must be > 0: $sourceColumn'); |
| 115 } |
| 116 } |
| 117 |
| 118 TargetLineEntry lineEntry = |
| 119 lineEntryMap.putIfAbsent(lineNo, () => new TargetLineEntry(lineNo, [])); |
| 120 lineEntry.entries.add(new TargetEntry( |
| 121 columnStart, sourceUrlId, sourceLine, sourceColumn, nameIndex)); |
| 122 if (columnEnd != null) { |
| 123 lineEntry.entries.add(new TargetEntry(columnEnd)); |
| 124 } |
| 125 } |
| 126 |
| 127 List<TargetLineEntry> lineEntries = lineEntryMap.values.toList(); |
| 128 |
| 129 Map outputMap = { |
| 130 'version': 3, |
| 131 'sourceRoot': inputMap['sourceRoot'], |
| 132 'file': inputMap['file'], |
| 133 'sources': sources, |
| 134 'names': names.keys.toList(), |
| 135 'mappings': '', |
| 136 }; |
| 137 |
| 138 SingleMapping mapping = new SingleMapping.fromJson(outputMap); |
| 139 mapping.lines.addAll(lineEntries); |
| 140 return mapping; |
| 141 } |
| OLD | NEW |