Skip to content

Commit

Permalink
fix: provide artifacts lazily
Browse files Browse the repository at this point in the history
Signed-off-by: Gordon <[email protected]>
  • Loading branch information
gordonrousselle committed Oct 28, 2024
1 parent f82f1ce commit 010b8d1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 92 deletions.
23 changes: 10 additions & 13 deletions src/main/java/org/cyclonedx/gradle/CycloneDxPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public class CycloneDxPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getTasks().register("cyclonedxBom", CycloneDxTask.class, (task) -> {
final ResolvedBuild resolvedBuild = getResolvedBuild(project);
final Set<Provider<ArtifactInfoSet>> artifacts = getArtifacts(project);
final Provider<Set<ArtifactInfo>> artifacts = getArtifacts(project);
final File destination =
project.getLayout().getBuildDirectory().dir("reports").get().getAsFile();

task.getResolvedBuild().set(resolvedBuild);
task.getDestination().set(destination);
task.setGroup("Reporting");
task.setDescription("Generates a CycloneDX compliant Software Bill of Materials (SBOM)");
task.getArtifacts().set(new ResolvedArtifacts(artifacts));
task.getArtifacts().set(artifacts);
});
}

Expand All @@ -59,20 +59,17 @@ private ResolvedBuild getResolvedBuild(final Project project) {
return resolvedBuild;
}

private Set<Provider<ArtifactInfoSet>> getArtifacts(final Project project) {
private Provider<Set<ArtifactInfo>> getArtifacts(final Project project) {

return project.getAllprojects().stream()
final List<Configuration> configurations = project.getAllprojects().stream()
.flatMap(v -> v.getConfigurations().stream())
.collect(Collectors.toList());

return project.getProviders().provider(() -> configurations.stream()
.filter(Configuration::isCanBeResolved)
.map(config -> config.getIncoming()
.getArtifacts()
.getResolvedArtifacts()
.map(artifacts -> {
ArtifactInfoSet infoSet = new ArtifactInfoSet();
artifacts.forEach(artifact -> infoSet.addInfo(toArtifactInfo(artifact)));
return infoSet;
}))
.collect(Collectors.toSet());
.flatMap(config -> config.getIncoming().getArtifacts().getArtifacts().stream()
.map(artifact -> toArtifactInfo(artifact)))
.collect(Collectors.toSet()));
}

private ArtifactInfo toArtifactInfo(final ResolvedArtifactResult result) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/cyclonedx/gradle/CycloneDxTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import java.io.File;
import java.util.Map;
import java.util.Set;
import org.cyclonedx.gradle.model.ResolvedArtifacts;
import org.cyclonedx.gradle.model.ArtifactInfo;
import org.cyclonedx.gradle.model.ResolvedBuild;
import org.cyclonedx.gradle.model.ResolvedConfiguration;
import org.cyclonedx.gradle.utils.CycloneDxUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;

Expand All @@ -45,7 +46,7 @@ public CycloneDxTask() {
public abstract Property<File> getDestination();

@Input
public abstract Property<ResolvedArtifacts> getArtifacts();
public abstract SetProperty<ArtifactInfo> getArtifacts();

@TaskAction
public void createBom() {
Expand All @@ -71,6 +72,6 @@ private void buildChildDependencies(final Map<String, Set<ResolvedConfiguration>
}

private void registerArtifacts() {
getArtifacts().get().getArtifacts().forEach(v -> v.get().getInfoSet().forEach(traverser::registerArtifact));
getArtifacts().get().forEach(traverser::registerArtifact);
}
}
39 changes: 0 additions & 39 deletions src/main/java/org/cyclonedx/gradle/model/ArtifactInfoSet.java

This file was deleted.

35 changes: 0 additions & 35 deletions src/main/java/org/cyclonedx/gradle/model/ResolvedArtifacts.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ class PluginConfigurationSpec extends Specification {
dependencies {
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version:'2.8.11'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.5.18.RELEASE'
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-native-prebuilt', version: '2.0.20'
}""", "rootProject.name = 'hello-world'")

when:
def result = GradleRunner.create()
.withProjectDir(testDir)
.withArguments("cyclonedxBom")
.withArguments("cyclonedxBom", "--configuration-cache")
.withPluginClasspath()
.build()

Expand All @@ -181,6 +182,25 @@ class PluginConfigurationSpec extends Specification {
assert jsonBom.text.contains("\"name\" : \"hello-world\"")
}

def "should build bom successfully for native kotlin project"() {
given:
File testDir = TestUtils.duplicate("native-kotlin-project")

when:
def result = GradleRunner.create()
.withProjectDir(testDir)
.withArguments("cyclonedxBom")
.withPluginClasspath()
.build()

then:
result.task(":cyclonedxBom").outcome == TaskOutcome.SUCCESS
File reportDir = new File(testDir, "build/reports")

assert reportDir.exists()
reportDir.listFiles().length == 1
}

@Ignore
def "should use configured componentName"() {
given:
Expand Down Expand Up @@ -343,7 +363,6 @@ class PluginConfigurationSpec extends Specification {
.build()
then:
result.task(":cyclonedxBom").outcome == TaskOutcome.SUCCESS
println(result.output)
File reportDir = new File(testDir, "build/reports")

assert reportDir.exists()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id 'org.cyclonedx.bom'
id 'org.jetbrains.kotlin.multiplatform' version '2.0.21'
}

repositories {
mavenCentral()
}

group = 'com.example'
version = '1.0.0'
kotlin {
// macosX64('native') { // on macOS
// linuxX64('native') // on Linux
mingwX64('native'){ // on Windows
binaries {
executable()
}
}
}

wrapper {
gradleVersion = '8.5'
distributionType = 'BIN'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main() {
println("Hello, Kotlin/Native!")
}

0 comments on commit 010b8d1

Please sign in to comment.