[snap] refactor local packaging & upload

This commit is contained in:
Andres Almiray
2021-04-23 20:12:03 +02:00
parent aa1cf16a3b
commit 5997fdfb8f
10 changed files with 87 additions and 120 deletions

View File

@@ -51,6 +51,10 @@ processResources {
}
}
tasks.withType(Tar){
compression = Compression.GZIP
}
task versionFile {
doLast {
project.layout.buildDirectory.file('VERSION').get().asFile.text = project.version

View File

@@ -148,13 +148,12 @@ abstract class AbstractAssemblerProcessor<A extends Assembler> implements Assemb
props.putAll(assembler.getResolvedExtraProperties());
}
protected boolean executeCommand(Path directory, List<String> cmd) throws AssemblerProcessingException {
protected boolean executeCommand(ProcessExecutor processExecutor) throws AssemblerProcessingException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
int exitValue = new ProcessExecutor(cmd)
.directory(directory.toFile())
int exitValue = processExecutor
.redirectOutput(out)
.redirectError(err)
.execute()
@@ -167,38 +166,20 @@ abstract class AbstractAssemblerProcessor<A extends Assembler> implements Assemb
throw new AssemblerProcessingException("Command execution error. exitValue = " + exitValue);
} catch (ProcessInitException e) {
throw new AssemblerProcessingException("Unexpected error", e.getCause());
} catch (AssemblerProcessingException e) {
throw e;
} catch (Exception e) {
if (e instanceof AssemblerProcessingException) {
throw (AssemblerProcessingException) e;
}
throw new AssemblerProcessingException("Unexpected error", e);
}
}
protected boolean executeCommand(Path directory, List<String> cmd) throws AssemblerProcessingException {
return executeCommand(new ProcessExecutor(cmd)
.directory(directory.toFile()));
}
protected boolean executeCommand(List<String> cmd) throws AssemblerProcessingException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
int exitValue = new ProcessExecutor(cmd)
.redirectOutput(out)
.redirectError(err)
.execute()
.getExitValue();
info(out);
error(err);
if (exitValue == 0) return true;
throw new AssemblerProcessingException("Command execution error. exitValue = " + exitValue);
} catch (ProcessInitException e) {
throw new AssemblerProcessingException("Unexpected error", e.getCause());
} catch (Exception e) {
if (e instanceof AssemblerProcessingException) {
throw (AssemblerProcessingException) e;
}
throw new AssemblerProcessingException("Unexpected error", e);
}
return executeCommand(new ProcessExecutor(cmd));
}
protected boolean executeCommandCapturing(List<String> cmd, OutputStream out) throws AssemblerProcessingException {

View File

@@ -40,7 +40,7 @@ public class Snap extends AbstractRepositoryTool {
private final List<Plug> plugs = new ArrayList<>();
private final List<Slot> slots = new ArrayList<>();
private final SnapTap snap = new SnapTap();
private String base = "core18";
private String base = "core20";
private String grade = "stable";
private String confinement = "strict";
private String exportedLogin;
@@ -54,6 +54,7 @@ public class Snap extends AbstractRepositoryTool {
public Set<String> getSupportedExtensions() {
Set<String> set = new LinkedHashSet<>();
set.add(".tar.gz");
set.add(".tgz");
set.add(".tar");
return set;
}

View File

@@ -1,13 +1,13 @@
name: {{distributionName}}
base: {{snapBase}}
version: {{projectVersion}}
license: {{projectLicense}}
grade: {{snapGrade}}
type: app
confinement: {{snapConfinement}}
summary: {{projectDescription}}
description: {{projectLongDescription}}
grade: {{snapGrade}}
confinement: {{snapConfinement}}
base: {{snapBase}}
type: app
apps:
{{distributionExecutable}}:
command: $SNAP/bin/{{distributionExecutable}}

View File

@@ -1,13 +1,13 @@
name: {{distributionName}}
base: {{snapBase}}
version: {{projectVersion}}
license: {{projectLicense}}
grade: {{snapGrade}}
type: app
confinement: {{snapConfinement}}
summary: {{projectDescription}}
description: {{projectLongDescription}}
grade: {{snapGrade}}
confinement: {{snapConfinement}}
base: {{snapBase}}
type: app
apps:
{{distributionExecutable}}:
command: $SNAP/bin/{{distributionExecutable}}

View File

@@ -1,13 +1,13 @@
name: {{distributionName}}
base: {{snapBase}}
version: {{projectVersion}}
license: {{projectLicense}}
grade: {{snapGrade}}
type: app
confinement: {{snapConfinement}}
summary: {{projectDescription}}
description: {{projectLongDescription}}
grade: {{snapGrade}}
confinement: {{snapConfinement}}
base: {{snapBase}}
type: app
apps:
{{distributionExecutable}}:
command: $SNAP/{{artifactFileName}}

View File

@@ -1,13 +1,13 @@
name: {{distributionName}}
base: {{snapBase}}
version: {{projectVersion}}
license: {{projectLicense}}
grade: {{snapGrade}}
type: app
confinement: {{snapConfinement}}
summary: {{projectDescription}}
description: {{projectLongDescription}}
grade: {{snapGrade}}
confinement: {{snapConfinement}}
base: {{snapBase}}
type: app
apps:
{{distributionExecutable}}:
command: ${JAVA_HOME}/bin/java -jar $SNAP/{{artifactFileName}}

View File

@@ -31,9 +31,11 @@ import org.jreleaser.util.Version;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessInitException;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Files;
@@ -228,12 +230,12 @@ abstract class AbstractToolProcessor<T extends Tool> implements ToolProcessor<T>
protected abstract void fillToolProperties(Map<String, Object> context, Distribution distribution) throws ToolProcessingException;
protected boolean executeCommand(List<String> cmd) throws ToolProcessingException {
protected boolean executeCommand(ProcessExecutor processExecutor) throws ToolProcessingException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
int exitValue = new ProcessExecutor(cmd)
int exitValue = processExecutor
.redirectOutput(out)
.redirectError(err)
.execute()
@@ -246,39 +248,25 @@ abstract class AbstractToolProcessor<T extends Tool> implements ToolProcessor<T>
throw new ToolProcessingException("Command execution error. exitValue = " + exitValue);
} catch (ProcessInitException e) {
throw new ToolProcessingException("Unexpected error", e.getCause());
} catch (ToolProcessingException e) {
throw e;
} catch (Exception e) {
if (e instanceof ToolProcessingException) {
throw (ToolProcessingException) e;
}
throw new ToolProcessingException("Unexpected error", e);
}
}
protected boolean executeCommand(Path directory, List<String> cmd) throws ToolProcessingException {
return executeCommand(new ProcessExecutor(cmd)
.directory(directory.toFile()));
}
protected boolean executeCommand(List<String> cmd) throws ToolProcessingException {
return executeCommand(new ProcessExecutor(cmd));
}
protected boolean executeCommandWithInput(List<String> cmd, InputStream in) throws ToolProcessingException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
int exitValue = new ProcessExecutor(cmd)
.redirectOutput(out)
.redirectError(err)
.redirectInput(in)
.execute()
.getExitValue();
info(out);
error(err);
if (exitValue == 0) return true;
throw new ToolProcessingException("Command execution error. exitValue = " + exitValue);
} catch (ProcessInitException e) {
throw new ToolProcessingException("Unexpected error", e.getCause());
} catch (Exception e) {
if (e instanceof ToolProcessingException) {
throw (ToolProcessingException) e;
}
throw new ToolProcessingException("Unexpected error", e);
}
return executeCommand(new ProcessExecutor(cmd)
.redirectInput(in));
}
protected void copyPreparedFiles(Distribution distribution, Map<String, Object> props) throws ToolProcessingException {

View File

@@ -25,12 +25,9 @@ import org.jreleaser.model.Snap;
import org.jreleaser.model.releaser.spi.Releaser;
import org.jreleaser.model.tool.spi.ToolProcessingException;
import org.jreleaser.util.Constants;
import org.jreleaser.util.FileUtils;
import org.jreleaser.util.MustacheUtils;
import org.jreleaser.util.PlatformUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
@@ -38,9 +35,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.jreleaser.templates.TemplateUtils.trimTplExtension;
import static org.jreleaser.util.FileUtils.createDirectoriesWithFullAccess;
/**
* @author Andres Almiray
@@ -54,7 +49,17 @@ public class SnapToolProcessor extends AbstractRepositoryToolProcessor<Snap> {
@Override
protected boolean doPackageDistribution(Distribution distribution, Map<String, Object> props) throws ToolProcessingException {
copyPreparedFiles(distribution, props);
return true;
if (tool.isRemoteBuild()) {
return true;
}
if (PlatformUtils.isWindows()) {
context.getLogger().debug("must not run on Windows", getToolName());
return false;
}
return createSnap(distribution, props);
}
@Override
@@ -63,24 +68,22 @@ public class SnapToolProcessor extends AbstractRepositoryToolProcessor<Snap> {
return super.doUploadDistribution(distribution, releaser, props);
}
if (PlatformUtils.isWindows()) {
context.getLogger().debug("must not run on Windows", getToolName());
return false;
}
if (context.isDryrun()) {
context.getLogger().error("dryun is set to true. Skipping");
return true;
}
Path primeDirectory = createPackage(props);
if (PlatformUtils.isWindows()) {
context.getLogger().debug("must not run on Windows", getToolName());
return false;
}
if (!login(distribution, props)) {
context.getLogger().error("could not log into snapcraft store");
return false;
}
return createSnap(distribution, props, primeDirectory);
return push(distribution, props);
}
@Override
@@ -123,39 +126,28 @@ public class SnapToolProcessor extends AbstractRepositoryToolProcessor<Snap> {
writeFile(content, outputFile);
}
private Path createPackage(Map<String, Object> props) throws ToolProcessingException {
try {
Path prepareDirectory = (Path) props.get(Constants.KEY_PREPARE_DIRECTORY);
Path snapDirectory = prepareDirectory.resolve("snap");
Path packageDirectory = (Path) props.get(Constants.KEY_PACKAGE_DIRECTORY);
Path primeDirectory = packageDirectory.resolve("prime");
Path metaDirectory = primeDirectory.resolve("meta");
createDirectoriesWithFullAccess(metaDirectory);
if (FileUtils.copyFilesRecursive(context.getLogger(), snapDirectory, metaDirectory)) {
Files.move(metaDirectory.resolve("snapcraft.yaml"),
metaDirectory.resolve("snap.yaml"),
REPLACE_EXISTING);
return primeDirectory;
} else {
throw new ToolProcessingException("Could not copy files from " +
prepareDirectory.toAbsolutePath().toString() + " to " +
metaDirectory.toAbsolutePath().toString());
}
} catch (IOException e) {
throw new ToolProcessingException("Unexpected error when creating package", e);
}
}
private boolean login(Distribution distribution, Map<String, Object> props) throws ToolProcessingException {
List<String> cmd = new ArrayList<>();
cmd.add("snapcraft");
cmd.add("login");
cmd.add("--with");
cmd.add(distribution.getSnap().getExportedLogin());
cmd.add(context.getBasedir().resolve(distribution.getSnap().getExportedLogin()).toAbsolutePath().toString());
return executeCommand(cmd);
}
private boolean createSnap(Distribution distribution, Map<String, Object> props, Path primeDirectory) throws ToolProcessingException {
private boolean push(Distribution distribution, Map<String, Object> props) throws ToolProcessingException {
Path packageDirectory = (Path) props.get(Constants.KEY_PACKAGE_DIRECTORY);
String version = (String) props.get(Constants.KEY_PROJECT_EFFECTIVE_VERSION);
String snapName = distribution.getName() + "-" + version + ".snap";
List<String> cmd = new ArrayList<>();
cmd.add("snapcraft");
cmd.add("push");
cmd.add(snapName);
return executeCommand(packageDirectory, cmd);
}
private boolean createSnap(Distribution distribution, Map<String, Object> props) throws ToolProcessingException {
Path packageDirectory = (Path) props.get(Constants.KEY_PACKAGE_DIRECTORY);
String version = (String) props.get(Constants.KEY_PROJECT_EFFECTIVE_VERSION);
String snapName = distribution.getName() + "-" + version + ".snap";
@@ -163,9 +155,8 @@ public class SnapToolProcessor extends AbstractRepositoryToolProcessor<Snap> {
List<String> cmd = new ArrayList<>();
cmd.add("snapcraft");
cmd.add("snap");
cmd.add(primeDirectory.toAbsolutePath().toString());
cmd.add("--output");
cmd.add(packageDirectory.resolve(snapName).toAbsolutePath().toString());
return executeCommand(cmd);
cmd.add(snapName);
return executeCommand(packageDirectory, cmd);
}
}

View File

@@ -42,6 +42,8 @@ distributions:
artifacts:
- path: apps/{{distributionName}}/build/distributions/{{distributionName}}-{{projectVersion}}.zip
transform: '{{distributionName}}/{{distributionName}}-{{projectEffectiveVersion}}.zip'
- path: apps/{{distributionName}}/build/distributions/{{distributionName}}-{{projectVersion}}.tgz
transform: '{{distributionName}}/{{distributionName}}-{{projectEffectiveVersion}}.tgz'
jreleaser-ant-tasks:
extraProperties: