2
0
mirror of synced 2025-02-21 14:08:14 +00:00

misc/androidstudio: source code for gomobile bind gradle plugin.

Currently version 0.2.1

I messed up previous versions and plugins.gradle.org doesn't let me
remove the bad versions.

Change-Id: I9eef512633b461ff5a7fcbe11a3e104efb250d61
Reviewed-on: https://go-review.googlesource.com/12538
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Hyang-Ah (Hana) Kim 2015-07-22 16:56:39 -04:00 committed by Hyang-Ah Hana Kim
parent 103c0611a8
commit 8c8ee830af
10 changed files with 322 additions and 0 deletions

2
misc/androidstudio/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.gradle
/build

View File

@ -0,0 +1,28 @@
gobindPlugin invokes gomobile bind command on the specified package.
# Usage
build.gradle:
<pre>
plugins {
id "org.golang.mobile.bind" version "0.2.1"
}
gobind {
// package to bind
pkg "github.com/someone/somepackage"
// GOPATH
GOPATH "/home/gopher"
// PATH to directories with "go" and "gomobile" tools.
PATH "path1:path2:"
}
</pre>
For details:
https://plugins.gradle.org/plugin/org.golang.mobile.bind
# TODO
* Find the stale aar file (how?)

View File

@ -0,0 +1,39 @@
apply plugin: 'groovy'
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.gradle.publish:plugin-publish-plugin:0.9.1"
}
}
apply plugin: "com.gradle.plugin-publish"
repositories {
jcenter()
}
dependencies {
compile gradleApi()
compile localGroovy()
testCompile 'junit:junit:4.11'
}
pluginBundle {
website = 'https://golang.org'
vcsUrl = 'https://github.com/golang/mobile'
description = 'Plugin for gomobile projects (beta)'
version = '0.2.1'
plugins {
gobindPlugin {
id = 'org.golang.mobile.bind'
displayName = 'gomobile bind plugin'
tags = ['golang', 'gomobile', 'gobind']
}
}
}

View File

@ -0,0 +1 @@
rootProject.name = 'gobindPlugin'

View File

@ -0,0 +1,89 @@
/**
* Copyright 2015 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package org.golang.mobile;
import org.golang.mobile.OutputFileTask;
import org.gradle.api.Task;
import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.tasks.TaskDependency;
import java.io.File;
import java.util.Collections;
import java.util.Date;
import java.util.Set;
/**
* custom implementation of PublishArtifact for published AAR
*/
public class AARPublishArtifact implements PublishArtifact {
private final String name;
private final String classifier;
private final OutputFileTask task;
private final TaskDependency taskDependency;
private static final class DefaultTaskDependency implements TaskDependency {
private final Set<Task> tasks;
DefaultTaskDependency(Task task) {
this.tasks = Collections.singleton(task);
}
@Override
public Set<? extends Task> getDependencies(Task task) {
return tasks;
}
}
public AARPublishArtifact(
String name,
String classifier,
OutputFileTask task) {
this.name = name;
this.classifier = classifier;
this.task = task;
this.taskDependency = new DefaultTaskDependency((Task) task);
}
@Override
public String getName() {
return name;
}
@Override
public String getExtension() {
return "aar";
}
@Override
public String getType() {
return "aar";
}
@Override
public String getClassifier() {
return classifier;
}
@Override
public File getFile() {
return task.getOutputFile();
}
@Override
public Date getDate() {
return null;
}
@Override
public TaskDependency getBuildDependencies() {
return taskDependency;
}
}

View File

@ -0,0 +1,103 @@
/*
* Copyright 2015 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package org.golang.mobile
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.Task
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.golang.mobile.OutputFileTask
import org.golang.mobile.AARPublishArtifact
/*
* GobindPlugin configures the default project that builds .AAR file
* from a go package, using gomobile bind command.
* For gomobile bind command, see https://golang.org/x/mobile/cmd/gomobile
*/
class GobindPlugin implements Plugin<Project> {
void apply(Project project) {
project.configurations.create("default")
project.extensions.create('gobind', GobindExtension)
Task gobindTask = project.tasks.create("gobind", GobindTask)
gobindTask.outputFile = project.file(project.name+".aar")
project.artifacts.add("default", new AARPublishArtifact(
'mylib',
null,
gobindTask))
Task cleanTask = project.tasks.create("clean", {
project.delete(project.name+".aar")
})
}
}
class GobindTask extends DefaultTask implements OutputFileTask {
@OutputFile
File outputFile
@TaskAction
def gobind() {
def pkg = project.gobind.pkg.trim()
def gopath = project.gobind.GOPATH.trim()
def paths = project.gobind.PATH.trim() + File.pathSeparator + System.getenv("PATH")
if (!pkg || !gopath) {
throw new GradleException('gobind.pkg and gobind.GOPATH must be set')
}
def gomobile = findGomobile()
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def androidHome = properties.getProperty('sdk.dir')
if (!androidHome?.trim()) {
// fallback to ANDROID_HOME
androidHome = System.getenv("ANDROID_HOME")
}
project.exec {
executable(gomobile)
args("bind", "-target=android", "-i", "-o", project.name+".aar", pkg)
if (!androidHome?.trim()) {
throw new GradleException('Neither sdk.dir or ANDROID_HOME is set')
}
environment("GOPATH", gopath)
environment("PATH", paths)
environment("ANDROID_HOME", androidHome)
}
}
def findGomobile() {
def gomobile = "gomobile"
if (System.getProperty("os.name").startsWith("Windows")) {
gomobile = "gomobile.exe"
}
def paths = project.gobind.PATH + File.pathSeparator + System.getenv("PATH")
for (p in paths.split(File.pathSeparator)) {
def f = new File(p + File.separator + gomobile)
if (f.exists()) {
return p + File.separator + gomobile
}
}
throw new GradleException('failed to find gomobile command from ' + paths)
}
}
class GobindExtension {
// Package to bind.
def String pkg = ""
// GOPATH: necessary for gomobile tool.
def String GOPATH = System.getenv("GOPATH")
// PATH: must include path to 'gomobile' and 'go' binary.
def String PATH = ""
}

View File

@ -0,0 +1,16 @@
/**
* Copyright 2015 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package org.golang.mobile;
import java.io.File;
/**
* A task that outputs a file.
*/
public interface OutputFileTask {
File getOutputFile();
}

View File

@ -0,0 +1 @@
implementation-class=org.golang.mobile.GobindPlugin

View File

@ -0,0 +1,22 @@
/**
* Copyright 2015 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package org.golang.mobile
import org.junit.Test
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.api.Project
import static org.junit.Assert.*
class GobindPluginTest {
@Test
public void gobindPluginAddsGobindTaskToProject() {
Project project = ProjectBuilder.builder().build()
project.apply plugin: 'org.golang.mobile.bind'
assertTrue(project.tasks.gobind instanceof GobindTask)
}
}

View File

@ -0,0 +1,21 @@
/**
* Copyright 2015 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package org.golang.mobile
import org.junit.Test
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.api.Project
import static org.junit.Assert.*
class GobindTaskTest {
@Test
public void canAddTaskToProject() {
Project project = ProjectBuilder.builder().build()
def task = project.task('gobind', type: GobindTask)
assertTrue(task instanceof GobindTask)
}
}