| import java.nio.file.Path |
| import java.nio.file.Paths |
| import java.nio.file.Files |
| import java.text.MessageFormat |
| |
| |
| class UpdateLibrariesTask extends DefaultTask { |
| Path directory |
| List<Map> libraries |
| Set<String> manualLibraries = new HashSet<>(); |
| |
| @TaskAction |
| def update() { |
| Set<String> librariesNames = new HashSet<>() |
| |
| libraries.each({ |
| String url = it.get("url") |
| String libraryName = it.get("name") |
| |
| librariesNames.add(libraryName) |
| |
| Path libraryFile = directory.resolve(libraryName) |
| if (Files.notExists(libraryFile)) { |
| logger.lifecycle(MessageFormat.format("Downloading library {0} from {1}", libraryName, url)) |
| ant.get(src: url, dest: libraryFile) |
| } else { |
| logger.lifecycle(MessageFormat.format("Skipping download of library {0} because it already exists", libraryName)) |
| } |
| }) |
| |
| Files.list(directory) |
| .filter({ |
| String filename = it.getFileName().toString() |
| !librariesNames.contains(filename) && !manualLibraries.contains(filename) |
| }) |
| .each({ |
| logger.lifecycle(MessageFormat.format("Deleting old library {0}", it.getFileName())) |
| Files.delete(it) |
| }) |
| } |
| |
| } |
| |
| ext.UpdateLibrariesTask = UpdateLibrariesTask |
| |
| |
| class BuildLibraryTask extends DefaultTask { |
| Path targetDirectory |
| String targetLibraryName |
| String builderUrl |
| String[] buildCommand |
| Map<String, String> buildEnv = new HashMap<>() |
| String[] builtLibraryName |
| String builderName = "library_builder" |
| |
| def getBuildDirectory() { |
| return getProject().buildDir.toPath().toAbsolutePath().resolve("libraries").resolve(targetLibraryName) |
| } |
| |
| def getBuilderPath() { |
| return getBuildDirectory().resolve(builderName) |
| } |
| |
| @TaskAction |
| def update() { |
| Path libraryFile = targetDirectory.resolve(targetLibraryName) |
| Path buildDirectory = getBuildDirectory() |
| Files.createDirectories(buildDirectory) |
| if (Files.notExists(libraryFile)) { |
| logger.lifecycle(MessageFormat.format("Building library {0} from {1}", targetLibraryName, builderUrl)) |
| |
| ant.get(src: builderUrl, dest: getBuilderPath()) |
| |
| Map<String, String> finalBuildEnv = new HashMap<String, String>(); |
| finalBuildEnv.putAll(System.getenv()); |
| finalBuildEnv.putAll(buildEnv) |
| getProject().exec { |
| workingDir = buildDirectory.toFile() |
| commandLine = Arrays.asList(buildCommand) |
| environment = finalBuildEnv |
| } |
| |
| Path builtLibraryFile = buildDirectory |
| for (String builtLibraryElementName : builtLibraryName) { |
| builtLibraryFile = builtLibraryFile.resolve(builtLibraryElementName) |
| } |
| Files.copy(builtLibraryFile, libraryFile) |
| buildDirectory.toFile().deleteDir() |
| |
| logger.lifecycle(MessageFormat.format("Successfully built library {0}", targetLibraryName)) |
| } else { |
| logger.lifecycle(MessageFormat.format("Skipping building of library {0} because it already exists", targetLibraryName)) |
| } |
| } |
| } |
| |
| ext.BuildLibraryTask = BuildLibraryTask |