-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid configuration resolution during project configuration (#82)
- Loading branch information
1 parent
5e491ea
commit 39d5caf
Showing
3 changed files
with
96 additions
and
63 deletions.
There are no files selected for viewing
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
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
78 changes: 78 additions & 0 deletions
78
src/main/groovy/com/palantir/gradle/docker/GenerateDockerCompose.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.palantir.gradle.docker | ||
|
||
import com.google.common.base.Preconditions | ||
import groovy.transform.Memoized | ||
import groovy.util.logging.Slf4j | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.artifacts.ModuleVersionIdentifier | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputFiles | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
@Slf4j | ||
class GenerateDockerCompose extends DefaultTask { | ||
|
||
DockerComposeExtension ext | ||
Configuration configuration | ||
|
||
GenerateDockerCompose() { | ||
group = 'Docker' | ||
} | ||
|
||
@TaskAction | ||
void run() { | ||
if (!template.file) { | ||
throw new IllegalStateException("Could not find specified template file ${template.file}") | ||
} | ||
def templateTokens = moduleDependencies.collectEntries { | ||
[("{{${it.group}:${it.name}}}"): it.version] | ||
} | ||
|
||
dockerComposeFile.withPrintWriter { writer -> | ||
template.eachLine { line -> | ||
writer.println this.replaceAll(line, templateTokens) | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
String getDescription() { | ||
def defaultDescription = "Populates ${ext.template.name} file with versions" + | ||
" of dependencies from the '${configuration.name}' configuration" | ||
return super.description ?: defaultDescription | ||
} | ||
|
||
@Input | ||
@Memoized | ||
Set<ModuleVersionIdentifier> getModuleDependencies() { | ||
log.info "Resolving Docker template dependencies from configuration ${configuration.name}..." | ||
return configuration.resolvedConfiguration | ||
.resolvedArtifacts | ||
*.moduleVersion | ||
*.id | ||
.toSet() | ||
} | ||
|
||
@InputFiles | ||
File getTemplate() { | ||
return ext.template | ||
} | ||
|
||
@OutputFile | ||
File getDockerComposeFile() { | ||
return ext.dockerComposeFile | ||
} | ||
|
||
/** Replaces all occurrences of templatesTokens's keys by their corresponding values in the given line. */ | ||
// Protected to work around GRADLE-1439 | ||
protected String replaceAll(String line, Map<String, String> templateTokens) { | ||
templateTokens.each { mapping -> line = line.replace(mapping.key, mapping.value) } | ||
def unmatchedTokens = line.findAll(/\{\{.*\}\}/) | ||
Preconditions.checkState(unmatchedTokens.size() == 0, | ||
"Failed to resolve Docker dependencies declared in %s: %s. Known dependencies: %s", | ||
template, unmatchedTokens, templateTokens) | ||
return line | ||
} | ||
} |