use of org.gradle.swiftpm.internal.DefaultPackage 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);
}
}
Aggregations