github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/misc/androidstudio/src/main/groovy/org/golang/mobile/GobindPlugin.groovy (about) 1 /* 2 * Copyright 2015 The Go Authors. All rights reserved. 3 * Use of this source code is governed by a BSD-style 4 * license that can be found in the LICENSE file. 5 */ 6 7 package org.golang.mobile 8 9 import org.gradle.api.DefaultTask 10 import org.gradle.api.GradleException 11 import org.gradle.api.Project 12 import org.gradle.api.Plugin 13 import org.gradle.api.Task 14 import org.gradle.api.tasks.OutputFile 15 import org.gradle.api.tasks.TaskAction 16 17 import org.golang.mobile.OutputFileTask 18 import org.golang.mobile.AARPublishArtifact 19 20 /* 21 * GobindPlugin configures the default project that builds .AAR file 22 * from a go package, using gomobile bind command. 23 * For gomobile bind command, see https://github.com/c-darwin/mobile/cmd/gomobile 24 */ 25 class GobindPlugin implements Plugin<Project> { 26 void apply(Project project) { 27 project.configurations.create("default") 28 project.extensions.create('gobind', GobindExtension) 29 30 Task gobindTask = project.tasks.create("gobind", GobindTask) 31 gobindTask.outputFile = project.file(project.name+".aar") 32 project.artifacts.add("default", new AARPublishArtifact( 33 'mylib', 34 null, 35 gobindTask)) 36 37 Task cleanTask = project.tasks.create("clean", { 38 project.delete(project.name+".aar") 39 }) 40 } 41 } 42 43 class GobindTask extends DefaultTask implements OutputFileTask { 44 @OutputFile 45 File outputFile 46 47 @TaskAction 48 def gobind() { 49 def pkg = project.gobind.pkg 50 def gopath = (project.gobind.GOPATH ?: System.getenv("GOPATH"))?.trim() 51 if (!pkg || !gopath) { 52 throw new GradleException('gobind.pkg and gobind.GOPATH must be set') 53 } 54 55 def paths = (gopath.split(File.pathSeparator).collect{ "$it/bin" } + 56 System.getenv("PATH").split(File.pathSeparator)).flatten() 57 // Default installation path of go distribution. 58 if (isWindows()) { 59 paths = paths + "c:\\Go\\bin" 60 } else { 61 paths = paths + "/usr/local/go/bin" 62 } 63 64 def gomobile = (project.gobind.GOMOBILE ?: findExecutable("gomobile", paths))?.trim() 65 def gobin = (project.gobind.GO ?: findExecutable("go", paths))?.trim() 66 def gomobileFlags = project.gobind.GOMOBILEFLAGS?.trim() 67 68 if (!gomobile || !gobin) { 69 throw new GradleException('failed to find gomobile/go tools. Set gobind.GOMOBILE and gobind.GO') 70 } 71 72 paths = [findDir(gomobile), findDir(gobin), paths].flatten() 73 74 Properties properties = new Properties() 75 properties.load(project.rootProject.file('local.properties').newDataInputStream()) 76 def androidHome = properties.getProperty('sdk.dir') 77 if (!androidHome?.trim()) { 78 // fallback to ANDROID_HOME 79 androidHome = System.getenv("ANDROID_HOME") 80 } 81 82 project.exec { 83 executable(gomobile) 84 85 def cmd = ["bind", "-target=android", "-i", "-o", project.name+".aar"] 86 if (gomobileFlags) { 87 cmd = (cmd+gomobileFlags.split(" ")).flatten() 88 } 89 cmd << pkg 90 91 args(cmd) 92 if (!androidHome?.trim()) { 93 throw new GradleException('Neither sdk.dir or ANDROID_HOME is set') 94 } 95 environment("GOPATH", gopath) 96 environment("PATH", paths.join(File.pathSeparator)) 97 environment("ANDROID_HOME", androidHome) 98 } 99 } 100 101 def isWindows() { 102 return System.getProperty("os.name").startsWith("Windows") 103 } 104 105 def findExecutable(String name, ArrayList<String> paths) { 106 if (isWindows() && !name.endsWith(".exe")) { 107 name = name + ".exe" 108 } 109 for (p in paths) { 110 def f = new File(p + File.separator + name) 111 if (f.exists()) { 112 return p + File.separator + name 113 } 114 } 115 throw new GradleException('binary ' + name + ' is not found in $PATH (' + paths + ')') 116 } 117 118 def findDir(String binpath) { 119 if (!binpath) { 120 return "" 121 } 122 123 def f = new File(binpath) 124 return f.getParentFile().getAbsolutePath(); 125 } 126 } 127 128 class GobindExtension { 129 // Package to bind. (required) 130 def String pkg = "" 131 132 // GOPATH: necessary for gomobile tool. (required) 133 def String GOPATH = System.getenv("GOPATH") 134 135 // GO: path to go tool. (can omit if 'go' is in the paths visible by Android Studio) 136 def String GO = "" 137 138 // GOMOBILE: path to gomobile binary. (can omit if 'gomobile' is under GOPATH) 139 def String GOMOBILE = "" 140 141 // GOMOBILEFLAGS: extra flags to be passed to gomobile command. (optional) 142 def String GOMOBILEFLAGS = "" 143 }