Search in sources :

Example 1 with VersionDependency

use of org.gradle.swiftpm.internal.VersionDependency in project gradle by gradle.

the class GenerateSwiftPackageManagerManifest method generate.

@TaskAction
public void generate() {
    DefaultPackage srcPackage = (DefaultPackage) packageProperty.get();
    Path manifest = manifestFile.get().getAsFile().toPath();
    try {
        Path baseDir = manifest.getParent();
        Files.createDirectories(baseDir);
        PrintWriter writer = new PrintWriter(Files.newBufferedWriter(manifest, Charset.forName("utf-8")));
        try {
            writer.println("// swift-tools-version:4.0");
            writer.println("//");
            writer.println("// GENERATED FILE - do not edit");
            writer.println("//");
            writer.println("import PackageDescription");
            writer.println();
            writer.println("let package = Package(");
            writer.println("    name: \"" + getProject().getName() + "\",");
            writer.println("    products: [");
            for (AbstractProduct product : srcPackage.getProducts()) {
                if (product.isExecutable()) {
                    writer.print("        .executable(");
                    writer.print("name: \"");
                    writer.print(product.getName());
                    writer.print("\"");
                } else {
                    writer.print("        .library(");
                    writer.print("name: \"");
                    writer.print(product.getName());
                    DefaultLibraryProduct library = (DefaultLibraryProduct) product;
                    if (library.getLinkage() == Linkage.SHARED) {
                        writer.print("\", type: .dynamic");
                    } else {
                        writer.print("\", type: .static");
                    }
                }
                writer.print(", targets: [\"");
                writer.print(product.getTarget().getName());
                writer.println("\"]),");
            }
            writer.println("    ],");
            if (!srcPackage.getDependencies().isEmpty()) {
                writer.println("    dependencies: [");
                for (Dependency dependency : srcPackage.getDependencies()) {
                    writer.print("        .package(url: \"");
                    if (dependency.getUrl().getScheme().equals("file")) {
                        writer.print(baseDir.relativize(new File(dependency.getUrl()).toPath()));
                    } else {
                        writer.print(dependency.getUrl());
                    }
                    writer.print("\", ");
                    if (dependency instanceof VersionDependency) {
                        VersionDependency versionDependency = (VersionDependency) dependency;
                        if (versionDependency.getUpperBound() == null) {
                            writer.print("from: \"");
                            writer.print(versionDependency.getLowerBound());
                            writer.print("\"");
                        } else if (versionDependency.isUpperInclusive()) {
                            writer.print("\"");
                            writer.print(versionDependency.getLowerBound());
                            writer.print("\"...\"");
                            writer.print(versionDependency.getUpperBound());
                            writer.print("\"");
                        } else {
                            writer.print("\"");
                            writer.print(versionDependency.getLowerBound());
                            writer.print("\"..<\"");
                            writer.print(versionDependency.getUpperBound());
                            writer.print("\"");
                        }
                    } else {
                        writer.print(".branch(\"");
                        writer.print(((BranchDependency) dependency).getBranch());
                        writer.print("\")");
                    }
                    writer.println("),");
                }
                writer.println("    ],");
            }
            writer.println("    targets: [");
            for (DefaultTarget target : srcPackage.getTargets()) {
                writer.println("        .target(");
                writer.print("            name: \"");
                writer.print(target.getName());
                writer.println("\",");
                if (!target.getRequiredTargets().isEmpty() || !target.getRequiredProducts().isEmpty()) {
                    writer.println("            dependencies: [");
                    for (String dep : target.getRequiredTargets()) {
                        writer.print("                .target(name: \"");
                        writer.print(dep);
                        writer.println("\"),");
                    }
                    for (String dep : target.getRequiredProducts()) {
                        writer.print("                .product(name: \"");
                        writer.print(dep);
                        writer.println("\"),");
                    }
                    writer.println("            ],");
                }
                writer.print("            path: \"");
                Path productPath = target.getPath().toPath();
                String relPath = baseDir.relativize(productPath).toString();
                writer.print(relPath.isEmpty() ? "." : relPath);
                writer.println("\",");
                writer.println("            sources: [");
                Set<String> sorted = new TreeSet<String>();
                for (File sourceFile : target.getSourceFiles()) {
                    sorted.add(productPath.relativize(sourceFile.toPath()).toString());
                }
                for (String sourcePath : sorted) {
                    writer.print("                \"");
                    writer.print(sourcePath);
                    writer.println("\",");
                }
                writer.print("            ]");
                if (target.getPublicHeaderDir() != null) {
                    writer.println(",");
                    writer.print("            publicHeadersPath: \"");
                    writer.print(productPath.relativize(target.getPublicHeaderDir().toPath()));
                    writer.print("\"");
                }
                writer.println();
                writer.println("        ),");
            }
            writer.print("    ]");
            if (srcPackage.getSwiftLanguageVersion() != null) {
                writer.println(",");
                writer.print("    swiftLanguageVersions: [");
                writer.print(srcPackage.getSwiftLanguageVersion().getVersion());
                writer.print("]");
            }
            writer.println();
            writer.println(")");
        } finally {
            writer.close();
        }
    } catch (IOException e) {
        throw new GradleException(String.format("Could not write manifest file %s.", manifest), e);
    }
}
Also used : Path(java.nio.file.Path) DefaultTarget(org.gradle.swiftpm.internal.DefaultTarget) AbstractProduct(org.gradle.swiftpm.internal.AbstractProduct) DefaultPackage(org.gradle.swiftpm.internal.DefaultPackage) VersionDependency(org.gradle.swiftpm.internal.VersionDependency) Dependency(org.gradle.swiftpm.internal.Dependency) BranchDependency(org.gradle.swiftpm.internal.BranchDependency) IOException(java.io.IOException) TreeSet(java.util.TreeSet) GradleException(org.gradle.api.GradleException) DefaultLibraryProduct(org.gradle.swiftpm.internal.DefaultLibraryProduct) File(java.io.File) OutputFile(org.gradle.api.tasks.OutputFile) VersionDependency(org.gradle.swiftpm.internal.VersionDependency) PrintWriter(java.io.PrintWriter) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Path (java.nio.file.Path)1 TreeSet (java.util.TreeSet)1 GradleException (org.gradle.api.GradleException)1 OutputFile (org.gradle.api.tasks.OutputFile)1 TaskAction (org.gradle.api.tasks.TaskAction)1 AbstractProduct (org.gradle.swiftpm.internal.AbstractProduct)1 BranchDependency (org.gradle.swiftpm.internal.BranchDependency)1 DefaultLibraryProduct (org.gradle.swiftpm.internal.DefaultLibraryProduct)1 DefaultPackage (org.gradle.swiftpm.internal.DefaultPackage)1 DefaultTarget (org.gradle.swiftpm.internal.DefaultTarget)1 Dependency (org.gradle.swiftpm.internal.Dependency)1 VersionDependency (org.gradle.swiftpm.internal.VersionDependency)1