|
1 | 1 | package com.appswithlove.updraft |
2 | 2 |
|
3 | | -import groovy.json.JsonSlurperClassic |
4 | 3 | import org.gradle.api.GradleException |
5 | 4 | import org.gradle.api.Plugin |
6 | 5 | import org.gradle.api.Project |
7 | | -import org.gradle.process.ExecOperations |
8 | | -import javax.inject.Inject |
9 | 6 |
|
10 | 7 | /** |
11 | 8 | * A plugin for uploading apk files to updraft.*/ |
12 | 9 | class UpdraftPlugin implements Plugin<Project> { |
13 | 10 |
|
14 | | - private final ExecOperations execOps |
15 | | - |
16 | | - @Inject |
17 | | - UpdraftPlugin(ExecOperations execOperations) { |
18 | | - this.execOps = execOperations |
19 | | - } |
20 | | - |
21 | 11 | @Override |
22 | 12 | void apply(Project project) { |
23 | 13 | project.extensions.create('updraft', UpdraftExtension.class) |
24 | 14 | project.android.applicationVariants.all { variant -> |
25 | 15 |
|
26 | | - def plugin = this |
27 | 16 | String variantName = variant.name |
28 | 17 | String variantNameCapitalized = variantName.capitalize() |
29 | | - String basePath = getProject().getProjectDir().getAbsolutePath() |
30 | | - String filename = variant.outputs[0].outputFile |
| 18 | + String projectBasePath = getProject().getProjectDir().getAbsolutePath() |
| 19 | + |
| 20 | + def uploadUrlsProvider = project.providers.provider { project.updraft.urls } |
| 21 | + def uploadUrls = uploadUrlsProvider.map { urlsMap -> |
| 22 | + def urlsForVariant = urlsMap[variantNameCapitalized] |
| 23 | + if (urlsForVariant == null) { |
| 24 | + return [] |
| 25 | + } |
| 26 | + if (urlsForVariant instanceof String) { |
| 27 | + return [urlsForVariant] |
| 28 | + } |
| 29 | + return urlsForVariant |
| 30 | + } |
31 | 31 |
|
32 | | - def urls = project.updraft.urls[variantNameCapitalized] |
33 | 32 | def gitBranchProvider = project.providers.of(GitBranchValueSource.class) {} |
34 | 33 | def gitTagsProvider = project.providers.of(GitTagsValueSource.class) {} |
35 | 34 | def gitCommitProvider = project.providers.of(GitCommitValueSource.class) {} |
36 | 35 | def gitUrlProvider = project.providers.of(GitUrlValueSource.class) {} |
37 | | - String releaseNotes = getReleaseNotes(project, variant) |
38 | | - String whatsNew = createCurlParam(releaseNotes, "whats_new") |
| 36 | + def releaseNotesProvider = project.providers.provider { getReleaseNotes(project, variant) } |
39 | 37 |
|
40 | | - // Registers a task for every available build variant / flavor |
41 | | - project.tasks.register("updraft${variantNameCapitalized}") { |
42 | | - // Declares that the project runs after the build, not before. |
43 | | - // If not stated, it will run at every gradle sync. |
44 | | - |
45 | | - String gitBranch = createCurlParam(gitBranchProvider.get(), "custom_branch") |
46 | | - String gitTags = createCurlParam(gitTagsProvider.get(), "custom_tags") |
47 | | - String gitCommit = createCurlParam(gitCommitProvider.get(), "custom_commit") |
48 | | - String gitUrl = createCurlParam(gitUrlProvider.get(), "custom_URL") |
49 | | - |
50 | | - doLast { |
51 | | - // APK |
52 | | - def apkFile = new File(filename) |
53 | | - String fileWithoutExt = apkFile.name.take(apkFile.name.lastIndexOf('.')) |
54 | | - |
55 | | - if (apkFile != null && !apkFile.exists()) { |
56 | | - def apkFolder = new File(apkFile.getParent()) |
57 | | - for (File currentFile in apkFolder.listFiles()) { |
58 | | - if (currentFile.getName().startsWith(fileWithoutExt)) { |
59 | | - apkFile = currentFile |
60 | | - } |
61 | | - } |
62 | | - } |
| 38 | + def apkFileProvider = project.provider { |
| 39 | + def buildDir = project.layout.buildDirectory.get().asFile |
| 40 | + def apkDir = new File(buildDir, "outputs/apk/${variant.flavorName}/${variant.buildType.name}") |
63 | 41 |
|
64 | | - if (apkFile != null && !apkFile.exists()) { |
65 | | - throw new GradleException("Could not find a build artifact. (Make sure to run assemble${variantNameCapitalized} before)") |
| 42 | + if (apkDir.exists() && apkDir.isDirectory()) { |
| 43 | + def apkFiles = apkDir.listFiles({ file -> file.name.endsWith(".apk") } as FileFilter) |
| 44 | + |
| 45 | + if (apkFiles && apkFiles.size() > 1) { |
| 46 | + throw new GradleException("More than one .apk file exists in ${apkDir}: ${apkFiles*.name}") |
66 | 47 | } |
67 | 48 |
|
68 | | - plugin.upload(apkFile, urls, gitBranch, gitTags, gitCommit, gitUrl, whatsNew) |
| 49 | + return apkFiles ? apkFiles[0] : null |
69 | 50 | } |
| 51 | + return null |
| 52 | + } |
| 53 | + def apkFile = apkFileProvider.flatMap { file -> |
| 54 | + file ? project.layout.file(project.provider { file }) : project.objects.fileProperty() |
70 | 55 | } |
71 | 56 |
|
72 | | - // Registers a task for every available build variant / flavor |
73 | | - project.tasks.register("updraftBundle${variantNameCapitalized}") { |
74 | | - // Declares that the project runs after the build, not before. |
75 | | - // If not stated, it will run at every gradle sync. |
76 | | - |
77 | | - String gitBranch = createCurlParam(gitBranchProvider.get(), "custom_branch") |
78 | | - String gitTags = createCurlParam(gitTagsProvider.get(), "custom_tags") |
79 | | - String gitCommit = createCurlParam(gitCommitProvider.get(), "custom_commit") |
80 | | - String gitUrl = createCurlParam(gitUrlProvider.get(), "custom_URL") |
81 | | - |
82 | | - doLast { |
83 | | - def apkFile = new File(filename) |
84 | | - String fileWithoutExt = apkFile.name.take(apkFile.name.lastIndexOf('.')) |
85 | | - |
86 | | - // AAB |
87 | | - def bundlePath = |
88 | | - "${basePath}/build/outputs/bundle/${variantName}/${fileWithoutExt}.aab" |
89 | | - |
90 | | - def aabFile = new File(bundlePath) |
91 | | - if (aabFile != null && !aabFile.exists()) { |
92 | | - def aabFolder = new File(aabFile.getParent()) |
93 | | - for (File currentFile in aabFolder.listFiles()) { |
94 | | - println("file $currentFile.path") |
95 | | - if (currentFile.getName().startsWith(fileWithoutExt)) { |
96 | | - aabFile = currentFile |
97 | | - } |
98 | | - } |
99 | | - } |
| 57 | + def aabFileProvider = project.provider { |
| 58 | + def buildDir = project.layout.buildDirectory.get().asFile |
| 59 | + def bundleDir = new File(buildDir, "outputs/bundle/${variantName}") |
100 | 60 |
|
101 | | - if (aabFile != null && !aabFile.exists()) { |
102 | | - throw new GradleException("Could not find a build artifact. (Make sure to run bundle${variantNameCapitalized} before). \n We tried following location ${bundlePath}") |
| 61 | + if (bundleDir.exists() && bundleDir.isDirectory()) { |
| 62 | + def aabFiles = bundleDir.listFiles({ file -> file.name.endsWith(".aab") } as FileFilter) |
| 63 | + |
| 64 | + if (aabFiles && aabFiles.size() > 1) { |
| 65 | + throw new GradleException("More than one .aab file exists in ${bundleDir}: ${aabFiles*.name}") |
103 | 66 | } |
104 | 67 |
|
105 | | - plugin.upload(aabFile, urls, gitBranch, gitTags, gitCommit, gitUrl, whatsNew) |
| 68 | + return aabFiles ? aabFiles[0] : null |
106 | 69 | } |
| 70 | + return null |
| 71 | + } |
| 72 | + def aabFile = aabFileProvider.flatMap { file -> |
| 73 | + file ? project.layout.file(project.provider { file }) : project.objects.fileProperty() |
107 | 74 | } |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - private void upload(file, urls, gitBranch, gitTags, gitCommit, gitUrl, whatsNew) { |
112 | | - if (urls == null || urls.isEmpty()) { |
113 | | - throw new GradleException( |
114 | | - 'There was no url provided for this buildVariant. Please check for typos.' |
115 | | - ) |
116 | | - } |
117 | | - |
118 | | - if (urls instanceof String) { |
119 | | - println("--------------------------------------") |
120 | | - println("Url was not wrapped in array. Doing it for you. :)") |
121 | | - println("url --> [url]") |
122 | | - println("--------------------------------------") |
123 | | - urls = [urls] |
124 | | - } |
125 | 75 |
|
126 | | - for (String url in urls) { |
127 | | - //Build and execute of the curl command for Updraft upload |
128 | | - List<String> curlArgs = [ |
129 | | - '-X', 'PUT', |
130 | | - '-F', "app=@${file}", |
131 | | - '-F', "build_type=Gradle", |
132 | | - gitBranch, |
133 | | - gitUrl, |
134 | | - gitTags, |
135 | | - gitCommit, |
136 | | - whatsNew, |
137 | | - url |
138 | | - ].findAll { it != '' } // Filter out empty strings |
139 | | - |
140 | | - new ByteArrayOutputStream().withStream { os -> |
141 | | - execOps.exec { |
142 | | - executable 'curl' |
143 | | - args curlArgs |
144 | | - standardOutput os |
| 76 | + // Registers a task for every available build variant / flavor |
| 77 | + project.tasks.register("updraft${variantNameCapitalized}", UpdraftTask) { |
| 78 | + outputFile.set(apkFile) |
| 79 | + isBundle.set(false) |
| 80 | + basePath.set(projectBasePath) |
| 81 | + currentVariantName.set(variantName) |
| 82 | + urls.set(uploadUrls) |
| 83 | + gitBranch.set(gitBranchProvider) |
| 84 | + gitTags.set(gitTagsProvider) |
| 85 | + gitCommit.set(gitCommitProvider) |
| 86 | + gitUrl.set(gitUrlProvider) |
| 87 | + releaseNotes.set(releaseNotesProvider) |
| 88 | + |
| 89 | + doFirst { |
| 90 | + def outputFile = apkFile.getOrNull()?.asFile |
| 91 | + if (outputFile == null || !outputFile.exists()) { |
| 92 | + throw new GradleException("Could not find a build artifact. (Make sure to run assemble${variantNameCapitalized} task first)") |
| 93 | + } |
145 | 94 | } |
| 95 | + } |
146 | 96 |
|
147 | | - def execResponse = new JsonSlurperClassic().parseText(os.toString()) |
148 | | - |
149 | | - if (execResponse instanceof HashMap && execResponse.size() > 0) { |
150 | | - if (execResponse.containsKey("success") && execResponse["success"] == "ok") { |
151 | | - def publicUrl = execResponse["public_link"] |
152 | | - ok(publicUrl) |
153 | | - } else if (execResponse.containsKey("detail") && execResponse["detail"] == "Not found.") { |
154 | | - throw new GradleException('Could not updraft to the given url. Please recheck that.') |
155 | | - } else { |
156 | | - throw new GradleException(os.toString()) |
| 97 | + // Registers a task for every available build variant / flavor |
| 98 | + project.tasks.register("updraftBundle${variantNameCapitalized}", UpdraftTask) { |
| 99 | + outputFile.set(aabFile) |
| 100 | + isBundle.set(true) |
| 101 | + basePath.set(projectBasePath) |
| 102 | + currentVariantName.set(variantName) |
| 103 | + urls.set(uploadUrls) |
| 104 | + gitBranch.set(gitBranchProvider) |
| 105 | + gitTags.set(gitTagsProvider) |
| 106 | + gitCommit.set(gitCommitProvider) |
| 107 | + gitUrl.set(gitUrlProvider) |
| 108 | + releaseNotes.set(releaseNotesProvider) |
| 109 | + |
| 110 | + doFirst { |
| 111 | + def outputFile = aabFile.getOrNull()?.asFile |
| 112 | + if (outputFile == null || !outputFile.exists()) { |
| 113 | + throw new GradleException("Could not find a build artifact. (Make sure to run bundle${variantNameCapitalized} task first)") |
157 | 114 | } |
158 | | - } else { |
159 | | - println(execResponse) |
160 | | - ok(null) |
161 | 115 | } |
162 | 116 | } |
163 | 117 | } |
@@ -198,22 +152,4 @@ class UpdraftPlugin implements Plugin<Project> { |
198 | 152 | } |
199 | 153 | } |
200 | 154 | } |
201 | | - |
202 | | - private static void ok(String publicUrl) { |
203 | | - println() |
204 | | - println("--------------------------------------") |
205 | | - println("Your App was successfully updrafted!") |
206 | | - if (publicUrl != null) { |
207 | | - println("Get it here -> $publicUrl") |
208 | | - } |
209 | | - println("--------------------------------------") |
210 | | - } |
211 | | - |
212 | | - private static String createCurlParam(String text, String name) { |
213 | | - if (text.isBlank() || text.isEmpty()) { |
214 | | - "" |
215 | | - } else { |
216 | | - "-F ${name}=${text} " |
217 | | - } |
218 | | - } |
219 | 155 | } |
0 commit comments