use of org.stjs.generator.STJSClass in project st-js by st-js.
the class AbstractSTJSMojo method packFiles.
/**
* packs all the files in a single file
*
* @param generator
* a {@link org.stjs.generator.Generator} object.
* @param gendir
* a {@link org.stjs.generator.GenerationDirectory} object.
* @throws org.apache.maven.plugin.MojoFailureException
* @throws org.apache.maven.plugin.MojoExecutionException
*/
protected void packFiles(Generator generator, GenerationDirectory gendir) throws MojoFailureException, MojoExecutionException {
if (!pack) {
return;
}
OutputStream allSourcesFile = null;
Writer packMapStream = null;
ClassLoader builtProjectClassLoader = getBuiltProjectClassLoader();
Map<String, File> currentProjectsFiles = new HashMap<String, File>();
// pack the files
try {
DirectedGraph<String, DefaultEdge> dependencyGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
File outputFile = new File(gendir.getGeneratedSourcesAbsolutePath(), project.getArtifactId() + ".js");
allSourcesFile = new BufferedOutputStream(new FileOutputStream(outputFile));
for (String sourceRoot : getCompileSourceRoots()) {
File sourceDir = new File(sourceRoot);
List<File> sources = new ArrayList<File>();
SourceMapping mapping = new SuffixMapping(".java", ".js");
SourceMapping stjsMapping = new SuffixMapping(".java", ".stjs");
// take all the files
sources = accumulateSources(gendir, sourceDir, mapping, stjsMapping, Integer.MIN_VALUE);
for (File source : sources) {
File absoluteTarget = (File) mapping.getTargetFiles(gendir.getGeneratedSourcesAbsolutePath(), source.getPath()).iterator().next();
String className = getClassNameForSource(source.getPath());
if (!absoluteTarget.exists()) {
getLog().debug(className + " is a bridge. Don't add it to the pack file");
continue;
}
// add this file to the hashmap to know that this class is part of the project
currentProjectsFiles.put(className, absoluteTarget);
if (getLog().isDebugEnabled()) {
getLog().debug("Packing " + absoluteTarget);
}
ClassWithJavascript cjs = generator.getExistingStjsClass(builtProjectClassLoader, builtProjectClassLoader.loadClass(className));
dependencyGraph.addVertex(className);
for (Map.Entry<ClassWithJavascript, DependencyType> dep : cjs.getDirectDependencyMap().entrySet()) {
if (dep.getKey() instanceof STJSClass) {
dependencyGraph.addVertex(dep.getKey().getJavaClassName());
if (dep.getValue() != DependencyType.OTHER) {
dependencyGraph.addEdge(dep.getKey().getJavaClassName(), className);
}
}
}
}
}
// check for cycles
detectCycles(dependencyGraph);
// dump all the files in the dependency order in the pack file
SourceMapGeneratorV3 packSourceMap = (SourceMapGeneratorV3) SourceMapGeneratorFactory.getInstance(SourceMapFormat.V3);
int currentLine = 0;
Iterator<String> it = new TopologicalOrderIterator<String, DefaultEdge>(dependencyGraph);
while (it.hasNext()) {
File targetFile = currentProjectsFiles.get(it.next());
// target file is absolute
if (targetFile != null) {
// for this project's files
if (generateSourceMap) {
currentLine = SourceMapUtils.appendFileSkipSourceMap(gendir.getGeneratedSourcesAbsolutePath(), allSourcesFile, targetFile, currentLine, packSourceMap, sourceEncoding);
} else {
Files.copy(targetFile, allSourcesFile);
}
allSourcesFile.flush();
}
}
if (generateSourceMap) {
File packMapFile = new File(gendir.getGeneratedSourcesAbsolutePath(), project.getArtifactId() + ".map");
packMapStream = new BufferedWriter(new FileWriter(packMapFile));
packSourceMap.appendTo(packMapStream, project.getArtifactId() + ".js");
allSourcesFile.write(("//# sourceMappingURL=" + project.getArtifactId() + ".map\n").getBytes());
allSourcesFile.flush();
}
} catch (Exception ex) {
throw new MojoFailureException("Error when packing files:" + ex.getMessage(), ex);
} finally {
try {
Closeables.close(allSourcesFile, true);
} catch (IOException e) {
LOG.log(Level.SEVERE, "IOException should not have been thrown.", e);
}
try {
Closeables.close(packMapStream, true);
} catch (IOException e) {
LOG.log(Level.SEVERE, "IOException should not have been thrown.", e);
}
}
}
Aggregations