OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2008 Google Inc. | |
3 * | |
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not | |
5 * use this file except in compliance with the License. You may obtain a copy of | |
6 * the License at | |
7 * | |
8 * http://www.apache.org/licenses/LICENSE-2.0 | |
9 * | |
10 * Unless required by applicable law or agreed to in writing, software | |
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
13 * License for the specific language governing permissions and limitations under | |
14 * the License. | |
15 */ | |
16 package org.chromium.distiller.dev; | |
17 | |
18 import com.google.gwt.core.ext.LinkerContext; | |
19 import com.google.gwt.core.ext.TreeLogger; | |
20 import com.google.gwt.core.ext.UnableToCompleteException; | |
21 import com.google.gwt.core.ext.linker.Artifact; | |
22 import com.google.gwt.core.ext.linker.ArtifactSet; | |
23 import com.google.gwt.core.ext.linker.CompilationResult; | |
24 import com.google.gwt.core.ext.linker.EmittedArtifact; | |
25 import com.google.gwt.core.ext.linker.LinkerOrder; | |
26 import com.google.gwt.core.ext.linker.LinkerOrder.Order; | |
27 import com.google.gwt.core.ext.linker.Shardable; | |
28 import com.google.gwt.core.ext.linker.Transferable; | |
29 import com.google.gwt.core.ext.linker.impl.SelectionScriptLinker; | |
30 import com.google.gwt.dev.About; | |
31 import com.google.gwt.dev.util.DefaultTextOutput; | |
32 | |
33 import java.util.ArrayList; | |
34 import java.util.Collection; | |
35 import java.util.Set; | |
36 | |
37 /** | |
38 * A Linker for producing a single JavaScript file from a GWT module. The use of | |
39 * this Linker requires that the module has exactly one distinct compilation | |
40 * result. This linker produces less unused boilerplate than the GWT | |
41 * SingleScriptLinker. | |
42 */ | |
43 @LinkerOrder(Order.PRIMARY) | |
44 @Shardable | |
45 public class DistillerScriptLinker extends SelectionScriptLinker { | |
46 @Override | |
47 public String getDescription() { | |
48 return "Distiller Script"; | |
49 } | |
50 | |
51 @Transferable | |
52 private static class Script extends Artifact<Script> { | |
53 private final String javaScript; | |
54 private final String strongName; | |
55 | |
56 public Script(String strongName, String javaScript) { | |
57 super(DistillerScriptLinker.class); | |
58 this.strongName = strongName; | |
59 this.javaScript = javaScript; | |
60 } | |
61 | |
62 @Override | |
63 public int compareToComparableArtifact(Script that) { | |
64 int res = strongName.compareTo(that.strongName); | |
65 if (res == 0) { | |
66 res = javaScript.compareTo(that.javaScript); | |
67 } | |
68 return res; | |
69 } | |
70 | |
71 @Override | |
72 public Class<Script> getComparableArtifactType() { | |
73 return Script.class; | |
74 } | |
75 | |
76 public String getJavaScript() { | |
77 return javaScript; | |
78 } | |
79 | |
80 public String getStrongName() { | |
81 return strongName; | |
82 } | |
83 | |
84 @Override | |
85 public int hashCode() { | |
86 return strongName.hashCode() ^ javaScript.hashCode(); | |
87 } | |
88 | |
89 @Override | |
90 public String toString() { | |
91 return "Script " + strongName; | |
92 } | |
93 } | |
94 | |
95 @Override | |
96 protected Collection<Artifact<?>> doEmitCompilation(TreeLogger logger, | |
97 LinkerContext context, CompilationResult result, ArtifactSet artifacts) | |
98 throws UnableToCompleteException { | |
99 | |
100 String[] js = result.getJavaScript(); | |
101 if (js.length != 1) { | |
102 logger.branch(TreeLogger.ERROR, | |
103 "The module must not have multiple fragments when using the " | |
104 + getDescription() + " Linker.", null); | |
105 throw new UnableToCompleteException(); | |
106 } | |
107 | |
108 Collection<Artifact<?>> toReturn = new ArrayList<Artifact<?>>(); | |
109 toReturn.add(new Script(result.getStrongName(), js[0])); | |
110 toReturn.addAll(emitSelectionInformation(result.getStrongName(), result)); | |
111 return toReturn; | |
112 } | |
113 | |
114 @Override | |
115 protected EmittedArtifact emitSelectionScript(TreeLogger logger, | |
116 LinkerContext context, ArtifactSet artifacts) | |
117 throws UnableToCompleteException { | |
118 | |
119 // Find the single Script result. | |
120 Set<Script> results = artifacts.find(Script.class); | |
121 if (results.size() != 1) { | |
122 logger.log(TreeLogger.ERROR, "The module must have exactly one distinct" | |
123 + " permutation when using the " + getDescription() + " Linker; found
" + results.size(), | |
124 null); | |
125 throw new UnableToCompleteException(); | |
126 } | |
127 Script result = results.iterator().next(); | |
128 | |
129 DefaultTextOutput out = new DefaultTextOutput(true); | |
130 | |
131 // Emit the module's JS as a closure. | |
132 out.print("(function () {"); | |
133 out.newlineOpt(); | |
134 out.print("var $gwt_version = \"" + About.getGwtVersionNum() + "\";"); | |
135 out.newlineOpt(); | |
136 out.print("var $wnd = window; /* our linker */"); | |
137 out.newlineOpt(); | |
138 out.print("var $doc = $wnd.document;"); | |
139 out.newlineOpt(); | |
140 out.print("var $moduleName, $moduleBase;"); | |
141 out.newlineOpt(); | |
142 out.print("var $stats = $wnd.__gwtStatsEvent ? function(a) {$wnd.__gwtStatsE
vent(a)} : null;"); | |
143 out.newlineOpt(); | |
144 | |
145 out.print("var $strongName = '" + result.getStrongName() + "';"); | |
146 out.newlineOpt(); | |
147 | |
148 out.print(result.getJavaScript()); | |
149 | |
150 out.newlineOpt(); | |
151 out.print("gwtOnLoad(undefined, \"" + context.getModuleFunctionName() + "\",
\"\", 0);"); | |
152 out.newlineOpt(); | |
153 out.print("})();"); | |
154 out.newlineOpt(); | |
155 | |
156 return emitString(logger, out.toString(), context.getModuleName() | |
157 + ".nocache.js"); | |
158 } | |
159 | |
160 /** | |
161 * Unimplemented. Normally required by | |
162 * {@link #doEmitCompilation(TreeLogger, LinkerContext, CompilationResult, Art
ifactSet)}. | |
163 */ | |
164 @Override | |
165 protected String getCompilationExtension(TreeLogger logger, | |
166 LinkerContext context) throws UnableToCompleteException { | |
167 throw new UnableToCompleteException(); | |
168 } | |
169 | |
170 /** | |
171 * Unimplemented. Normally required by | |
172 * {@link #doEmitCompilation(TreeLogger, LinkerContext, CompilationResult, Art
ifactSet)}. | |
173 */ | |
174 @Override | |
175 protected String getModulePrefix(TreeLogger logger, LinkerContext context, | |
176 String strongName) throws UnableToCompleteException { | |
177 throw new UnableToCompleteException(); | |
178 } | |
179 | |
180 @Override | |
181 protected String getSelectionScriptTemplate(TreeLogger logger, LinkerContext c
ontext) | |
182 throws UnableToCompleteException { | |
183 return "com/google/gwt/core/linker/SingleScriptTemplate.js"; | |
184 } | |
185 } | |
OLD | NEW |