Search in sources :

Example 6 with ArtifactCollection

use of org.gradle.api.artifacts.ArtifactCollection in project atlas by alibaba.

the class ScanDupResTask method generate.

@TaskAction
void generate() {
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == atlasDependencyTree) {
        return;
    }
    File dupResFile = new File(appVariantContext.getProject().getBuildDir(), "outputs/warning-dup-res.properties");
    File dupAssetsFile = new File(appVariantContext.getProject().getBuildDir(), "outputs/warning-dup-assets.properties");
    ArtifactCollection res = appVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AndroidArtifacts.ArtifactType.ANDROID_RES);
    ArtifactCollection assets = appVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AndroidArtifacts.ArtifactType.ASSETS);
    Set<String> errors = new HashSet<>();
    Map<Pair<String, File>, String> map1 = new HashMap<>();
    for (File file : assets.getArtifactFiles().getFiles()) {
        Collection<File> files = FileUtils.listFiles(file, null, true);
        for (File file1 : files) {
            boolean e = false;
            for (Pair stringFilePair : map1.keySet()) {
                if (stringFilePair.getKey().equals(file1.getAbsolutePath().substring(file1.getAbsolutePath().indexOf("assets"))) && !isSameFile((File) stringFilePair.getValue(), file1) && !map1.get(stringFilePair).equals(file.getAbsolutePath())) {
                    errors.add("dup assets:" + file1.getName() + " in " + map1.get(stringFilePair) + " and " + file.getAbsolutePath());
                    e = true;
                    break;
                }
            }
            if (!e) {
                map1.put(new Pair<>(file1.getAbsolutePath().substring(file1.getAbsolutePath().indexOf("assets")), file1), file.getAbsolutePath());
            }
        }
    }
    try {
        FileUtils.writeLines(dupAssetsFile, errors);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Map<String, File> map = new HashMap<>();
    Map<String, Pair<String, File>> valuesMap = new HashMap<>();
    List<String> exceptions = new ArrayList<>();
    ResourceMerger resourceMerger = new ResourceMerger(14);
    if (res != null) {
        Set<ResolvedArtifactResult> libArtifacts = res.getArtifacts();
        List<ResourceSet> resourceSetList = Lists.newArrayListWithExpectedSize(libArtifacts.size());
        // the order of the artifact is descending order, so we need to reverse it.
        for (ResolvedArtifactResult artifact : libArtifacts) {
            ResourceSet resourceSet = new ResourceSet(MergeManifests.getArtifactName(artifact), null, null, true);
            resourceSet.setFromDependency(true);
            resourceSet.addSource(artifact.getFile());
            // add to 0 always, since we need to reverse the order.
            resourceSetList.add(0, resourceSet);
        }
        resourceSetList.forEach(resourceSet -> {
            try {
                resourceSet.loadFromFiles(getILogger());
            } catch (MergingException e) {
                e.printStackTrace();
            }
            resourceMerger.addDataSet(resourceSet);
        });
        ListMultimap<String, ResourceItem> mValuesResMap = ArrayListMultimap.create();
        try {
            resourceMerger.mergeData(new MergeConsumer<ResourceItem>() {

                @Override
                public void start(DocumentBuilderFactory documentBuilderFactory) throws ConsumerException {
                }

                @Override
                public void end() throws ConsumerException {
                }

                @Override
                public void addItem(ResourceItem item) throws ConsumerException {
                    DataFile.FileType type = item.getSourceType();
                    if (type == DataFile.FileType.XML_VALUES) {
                        mValuesResMap.put(item.getQualifiers(), item);
                    } else {
                        File file = null;
                        String folderName = null;
                        Preconditions.checkState(item.getSource() != null);
                        file = item.getFile();
                        folderName = getFolderName(item);
                        // if (ResourceItem.isTouched()) {
                        String tag = folderName + "/" + file.getName();
                        if (type == DataFile.FileType.GENERATED_FILES) {
                            if (!map.containsKey(tag)) {
                                map.put(tag, file);
                            } else if (!map.get(tag).equals(file)) {
                                if (!isSameBundle(map.get(tag), file, atlasDependencyTree) && allInMainBundle(getId(map.get(tag)), getId(file), atlasDependencyTree) && !isSameFile(map.get(tag), file))
                                    exceptions.add("dup File:" + tag + "|" + getId(map.get(tag)) + "|" + getId(file));
                            }
                        // try {
                        // MergedResourceWriter.FileGenerationParameters workItem = new MergedResourceWriter.FileGenerationParameters(item, this.mPreprocessor);
                        // if (workItem.resourceItem.getSource() != null) {
                        // this.getExecutor().submit(workItem);
                        // }
                        // } catch (Exception var6) {
                        // throw new ConsumerException(var6, ((ResourceFile)item.getSource()).getFile());
                        // }
                        } else if (type == DataFile.FileType.SINGLE_FILE) {
                            if (!map.containsKey(tag)) {
                                map.put(tag, file);
                            } else if (!map.get(tag).equals(file)) {
                                if (!isSameBundle(map.get(tag), file, atlasDependencyTree) && allInMainBundle(getId(map.get(tag)), getId(file), atlasDependencyTree) && !isSameFile(map.get(tag), file))
                                    exceptions.add("dup File:" + tag + "|" + getId(map.get(tag)) + "|" + getId(file));
                            }
                        }
                    // this.mCompileResourceRequests.add(new CompileResourceRequest(file, this.getRootFolder(), folderName));
                    // }
                    }
                }

                @Override
                public void removeItem(ResourceItem removedItem, ResourceItem replacedBy) throws ConsumerException {
                    DataFile.FileType removedType = removedItem.getSourceType();
                    DataFile.FileType replacedType = replacedBy != null ? replacedBy.getSourceType() : null;
                    switch(removedType) {
                        case SINGLE_FILE:
                        case GENERATED_FILES:
                            if (replacedType == DataFile.FileType.SINGLE_FILE || replacedType == DataFile.FileType.GENERATED_FILES) {
                            // System.out.println(removedItem.getQualifiers()+":"+removedItem.getQualifiers());
                            // File removedFile = getResourceOutputFile(removedItem);
                            // File replacedFile = getResourceOutputFile(replacedBy);
                            // if (removedFile.equals(replacedFile)) {
                            // break;
                            // }
                            }
                            // this.removeOutFile(removedItem);
                            break;
                        case XML_VALUES:
                            mValuesResMap.put(removedItem.getQualifiers(), replacedBy);
                            // this.mQualifierWithDeletedValues.add(removedItem.getQualifiers());
                            break;
                        default:
                            throw new IllegalStateException();
                    }
                }

                @Override
                public boolean ignoreItemInMerge(ResourceItem item) {
                    DataFile.FileType type = item.getSourceType();
                    if (type == DataFile.FileType.XML_VALUES) {
                        mValuesResMap.put(item.getQualifiers(), item);
                    } else {
                        File file = null;
                        String folderName = null;
                        Preconditions.checkState(item.getSource() != null);
                        file = item.getFile();
                        folderName = getFolderName(item);
                        // if (ResourceItem.isTouched()) {
                        String tag = folderName + "/" + file.getName();
                        if (type == DataFile.FileType.GENERATED_FILES) {
                            if (!map.containsKey(tag)) {
                                map.put(tag, file);
                            } else if (!map.get(tag).equals(file)) {
                                if (!isSameBundle(map.get(tag), file, atlasDependencyTree) && allInMainBundle(getId(map.get(tag)), getId(file), atlasDependencyTree) && !isSameFile(map.get(tag), file))
                                    exceptions.add("dup File:" + tag + "|" + getId(map.get(tag)) + "|" + getId(file));
                            }
                        // try {
                        // MergedResourceWriter.FileGenerationParameters workItem = new MergedResourceWriter.FileGenerationParameters(item, this.mPreprocessor);
                        // if (workItem.resourceItem.getSource() != null) {
                        // this.getExecutor().submit(workItem);
                        // }
                        // } catch (Exception var6) {
                        // throw new ConsumerException(var6, ((ResourceFile)item.getSource()).getFile());
                        // }
                        } else if (type == DataFile.FileType.SINGLE_FILE) {
                            if (!map.containsKey(tag)) {
                                map.put(tag, file);
                                if (!isSameBundle(map.get(tag), file, atlasDependencyTree) && allInMainBundle(getId(map.get(tag)), getId(file), atlasDependencyTree) && !isSameFile(map.get(tag), file))
                                    exceptions.add("dup File:" + tag + "|" + getId(map.get(tag)) + "|" + getId(file));
                            }
                        }
                    // this.mCompileResourceRequests.add(new CompileResourceRequest(file, this.getRootFolder(), folderName));
                    // }
                    }
                    return false;
                }
            }, false);
        } catch (MergingException e) {
            e.printStackTrace();
        }
        mValuesResMap.asMap().values().forEach(resourceItems -> {
            for (ResourceItem resourceItem : resourceItems) {
                String tag = null;
                if (resourceItem.getSource() == null) {
                    tag = resourceItem.getQualifiers() + ":" + resourceItem.getType().getName() + ":" + resourceItem.getName();
                } else {
                    tag = resourceItem.getQualifiers() + ":" + resourceItem.getKey();
                }
                if (!valuesMap.containsKey(tag)) {
                    String value = getOtherString(resourceItem);
                    if (resourceItem.getSource() == null || resourceItem.getFile() == null) {
                        valuesMap.put(tag, new Pair<String, File>(value, new File("aa")));
                    } else {
                        valuesMap.put(tag, new Pair<String, File>(value, resourceItem.getFile()));
                    }
                } else {
                    if (resourceItem.getFile() != null && valuesMap.get(tag) != null) {
                        if (!valuesMap.get(tag).equals(resourceItem.getFile()) && !isSameValue(resourceItem, valuesMap.get(tag).getKey()))
                            if (!tag.equals(":string/app_name")) {
                                exceptions.add("dup value " + tag + "|" + valuesMap.get(tag).getKey() + "|" + getOtherString(resourceItem) + "|" + getId(valuesMap.get(tag).getValue()) + "|" + getId(resourceItem.getFile()));
                            }
                    }
                }
            }
        });
        Collections.sort(exceptions);
        try {
            FileUtils.writeLines(dupResFile, exceptions);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) Pair(javafx.util.Pair) IOException(java.io.IOException) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) File(java.io.File) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 7 with ArtifactCollection

use of org.gradle.api.artifacts.ArtifactCollection in project atlas by alibaba.

the class AtlasDependencyGraph method createDependencies.

/**
 * Create a level 1 dependency list.
 */
@NonNull
public DependenciesImpl createDependencies(@NonNull VariantScope variantScope, boolean downloadSources, @NonNull Consumer<SyncIssue> failureConsumer) {
    try {
        ImmutableList.Builder<String> projects = ImmutableList.builder();
        ImmutableList.Builder<AndroidLibrary> androidLibraries = ImmutableList.builder();
        ImmutableList.Builder<JavaLibrary> javaLibrary = ImmutableList.builder();
        // get the runtime artifact. We only care about the ComponentIdentifier so we don't
        // need to call getAllArtifacts() which computes a lot more many things.
        // Instead just get all the jars to get all the dependencies.
        ArtifactCollection runtimeArtifactCollection = computeArtifactList(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AtlasAndroidArtifacts.AtlasArtifactType.JAR);
        // build a list of the artifacts
        Set<ComponentIdentifier> runtimeIdentifiers = new HashSet<>(runtimeArtifactCollection.getArtifacts().size());
        for (ResolvedArtifactResult result : runtimeArtifactCollection.getArtifacts()) {
            runtimeIdentifiers.add(result.getId().getComponentIdentifier());
        }
        Set<AtlasDependencyGraph.HashableResolvedArtifactResult> artifacts = getAllArtifacts(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH, dependencyFailureHandler);
        for (AtlasDependencyGraph.HashableResolvedArtifactResult artifact : artifacts) {
            ComponentIdentifier id = artifact.getId().getComponentIdentifier();
            boolean isProvided = !runtimeIdentifiers.contains(id);
            boolean isSubproject = id instanceof ProjectComponentIdentifier;
            String projectPath = isSubproject ? ((ProjectComponentIdentifier) id).getProjectPath() : null;
            if (artifact.getDependencyType() == DependencyType.JAVA) {
                if (projectPath != null) {
                    projects.add(projectPath);
                    continue;
                }
                // FIXME: Dependencies information is not set correctly.
                javaLibrary.add(new com.android.build.gradle.internal.ide.JavaLibraryImpl(artifact.getFile(), null, ImmutableList.of(), /* dependencies */
                null, /* requestedCoordinates */
                Preconditions.checkNotNull(sMavenCoordinatesCache.get(artifact)), false, /* isSkipped */
                isProvided));
            } else {
                if (artifact.isWrappedModule()) {
                    // force external dependency mode.
                    projectPath = null;
                }
                final File explodedFolder = artifact.getFile();
                // noinspection VariableNotUsedInsideIf
                androidLibraries.add(new com.android.build.gradle.internal.ide.AndroidLibraryImpl(// FIXME: Dependencies information is not set correctly.
                Preconditions.checkNotNull(sMavenCoordinatesCache.get(artifact)), projectPath, artifact.bundleResult != null ? artifact.bundleResult.getFile() : explodedFolder, // fallback so that the value is non-null
                explodedFolder, /*exploded folder*/
                getVariant(artifact), isProvided, false, /* dependencyItem.isSkipped() */
                ImmutableList.of(), /* androidLibraries */
                ImmutableList.of(), /* javaLibraries */
                findLocalJarsAsFiles(explodedFolder)));
            }
        }
        // force download the source artifacts of the compile classpath only.
        if (downloadSources) {
            Set<ComponentIdentifier> ids = Sets.newHashSetWithExpectedSize(artifacts.size());
            for (AtlasDependencyGraph.HashableResolvedArtifactResult artifact : artifacts) {
                ids.add(artifact.getId().getComponentIdentifier());
            }
            handleSources(variantScope.getGlobalScope().getProject(), ids, failureConsumer);
        }
        return new DependenciesImpl(androidLibraries.build(), javaLibrary.build(), projects.build());
    } finally {
        dependencyFailureHandler.collectIssues().forEach(failureConsumer);
    }
}
Also used : ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) File(java.io.File) NonNull(com.android.annotations.NonNull)

