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

build: generate unique p2 repo for quarkus & qute #952

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 31 additions & 0 deletions p2repo/category.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<site>
<bundle id="com.redhat.qute.jdt.test" version="0.0.0">
<category name="qute.sdk"/>
</bundle>
<bundle id="com.redhat.qute.jdt.test.source" version="0.0.0">
<category name="qute.sdk"/>
</bundle>
<bundle id="com.redhat.qute.jdt.source" version="0.0.0">
<category name="qute.sdk"/>
</bundle>
<bundle id="com.redhat.qute.jdt" version="0.0.0">
<category name="qute.core"/>
</bundle>
<category-def name="qute.core" label="Qute JDT.LS extension"/>
<category-def name="qute.sdk" label="Qute JDT.LS extension (Developer Resources)"/>

<bundle id="com.redhat.microprofile.jdt.quarkus.test" version="0.0.0">
<category name="sdk"/>
</bundle>
<bundle id="com.redhat.microprofile.jdt.quarkus.test.source" version="0.0.0">
<category name="sdk"/>
</bundle>
<bundle id="com.redhat.microprofile.jdt.quarkus.source" version="0.0.0">
<category name="sdk"/>
</bundle>
<bundle id="com.redhat.microprofile.jdt.quarkus" version="0.0.0">
<category name="core"/>
</bundle>
<category-def name="core" label="Quarkus JDT.LS extension"/>
<category-def name="sdk" label="Quarkus JDT.LS extension (Developer Resources)"/>
</site>
91 changes: 91 additions & 0 deletions p2repo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.redhat.microprofile</groupId>
<artifactId>p2repo</artifactId>
<version>0.18.0-SNAPSHOT</version>
<packaging>eclipse-repository</packaging>
<name>Quarkus &amp; Qute :: p2 repository</name>
<description>Combines Quarkus &amp; Qute p2 repositories into one flat repository, compatible with Github releases</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<tycho.version>4.0.5</tycho.version>
<tycho.extras.version>${tycho.version}</tycho.extras.version>
<tycho.scmUrl>scm:git:https://github.com/redhat-developer/quarkus-ls</tycho.scmUrl>
<jdt.ls.version>1.32.0-SNAPSHOT</jdt.ls.version>
<lsp4mp.p2.url>https://download.eclipse.org/lsp4mp/snapshots/0.11.0/repository/</lsp4mp.p2.url>
</properties>
<repositories>
<repository>
<id>jdt.ls.p2</id>
<layout>p2</layout>
<url>https://download.eclipse.org/jdtls/snapshots/repository/latest/</url>
</repository>
<repository>
<id>lsp4mp.p2</id>
<layout>p2</layout>
<url>${lsp4mp.p2.url}</url>
</repository>
<repository>
<id>qute.p2</id>
<layout>p2</layout>
<url>file://${project.basedir}/../qute.jdt/com.redhat.qute.jdt.site/target/repository/</url>
</repository>
<repository>
<id>quarkus.p2</id>
<layout>p2</layout>
<url>file://${project.basedir}/../quarkus.jdt.ext/com.redhat.microprofile.jdt.quarkus.site/target/repository/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<resolver>p2</resolver>
<target>
<artifact>
<groupId>org.eclipse.jdt.ls</groupId>
<artifactId>org.eclipse.jdt.ls.tp</artifactId>
<version>${jdt.ls.version}</version>
</artifact>
</target>
<resolver>p2</resolver>
<pomDependencies>consider</pomDependencies>
<ignoreTychoRepositories>true</ignoreTychoRepositories>
</configuration>
</plugin>
<plugin>
<groupId>dev.jbang</groupId>
<artifactId>jbang-maven-plugin</artifactId>
<version>0.0.7</version>
<executions>
<execution>
<id>run</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<script>${project.basedir}/src/jbang/java/repoflattener.java</script>
<args>
<arg>${project.basedir}</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
122 changes: 122 additions & 0 deletions p2repo/src/jbang/java/repoflattener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
/**
* Copyright 2022 Fred Bricon
*
* Licensed 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
*
* http://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.
*/

//DEPS commons-io:commons-io:2.11.0
//DEPS org.tukaani:xz:1.9
//JAVA 17+
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.jar.JarInputStream;
import java.util.stream.Collectors;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.file.PathUtils;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZOutputStream;

public class repoflattener {

public static void main(String... args) throws IOException {
Path baseDir = (args == null || args.length == 0) ? Path.of("") : Path.of(args[0]);

Path originalRepo = baseDir.resolve("target").resolve("repository").toAbsolutePath();
System.out.println("🛠 flattening " + originalRepo);
Path flatRepo = originalRepo.resolveSibling("flat-repository");
if (Files.exists(flatRepo)) {
PathUtils.deleteDirectory(flatRepo);
}
Files.createDirectory(flatRepo);

var files = Files.walk(originalRepo).filter(path -> {
if (!Files.isRegularFile(path)) {
return false;
}
var fileName = FilenameUtils.getName(path.toString());
return !fileName.startsWith("artifacts");
}).toList();

for (Path file : files) {
PathUtils.copyFileToDirectory(file, flatRepo);
}
Path artifactsXml = extractAndRewriteArtifactXml(originalRepo.resolve("artifacts.jar"));
createXZ(artifactsXml, flatRepo);
createJar(artifactsXml, flatRepo);

System.out.println("🙌 repository was flattened to " + flatRepo.toAbsolutePath());
}

private static Path extractAndRewriteArtifactXml(Path archive) throws IOException {
var extracted = Files.createTempFile("artifacts", ".xml");
try (JarInputStream archiveInputStream = new JarInputStream(
new BufferedInputStream(Files.newInputStream(archive)))) {
// we assume only 1 entry
archiveInputStream.getNextJarEntry();
streamRewrite(archiveInputStream, extracted);
}
if (Files.size(extracted) == 0) {
throw new IOException("💥 Failed to extract/rewrite artifacts.xml");
}
return extracted;
}

private static void streamRewrite(InputStream src, Path dst) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(src));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(dst)))) {
String line;
while ((line = br.readLine()) != null) {
line = line.replace("/plugins/", "/").replace("/features/", "/");
bw.write(line);
bw.newLine();
}
}
}

private static void createXZ(Path artifactsXml, Path flatRepo) throws IOException {
Path artifactsXmlXZ = flatRepo.resolve("artifacts.xml.xz");
try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(artifactsXml));
XZOutputStream xzOut = new XZOutputStream(
new BufferedOutputStream(Files.newOutputStream(artifactsXmlXZ)), new LZMA2Options());) {
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = in.read(buffer))) {
xzOut.write(buffer, 0, n);
}
}
}

private static void createJar(Path artifactXml, Path flatRepo) throws IOException {
Path artifactsJar = flatRepo.resolve("artifacts.jar").toAbsolutePath();
var env = Collections.singletonMap("create", "true");// Create the zip file if it doesn't exist
URI uri = URI.create("jar:file:" + artifactsJar.toString().replace('\\', '/'));
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path pathInZipfile = zipfs.getPath("artifacts.xml");
Files.copy(artifactXml, pathInZipfile, StandardCopyOption.REPLACE_EXISTING);
}
}
}
Loading