maartenc commented on code in PR #2: URL: https://github.com/apache/ant-antlibs-cyclonedx/pull/2#discussion_r3744445913
########## src/main/org/apache/ant/cyclonedx/IvyModuleComponentResolver.java: ########## @@ -0,0 +1,394 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ant.cyclonedx; + +import java.io.File; +import java.io.IOException; +import java.text.ParseException; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.ivy.Ivy; +import org.apache.ivy.ant.IvyAntSettings; +import org.apache.ivy.core.module.descriptor.Artifact; +import org.apache.ivy.core.module.descriptor.License; +import org.apache.ivy.core.module.descriptor.ModuleDescriptor; +import org.apache.ivy.core.module.id.ModuleRevisionId; +import org.apache.ivy.core.report.ArtifactDownloadReport; +import org.apache.ivy.core.report.ResolveReport; +import org.apache.ivy.core.resolve.IvyNode; +import org.apache.ivy.core.resolve.IvyNodeCallers.Caller; +import org.apache.ivy.core.retrieve.RetrieveEngine; +import org.apache.ivy.core.retrieve.RetrieveOptions; +import org.apache.ivy.core.settings.IvySettings; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.Reference; +import org.apache.tools.ant.types.resources.FileResource; +import org.apache.tools.ant.types.resources.URLResource; + +import org.cyclonedx.model.Component.Scope; + +/** + * Resolver that populates a Component from Ivy module data. + * + * <p>Assumes the Ivy file has already been resolved (i.e., the project + * has run ivy:resolve already). Uses the Ivy engine to resolve dependencies + * from the Ivy cache.</p> + * + * <p>Strongly influenced by Ivy's dependencytree task.</p> + * + * @since CycloneDX Antlib 0.2 + */ +class IvyModuleComponentResolver { + + private final Component.IvyModule ivyModule; + private final Project project; + private boolean includeAllConfigurations; + private Set<String> includedConfigurations; + private Set<String> optionalConfigurations; + private Set<String> externalConfigurations; + + IvyModuleComponentResolver(Component.IvyModule ivyModule, Project project) { + this.ivyModule = ivyModule; + this.project = project; + } + + /** + * Resolves the Ivy module and populates the component with its data. + * + * @param component the component to populate + * @return additional components that are (transitive) dependencies of this component + * @throws IOException if resolution fails + */ + public Collection<Component> resolve(Component component) throws IOException, BuildException { + Ivy ivy = createIvyInstance(component); + IvySettings settings = ivy.getSettings(); + + parseConfigurations(settings); + + ResolveReport report = loadResolveReport(settings); + ModuleDescriptor root = report.getModuleDescriptor(); + + Set<ModuleRevisionId> optionalModules = new HashSet<>(); + Set<ModuleRevisionId> externalModules = new HashSet<>(); + Map<ModuleRevisionId, Set<IvyNode>> dependencyTree = + populateDependencyTree(settings, report, optionalModules, externalModules); + fillFromModuleDescriptor(component, root, dependencyTree); + + Map<ModuleRevisionId, File> componentFiles = findDownloadedArtifacts(ivy, settings, root.getModuleRevisionId()); + Collection<ModuleDescriptor> allDependencies = getDependencies(dependencyTree, root); + + return allDependencies.stream() + .map(d -> toComponent(d, dependencyTree, optionalModules, externalModules, componentFiles)) + .collect(Collectors.toList()); + } + + private Ivy createIvyInstance(Component component) { + Reference settingRef = ivyModule.getSettingsRef(); + IvyAntSettings engine; + if (settingRef == null) { + engine = IvyAntSettings.getDefaultInstance(component); + } + else { + engine = settingRef.getReferencedObject(project); + } + return engine.getConfiguredIvyInstance(component); + } + + private void parseConfigurations(IvySettings settings) { + String conf = ivyModule.getConf(); + if (conf == null || "*".equals(conf)) { + conf = settings.getVariable("ivy.resolved.configurations"); + } + if (conf == null) { + throw new BuildException("no conf provided, you need to call to <resolve/> before using this task"); + } + includeAllConfigurations = "*".equals(conf); + if (includeAllConfigurations) { + includedConfigurations = new HashSet<>(); + } else { + includedConfigurations = confAsSet(conf); + } + optionalConfigurations = confAsSet(ivyModule.getOptionalConf()); + externalConfigurations = confAsSet(ivyModule.getExternalConf()); + } + + private ResolveReport loadResolveReport(IvySettings settings) { + // explicit values for organisation and module would come in here, once supported + String organisation = settings.getVariable("ivy.organisation"); + if (organisation == null) { + throw new BuildException("no organisation provided, you need to call to <resolve/> before using this task"); + } + + String module = settings.getVariable("ivy.module"); + if (organisation == null) { + throw new BuildException("no module provided, you need to call to <resolve/> before using this task"); + } + + ResolveReport report = getResolvedReport(organisation, module); + if (report == null) { + throw new BuildException("No resolution report was available," + + " you need to call to <resolve/> before using this task"); + } + return report; + } + + private ResolveReport getResolvedReport(String org, String module) { + String resolveId = ivyModule.getResolveId(); + ResolveReport report; + if (resolveId != null) { + report = project.getReference("ivy.resolved.report."+ org + "." + resolveId); Review Comment: I think you can skip the 'org' where there is a resolveId specified -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
