diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5897e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.swp +vendor +.gradle +.idea +*.iml +build diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d8640bc --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2018, Vít Kotačka +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..51f8b62 --- /dev/null +++ b/build.gradle @@ -0,0 +1,37 @@ +plugins { + id 'groovy' + id 'com.gradle.plugin-publish' version '0.9.10' +} + +repositories { + mavenCentral() +} + +dependencies { + compile gradleApi() + compile localGroovy() +} + +version = '0.1.0' +group = 'cz.swsamuraj' + +jar { + from(projectDir) { + include 'LICENSE' + into 'META-INF' + } +} + +pluginBundle { + website = 'https://github.com/sw-samuraj/gradle-godep-plugin' + vcsUrl = 'https://github.com/sw-samuraj/gradle-godep-plugin' + description = 'Simple Gradle wrapper for go and dep commands.' + tags = ['go', 'golang', 'dep'] + + plugins { + godepPlugin { + id = 'cz.swsamuraj.godep' + displayName = 'Gradle go & dep wrapper' + } + } +} diff --git a/example/build.gradle b/example/build.gradle new file mode 100644 index 0000000..8c13d61 --- /dev/null +++ b/example/build.gradle @@ -0,0 +1,7 @@ +plugins { + id 'cz.swsamuraj.godep' version '0.1.0' +} + +godep { + importPath = 'github.com/sw-samuraj/hello' +} diff --git a/example/hello.go b/example/hello.go new file mode 100644 index 0000000..b2ed207 --- /dev/null +++ b/example/hello.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "github.com/gorilla/mux" + "net/http" +) + +func main() { + r := mux.NewRouter() + http.Handle("/", r) + fmt.Println("Hello, world!") +} diff --git a/src/main/groovy/cz/swsamuraj/gradle/godep/CleanTask.groovy b/src/main/groovy/cz/swsamuraj/gradle/godep/CleanTask.groovy new file mode 100644 index 0000000..10d70d6 --- /dev/null +++ b/src/main/groovy/cz/swsamuraj/gradle/godep/CleanTask.groovy @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018, Vít Kotačka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package cz.swsamuraj.gradle.godep + +import groovy.transform.CompileStatic +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.TaskAction + +@CompileStatic +class CleanTask extends DefaultTask { + + CleanTask() { + group = 'go & dep' + description = 'Deletes the build directory.' + } + + @TaskAction + void clean() { + project.delete project.buildDir + + logger.info("[godep] ${project.buildDir.name} directory has been deleted") + } +} diff --git a/src/main/groovy/cz/swsamuraj/gradle/godep/GoBuildTask.groovy b/src/main/groovy/cz/swsamuraj/gradle/godep/GoBuildTask.groovy new file mode 100644 index 0000000..e8ddcc9 --- /dev/null +++ b/src/main/groovy/cz/swsamuraj/gradle/godep/GoBuildTask.groovy @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2018, Vít Kotačka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package cz.swsamuraj.gradle.godep + +import groovy.transform.CompileStatic +import org.gradle.api.Action +import org.gradle.api.DefaultTask +import org.gradle.api.provider.Property +import org.gradle.api.tasks.TaskAction +import org.gradle.process.ExecSpec + +@CompileStatic +class GoBuildTask extends DefaultTask { + + final Property importPath = project.objects.property(String) + + GoBuildTask() { + group = 'go & dep' + description = 'Builds the Go project.' + dependsOn "test" + } + + @TaskAction + void goBuild() { + File outDir = new File(project.buildDir, 'out') + + if (!outDir.exists()) { + outDir.mkdirs() + } + + int lastSeparator = importPath.get().lastIndexOf(File.separator) + String packageShortName = importPath.get().substring(lastSeparator + 1) + File packageDir = new File(project.buildDir, "go/src/${importPath.get()}") + + logger.info("[godep] go build -o ${outDir}/${packageShortName}") + + project.exec(new Action() { + @Override + void execute(ExecSpec execSpec) { + execSpec.environment('GOPATH', "${project.buildDir}/go") + execSpec.commandLine('/bin/sh', '-c', "cd ${packageDir} && go build -o ${outDir}/${packageShortName}") + } + }) + } +} diff --git a/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepExtension.groovy b/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepExtension.groovy new file mode 100644 index 0000000..f0e6e59 --- /dev/null +++ b/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepExtension.groovy @@ -0,0 +1,16 @@ +package cz.swsamuraj.gradle.godep + +import groovy.transform.CompileStatic +import org.gradle.api.Project +import org.gradle.api.provider.Property + +@CompileStatic +class GoDepExtension { + + final Property importPath + + GoDepExtension(Project project) { + importPath = project.objects.property(String) + importPath.set('github.com/user/package') + } +} diff --git a/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepPlugin.groovy b/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepPlugin.groovy new file mode 100644 index 0000000..5aa5310 --- /dev/null +++ b/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepPlugin.groovy @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018, Vít Kotačka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package cz.swsamuraj.gradle.godep + +import groovy.transform.CompileStatic +import org.gradle.api.Plugin +import org.gradle.api.Project + +@CompileStatic +class GoDepPlugin implements Plugin { + + @Override + void apply(Project project) { + GoDepExtension extension = project.extensions.create('godep', GoDepExtension, project) + + project.tasks.create('clean', CleanTask) + project.tasks.create('prepareWorkspace', PrepareWorkspaceTask) { + it.importPath = extension.importPath + } + project.tasks.create('dep', GoDepTask) { + it.importPath = extension.importPath + } + project.tasks.create('test', GoTestTask) + project.tasks.create('build', GoBuildTask) { + it.importPath = extension.importPath + } + } +} diff --git a/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepTask.groovy b/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepTask.groovy new file mode 100644 index 0000000..1632c7b --- /dev/null +++ b/src/main/groovy/cz/swsamuraj/gradle/godep/GoDepTask.groovy @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2018, Vít Kotačka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package cz.swsamuraj.gradle.godep + +import groovy.transform.CompileStatic +import org.gradle.api.Action +import org.gradle.api.DefaultTask +import org.gradle.api.provider.Property +import org.gradle.api.tasks.TaskAction +import org.gradle.process.ExecSpec + +@CompileStatic +class GoDepTask extends DefaultTask { + + final Property importPath = project.objects.property(String) + + GoDepTask() { + group = 'go & dep' + description = 'Builds the Go project.' + dependsOn "prepareWorkspace" + } + + @TaskAction + void goDep() { + File gopkgToml = new File(project.projectDir, "Gopkg.toml") + String depCommand + + if (!gopkgToml.exists()) { + depCommand = 'init' + } else { + depCommand = 'ensure' + } + + File packageDir = new File(project.buildDir, "go/src/${importPath.get()}") + + logger.info("[godep] dep ${depCommand}") + + project.exec(new Action() { + @Override + void execute(ExecSpec execSpec) { + execSpec.environment('GOPATH', "${project.buildDir}/go") + execSpec.commandLine('/bin/sh', '-c', "cd ${packageDir} && dep ${depCommand}") + } + }) + } +} diff --git a/src/main/groovy/cz/swsamuraj/gradle/godep/GoTestTask.groovy b/src/main/groovy/cz/swsamuraj/gradle/godep/GoTestTask.groovy new file mode 100644 index 0000000..cbfeefb --- /dev/null +++ b/src/main/groovy/cz/swsamuraj/gradle/godep/GoTestTask.groovy @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018, Vít Kotačka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package cz.swsamuraj.gradle.godep + +import groovy.transform.CompileStatic +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.TaskAction + +@CompileStatic +class GoTestTask extends DefaultTask { + + GoTestTask() { + group = 'go & dep' + description = 'Runs all the tests.' + dependsOn "dep" + } + + @TaskAction + void test() { + logger.lifecycle("Test taks TBD") + } +} diff --git a/src/main/groovy/cz/swsamuraj/gradle/godep/PrepareWorkspaceTask.groovy b/src/main/groovy/cz/swsamuraj/gradle/godep/PrepareWorkspaceTask.groovy new file mode 100644 index 0000000..edecaf4 --- /dev/null +++ b/src/main/groovy/cz/swsamuraj/gradle/godep/PrepareWorkspaceTask.groovy @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2018, Vít Kotačka + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package cz.swsamuraj.gradle.godep + +import groovy.transform.CompileStatic +import org.gradle.api.DefaultTask +import org.gradle.api.provider.Property +import org.gradle.api.tasks.StopExecutionException +import org.gradle.api.tasks.TaskAction + +import java.nio.file.Files + +@CompileStatic +class PrepareWorkspaceTask extends DefaultTask { + + final Property importPath = project.objects.property(String) + + PrepareWorkspaceTask() { + group = 'go & dep' + description = 'Checks for go and dep binaries and sets GOPATH to the project workspace.' + } + + @TaskAction + void prepareWorkspace() { + doChecks() + prepareBuildDir() + } + + void doChecks() { + checkGoBinary() + checkDepBinary() + } + + void checkGoBinary() { + // TODO Guido: check for 'go' binary + boolean isGoBinary = false + + if (isGoBinary) { + logger.info('[godep] go binary not found on PATH') + + throw new StopExecutionException() + } + + logger.info("[godep] go binary found: ${isGoBinary}") + } + + void checkDepBinary() { + // TODO Guido: check for 'dep' binary + } + + void prepareBuildDir() { + int lastSeparator = importPath.get().lastIndexOf(File.separator) + String symlinkName = importPath.get().substring(lastSeparator + 1) + String pathToSymlink= importPath.get().substring(0, lastSeparator) + File packageDir = new File(project.buildDir, "go/src/${pathToSymlink}") + + if (!packageDir.exists()) { + packageDir.mkdirs() + Files.createSymbolicLink(new File(packageDir, symlinkName).toPath(), project.projectDir.toPath()) + + logger.info("[godep] go package directory has been created: ${packageDir}/${symlinkName}") + } + } +} diff --git a/src/main/resources/META-INF/gradle-plugins/cz.swsamuraj.godep.properties b/src/main/resources/META-INF/gradle-plugins/cz.swsamuraj.godep.properties new file mode 100644 index 0000000..1781451 --- /dev/null +++ b/src/main/resources/META-INF/gradle-plugins/cz.swsamuraj.godep.properties @@ -0,0 +1 @@ +implementation-class=cz.swsamuraj.gradle.godep.GoDepPlugin \ No newline at end of file