Skip to content
Merged
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
20 changes: 11 additions & 9 deletions buildSrc/src/main/java/GenerateSources.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.services.ServiceReference;
import org.gradle.api.tasks.*;
import org.javagi.JavaGI;

import java.io.*;
import java.nio.file.*;
Expand All @@ -41,10 +43,7 @@ public abstract class GenerateSources extends DefaultTask {
public abstract Property<GirParserService> getGirParserService();

@Input
public abstract Property<String> getNamespace();

@Input
public abstract Property<String> getVersion();
public abstract ListProperty<String> getGirFiles();

@InputFiles
public abstract DirectoryProperty getMainJavaSourcesDirectory();
Expand All @@ -56,12 +55,15 @@ public abstract class GenerateSources extends DefaultTask {
void execute() {
try {
var buildService = getGirParserService().get();
var name = getNamespace().get();
var version = getVersion().get();
var library = buildService.getLibrary(name, version);
var packages = getPackages();
var girFiles = getGirFiles().get();
var library = buildService.getLibrary(girFiles);
var outputDirectory = getOutputDirectory().get().getAsFile();
generate(name, library, packages, outputDirectory);
for (String repo : girFiles) {
String name = repo.substring(0, repo.indexOf('-'));
library.setExported(name);
generate(name, library, outputDirectory);
}
JavaGI.generateModuleInfo(library, getPackages(), outputDirectory);
} catch (Exception e) {
throw new TaskExecutionException(this, e);
}
Expand Down
44 changes: 27 additions & 17 deletions buildSrc/src/main/java/GirParserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,50 @@ public interface Params extends BuildServiceParameters {
ConfigurableFileCollection getInputDirectories();
}

private final Library library = new Library();

// A list of loaded repositories, used as a locking mechanism to prevent
// multiple repositories from being loaded concurrently in parallel builds.
// A list of loaded repositories, also used as a locking mechanism to
// prevent multiple repositories from being loaded concurrently in
// parallel builds.
private final Map<String, Repository> repositories = new ConcurrentHashMap<>();

/**
* Create and return a Library that contains the requested Repository with
* its dependencies.
* <p>
* When building multiple modules, the library is shared, so it will often
* contain more Repositories than were requested.
* Create and return a Library that contains the requested Repositories
* with their dependencies.
*
* @param name the name of the requested Repository
* @return a Library object with the Repository and its dependencies, and
* possibly other Repositories
* @param girFiles the names and versions of the requested Repositories
* @return a Library object with the Repositories and their dependencies
*/
public Library getLibrary(String name, String version) {
public Library getLibrary(List<String> girFiles) {
Library library = new Library();
for (String repo : girFiles) {
int dash = repo.indexOf('-');
if (dash <= 0 || dash >= (repo.length() - 1))
throw new IllegalArgumentException("Repository not in 'name-version' format: " + repo);
String name = repo.substring(0, dash);
String version = repo.substring(dash + 1);
addToLibrary(library, name, version);
}
return library;
}

/*
* Parse the requested gir file and its dependencies, and add them
* to the library.
*/
private void addToLibrary(Library library, String name, String version) {
Repository repository = library.get(name);
if (repository != null)
return library;
return;

// Parse the repository
repositories.computeIfAbsent(name, n -> parse(n, version));
repository = repositories.get(name);

// Parse all dependencies and add them to the library
for (var include : repository.includes())
getLibrary(include.name(), include.version());
addToLibrary(library, include.name(), include.version());

// Add the repository to the library
library.put(name, repository);

return library;
}

/*
Expand Down
29 changes: 17 additions & 12 deletions generator/src/main/java/org/javagi/JavaGI.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public Integer call() throws Exception {
var name = repository.namespace().name();
var packageName = generatePackageName(name);
ModuleInfo.add(name, packageName, packageName, docUrl, summary);
library.put(girFile.getName(), repository);
library.put(name, repository);
library.setExported(name);

// Create a directory for each module
var libDirectory = new File(outputDirectory, name.toLowerCase());
Expand All @@ -199,7 +200,8 @@ public Integer call() throws Exception {
var ignored = srcDirectory.mkdirs();

// Generate the language bindings
generate(name, library, packages, srcDirectory);
generate(name, library, srcDirectory);
generateModuleInfo(library, packages, srcDirectory);

if (generateProject) {
// Generate build.gradle script
Expand Down Expand Up @@ -255,11 +257,10 @@ private String generatePackageName(String namespace) {
// Generate Java language bindings for a GIR repository
public static void generate(String name,
Library library,
Set<String> packages,
File outputDirectory) throws IOException {

Namespace ns = library.lookupNamespace(name);
String packageName = ModuleInfo.packageName(name);
String packageName = ModuleInfo.javaPackage(name);

String licenseNotice = licenseNotice();

Expand All @@ -274,11 +275,6 @@ public static void generate(String name,
String packageInfo = new PackageInfoGenerator(ns).generate();
Files.writeString(path, packageInfo, CREATE, WRITE, TRUNCATE_EXISTING);

// Generate module-info.java
path = outputDirectory.toPath().resolve("module-info.java");
String moduleInfo = new ModuleInfoGenerator(ns, packages).generate();
Files.writeString(path, moduleInfo, CREATE, WRITE, TRUNCATE_EXISTING);

// Generate classes for all registered types in this namespace
for (var rt : ns.registeredTypes().values()) {

Expand Down Expand Up @@ -307,13 +303,22 @@ public static void generate(String name,
var generator = new InterfaceGenerator(i);
if (generator.hasDowncallHandles())
writeJavaFile(generator.downcallHandlesClass(),
packageName,
licenseNotice,
outputDirectory);
packageName,
licenseNotice,
outputDirectory);
}
}
}

// Generate module-info.java
public static void generateModuleInfo(Library library,
Set<String> packages,
File outputDirectory) throws IOException {
Path path = outputDirectory.toPath().resolve("module-info.java");
String moduleInfo = new ModuleInfoGenerator(library, packages).generate();
Files.writeString(path, moduleInfo, CREATE, WRITE, TRUNCATE_EXISTING);
}

// Write a generated class into a Java file
private static void writeJavaFile(TypeSpec typeSpec,
String packageName,
Expand Down
Loading