Skip to content

Commit

Permalink
Proof of concept support for native image layers
Browse files Browse the repository at this point in the history
  • Loading branch information
melix committed Sep 10, 2024
1 parent eaf111a commit f1b1036
Show file tree
Hide file tree
Showing 12 changed files with 810 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ repositories {
group = "org.graalvm.buildtools"

extensions.findByType<VersionCatalogsExtension>()?.also { catalogs ->
val versionFromCatalog = catalogs.named("libs")
if (catalogs.find("libs").isPresent) {
val versionFromCatalog = catalogs.named("libs")
.findVersion("nativeBuildTools")
if (versionFromCatalog.isPresent()) {
version = versionFromCatalog.get().requiredVersion
if (versionFromCatalog.isPresent()) {
version = versionFromCatalog.get().requiredVersion
} else {
throw GradleException("Version catalog doesn't define project version 'nativeBuildTools'")
}
} else {
throw GradleException("Version catalog doesn't define project version 'nativeBuildTools'")
version = "undefined"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* 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
*
* https://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.
*/
package org.graalvm.buildtools.utils;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class JarMetadata {
private final List<String> packageList;

public JarMetadata(List<String> packageList) {
this.packageList = packageList;
}

public List<String> getPackageList() {
return packageList;
}

public static JarMetadata readFrom(Path propertiesFile) {
Properties props = new Properties();
try (InputStream is = Files.newInputStream(propertiesFile)) {
props.load(is);
} catch (Exception e) {
throw new RuntimeException("Unable to read metadata from properties file " + propertiesFile, e);
}
String packages = (String) props.get("packages");
List<String> packageList = packages == null ? List.of() : Arrays.asList(packages.split(","));
return new JarMetadata(packageList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* 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
*
* https://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.
*/
package org.graalvm.buildtools.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

/**
* Performs scanning of a JAR file and extracts some metadata in the
* form of a properties file. For now this type only extracts the list
* of packages from a jar.
*/
public class JarScanner {
/**
* Scans a jar and creates a properties file with metadata about the jar contents.
* @param inputJar the input jar
* @param outputFile the output file
* @throws IOException
*/
public static void scanJar(Path inputJar, Path outputFile) throws IOException {
try (Writer fileWriter = Files.newBufferedWriter(outputFile); PrintWriter writer = new PrintWriter(fileWriter)) {
Set<String> packageList = new TreeSet<>();
try (FileSystem jarFileSystem = FileSystems.newFileSystem(inputJar, null)) {
Path root = jarFileSystem.getPath("/");
try (Stream<Path> files = Files.walk(root)) {
files.forEach(path -> {
if (path.toString().endsWith(".class")) {
Path relativePath = root.relativize(path);
String className = relativePath.toString()
.replace('/', '.')
.replace('\\', '.')
.replace(".class", "");
var lastDot = className.lastIndexOf(".");
if (lastDot > 0) {
packageList.add(className.substring(0, lastDot));
}
}
});
}
}
writer.println("packages=" + String.join(",", packageList));
} catch (IOException ex) {
throw new RuntimeException("Unable to write JAR analysis", ex);
}
}
}
Loading

0 comments on commit f1b1036

Please sign in to comment.