Skip to content

Commit

Permalink
feat: allow to set custom project name prefix Fixes #231
Browse files Browse the repository at this point in the history
  • Loading branch information
augi committed May 11, 2020
1 parent 326bf14 commit 893d72d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -110,6 +110,7 @@ dockerCompose {
removeOrphans = false // removes containers for services not defined in the Compose file; default is false
projectName = 'my-project' // allow to set custom docker-compose project name (defaults to a stable name derived from absolute path of the project and nested settings name), set to null to Docker Compose default (directory name)
projectNamePrefix = 'my_prefix_' // allow to set custom prefix of docker-compose project name, the final project name has nested configuration name appended
executable = '/path/to/docker-compose' // allow to set the path of the docker-compose executable (useful if not present in PATH)
dockerExecutable = '/path/to/docker' // allow to set the path of the docker executable (useful if not present in PATH)
dockerComposeWorkingDirectory = '/path/where/docker-compose/is/invoked/from'
Expand Down
Expand Up @@ -40,8 +40,9 @@ class ComposeExecutor {
finalArgs.add('--no-ansi')
}
finalArgs.addAll(ex.useComposeFiles.collectMany { ['-f', it].asCollection() })
if (ex.projectName) {
finalArgs.addAll(['-p', ex.projectName])
String pn = ex.projectName
if (pn) {
finalArgs.addAll(['-p', pn])
}
finalArgs.addAll(args)
e.commandLine finalArgs
Expand Down
Expand Up @@ -63,7 +63,27 @@ class ComposeSettings {
List<String> pullAdditionalArgs = []
List<String> upAdditionalArgs = []
List<String> downAdditionalArgs = []
String projectName

protected String customProjectName
protected Boolean customProjectNameSet
void setProjectName(String customProjectName)
{
this.customProjectName = customProjectName
this.customProjectNameSet = true
}
String getProjectName() {
if (customProjectNameSet) {
return customProjectName
}
else if (projectNamePrefix) {
return "${projectNamePrefix}_${nestedName}"
}
else {
return "${generateSafeProjectNamePrefix(project)}_${nestedName}"
}
}
String projectNamePrefix
protected String nestedName

boolean stopContainers = true
boolean removeContainers = true
Expand All @@ -85,6 +105,7 @@ class ComposeSettings {

ComposeSettings(Project project, String name = '') {
this.project = project
this.nestedName = name

upTask = project.tasks.register(name ? "${name}ComposeUp" : 'composeUp', ComposeUp, { it.settings = this })
buildTask = project.tasks.register(name ? "${name}ComposeBuild" : 'composeBuild', ComposeBuild, { it.settings = this })
Expand All @@ -98,8 +119,6 @@ class ComposeSettings {
this.composeExecutor = new ComposeExecutor(this)
this.serviceInfoCache = new ServiceInfoCache(this)

this.projectName = this.projectName ?: generateProjectName(project, name)

this.containerLogToDir = project.buildDir.toPath().resolve('containers-logs').toFile()

if (OperatingSystem.current().isMacOsX()) {
Expand All @@ -110,9 +129,9 @@ class ComposeSettings {
}
}

private static String generateProjectName(Project project, String name) {
private static String generateSafeProjectNamePrefix(Project project) {
def fullPathMd5 = MessageDigest.getInstance("MD5").digest(project.projectDir.absolutePath.toString().getBytes(StandardCharsets.UTF_8)).encodeHex().toString()
"${fullPathMd5}_${project.name}_${name}"
"${fullPathMd5}_${project.name}"
}

ComposeSettings createNested(String name) {
Expand Down Expand Up @@ -141,6 +160,8 @@ class ComposeSettings {
r.upAdditionalArgs = new ArrayList<>(this.upAdditionalArgs)
r.downAdditionalArgs = new ArrayList<>(this.downAdditionalArgs)

r.projectNamePrefix = this.projectNamePrefix

r.stopContainers = this.stopContainers
r.removeContainers = this.removeContainers
r.removeImages = this.removeImages
Expand Down

0 comments on commit 893d72d

Please sign in to comment.