-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from palantir/feature/separate-plugins
Separate PalantirDockerPlugin and DockerComposeTemplatePlugin into tw…
- Loading branch information
Showing
10 changed files
with
351 additions
and
259 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
56 changes: 56 additions & 0 deletions
56
src/main/groovy/com/palantir/gradle/docker/DockerComposeExtension.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,56 @@ | ||
/* | ||
* Copyright 2015 Palantir Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* <http://www.apache.org/licenses/LICENSE-2.0> | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.palantir.gradle.docker | ||
|
||
import com.google.common.base.Preconditions | ||
import org.gradle.api.Project | ||
|
||
class DockerComposeExtension { | ||
Project project | ||
|
||
private String template = 'docker-compose.yml.template' | ||
private String dockerComposeFile = 'docker-compose.yml' | ||
|
||
private File resolvedDockerComposeTemplate = null | ||
private File resolvedDockerComposeFile = null | ||
|
||
public DockerComposeExtension(Project project) { | ||
this.project = project | ||
} | ||
|
||
public void setTemplate(String dockerComposeTemplate) { | ||
this.template = dockerComposeTemplate | ||
Preconditions.checkArgument(project.file(dockerComposeTemplate).exists(), | ||
"Could not find specified template file: %s", project.file(dockerComposeTemplate)) | ||
} | ||
|
||
public void setDockerComposeFile(String dockerComposeFile) { | ||
this.dockerComposeFile = dockerComposeFile | ||
} | ||
|
||
File getResolvedDockerComposeTemplate() { | ||
return resolvedDockerComposeTemplate | ||
} | ||
|
||
File getResolvedDockerComposeFile() { | ||
return resolvedDockerComposeFile | ||
} | ||
|
||
public void resolvePathsAndValidate() { | ||
resolvedDockerComposeFile = project.file(dockerComposeFile) | ||
resolvedDockerComposeTemplate = project.file(template) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/groovy/com/palantir/gradle/docker/DockerComposePlugin.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,51 @@ | ||
package com.palantir.gradle.docker | ||
|
||
import com.google.common.base.Preconditions | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.tasks.Copy | ||
|
||
class DockerComposePlugin implements Plugin<Project> { | ||
@Override | ||
void apply(Project project) { | ||
DockerComposeExtension ext = | ||
project.extensions.create('dockerCompose', DockerComposeExtension, project) | ||
project.configurations.create("docker") | ||
|
||
Copy generateDockerCompose = project.tasks.create('generateDockerCompose', Copy, { | ||
description = 'Populates docker-compose.yml.template file with image versions specified by "docker" ' + | ||
'dependencies' | ||
}) | ||
|
||
project.afterEvaluate { | ||
ext.resolvePathsAndValidate() | ||
if (ext.resolvedDockerComposeTemplate.exists()) { | ||
def dockerDependencies = project.configurations.docker.resolvedConfiguration.resolvedArtifacts | ||
def templateTokens = dockerDependencies.collectEntries { | ||
def version = it.moduleVersion.id | ||
[("{{${version.group}:${version.name}}}"): version.version] | ||
} | ||
|
||
generateDockerCompose.with { | ||
from(ext.resolvedDockerComposeTemplate) | ||
into(ext.resolvedDockerComposeFile.parentFile) | ||
rename { fileName -> | ||
fileName.replace( | ||
ext.resolvedDockerComposeTemplate.name, ext.resolvedDockerComposeFile.name) | ||
} | ||
filter { String line -> replaceAll(line, templateTokens, ext) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** Replaces all occurrences of templatesTokens's keys by their corresponding values in the given line. */ | ||
static def replaceAll(String line, Map<String, String> templateTokens, DockerComposeExtension ext) { | ||
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", | ||
ext.resolvedDockerComposeTemplate, unmatchedTokens, templateTokens) | ||
return line | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/gradle-plugins/com.palantir.docker-compose.properties
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 @@ | ||
implementation-class=com.palantir.gradle.docker.DockerComposePlugin |
Oops, something went wrong.