Aggregations

ArtifactCollection (org.gradle.api.artifacts.ArtifactCollection)7 ResolvedArtifactResult (org.gradle.api.artifacts.result.ResolvedArtifactResult)6 FilteredArtifactCollection (com.android.build.gradle.internal.dependency.FilteredArtifactCollection)5 ComponentIdentifier (org.gradle.api.artifacts.component.ComponentIdentifier)5 ProjectComponentIdentifier (org.gradle.api.artifacts.component.ProjectComponentIdentifier)5 File (java.io.File)4 ModuleComponentIdentifier (org.gradle.api.artifacts.component.ModuleComponentIdentifier)4 NonNull (com.android.annotations.NonNull)3 IOException (java.io.IOException)3 Nullable (com.android.annotations.Nullable)2 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)2 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)2 AndroidArtifacts (com.android.build.gradle.internal.publishing.AndroidArtifacts)2 AtlasAndroidArtifacts (com.android.build.gradle.internal.publishing.AtlasAndroidArtifacts)2 GraphItem (com.android.builder.model.level2.GraphItem)2 Preconditions (com.google.common.base.Preconditions)2 com.google.common.collect (com.google.common.collect)2 Field (java.lang.reflect.Field)2 java.util (java.util)2 Consumer (java.util.function.Consumer)2