public AntlrSpec create(AntlrTask antlrTask, Set<File> grammarFiles, SourceDirectorySet sourceDirectorySet) { List<String> arguments = Lists.newLinkedList(antlrTask.getArguments()); if (antlrTask.isTrace() && !arguments.contains("-trace")) { arguments.add("-trace"); } if (antlrTask.isTraceLexer() && !arguments.contains("-traceLexer")) { arguments.add("-traceLexer"); } if (antlrTask.isTraceParser() && !arguments.contains("-traceParser")) { arguments.add("-traceParser"); } if (antlrTask.isTraceTreeWalker() && !arguments.contains("-traceTreeWalker")) { arguments.add("-traceTreeWalker"); } return new AntlrSpec(arguments, grammarFiles, sourceDirectorySet.getSrcDirs(), antlrTask.getOutputDirectory(), antlrTask.getMaxHeapSize()); }
private void renderSourceSetDirectories(LanguageSourceSet sourceSet, TextReportBuilder builder) { Set<File> srcDirs = sourceSet.getSource().getSrcDirs(); if (srcDirs.isEmpty()) { builder.item("No source directories"); } else { for (File file : srcDirs) { builder.item("srcDir", file); } SourceDirectorySet source = sourceSet.getSource(); Set<String> includes = source.getIncludes(); if (!includes.isEmpty()) { builder.item("includes", includes); } Set<String> excludes = source.getExcludes(); if (!excludes.isEmpty()) { builder.item("excludes", excludes); } Set<String> filterIncludes = source.getFilter().getIncludes(); if (!filterIncludes.isEmpty()) { builder.item("limit to", filterIncludes); } } }
List<File> getSourceRoots() { if (sourceRoots == null) { sourceRoots = Lists.newArrayList(); for (Object source : sources) { if (isDirectory(source)) { sourceRoots.add((File) source); } else if (isDirectoryTree(source)) { sourceRoots.add(((DirectoryTree) source).getDir()); } else if (isSourceDirectorySet(source)) { sourceRoots.addAll(((SourceDirectorySet) source).getSrcDirs()); } else { throw new UnsupportedOperationException(); } } } return sourceRoots; }
private Set<DirectoryTree> doGetSrcDirTrees() { Set<DirectoryTree> result = new LinkedHashSet<DirectoryTree>(); for (Object path : source) { if (path instanceof SourceDirectorySet) { SourceDirectorySet nested = (SourceDirectorySet) path; result.addAll(nested.getSrcDirTrees()); } else { for (File srcDir : fileResolver.resolveFiles(path)) { if (srcDir.exists() && !srcDir.isDirectory()) { throw new InvalidUserDataException(String.format("Source directory '%s' is not a directory.", srcDir)); } result.add(directoryFileTreeFactory.create(srcDir, patterns)); } } } return result; }
private void createCompileJavaTaskForBinary(final SourceSet sourceSet, SourceDirectorySet javaSourceSet, Project target) { JavaCompile compileTask = target.getTasks().create(sourceSet.getCompileJavaTaskName(), JavaCompile.class); compileTask.setDescription("Compiles " + javaSourceSet + "."); compileTask.setSource(javaSourceSet); ConventionMapping conventionMapping = compileTask.getConventionMapping(); conventionMapping.map("classpath", new Callable<Object>() { public Object call() throws Exception { return sourceSet.getCompileClasspath(); } }); conventionMapping.map("destinationDir", new Callable<Object>() { public Object call() throws Exception { return sourceSet.getOutput().getClassesDir(); } }); }
/** * Set the default directory for each source sets if it is empty. */ public void setDefaultSrcDir() { all(new Action<FunctionalSourceSet>() { @Override public void execute(final FunctionalSourceSet functionalSourceSet) { functionalSourceSet.all( new Action<LanguageSourceSet>() { @Override public void execute(LanguageSourceSet languageSourceSet) { SourceDirectorySet source = languageSourceSet.getSource(); if (source.getSrcDirs().isEmpty()) { source.srcDir("src/" + functionalSourceSet.getName() + "/" + languageSourceSet.getName()); } } }); } }); }
/** * Convert a FunctionalSourceSet to an AndroidSourceFile. */ private static void convertSourceFile( AndroidSourceFile androidFile, FunctionalSourceSet source, String sourceName) { LanguageSourceSet languageSourceSet = source.findByName(sourceName); if (languageSourceSet == null) { return; } SourceDirectorySet dir = languageSourceSet.getSource(); if (dir == null) { return; } // We use the first file in the file tree until Gradle has a way to specify one source file // instead of an entire source set. Set<File> files = dir.getAsFileTree().getFiles(); if (!files.isEmpty()) { androidFile.srcFile(Iterables.getOnlyElement(files)); } }
/** * Convert a FunctionalSourceSet to an AndroidSourceDirectorySet. */ private static void convertSourceSet( AndroidSourceDirectorySet androidDir, FunctionalSourceSet source, String sourceName) { LanguageSourceSet languageSourceSet = source.findByName(sourceName); if (languageSourceSet == null) { return; } SourceDirectorySet dir = languageSourceSet.getSource(); if (dir == null) { return; } androidDir.setSrcDirs(dir.getSrcDirs()); androidDir.include(dir.getIncludes()); androidDir.exclude(dir.getExcludes()); }
private Set<DirectoryTree> doGetSrcDirTrees() { Set<DirectoryTree> result = new LinkedHashSet<DirectoryTree>(); for (Object path : source) { if (path instanceof SourceDirectorySet) { SourceDirectorySet nested = (SourceDirectorySet) path; result.addAll(nested.getSrcDirTrees()); } else { File srcDir = fileResolver.resolve(path); if (srcDir.exists() && !srcDir.isDirectory()) { throw new InvalidUserDataException(String.format("Source directory '%s' is not a directory.", srcDir)); } result.add(new DirectoryFileTree(srcDir, patterns)); } } return result; }
private Collection<File> tryToGetSourceDirectoriesFromSourceSet(final Object sourceSet) { if (sourceSet == null) { return null; } if (sourceSet instanceof SourceDirectorySet) { final SourceDirectorySet set = (SourceDirectorySet) sourceSet; return set.getSrcDirs(); } final String methodName = "getSrcDirs"; final Object srcDirs = tryToInvokeMethod(sourceSet, methodName); if (srcDirs instanceof Collection<?>) { try { return (Collection<File>) srcDirs; } catch (ClassCastException e) { LOGGER.debug("Can not cast result of '" + methodName + "' to Collection<File>"); } } return null; }
private static void configureSourceSetDefaults(final Project project, final SourceDirectorySetFactory sourceDirectorySetFactory) { final JavaBasePlugin javaPlugin = project.getPlugins().getPlugin(JavaBasePlugin.class); project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() { @Override public void execute(final SourceSet sourceSet) { String displayName = (String) InvokerHelper.invokeMethod(sourceSet, "getDisplayName", null); Convention sourceSetConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention"); DefaultScalaSourceSet scalaSourceSet = new DefaultScalaSourceSet(displayName, sourceDirectorySetFactory); sourceSetConvention.getPlugins().put("scala", scalaSourceSet); final SourceDirectorySet scalaDirectorySet = scalaSourceSet.getScala(); scalaDirectorySet.srcDir(new Callable<File>() { @Override public File call() throws Exception { return project.file("src/" + sourceSet.getName() + "/scala"); } }); sourceSet.getAllJava().source(scalaDirectorySet); sourceSet.getAllSource().source(scalaDirectorySet); sourceSet.getResources().getFilter().exclude(new Spec<FileTreeElement>() { @Override public boolean isSatisfiedBy(FileTreeElement element) { return scalaDirectorySet.contains(element.getFile()); } }); configureScalaCompile(project, javaPlugin, sourceSet); } }); }
@Override public void visitDependencies(TaskDependencyResolveContext context) { for (Object path : source) { if (path instanceof SourceDirectorySet) { context.add(((SourceDirectorySet) path).getBuildDependencies()); } else { context.add(fileResolver.resolveFiles(path)); } } }
private void createProcessResourcesTaskForBinary(final SourceSet sourceSet, SourceDirectorySet resourceSet, final Project target) { Copy resourcesTask = target.getTasks().create(sourceSet.getProcessResourcesTaskName(), ProcessResources.class); resourcesTask.setDescription("Processes " + resourceSet + "."); new DslObject(resourcesTask).getConventionMapping().map("destinationDir", new Callable<File>() { public File call() throws Exception { return sourceSet.getOutput().getResourcesDir(); } }); resourcesTask.from(resourceSet); }
@Internal private List<File> getSourceRootsFiles() { // accessing the List<Object> field not the FileTree from getSource return source.stream() .filter(it -> it instanceof SourceDirectorySet) .flatMap(it -> ((SourceDirectorySet) it).getSrcDirs().stream()) .collect(Collectors.toList()); }
@TaskAction public void generateClasses() throws IOException { // Clean the destination directory getProject().delete(getDestinationDir()); // Initialize spoon SpoonAPI spoon = new Launcher(); spoon.addProcessor(EVENT_CLASS_PROCESSOR); final Environment environment = spoon.getEnvironment(); environment.setComplianceLevel(Integer.parseInt(JavaVersion.toVersion(getSourceCompatibility()).getMajorVersion())); environment.setNoClasspath(!this.validateCode); // Configure AST generator final SpoonModelBuilder compiler = spoon.createCompiler(); compiler.setSourceClasspath(toPathArray(getClasspath().getFiles())); for (Object source : this.source) { if (!(source instanceof SourceDirectorySet)) { throw new UnsupportedOperationException("Source of type " + source.getClass() + " is not supported."); } ((SourceDirectorySet) source).getSrcDirs().forEach(compiler::addInputSource); } this.factory = compiler.getFactory(); // Generate AST compiler.build(); // Analyse AST final EventInterfaceProcessor processor = new EventInterfaceProcessor(getSource()); compiler.process(Collections.singletonList(processor)); final Map<CtType<?>, List<Property>> foundProperties = processor.getFoundProperties(); this.sorter = new PropertySorter(this.sortPriorityPrefix, this.groupingPrefixes); dumpClasses(foundProperties); }
public AbstractLanguageSourceSet(String name, FunctionalSourceSet parent, String typeName, SourceDirectorySet source) { this.name = name; this.fullName = parent.getName() + StringUtils.capitalize(name); this.displayName = String.format("%s '%s:%s'", typeName, parent.getName(), name); this.source = source; super.builtBy(source.getBuildDependencies()); }
List<File> getSourceDirs() { List<File> sourceDirs = new LinkedList<File>(); for (Object s : source) { if (s instanceof SourceDirectorySet) { sourceDirs.addAll(((SourceDirectorySet) s).getSrcDirs()); } else { throw new UnsupportedOperationException(); } } return sourceDirs; }
public boolean areSourceDirsKnown() { for (Object s : source) { if (!(s instanceof SourceDirectorySet)) { return false; } } return true; }
private void maybeSetSourceDir(SourceDirectorySet sourceSet, Task task, String propertyName) { // TODO:DAZ Handle multiple output directories Object value = task.property(propertyName); if (value != null) { sourceSet.srcDir(value); } }
private List<File> getSourceDirs() { List<File> sourceDirs = new LinkedList<File>(); for (Object s : source) { if (s instanceof SourceDirectorySet) { sourceDirs.addAll(((SourceDirectorySet) s).getSrcDirs()); } else { return emptyList(); } } return sourceDirs; }
private void collectUnknown(@Nonnull Collector collector, @Nonnull Iterable<Object> sources) { for (Object source : sources) { getLogger().info("Attepmting to collect " + source.getClass() + ":" + source); if (source instanceof File) collectDir(collector, (File) source); else if (source instanceof SourceDirectorySet) collectDirs(collector, ((SourceDirectorySet) source).getSrcDirs()); // I wish we could introspect CompositeFileTree. } }
@Override public void apply(final Project project) { boolean javaPlugin = project.getPlugins().hasPlugin(JavaPlugin.class); boolean warPlugin = project.getPlugins().hasPlugin(WarPlugin.class); final Logger logger = project.getLogger(); if (!(javaPlugin && warPlugin)) { logger.error("st-js plugin can only be applied if jar or war plugin is applied, too!"); throw new IllegalStateException("st-js plugin can only be applied if jar or war plugin is applied, too!"); } final JavaPluginConvention javaPluginConvention = project.getConvention().getPlugin(JavaPluginConvention.class); final SourceSet main = javaPluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME); final SourceDirectorySet allJava = main.getAllJava(); if (allJava.getSrcDirs().size() != 1) { throw new IllegalStateException("Only a single source directory is supported!"); } final GenerateStJsTask task = project.getTasks().create("stjs", GenerateStJsTask.class); task.setClasspath( main.getCompileClasspath() ); task.setWar(warPlugin); File generatedSourcesDirectory; if (warPlugin) { generatedSourcesDirectory = new File(project.getBuildDir(), "stjs"); project.getTasks().getByPath(WarPlugin.WAR_TASK_NAME).dependsOn(task); } else { generatedSourcesDirectory = main.getOutput().getClassesDir(); project.getTasks().getByPath(JavaPlugin.JAR_TASK_NAME).dependsOn(task); } task.setGeneratedSourcesDirectory(generatedSourcesDirectory); task.setCompileSourceRoots(allJava); task.setOutput(main.getOutput()); }
public AbstractLanguageSourceSet(ComponentSpecIdentifier identifier, Class<? extends BuildableComponentSpec> publicType, SourceDirectorySet source) { super(identifier, publicType); this.source = source; this.languageName = guessLanguageName(getTypeName()); super.builtBy(source.getBuildDependencies()); }
@Override public SourceDirectorySet getSource() { return source; }
public SourceDirectorySet getAntlr() { return antlr; }
public SourceDirectorySet getScala() { return scala; }
@Override public ScalaSourceSet scala(Action<? super SourceDirectorySet> configureAction) { configureAction.execute(getScala()); return this; }
public SourceDirectorySet getAllScala() { return allScala; }
@Override public SourceDirectorySet getHeaders() { return headers; }
private void maybeSetSourceDir(SourceDirectorySet sourceSet, Task task, String propertyName) { Object value = task.property(propertyName); if (value != null) { sourceSet.srcDir(value); } }
private boolean isSourceDirectorySet(Object source) { return source instanceof SourceDirectorySet; }
@Override public SourceDirectorySet getExportedHeaders() { return exportedHeaders; }
@Override public SourceDirectorySet getImplicitHeaders() { return implicitHeaders; }
public SourceDirectorySet setSrcDirs(Iterable<?> srcPaths) { source.clear(); GUtil.addToCollection(source, srcPaths); return this; }
public DefaultJvmResourceSet(ComponentSpecIdentifier componentIdentifier, SourceDirectorySet source) { super(componentIdentifier, JvmResourceSet.class, source); }
public DefaultJavaSourceSet(ComponentSpecIdentifier componentIdentifier, SourceDirectorySet source, Classpath compileClasspath) { super(componentIdentifier, JavaSourceSet.class, source); this.compileClasspath = compileClasspath; }
public SourceDirectorySet getGroovy() { return groovy; }
@Override public GroovySourceSet groovy(Action<? super SourceDirectorySet> configureAction) { configureAction.execute(getGroovy()); return this; }