Skip to content

Commit

Permalink
add support for an installation utilities script that automatically g…
Browse files Browse the repository at this point in the history
…ets prepended to all (pre|post)(un)? scripts
  • Loading branch information
AlanKrueger committed Jun 28, 2011
1 parent 908dd40 commit cafe704
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'eclipse'

group = 'com.trigonic'
artifact = 'gradle-rpm-plugin'
version = '0.6'
version = '0.7'

def props = new Properties();
def localProperties = new File("local.properties")
Expand Down
1 change: 1 addition & 0 deletions src/main/groovy/com/trigonic/gradle/plugins/rpm/Rpm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Rpm extends AbstractArchiveTask {
String url = ''
String sourcePackage
String provides
File installUtils
File preInstall
File postInstall
File preUninstall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class RpmCopySpecVisitor extends EmptyCopySpecVisitor {
ReadableCopySpec spec
boolean didWork

RpmCopySpecVisitor() {
}

@Override
void startVisit(CopyAction action) {
task = action.task
Expand Down Expand Up @@ -66,10 +63,10 @@ class RpmCopySpecVisitor extends EmptyCopySpecVisitor {
}
builder.addHeaderEntry HeaderTag.SOURCERPM, sourcePackage

builder.setPreInstallScript task.preInstall
builder.setPostInstallScript task.postInstall
builder.setPreUninstallScript task.preUninstall
builder.setPostUninstallScript task.postUninstall
builder.setPreInstallScript(scriptWithUtils(task.installUtils, task.preInstall))
builder.setPostInstallScript(scriptWithUtils(task.installUtils, task.postInstall))
builder.setPreUninstallScript(scriptWithUtils(task.installUtils, task.preUninstall))
builder.setPostUninstallScript(scriptWithUtils(task.installUtils, task.postUninstall))
}

@Override
Expand Down Expand Up @@ -110,4 +107,20 @@ class RpmCopySpecVisitor extends EmptyCopySpecVisitor {
boolean getDidWork() {
didWork
}

Object scriptWithUtils(File utils, File script) {
utils == null ? script : concat(utils, script)
}

String concat(File... files) {
String shebang
StringBuilder script = new StringBuilder();
files.each { file ->
file?.eachLine { line ->
script.append line
script.append "\n"
}
}
script.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2011 the original author or authors.
*
* 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.trigonic.gradle.plugins.rpm

import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertSame
import static org.junit.Assert.assertTrue

import org.junit.Before
import org.junit.Test

class RpmCopySpecVisitorTest {
RpmCopySpecVisitor visitor;

@Before
public void setup() {
visitor = new RpmCopySpecVisitor();
}

@Test
public void withoutUtils() {
File script = resourceFile("script.sh")
Object result = visitor.scriptWithUtils(null, script)
assertSame script, result
}

@Test
public void withUtils() {
Object result = visitor.scriptWithUtils(resourceFile("utils.sh"), resourceFile("script.sh"))
assertTrue result instanceof String
assertEquals "#!/bin/bash\nfunction hello() {\n echo 'Hello, world.'\n}\n#!/bin/bash\nhello\n", result
}

File resourceFile(String name) {
new File(getClass().getResource(name).getPath())
}
}
2 changes: 2 additions & 0 deletions src/test/resources/com/trigonic/gradle/plugins/rpm/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
hello
4 changes: 4 additions & 0 deletions src/test/resources/com/trigonic/gradle/plugins/rpm/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
function hello() {
echo 'Hello, world.'
}

0 comments on commit cafe704

Please sign in to comment.