| 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 import java.io.BufferedReader; | |
| 6 import java.io.File; | |
| 7 import java.io.FileInputStream; | |
| 8 import java.io.FileOutputStream; | |
| 9 import java.io.InputStreamReader; | |
| 10 import java.io.LineNumberReader; | |
| 11 import java.io.OutputStreamWriter; | |
| 12 import java.io.Writer; | |
| 13 import java.util.Map; | |
| 14 import java.util.TreeMap; | |
| 15 | |
| 16 /** | |
| 17 * Used to generated the file dartlib.html (which contains all the dart | |
| 18 * core library files embedded in script tags on an html page). | |
| 19 * | |
| 20 * TODO - remove hard-coded paths and integrate into dartboard build process | |
| 21 */ | |
| 22 public class BuildDartlib { | |
| 23 public final String dartDir; | |
| 24 | |
| 25 // map from script tag id to File whose contents goes in that script tag | |
| 26 Map<String, File> idToFile = new TreeMap<>(); | |
| 27 | |
| 28 private BuildDartlib(String dartDir) { | |
| 29 this.dartDir = dartDir; | |
| 30 } | |
| 31 | |
| 32 public static void main(String [] argv) throws Exception { | |
| 33 BuildDartlib dartlib = new BuildDartlib("/usr/local/google/home/mattsh/dart-
code1/dart/"); | |
| 34 dartlib.go(); | |
| 35 } | |
| 36 | |
| 37 public void go() throws Exception { | |
| 38 loadLibraries(); | |
| 39 File outputFile = new File(dartDir + "samples/dartboard/dartlib.html"); | |
| 40 Writer writer = new OutputStreamWriter(new FileOutputStream(outputFile), "UT
F8"); | |
| 41 writer.write("<html><head>\n"); | |
| 42 for (Map.Entry<String, File> entry : idToFile.entrySet()) { | |
| 43 writer.write(makeScriptTag(entry.getKey(), entry.getValue())); | |
| 44 } | |
| 45 writer.write("</head></html>\n"); | |
| 46 writer.close(); | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Load all the libraries that are allowed to follow "dart:", e.g. dart:html | |
| 51 * dart:json, etc. | |
| 52 */ | |
| 53 private void loadLibraries() throws Exception { | |
| 54 File dartDef = new File(dartDir + "samples/dartboard/build_dartlib/core.dart
def"); | |
| 55 | |
| 56 LineNumberReader reader = new LineNumberReader( | |
| 57 new InputStreamReader(new FileInputStream(dartDef), "UTF8")); | |
| 58 | |
| 59 while (true) { | |
| 60 String line = reader.readLine(); | |
| 61 if (line == null) { | |
| 62 break; | |
| 63 } | |
| 64 line = line.trim(); | |
| 65 if (line.isEmpty()) { | |
| 66 continue; | |
| 67 } | |
| 68 if (line.startsWith("#")) { | |
| 69 continue; | |
| 70 } | |
| 71 String [] words = line.split(" +"); | |
| 72 String value = words[1]; | |
| 73 String relPath = value.substring(1, value.length() - 1); | |
| 74 File file = new File(dartDir + relPath); | |
| 75 if (!file.exists()) { | |
| 76 throw new Exception("can't find file " + file.getCanonicalPath()); | |
| 77 } | |
| 78 processFile(file); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 /** | |
| 83 * load this dart file, and all dart files this file depends on | |
| 84 * (search through the dart file for any #import and #source lines to | |
| 85 * figure out what other files this file depends on) | |
| 86 */ | |
| 87 void processFile(File file) throws Exception { | |
| 88 File dir = file.getParentFile(); | |
| 89 LineNumberReader reader = new LineNumberReader( | |
| 90 new InputStreamReader(new FileInputStream(file), "UTF8")); | |
| 91 | |
| 92 System.out.println("processing file " + file.getCanonicalPath()); | |
| 93 | |
| 94 while (true) { | |
| 95 String line = reader.readLine(); | |
| 96 if (line == null) { | |
| 97 break; | |
| 98 } | |
| 99 line = line.trim(); | |
| 100 if (line.isEmpty()) { | |
| 101 continue; | |
| 102 } | |
| 103 | |
| 104 if (line.startsWith("#import")) { | |
| 105 if (!line.substring("#import(\"".length()).startsWith("dart:")) { | |
| 106 throw new Exception("found import line " + line); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 if (line.startsWith("#source") || line.startsWith("#native")) { | |
| 111 char quoteChar = line.charAt("#source(".length()); | |
| 112 if (quoteChar != '\"' && quoteChar != '\'') { | |
| 113 throw new Exception("quote not found"); | |
| 114 } | |
| 115 int startQuote = line.indexOf(quoteChar); | |
| 116 int endQuote = line.indexOf(quoteChar, startQuote + 1); | |
| 117 String relPath = line.substring(startQuote + 1, endQuote); | |
| 118 File sourcedFile = new File(dir + "/" + relPath); | |
| 119 addFile(sourcedFile); | |
| 120 } | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 private void addFile(File file) throws Exception { | |
| 125 String path = file.getCanonicalPath(); | |
| 126 if (!file.exists()) { | |
| 127 throw new Exception("cannot find file " + path); | |
| 128 } | |
| 129 if (!path.startsWith(dartDir)) { | |
| 130 throw new Exception("file " + path + " is outside dart tree"); | |
| 131 } | |
| 132 String id = makeId(file); | |
| 133 if (idToFile.get(id) != null) { | |
| 134 throw new Exception("repeated id, same file used twice?"); | |
| 135 } | |
| 136 idToFile.put(id, file); | |
| 137 } | |
| 138 | |
| 139 private String makeId(File file) throws Exception { | |
| 140 String path = file.getCanonicalPath(); | |
| 141 path = path.replace(dartDir, "dartdir/"); | |
| 142 String id = path.replaceAll("/", "_"); | |
| 143 id = id.replace(".", "_"); | |
| 144 return id; | |
| 145 } | |
| 146 | |
| 147 /** | |
| 148 * create a script tag whose id is derived from the file name | |
| 149 */ | |
| 150 private static String makeScriptTag(String id, File file) throws Exception { | |
| 151 System.out.println("creating script tag id " + id); | |
| 152 return String.format("<script type=\"application//inert\" id=\"%s\">\n%s\n</
script>\n", id, readFile(file)); | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * read contents of file to string | |
| 157 */ | |
| 158 private static String readFile(File file) throws Exception { | |
| 159 StringBuilder sb = new StringBuilder(); | |
| 160 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInp
utStream(file), "UTF8")); | |
| 161 while (true) { | |
| 162 String line = reader.readLine(); | |
| 163 if (line == null) { | |
| 164 break; | |
| 165 } | |
| 166 sb.append(line).append("\n"); | |
| 167 } | |
| 168 return sb.toString(); | |
| 169 } | |
| 170 } | |
| OLD | NEW |