public Spec<FileTreeElement> createExcludeSpec(PatternSet patternSet) { List<Spec<FileTreeElement>> allExcludeSpecs = new ArrayList<Spec<FileTreeElement>>(2 + patternSet.getExcludeSpecs().size()); if (!patternSet.getExcludes().isEmpty()) { allExcludeSpecs.add(createSpec(patternSet.getExcludes(), false, patternSet.isCaseSensitive())); } List<String> defaultExcludes = Arrays.asList(DirectoryScanner.getDefaultExcludes()); if (!defaultExcludes.isEmpty()) { allExcludeSpecs.add(createSpec(defaultExcludes, false, patternSet.isCaseSensitive())); } allExcludeSpecs.addAll(patternSet.getExcludeSpecs()); if (allExcludeSpecs.isEmpty()) { return Specs.satisfyNone(); } else { return Specs.union(allExcludeSpecs); } }
private void configureSourceSetDefaults(final JavaBasePlugin javaBasePlugin) { project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() { public void execute(SourceSet sourceSet) { final DefaultGroovySourceSet groovySourceSet = new DefaultGroovySourceSet(((DefaultSourceSet) sourceSet).getDisplayName(), sourceDirectorySetFactory); new DslObject(sourceSet).getConvention().getPlugins().put("groovy", groovySourceSet); groovySourceSet.getGroovy().srcDir("src/" + sourceSet.getName() + "/groovy"); sourceSet.getResources().getFilter().exclude(new Spec<FileTreeElement>() { public boolean isSatisfiedBy(FileTreeElement element) { return groovySourceSet.getGroovy().contains(element.getFile()); } }); sourceSet.getAllJava().source(groovySourceSet.getGroovy()); sourceSet.getAllSource().source(groovySourceSet.getGroovy()); String compileTaskName = sourceSet.getCompileTaskName("groovy"); GroovyCompile compile = project.getTasks().create(compileTaskName, GroovyCompile.class); javaBasePlugin.configureForSourceSet(sourceSet, compile); compile.dependsOn(sourceSet.getCompileJavaTaskName()); compile.setDescription("Compiles the " + sourceSet.getName() + " Groovy source."); compile.setSource(groovySourceSet.getGroovy()); project.getTasks().getByName(sourceSet.getClassesTaskName()).dependsOn(compileTaskName); } }); }
public DefaultSourceSet(String name, FileResolver fileResolver) { this.name = name; displayName = GUtil.toWords(this.name); String javaSrcDisplayName = String.format("%s Java source", displayName); javaSource = new DefaultSourceDirectorySet(javaSrcDisplayName, fileResolver); javaSource.getFilter().include("**/*.java"); allJavaSource = new DefaultSourceDirectorySet(javaSrcDisplayName, fileResolver); allJavaSource.getFilter().include("**/*.java"); allJavaSource.source(javaSource); String resourcesDisplayName = String.format("%s resources", displayName); resources = new DefaultSourceDirectorySet(resourcesDisplayName, fileResolver); resources.getFilter().exclude(new Spec<FileTreeElement>() { public boolean isSatisfiedBy(FileTreeElement element) { return javaSource.contains(element.getFile()); } }); String allSourceDisplayName = String.format("%s source", displayName); allSource = new DefaultSourceDirectorySet(allSourceDisplayName, fileResolver); allSource.source(resources); allSource.source(javaSource); }
private void configureSourceSetDefaults(final JavaBasePlugin javaBasePlugin) { project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() { public void execute(SourceSet sourceSet) { final DefaultGroovySourceSet groovySourceSet = new DefaultGroovySourceSet(((DefaultSourceSet) sourceSet).getDisplayName(), fileResolver); new DslObject(sourceSet).getConvention().getPlugins().put("groovy", groovySourceSet); groovySourceSet.getGroovy().srcDir(String.format("src/%s/groovy", sourceSet.getName())); sourceSet.getResources().getFilter().exclude(new Spec<FileTreeElement>() { public boolean isSatisfiedBy(FileTreeElement element) { return groovySourceSet.getGroovy().contains(element.getFile()); } }); sourceSet.getAllJava().source(groovySourceSet.getGroovy()); sourceSet.getAllSource().source(groovySourceSet.getGroovy()); String compileTaskName = sourceSet.getCompileTaskName("groovy"); GroovyCompile compile = project.getTasks().create(compileTaskName, GroovyCompile.class); javaBasePlugin.configureForSourceSet(sourceSet, compile); compile.dependsOn(sourceSet.getCompileJavaTaskName()); compile.setDescription(String.format("Compiles the %s Groovy source.", sourceSet.getName())); compile.setSource(groovySourceSet.getGroovy()); project.getTasks().getByName(sourceSet.getClassesTaskName()).dependsOn(compileTaskName); } }); }
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 protected Spec<FileTreeElement> createSpec(final Collection<String> patterns, final boolean include, final boolean caseSensitive) { final SpecKey key = new SpecKey(ImmutableList.copyOf(patterns), include, caseSensitive); try { return Cast.uncheckedCast(specInstanceCache.get(key, new Callable<Spec<FileTreeElement>>() { @Override public Spec<FileTreeElement> call() throws Exception { Spec<FileTreeElement> spec = CachingPatternSpecFactory.super.createSpec(patterns, include, caseSensitive); return new CachingSpec(key, spec); } })); } catch (ExecutionException e) { throw UncheckedException.throwAsUncheckedException(e); } }
@Override public boolean isSatisfiedBy(final FileTreeElement element) { CacheKey cacheKey = new CacheKey(element.getRelativePath(), key); try { return specResultCache.get(cacheKey, new Callable<Boolean>() { @Override public Boolean call() throws Exception { return spec.isSatisfiedBy(element); } }); } catch (ExecutionException e) { throw UncheckedException.throwAsUncheckedException(e); } }
public Spec<FileTreeElement> createIncludeSpec(PatternSet patternSet) { List<Spec<FileTreeElement>> allIncludeSpecs = new ArrayList<Spec<FileTreeElement>>(1 + patternSet.getIncludeSpecs().size()); if (!patternSet.getIncludes().isEmpty()) { allIncludeSpecs.add(createSpec(patternSet.getIncludes(), true, patternSet.isCaseSensitive())); } allIncludeSpecs.addAll(patternSet.getIncludeSpecs()); return Specs.union(allIncludeSpecs); }
protected Spec<FileTreeElement> createSpec(Collection<String> patterns, boolean include, boolean caseSensitive) { if (patterns.isEmpty()) { return include ? Specs.<FileTreeElement>satisfyAll() : Specs.<FileTreeElement>satisfyNone(); } List<Spec<RelativePath>> matchers = new ArrayList<Spec<RelativePath>>(patterns.size()); for (String pattern : patterns) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(include, caseSensitive, pattern); matchers.add(patternMatcher); } return new RelativePathSpec(Specs.union(matchers)); }
public SingleIncludePatternFileTree(File baseDir, String includePattern, Spec<FileTreeElement> excludeSpec) { this.baseDir = baseDir; if (includePattern.endsWith("/") || includePattern.endsWith("\\")) { includePattern += "**"; } this.includePattern = includePattern; this.patternSegments = Arrays.asList(includePattern.split("[/\\\\]")); this.excludeSpec = excludeSpec; }
@Override public void walkDir(File file, RelativePath path, FileVisitor visitor, Spec<? super FileTreeElement> spec, AtomicBoolean stopFlag, boolean postfix) { File[] children = file.listFiles(); if (children == null) { if (file.isDirectory() && !file.canRead()) { throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file)); } // else, might be a link which points to nothing, or has been removed while we're visiting, or ... throw new GradleException(String.format("Could not list contents of '%s'.", file)); } List<FileVisitDetails> dirs = new ArrayList<FileVisitDetails>(); for (int i = 0; !stopFlag.get() && i < children.length; i++) { File child = children[i]; boolean isFile = child.isFile(); RelativePath childPath = path.append(isFile, child.getName()); FileVisitDetails details = new DefaultFileVisitDetails(child, childPath, stopFlag, fileSystem, fileSystem, !isFile); if (DirectoryFileTree.isAllowed(details, spec)) { if (isFile) { visitor.visitFile(details); } else { dirs.add(details); } } } // now handle dirs for (int i = 0; !stopFlag.get() && i < dirs.size(); i++) { FileVisitDetails dir = dirs.get(i); if (postfix) { walkDir(dir.getFile(), dir.getRelativePath(), visitor, spec, stopFlag, postfix); visitor.visitDir(dir); } else { visitor.visitDir(dir); walkDir(dir.getFile(), dir.getRelativePath(), visitor, spec, stopFlag, postfix); } } }
/** * Process the specified file or directory. If it is a directory, then its contents * (but not the directory itself) will be checked with {@link #isAllowed(FileTreeElement, Spec)} and notified to * the listener. If it is a file, the file will be checked and notified. */ public void visitFrom(FileVisitor visitor, File fileOrDirectory, RelativePath path) { AtomicBoolean stopFlag = new AtomicBoolean(); Spec<FileTreeElement> spec = patternSet.getAsSpec(); if (fileOrDirectory.exists()) { if (fileOrDirectory.isFile()) { processSingleFile(fileOrDirectory, visitor, spec, stopFlag); } else { walkDir(fileOrDirectory, path, visitor, spec, stopFlag); } } else { LOGGER.info("file or directory '{}', not found", fileOrDirectory); } }
private void processSingleFile(File file, FileVisitor visitor, Spec<FileTreeElement> spec, AtomicBoolean stopFlag) { RelativePath path = new RelativePath(true, file.getName()); FileVisitDetails details = new DefaultFileVisitDetails(file, path, stopFlag, fileSystem, fileSystem, false); if (isAllowed(details, spec)) { visitor.visitFile(details); } }
public List<Spec<FileTreeElement>> getAllExcludeSpecs() { List<Spec<FileTreeElement>> result = new ArrayList<Spec<FileTreeElement>>(); if (parentResolver != null) { result.addAll(parentResolver.getAllExcludeSpecs()); } result.addAll(patternSet.getExcludeSpecs()); return result; }
public List<Spec<FileTreeElement>> getAllIncludeSpecs() { List<Spec<FileTreeElement>> result = new ArrayList<Spec<FileTreeElement>>(); if (parentResolver != null) { result.addAll(parentResolver.getAllIncludeSpecs()); } result.addAll(patternSet.getIncludeSpecs()); return result; }
public DefaultSourceSet(String name, SourceDirectorySetFactory sourceDirectorySetFactory) { this.name = name; displayName = GUtil.toWords(this.name); namingScheme = new ClassDirectoryBinaryNamingScheme(name); String javaSrcDisplayName = displayName + " Java source"; javaSource = sourceDirectorySetFactory.create(javaSrcDisplayName); javaSource.getFilter().include("**/*.java"); allJavaSource = sourceDirectorySetFactory.create(javaSrcDisplayName); allJavaSource.getFilter().include("**/*.java"); allJavaSource.source(javaSource); String resourcesDisplayName = displayName + " resources"; resources = sourceDirectorySetFactory.create(resourcesDisplayName); resources.getFilter().exclude(new Spec<FileTreeElement>() { public boolean isSatisfiedBy(FileTreeElement element) { return javaSource.contains(element.getFile()); } }); String allSourceDisplayName = displayName + " source"; allSource = sourceDirectorySetFactory.create(allSourceDisplayName); allSource.source(resources); allSource.source(javaSource); }
private static Spec<FileTreeElement> getMaindexSpec(PatternSet patternSet) { Spec<FileTreeElement> maindexSpec = null; if (patternSet != null) { Spec<FileTreeElement> includeSpec = null; Spec<FileTreeElement> excludeSpec = null; if (!patternSet.getIncludes().isEmpty()) { includeSpec = patternSet.getAsIncludeSpec(); } if (!patternSet.getExcludes().isEmpty()) { excludeSpec = patternSet.getAsExcludeSpec(); } if (includeSpec != null && excludeSpec != null) { maindexSpec = new OrSpec<>(includeSpec, new NotSpec<>(excludeSpec)); } else { if (excludeSpec != null) { // only exclude maindexSpec = new NotSpec<>(excludeSpec); } else if (includeSpec != null) { // only include maindexSpec = includeSpec; } } } // if (maindexSpec == null) { // maindexSpec = Specs.satisfyAll(); // } return maindexSpec; }
/** * Gets main classes from jar. * * @param jarMergingOutputFile the jar merging output file * @param mainDexPattern the main dex pattern * @param adtMainCls the filter mapping of suggest classes * @param logFilter * @return the main classes from jar * @throws Exception the exception * @author ceabie */ private static ArrayList<String> getMainClassesFromJar( File jarMergingOutputFile, PatternSet mainDexPattern, Map<String, Boolean> adtMainCls, boolean logFilter) throws Exception { ZipFile clsFile = new ZipFile(jarMergingOutputFile); Spec<FileTreeElement> asSpec = getMaindexSpec(mainDexPattern); ClassFileTreeElement treeElement = new ClassFileTreeElement(); // lists classes from jar. ArrayList<String> mainDexList = new ArrayList<>(); Enumeration<? extends ZipEntry> entries = clsFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryName = entry.getName(); if (entryName.endsWith(CLASS_SUFFIX)) { treeElement.setClassPath(entryName); if (isAtMainDex(adtMainCls, entryName, treeElement, asSpec, logFilter)) { mainDexList.add(entryName); } } } clsFile.close(); return mainDexList; }
/** * Gets main classes from mapping. * * @param mapping the mapping file * @param mainDexPattern the main dex pattern * @param recommendMainCls the filter mapping of suggest classes * @param logFilter * @return the main classes from mapping * @throws Exception the exception * @author ceabie */ private static List<String> getMainClassesFromMapping( File mapping, PatternSet mainDexPattern, Map<String, Boolean> recommendMainCls, boolean logFilter) throws Exception { String line; List<String> mainDexList = new ArrayList<>(); BufferedReader reader = new BufferedReader(new FileReader(mapping)); // all classes ClassFileTreeElement filterElement = new ClassFileTreeElement(); Spec<FileTreeElement> asSpec = getMaindexSpec(mainDexPattern); while ((line = reader.readLine()) != null) { line = line.trim(); if (line.endsWith(":")) { int flagPos = line.indexOf(MAPPING_FLAG); if (flagPos != -1) { String sOrgCls = line.substring(0, flagPos).replace('.', '/') + CLASS_SUFFIX; String sMapCls = line.substring(flagPos + MAPPING_FLAG_LEN, line.length() - 1) .replace('.', '/') + CLASS_SUFFIX; filterElement.setClassPath(sOrgCls); if (isAtMainDex(recommendMainCls, sMapCls, filterElement, asSpec, logFilter)) { mainDexList.add(sMapCls); } } } } reader.close(); return mainDexList; }
private static boolean isAtMainDex( Map<String, Boolean> mainCls, String sMapCls, ClassFileTreeElement treeElement, Spec<FileTreeElement> asSpec, boolean logFilter) { boolean isRecommend = false; // adt推荐 if (mainCls != null) { Boolean value = mainCls.get(sMapCls); if (value != null) { isRecommend = value; } } // 全局过滤 boolean inGlobalFilter = asSpec != null && asSpec.isSatisfiedBy(treeElement); if (logFilter) { String ret; if (isRecommend && inGlobalFilter) { ret = "true"; } else if (isRecommend) { ret = "Recommend"; } else if (inGlobalFilter) { ret = "Global"; } else { ret = "false"; } String s = "AtMainDex: " + treeElement.getPath() + " [" + ret + "]"; if (isRecommend || inGlobalFilter) { System.err.println(s); } else { System.out.println(s); } } // 合并结果 return isRecommend || inGlobalFilter; }
public Spec<FileTreeElement> getAsIncludeSpec() { List<Spec<FileTreeElement>> matchers = Lists.newArrayList(); for (String include : includes) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(true, caseSensitive, include); matchers.add(new RelativePathSpec(patternMatcher)); } matchers.addAll(includeSpecs); return new OrSpec<FileTreeElement>(matchers); }
public Spec<FileTreeElement> getAsExcludeSpec() { Collection<String> allExcludes = Sets.newLinkedHashSet(excludes); Collections.addAll(allExcludes, DirectoryScanner.getDefaultExcludes()); List<Spec<FileTreeElement>> matchers = Lists.newArrayList(); for (String exclude : allExcludes) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(false, caseSensitive, exclude); matchers.add(new RelativePathSpec(patternMatcher)); } matchers.addAll(excludeSpecs); return new OrSpec<FileTreeElement>(matchers); }
/** * {@inheritDoc} */ @Override public Test include(Spec<FileTreeElement> includeSpec) { patternSet.include(includeSpec); return this; }
/** * {@inheritDoc} */ @Override public Test exclude(Spec<FileTreeElement> excludeSpec) { patternSet.exclude(excludeSpec); return this; }
/** * {@inheritDoc} */ public AbstractCopyTask include(Spec<FileTreeElement> includeSpec) { getMainSpec().include(includeSpec); return this; }
/** * {@inheritDoc} */ public AbstractCopyTask exclude(Spec<FileTreeElement> excludeSpec) { getMainSpec().exclude(excludeSpec); return this; }
/** * {@inheritDoc} */ public SourceTask include(Spec<FileTreeElement> includeSpec) { patternSet.include(includeSpec); return this; }
/** * {@inheritDoc} */ public SourceTask exclude(Spec<FileTreeElement> excludeSpec) { patternSet.exclude(excludeSpec); return this; }
CachingSpec(SpecKey key, Spec<FileTreeElement> spec) { this.key = key; this.spec = spec; }
public Spec<FileTreeElement> createSpec(PatternSet patternSet) { return Specs.intersect(createIncludeSpec(patternSet), Specs.negate(createExcludeSpec(patternSet))); }
public Spec<FileTreeElement> getAsSpec() { return Specs.intersect(super.getAsSpec(), other.getAsSpec()); }
public Spec<FileTreeElement> getAsSpec() { return patternSpecFactory.createSpec(this); }
public Spec<FileTreeElement> getAsIncludeSpec() { return patternSpecFactory.createIncludeSpec(this); }
public Spec<FileTreeElement> getAsExcludeSpec() { return patternSpecFactory.createExcludeSpec(this); }