Created
April 5, 2019 03:36
-
-
Save big-guy/03a491cfc486cbd31e08a80727846103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OsDeps { | |
final ListProperty<Tool> tools | |
final DirectoryProperty pcDirectory | |
final ProviderFactory providers | |
final ObjectFactory objects | |
OsDeps(Project project) { | |
objects = project.objects | |
providers = project.providers | |
pcDirectory = objects.directoryProperty() | |
tools = objects.listProperty(Tool) | |
} | |
FileCollection systemLibrary(String packageName) { | |
return objects.fileCollection().from(pcDirectory.file(packageName)) | |
} | |
FileCollection toolDep(String executable) { | |
return objects.fileCollection().from(providers.provider { | |
locate(executable) | |
}) | |
} | |
void tool(String executable, String versionHint=null) { | |
tools.add(new Tool(executable, versionHint)) | |
} | |
Provider<Map<String, String>> getRequiredTools() { | |
return tools.map { list -> | |
list.collectEntries { tool -> | |
collectVersion(tool) | |
} | |
} | |
} | |
// TODO: Caching? | |
protected def collectVersion(Tool tool) { | |
final String version | |
if (tool.versionHint == null) { | |
version = versionFrom(locate(tool.executable)) | |
} else { | |
version = versionFrom(tool.executable + " " + tool.versionHint) | |
} | |
return [ tool.executable, version ] | |
} | |
protected File locate(String executable) { | |
def process = "which ${executable}".execute() | |
if (process.waitFor() == 0) { | |
return new File(process.text.trim()) | |
} | |
throw new IllegalArgumentException("Could not find " + executable + " on PATH") | |
} | |
private String versionFrom(File executableFile) { | |
return java.security.MessageDigest.getInstance("MD5").digest(executableFile.bytes).encodeHex().toString() | |
} | |
private String versionFrom(String commandLine) { | |
def process = commandLine.execute() | |
if (process.waitFor() == 0) { | |
return process.text | |
} | |
throw new IllegalArgumentException("Could not calculate version for " + commandLine) | |
} | |
} | |
class Tool { | |
final String executable | |
final String versionHint | |
Tool(String executable, String versionHint) { | |
this.executable = executable | |
this.versionHint = versionHint | |
} | |
} | |
extensions.create("osdeps", OsDeps, project) | |
osdeps { | |
pcDirectory = file("/usr/local//lib/pkgconfig") | |
} | |
apply plugin: 'base' | |
configurations { | |
test | |
} | |
dependencies { | |
test osdeps.systemLibrary("python-2") | |
test osdeps.toolDep("gcc") | |
} | |
osdeps { | |
tool("gcc") | |
tool("g++", "--version") | |
tool("bison", "--version") | |
} | |
// diagnostics | |
task printSystemLibraries { | |
dependsOn configurations.test | |
doLast { | |
configurations.test.each { | |
println it | |
} | |
} | |
} | |
task printTools { | |
doLast { | |
osdeps.requiredTools.get().each { k, v -> | |
println k + " => " + v | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment