You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/03/22 15:05:41 UTC

[GitHub] mrutkows commented on a change in pull request #812: Change the gradle build to use the gogradle plugin

mrutkows commented on a change in pull request #812: Change the gradle build to use the gogradle plugin
URL: https://github.com/apache/incubator-openwhisk-wskdeploy/pull/812#discussion_r176454657
 
 

 ##########
 File path: build.gradle
 ##########
 @@ -43,129 +93,217 @@ rat {
     ]
 }
 
-project.ext {
-    basePackageName = "openwhisk-wskdeploy"
-    packageExtension = "tar.gz"
-    if (project.hasProperty('projectVersion')) {
-        packageVersion = "${projectVersion}"
-    } else {
-        packageVersion = ""
-    }
-    buildFolder = "build"
-}
+/*
+    The OpenWhiskPlatform class is a utility class to make the rest of what
+    happens with platforms a bit more understandable.  A "Platform" is a tuple
+    of an operating system and a processor.  Currently, the OpenWhisk Wskdeploy
+    supports three OS's:  Linux, Mac/Darwin, and Windows.  It supports x86
+    (32-bit or 64-bit) on all OS's.  On Linux, it also support System Z (s390x),
+    PowerPC (ppc64le), and ARM (32-bit and 64-bit) architectures.
+
+    Different contexts use different codings to refer to these architectures --
+    the class attempts to provide and interpret all needed codings.  Internal
+    storage is in "GO" format:
 
-task taredSources(type: Tar) {
-    baseName basePackageName
-    description "Creates a combined tar.gz file of wskdeploy's sources"
-    group "Release artifact"
-    classifier "sources"
-
-    from(project.rootDir) {
-        include('cmd/*.go', 'deployers/*.go', 'parsers/*.go', 'utils/*.go',
-                'wskderrors/*.go', 'wskenv/*.go', 'wskprint/*.go', 'wski18n/**')
-        include('*.go')
-        include('gradle/**')
-        include('README.md', 'CONTRIBUTING.md', 'DEPENDENCIES.md')
-        include('gradlew', 'gradlew.bat', 'Dockerfile', 'build.gradle')
-        include('LICENSE.txt', 'NOTICE.txt', 'CHANGELOG.txt')
+        OS: linux, darwin, windows
+        Arch: 386, amd64, s390x, ppc64le, arm
+
+    TODO - It may be appropriate to refactor into a general purpose Platform
+           class for all builds, then to extend with specifics needed for
+           the OpenWhisk Wskdeploy build.
+ */
+class OpenWhiskPlatform {
+    String goOs
+    String goArch
+
+    /*
+        The 'zipFileName' property is the root file name to use for archives.
+     */
+    static String zipFileName
+
+    /*
+        Create a platform for the local platform
+     */
+    OpenWhiskPlatform() {
+        this(System.properties['os.name'], System.properties['os.arch'])
     }
-    destinationDir file(buildFolder)
-    extension packageExtension
-    version packageVersion
-    compression = Compression.GZIP
-}
 
-task cleanBuild(type: Delete) {
-    def folder = new File(buildFolder)
-    if(folder.exists()) {
-        delete file(buildFolder).listFiles()
+    OpenWhiskPlatform(String platformSpec) {
+        this(*platformSpec.split('-'))
     }
-}
 
-task removeBinary(type: Delete) {
-    delete "${projectDir}/bin/wskdeploy"
-    delete "${projectDir}/bin/mac"
-    delete "${projectDir}/bin/linux"
-    delete "${projectDir}/bin/windows"
-}
+    OpenWhiskPlatform(String inOs, String inArch) {
+        goOs=inOs.toLowerCase()
+                .replaceAll(~/^mac.*$/,'darwin')
+                .replaceAll(~/^.*n[ui]x.*$/,'linux')
+        goArch=inArch.toLowerCase()
+                .replaceAll('x86_64','amd64')
+                .replaceAll('i386','386')
+                .replaceAll('x86_32','386')
+    }
 
-task distBinary(dependsOn: [removeBinary, distDocker]) {
-    doLast {
-        run(dockerBinary + ["rm", "-f", dockerContainerName], true)
-        run(dockerBinary + ["run", "--name", dockerContainerName, dockerTaggedImageName])
+    /**
+     * Return the Openwhisk OS for this Platform
+     */
+    String getOwOs() {
+        ((goOs == 'darwin') ? 'mac' : goOs)
+    }
 
-        // Copy all Go binaries from Docker into openwhisk/bin folder
-        run(dockerBinary + ["cp", dockerContainerName +
-                ":/src/github.com/apache/incubator-openwhisk-wskdeploy/build/.", "${projectDir}/bin"])
+    String getGoPlatform() {
+        "${goOs}-${goArch}"
+    }
 
-        run(dockerBinary + ["rm", "-f", dockerContainerName])
+    /*
+        Everything below here is specific to the Wskdeploy build and could be
+        factored out into a subclass.
+     */
+    String getArchiveDirName() {
+        "${this.owOs}/${goArch}"
     }
-}
 
-task dumpOSInfo {
-    doLast {
-        println "os.name = "+getOsName()
-        println "os.arch = "+getOsArch()
-        println "go.name = "+mapOsNameToGoName(getOsName())
-        println "go.arch = "+mapOsArchToGoArch(getOsArch())
+    String getArchiveFileName() {
+        String suffix
+        switch (goArch) {
+            case "386": suffix = '-32bit'; break;
+            case "amd64": suffix = ''; break;
+            default: suffix = "-${goArch}"; break;
+        }
+        String archivetype = (goOs == 'linux') ? 'tgz' : 'zip'
+        "${zipFileName}-${this.owOs}${suffix}.${archivetype}"
     }
 }
 
-task copyWSKDEPLOYShortcut(type: Copy, dependsOn: [distBinary, dumpOSInfo]) {
-    String go_osname     = mapOsNameToGoName(getOsName())
-    String go_osarch     = mapOsArchToGoArch(getOsArch())
-    String from_path_wsk = "${projectDir}/bin/${go_osname}/${go_osarch}/wskdeploy"
-    String to_path_dir   = "${projectDir}/bin"
+/*
+    Configuration of OpenWhisk Platform behavior based on environment and defaults
+ */
+OpenWhiskPlatform.zipFileName =
+        System.env['zip_file_name'] ?:
+                (rootProject.findProperty('zipFileName') ?: 'openwhisk_wskdeploy')
 
-    from from_path_wsk
-    into to_path_dir
-}
+project.ext.packageVersion =
+        rootProject.findProperty('packageVersion') ?: 'latest'
+
+String buildFileName = System.env['build_file_name'] ?:
+        (rootProject.findProperty('buildFileName') ?: 'wskdeploy')
 
-pushImage.finalizedBy copyWSKDEPLOYShortcut
+/*
+    'platforms' property will be null for a local compile, or a list (comma or
+    space-separated) of hyphenated Goos-Goarch pairs.  Some transformation is
+    done when parsing to handle misconceptions.
 
-// Returns the Go CLI docker build args
-def getDockerBuildArgs() {
-    String local_os = mapOsNameToGoName(getOsName())
-    String local_arch = mapOsArchToGoArch(getOsArch())
-    def res = []
+    TODO:  More syntax/validity checking and feedback, perhaps as part of a
+    Platform object as proposed above...
+*/
+rootProject.ext.localPlatform = new OpenWhiskPlatform()
 
-    if(!project.hasProperty('crossCompileWSKDEPLOY') || project.crossCompileWSKDEPLOY == "false") {
-        res = ["WSKDEPLOY_OS=${local_os}", "WSKDEPLOY_ARCH=${local_arch}"]
+if (rootProject.hasProperty('buildPlatforms')) {
+    rootProject.ext.platforms = buildPlatforms.tokenize(' ,').collect {
+        new OpenWhiskPlatform(it)
+    }
+} else {
+    if (!rootProject.hasProperty('nativeCompile')) {
+        rootProject.ext.platforms = [
+                'linux-386', 'linux-amd64',
+                'linux-s390x', 'linux-ppc64le', 'linux-arm', 'linux-arm64',
+                'darwin-386', 'darwin-amd64',
+                'windows-386', 'windows-amd64'
+        ].collect { new OpenWhiskPlatform(it) }
     } else {
-        res = ["WSKDEPLOY_OS=mac linux windows", "WSKDEPLOY_ARCH=386 amd64"]
+        rootProject.ext.platforms = [ rootProject.localPlatform ]
     }
+}
 
-    return res
+/*
+    Checks -- add golint and scancode to the checks run prior to build.
+       The get step is needed to be sure a golint binary is available to run.
+ */
+task getGoLint(type: com.github.blindpirate.gogradle.Go) {
+    go 'get -u github.com/golang/lint/golint'
 }
 
-def run(cmd, ignoreError = false) {
-    println("Executing '${cmd.join(" ")}'")
-    def proc = cmd.execute()
-    proc.waitFor()
-    if(!ignoreError && proc.exitValue() != 0) {
-        println("Command '${cmd.join(" ")}' failed with exitCode ${proc.exitValue()}")
-    }
+task goLint(type: com.github.blindpirate.gogradle.Go, dependsOn: getGoLint) {
+    // WARNING:  The single quotes are intentional!  The gogradle plugin will
+    //           parse the command with the GString engine at execution time.
+    run '${GOPATH}/bin/golint ' + golang.packagePath
 }
 
-def getOsName() {
-    return System.properties['os.name']
+goCheck.dependsOn(goLint)
+
+goBuild {
+    targetPlatform = rootProject.platforms*.goPlatform
+
+    // WARNING:  The single quotes are intentional!  The gogradle plugin will
+    //           parse the command with the GString engine at execution time.
+    go(['build',
+        '-ldflags', "-X main.Version=${packageVersion}" as String,
+        '-o', './build/${GOOS}-${GOARCH}/'+buildFileName+'${GOEXE}',
+        golang.packagePath ] as List<String>)
 }
 
-def getOsArch() {
-    return System.properties['os.arch']
+gofmt {
+    gofmt "-s -w ."
 }
 
-def mapOsNameToGoName(String osname) {
-    String osname_l = osname.toLowerCase()
-    if (osname_l.contains("nux") || osname.contains("nix")) return "linux"
-    if (osname_l.contains("mac")) return "mac"
-    if (osname_l.contains("windows")) return "windows"
-    return osname_l
+task compile(type: Copy, dependsOn: goBuild) {
+    destinationDir = file('./build')
+    from("./build/${rootProject.localPlatform.goOs}-${rootProject.localPlatform.goArch}")
+}
+
+task build(type: DefaultTask, dependsOn: compile)
+
+/*
+    For each platform, create an individual archive in a platform appropriate
+    format (tarball for Linux, zipfile for Mac & Windows).
+ */
+task individualArchives(
+        dependsOn: rootProject.platforms.collect() { p ->
+            task("release${p.goOs.capitalize()}${p.goArch.capitalize()}",
+                    type: (p.goOs == 'linux') ? Tar : Zip, dependsOn: compile) {
+                if (p.goOs == 'linux') { compression = Compression.GZIP }
+                destinationDir = file('./release')
+                baseName = "${p.zipFileName}-${packageVersion}-${p.owOs}-${p.goArch}"
+                from "./build/${p.goOs}-${p.goArch}/"
+                include "${buildFileName}*"
+            }
+        })
+
+/*
+    Create a 'content.json' file representing all that was
+    compiled and its appropriate directory in the Tarball that will be created
+    for deployment to local Nginx instances.
+ */
+
+task index() {
+    def content = [:]
+    for (p in platforms) {
+        def pathObject = [ "path" : "${p.archiveDirName}/${p.archiveFileName}" ]
+        content.get(p.owOs,[:])[p.goArch] = pathObject
+        // TODO: Default architecture should be configurable as a property
+        if (p.goArch == 'amd64') {
+            content.get(p.owOs,[:])['default'] = pathObject
+        }
+    }
+
+    doLast {
+        mkdir('./build')
+        file('./build/content.json').text = groovy.json.JsonOutput.toJson(["wskdeploy": content])
+    }
+}
+
+task releaseBinaries(type: Tar, dependsOn: [individualArchives, index]) {
 
 Review comment:
   Have we run a tagged release PR to test?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services