Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rough cut of export project #1533

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 130 additions & 5 deletions src/main/java/dev/jbang/cli/Export.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.stream.Collectors;

import dev.jbang.Settings;
import dev.jbang.dependencies.ArtifactInfo;
Expand All @@ -25,7 +25,7 @@
import picocli.CommandLine.Command;

@Command(name = "export", description = "Export the result of a build.", subcommands = { ExportPortable.class,
ExportLocal.class, ExportMavenPublish.class, ExportNative.class })
ExportLocal.class, ExportMavenPublish.class, ExportNative.class, ExportProject.class })
public class Export {
}

Expand Down Expand Up @@ -260,7 +260,7 @@ int apply(Project prj, ProjectBuilder pb) throws IOException {
// not possible when native thus for now just regenerate it
Template pomTemplate = TemplateEngine.instance().getTemplate("pom.qute.xml");

Path pomPath = versionDir.resolve(artifact + "-" + version + ".pom");
Path pomPath = versionDir.resolve("pom.xml");
if (pomTemplate == null) {
// ignore
Util.warnMsg("Could not locate pom.xml template");
Expand All @@ -286,6 +286,131 @@ int apply(Project prj, ProjectBuilder pb) throws IOException {
}
}

@Command(name = "project", description = "Exports as a buildable project")
class ExportProject extends BaseExportCommand {

@CommandLine.Option(names = { "--group", "-g" }, description = "The group ID to use for the generated POM.")
String group;

@CommandLine.Option(names = { "--artifact", "-a" }, description = "The artifact ID to use for the generated POM.")
String artifact;

@CommandLine.Option(names = { "--version", "-v" }, description = "The version to use for the generated POM.")
String version;

@Override
int apply(Project prj, ProjectBuilder pb) throws IOException {
Path outputPath = exportMixin.outputFile;

if (outputPath == null) {
outputPath = Paths.get(".");
}

if (!outputPath.toFile().isDirectory()) {
if (outputPath.toFile().exists()) {
Util.warnMsg("Cannot export as maven repository as " + outputPath + " is not a directory.");
return EXIT_INVALID_INPUT;
}
if (exportMixin.force) {
outputPath.toFile().mkdirs();
} else {
Util.warnMsg("Cannot export as " + outputPath + " does not exist. Use --force to create.");
return EXIT_INVALID_INPUT;
}
}

if (prj.getGav().isPresent()) {
MavenCoordinate coord = MavenCoordinate.fromString(prj.getGav().get()).withVersion();
if (group == null) {
group = coord.getGroupId();
}
if (artifact == null) {
artifact = coord.getArtifactId();
}
if (version == null) {
version = coord.getVersion();
}
}

if (group == null) {
Util.warnMsg(
"Cannot export as maven repository as no group specified. Add --group=<group id> and run again.");
return EXIT_INVALID_INPUT;
}
Path groupdir = outputPath.resolve(Paths.get(group.replace(".", "/")));

artifact = artifact != null ? artifact
: Util.getBaseName(prj.getResourceRef().getFile().getFileName().toString());

version = version != null ? version : MavenCoordinate.DEFAULT_VERSION;

Path javaDir = outputPath.resolve("src/main/java");
Files.createDirectories(javaDir);

prj.getMainSourceSet().getSources().forEach(source -> {
Util.infoMsg("Copying " + source.getFile() + " to " + javaDir.resolve(source.getOriginalResource()));
try {
// TODO guard against file/releative path copying.
if (exportMixin.force) {
Files.copy(source.getFile(), javaDir.resolve(source.getOriginalResource()),
StandardCopyOption.REPLACE_EXISTING);
} else {
Files.copy(source.getFile(), javaDir.resolve(source.getOriginalResource()));
}
} catch (IOException e) {
throw new RuntimeException(e);
}

});

// generate pom.xml ... if jar could technically just copy from the jar ...but
// not possible when native thus for now just regenerate it
Template pomTemplate = TemplateEngine.instance().getTemplate("pom.qute.xml");

Path pomPath = outputPath.resolve("pom.xml");
if (pomTemplate == null) {
// ignore
Util.warnMsg("Could not locate pom.xml template");
} else {

Map<Boolean, List<String>> groupedArticles = prj.getMainSourceSet()
.getDependencies()
.stream()
.collect(
Collectors.partitioningBy(a -> a.endsWith("@pom")));

List managedDeps = groupedArticles .get(true)
.stream()
.map(aid -> MavenCoordinate.fromCanonicalString(
aid.substring(0, aid.lastIndexOf("@pom"))))
.collect(Collectors.toList());
List deps = groupedArticles .get(false)
.stream()
.map(aid -> MavenCoordinate.fromCanonicalString(aid))
.collect(Collectors.toList());

String pomfile = pomTemplate
.data("baseName",
Util.getBaseName(
prj.getResourceRef().getFile().getFileName().toString()))
.data("group", group)
.data("artifact", artifact)
.data("version", version)
.data("description", prj.getDescription().orElse(""))
.data("managedDependencies", managedDeps)
.data("dependencies", deps)
.data("javaTarget", prj.getJavaVersion().replace("+", ""))
.render();
Util.infoMsg("Writing " + pomPath);
Util.writeString(pomPath, pomfile);

}

Util.infoMsg("Exported to " + outputPath);
return EXIT_OK;
}
}

@Command(name = "native", description = "Exports native executable")
class ExportNative extends BaseExportCommand {

Expand Down
29 changes: 26 additions & 3 deletions src/main/resources/pom.qute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,35 @@
<artifactId>{artifact}</artifactId>
<version>{version}</version>
<description>{description}</description>

<properties>
<project.java.version>1.8</project.java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>{javaTarget}</maven.compiler.source>
<maven.compiler.target>{javaTarget}</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
{#for item in managedDependencies}
<dependency>
<groupId>{item.groupId}</groupId>
<artifactId>{item.artifactId}</artifactId>
<version>{item.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
{/for}
</dependencies>
</dependencyManagement>

<dependencies>
{#for item in dependencies}
<dependency>
<groupId>{item.coordinate.groupId}</groupId>
<artifactId>{item.coordinate.artifactId}</artifactId>
<version>{item.coordinate.version}</version>
<groupId>{item.groupId}</groupId>
<artifactId>{item.artifactId}</artifactId>
<version>{item.version}</version>
<scope>compile</scope>
</dependency>
{/for}
Expand Down