use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping 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);
}
}
}
use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project st-js by st-js.
the class AbstractSTJSMojo method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
GenerationDirectory gendir = getGeneratedSourcesDirectory();
long t1 = System.currentTimeMillis();
getLog().info("Generating JavaScript files to " + gendir.getGeneratedSourcesAbsolutePath());
ClassLoader builtProjectClassLoader = getBuiltProjectClassLoader();
GeneratorConfigurationBuilder configBuilder = new GeneratorConfigurationBuilder();
configBuilder.generateArrayHasOwnProperty(generateArrayHasOwnProperty);
configBuilder.generateSourceMap(generateSourceMap);
if (sourceEncoding != null) {
configBuilder.sourceEncoding(sourceEncoding);
}
// configBuilder.allowedPackage("org.stjs.javascript");
configBuilder.allowedPackage("org.junit");
if (allowedPackages != null) {
configBuilder.allowedPackages(allowedPackages);
}
if (annotations != null) {
configBuilder.annotations(annotations);
}
// scan all the packages
for (String sourceRoot : getCompileSourceRoots()) {
File sourceDir = new File(sourceRoot);
Collection<String> packages = accumulatePackages(sourceDir);
configBuilder.allowedPackages(packages);
}
configBuilder.stjsClassLoader(builtProjectClassLoader);
configBuilder.targetFolder(getBuildOutputDirectory());
configBuilder.generationFolder(gendir);
GeneratorConfiguration configuration = configBuilder.build();
Generator generator = new Generator(configuration);
int generatedFiles = 0;
boolean hasFailures = false;
// scan the modified sources
for (String sourceRoot : getCompileSourceRoots()) {
File sourceDir = new File(sourceRoot);
SourceMapping mapping = new SuffixMapping(".java", ".js");
SourceMapping stjsMapping = new SuffixMapping(".java", ".stjs");
List<File> sources = accumulateSources(gendir, sourceDir, mapping, stjsMapping, staleMillis);
for (File source : sources) {
if (source.getName().equals(PACKAGE_INFO_JAVA)) {
getLog().debug("Skipping " + source);
continue;
}
File absoluteSource = new File(sourceDir, source.getPath());
try {
File absoluteTarget = (File) mapping.getTargetFiles(gendir.getGeneratedSourcesAbsolutePath(), source.getPath()).iterator().next();
if (getLog().isDebugEnabled()) {
getLog().debug("Generating " + absoluteTarget);
}
buildContext.removeMessages(absoluteSource);
if (!absoluteTarget.getParentFile().exists() && !absoluteTarget.getParentFile().mkdirs()) {
getLog().error("Cannot create output directory:" + absoluteTarget.getParentFile());
continue;
}
String className = getClassNameForSource(source.getPath());
ClassWithJavascript stjsClass = generator.generateJavascript(className, sourceDir);
if (!(stjsClass instanceof BridgeClass)) {
++generatedFiles;
}
} catch (InclusionScanException e) {
throw new MojoExecutionException("Cannot scan the source directory:" + e, e);
} catch (MultipleFileGenerationException e) {
for (JavascriptFileGenerationException jse : e.getExceptions()) {
buildContext.addMessage(jse.getSourcePosition().getFile(), jse.getSourcePosition().getLine(), jse.getSourcePosition().getColumn(), jse.getMessage(), BuildContext.SEVERITY_ERROR, null);
}
hasFailures = true;
// continue with the next file
} catch (JavascriptFileGenerationException e) {
buildContext.addMessage(e.getSourcePosition().getFile(), e.getSourcePosition().getLine(), e.getSourcePosition().getColumn(), e.getMessage(), BuildContext.SEVERITY_ERROR, null);
hasFailures = true;
// continue with the next file
} catch (Exception e) {
// TODO - maybe should filter more here
buildContext.addMessage(absoluteSource, 1, 1, e.toString(), BuildContext.SEVERITY_ERROR, e);
hasFailures = true;
// throw new MojoExecutionException("Error generating javascript:" + e, e);
}
}
}
generator.close();
long t2 = System.currentTimeMillis();
getLog().info("Generated " + generatedFiles + " JavaScript files in " + (t2 - t1) + " ms");
if (generatedFiles > 0) {
filesGenerated(generator, gendir);
}
if (hasFailures) {
throw new MojoFailureException("Errors generating JavaScript");
}
}
use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project antlr4 by tunnelvisionlabs.
the class Antlr4Mojo method getImportFiles.
private Set<File> getImportFiles(File sourceDirectory) throws InclusionScanException {
if (!libDirectory.exists())
return Collections.emptySet();
Set<String> includes = new HashSet<String>();
includes.add("*.g4");
includes.add("*.tokens");
SourceInclusionScanner scan = new SimpleSourceInclusionScanner(includes, Collections.<String>emptySet());
scan.addSourceMapping(new SuffixMapping("G4", "g4"));
return scan.getIncludedSources(libDirectory, null);
}
use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project antlr4 by antlr.
the class Antlr4Mojo method getGrammarFiles.
private Set<File> getGrammarFiles(File sourceDirectory) throws InclusionScanException {
// Which files under the source set should we be looking for as grammar files
SourceMapping mapping = new SuffixMapping("g4", Collections.<String>emptySet());
// What are the sets of includes (defaulted or otherwise).
Set<String> includes = getIncludesPatterns();
// Now, to the excludes, we need to add the imports directory
// as this is autoscanned for imported grammars and so is auto-excluded from the
// set of grammar fields we should be analyzing.
excludes.add("imports/**");
SourceInclusionScanner scan = new SimpleSourceInclusionScanner(includes, excludes);
scan.addSourceMapping(mapping);
return scan.getIncludedSources(sourceDirectory, null);
}
use of org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping in project jangaroo-tools by CoreMedia.
the class MavenPluginHelper method computeStaleSources.
public List<File> computeStaleSources(List<File> compileSourceRoots, Set<String> includes, Set<String> excludes, File outputDirectory, String inputFileSuffix, String outputFileSuffix, int staleMillis) throws MojoExecutionException {
SourceInclusionScanner scanner = createSourceInclusionScanner(includes, excludes, inputFileSuffix, staleMillis);
scanner.addSourceMapping(new SuffixMapping(inputFileSuffix, outputFileSuffix));
log.debug("Searching for");
Set<File> staleSources = new LinkedHashSet<File>();
for (File rootFile : compileSourceRoots) {
if (!rootFile.isDirectory()) {
continue;
}
try {
log.debug("scanner.getIncludedSources(" + rootFile + ", " + outputDirectory + ")");
// noinspection unchecked
staleSources.addAll(scanner.getIncludedSources(rootFile, outputDirectory));
} catch (InclusionScanException e) {
throw new MojoExecutionException("Error scanning source root: \'" + rootFile.getAbsolutePath() + "\' " + "for stale files to recompile.", e);
}
}
return Collections.unmodifiableList(new ArrayList<File>(staleSources));
}
Aggregations