1
+
2
+ //
3
+ // Injects Swift relevant tasks into gradle build process
4
+ // Mostly these are performed by scripts in ~/.gradle/scripts
5
+ // installed when the plugin is installed by "create_scripts.sh"
6
+ //
7
+
1
8
package net.zhuoweizhang.swiftandroid
2
9
3
10
import org.gradle.api.*
@@ -6,9 +13,8 @@ import org.gradle.api.tasks.*
6
13
public class SwiftAndroidPlugin implements Plugin<Project > {
7
14
@Override
8
15
public void apply (Project project ) {
9
- Task writeNdkConfigTask= createWriteNdkConfigTask(project," writeNdkConfigSwift" )
10
- Task compileSwiftTask= createCompileSwiftTask(project," compileSwift" )
11
- compileSwiftTask. dependsOn(" writeNdkConfigSwift" )
16
+ Task generateSwiftTask= createGenerateSwiftTask(project," generateSwift" )
17
+ Task compileSwiftTask= createCompileSwiftTask(project," compileSwift" )
12
18
Task copySwiftStdlibTask= createCopyStdlibTask(project," copySwiftStdlib" )
13
19
Task copySwiftTask= createCopyTask(project," copySwift" )
14
20
copySwiftTask. dependsOn(" compileSwift" ," copySwiftStdlib" )
@@ -20,47 +26,41 @@ public class SwiftAndroidPlugin implements Plugin<Project> {
20
26
project. afterEvaluate {
21
27
// according to Protobuf gradle plugin, the Android variants are only available here
22
28
// TODO: read those variants; we only support debug right now
23
- Task compileNdkTask= project. tasks. getByName(" compileDebugNdk" )
24
- compileNdkTask. dependsOn(" copySwift" )
29
+ Task compileNdkTask= project. tasks. getByName(" compileDebugNdk" )
30
+ compileNdkTask. dependsOn(" copySwift" )
31
+ Task preBuildTask= project. tasks. getByName(" preBuild" )
32
+ preBuildTask. dependsOn(" generateSwift" )
25
33
Task cleanTask= project. tasks. getByName(" clean" )
26
34
cleanTask. dependsOn(" cleanSwift" )
27
35
}
28
36
}
29
- public static File getNdkRoot () {
30
- return new File (System . getenv(" ANDROID_NDK_HOME" ))
31
- }
32
-
33
- public static File getSwiftRoot () {
34
- return new File (" which swift" . execute(). text).
35
- parentFile. parentFile
36
- }
37
37
public static String getScriptRoot () {
38
38
return System . getenv(" HOME" )+ " /.gradle/scripts/"
39
39
}
40
40
41
- public static Task createCopyStdlibTask (Project project ,String name ) {
42
- return project. task(type :Exec , name) {
43
- commandLine(System . getenv(" HOME" )+ " /.gradle/scripts/copy-libraries.sh" ,
44
- " src/main/jniLibs/armeabi-v7a" )
45
- }
46
- }
41
+ public static Task createGenerateSwiftTask (Project project ,String name ) {
42
+ return project. task(type :Exec , name) {
43
+ commandLine(getScriptRoot()+ " generate-swift.sh" )
44
+ workingDir(" src/main/swift" )
45
+ }
46
+ }
47
+ public static Task createCompileSwiftTask (Project project ,String name ) {
48
+ return project. task(type :Exec , name) {
49
+ commandLine(getScriptRoot()+ " swift-build.sh" )
50
+ workingDir(" src/main/swift" )
51
+ }
52
+ }
53
+ public static Task createCopyStdlibTask (Project project ,String name ) {
54
+ return project. task(type :Exec , name) {
55
+ commandLine(getScriptRoot()+ " copy-libraries.sh" ,
56
+ " src/main/jniLibs/armeabi-v7a" )
57
+ }
58
+ }
47
59
public static Task createCopyTask (Project project ,String name ) {
48
60
return project. task(type :Copy , name) {
49
61
from(" src/main/swift/.build/debug" )
50
62
include(" *.so" )
51
63
into(" src/main/jniLibs/armeabi-v7a" )
52
64
}
53
65
}
54
- public static Task createWriteNdkConfigTask (Project project ,String name ) {
55
- return project. task(type :Exec , name) {
56
- commandLine(" bash" ," -c" ,' echo\" export ANDROID_NDK_HOME=\\\" $ANDROID_NDK_HOME\\\"\" >~/.swift-android-ndk-home' )
57
- }
58
- }
59
- public static Task createCompileSwiftTask (Project project ,String name ) {
60
- return project. task(type :Exec , name) {
61
- commandLine(" bash" ," -c" ," SWIFT_EXEC=\" " +
62
- System . getenv(" HOME" )+ " /.gradle/scripts/swiftc-android.sh\" swift build" )
63
- workingDir(" src/main/swift" )
64
- }
65
- }
66
66
}