Skip to content

Commit

Permalink
Merge pull request rockcrafters#21 from vpa1977/maven-plugin
Browse files Browse the repository at this point in the history
chore: refactor plugin into common and gradle part
  • Loading branch information
vpa1977 authored Oct 2, 2024
2 parents 16cedd3 + 6afeca1 commit 946af0e
Show file tree
Hide file tree
Showing 20 changed files with 442 additions and 225 deletions.
4 changes: 2 additions & 2 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Add 'documentation' label to any changes within 'docs' folder or any subfolders
documentation:
- changed-files:
- any-glob-to-any-file: docs/**
- changed-files:
- any-glob-to-any-file: docs/**
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ To use the plugin, apply the following two steps:
}
apply(plugin = "com.canonical.rockcraft")


### 2. Configure ROCK container

The plugin allows setting up container summary and description,
Expand Down Expand Up @@ -102,4 +101,3 @@ Contributions can be submitted via [Pull requests](https://github.com/canonical/

- Allow custom rockcraft.yaml/snippets
- Error handling (empty jar file), no main class

4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ snakeyaml = "2.3"

[libraries]
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
commons-text = { module="org.apache.commons:commons-text", version.ref = "commons-text" }
osdetector = { module ="com.google.gradle:osdetector-gradle-plugin", version.ref = "osdetector" }
commons-text = { module = "org.apache.commons:commons-text", version.ref = "commons-text" }
osdetector = { module = "com.google.gradle:osdetector-gradle-plugin", version.ref = "osdetector" }
snakeyaml = { module = "org.yaml:snakeyaml", version.ref = "snakeyaml" }
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ repositories {
}

dependencies {
implementation(project(":rockcraft"))
implementation(libs.osdetector)
implementation(libs.commons.text)
implementation(libs.snakeyaml)

// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)
testImplementation(libs.snakeyaml)

testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
Expand All @@ -32,7 +32,7 @@ gradlePlugin {
id = "com.canonical.rockcraft"
displayName = "Rockcraft plugin"
description = "Plugin for rock image generation"
implementationClass = "com.canonical.rockcraft.plugin.RockcraftPlugin"
implementationClass = "com.canonical.rockcraft.gradle.RockcraftPlugin"
tags = listOf("rockcraft", "rock", "container", "docker", "oci")
}
}
Expand All @@ -42,3 +42,8 @@ tasks.named<Test>("test") {
// Use JUnit Jupiter for unit tests.
useJUnitPlatform()
}

tasks.withType<JavaCompile> {
val compilerArgs = options.compilerArgs
compilerArgs.add("-Xlint:all")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,45 @@
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.canonical.rockcraft.plugin;
package com.canonical.rockcraft.gradle;

import com.canonical.rockcraft.builder.RockBuilder;
import com.canonical.rockcraft.builder.RockProjectSettings;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import java.io.IOException;
import java.nio.file.Files;

/**
* This task builds a ROCK image by calling <i>rockcraft pack</i>.
* It removes all previous ROCK artifacts from the build directory.
*/
public class BuildRockcraftTask extends DefaultTask {

private static final String ROCK_DIR = "rock";

/**
* Constructs BuildRockcraft task
*/
public BuildRockcraftTask() { super();}
public BuildRockcraftTask() {
super();
}

/**
* The task action
* @throws IOException - IO error while writing <i>rockcraft.yaml</i>
* @throws InterruptedException - <i>rockcraft</i> process was aborted
*/
@TaskAction
public void packRock() throws IOException, InterruptedException {
var buildDir = getProject().getLayout().getBuildDirectory();
var pb = new ProcessBuilder("rockcraft", "pack")
.directory(buildDir.getAsFile().get())
.inheritIO();
var process = pb.start();
int result = process.waitFor();
if (result != 0)
throw new UnsupportedOperationException("Failed to pack rock for "+ getProject().getName());

var rockDest = buildDir.dir(ROCK_DIR).get().getAsFile();
rockDest.mkdirs();
for (var f : rockDest.listFiles((dir,file) -> file.endsWith(".rock"))) {
f.delete();
}
// refresh rocks
for (var f : buildDir.getAsFile().get().listFiles((dir,file) -> file.endsWith(".rock"))) {
var source = f.toPath();
var dest = buildDir.dir(ROCK_DIR).get().getAsFile().toPath();
Files.move(source, dest.resolve(source.getFileName()));
}
var buildDir = getProject().getLayout().getBuildDirectory().getAsFile().get();
var settings = new RockProjectSettings(getProject().getName(), String.valueOf(getProject().getVersion()), getProject().getProjectDir().toPath());
RockBuilder.buildRock(settings, buildDir);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2024 Canonical Ltd.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.canonical.rockcraft.gradle;

import com.canonical.rockcraft.builder.RockCrafter;
import com.canonical.rockcraft.builder.RockProjectSettings;
import com.canonical.rockcraft.builder.RockcraftOptions;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

/**
* This task writes <i>rockcraft.yaml</i> file for the application.
*/
public abstract class CreateRockcraftTask extends DefaultTask {

private final RockcraftOptions options;

/**
* Constructs the CreateRockcraft task
* @param options - plugin options
*/
@Inject
public CreateRockcraftTask(RockcraftOptions options) {
this.options = options;
}

private RockcraftOptions getOptions() {
return options;
}

/**
* Task action to write <i>rockcraft.yaml</i>
*/
@TaskAction
public void writeRockcraft() {
HashSet<File> artifacts = new HashSet<File>();
for (var conf : getProject().getConfigurations()) {
artifacts.addAll(conf.getArtifacts().getFiles().getFiles().stream().filter(x -> x.getName().endsWith("jar")).toList());
}

try {
var buildDir = getProject().getLayout().getBuildDirectory();
var settings = new RockProjectSettings(getProject().getName(), String.valueOf(getProject().getVersion()), getProject().getProjectDir().toPath());
RockCrafter crafter = new RockCrafter(settings, getOptions(), buildDir.getAsFile().get(), new ArrayList<File>(artifacts));
crafter.writeRockcraft();
} catch (IOException e) {
throw new UnsupportedOperationException("Failed to write rockcraft.yaml: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.canonical.rockcraft.plugin;
package com.canonical.rockcraft.gradle;

import com.canonical.rockcraft.builder.RockBuilder;
import com.canonical.rockcraft.builder.RockcraftOptions;
import com.google.gradle.osdetector.OsDetector;
import com.google.gradle.osdetector.OsDetectorPlugin;
import org.gradle.api.Plugin;
Expand All @@ -29,7 +31,9 @@ public class RockcraftPlugin implements Plugin<Project> {
/**
* Constructs RockcraftPlugin
*/
public RockcraftPlugin() { super(); }
public RockcraftPlugin() {
super();
}


/**
Expand All @@ -48,16 +52,10 @@ public void apply(Project project) {
throw new UnsupportedOperationException("Rockcraft is only supported on linux systems");

var checkTask = project.getTasks().register("checkRockcraft", s -> {
s.doFirst( x -> {
s.doFirst(x -> {
try {
var pb = new ProcessBuilder("rockcraft", "--version");
pb.inheritIO();
var versionProcess = pb.start();
int ret = versionProcess.waitFor();
if (ret != 0)
throw new UnsupportedOperationException("Please install rockcraft 'snap install rockcraft'.");
}
catch (IOException | InterruptedException e) {
RockBuilder.checkRockcraft();
} catch (IOException | InterruptedException e) {
throw new UnsupportedOperationException(e.getMessage());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.canonical.rockcraft.plugin;
package com.canonical.rockcraft.gradle;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;

import org.gradle.testkit.runner.BuildResult;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -36,7 +35,9 @@ protected File getJavaSource() {
return Path.of(projectDir.getAbsolutePath(), "src", "main", "java", "Test.java").toFile();
}

protected File getProjectDir() { return projectDir; }
protected File getProjectDir() {
return projectDir;
}

protected File getBuildFile() {
return new File(projectDir, "build.gradle");
Expand Down
Loading

0 comments on commit 946af0e

Please sign in to comment.