forked from AndroidIDEOfficial/AndroidIDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
executable file
·224 lines (193 loc) · 7.57 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import com.google.googlejavaformat.java.Formatter
import com.google.googlejavaformat.java.JavaFormatterOptions
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Collectors
buildscript {
ext.kotlin_version = '1.6.10'
project.ext {
compileSdk = 31
buildTools = "31.0.0"
minSdk = 26
targetSdk = 28
versionCode = 201
versionName = "2.0.1-beta"
packageName = "com.itsaky.androidide"
javaSourceVersion = JavaVersion.VERSION_11
javaTargetVersion = JavaVersion.VERSION_11
}
repositories {
google()
mavenLocal()
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.googlejavaformat:google-java-format:1.15.0'
}
}
//dependencies {
// implementation 'com.google.googlejavaformat:google-java-format:1.15.0'
//}
subprojects {
apply from: "${rootDir}/gradle/dependencies.gradle"
project.afterEvaluate {
tasks.register("checkTranslations", task -> {
def php = new File("/usr/bin/php")
if (!php.exists()) {
project.logger.lifecycle("'${php.absolutePath}' not found. Skipping translation check.")
return
}
def resDir = project.file("src/main/res")
def strings = new File(resDir, "values/strings.xml")
def reportDir = new File(project.rootProject.buildDir, "translation-reports")
reportDir.delete()
if (resDir.exists() && strings.exists()) {
def translationDirs = resDir.listFiles((FileFilter) (file -> {
return file.isDirectory() && file.getName().startsWith("values-")
}))
for (def dir : translationDirs) {
final var translation = new File(dir, "strings.xml")
if (translation.exists()) {
def out = new File(reportDir, "${project.path.replace(':', '/')}/${dir.name}.txt")
if (!out.parentFile.exists()) {
out.parentFile.mkdirs()
}
if (out.exists()) {
out.delete()
}
out.createNewFile()
def result = exec {
ignoreExitValue true
standardOutput new FileOutputStream(out)
commandLine "${php.absolutePath}",
"${project.rootProject.file(".tools/strings-check.php")}",
"${strings.absolutePath}",
"${translation.absolutePath}"
}
if (result.getExitValue() == 0) {
out.delete()
} else {
project.logger.lifecycle("Translation report for '${project.path}/${dir.name}' is written to '${out.absolutePath}'")
}
} else {
project.logger.info("No translation file specifed for '${dir.name}'. Skipping..")
}
}
} else {
project.logger.info("Default strings.xml file does not exist for project '${project.name}'")
}
})
}
}
allprojects {
repositories {
google()
mavenLocal()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Collection<String> walkForSources(Path dir) {
try {
if (!Files.exists(dir)) {
return Collections.emptySet()
}
return Files.walk(dir)
.filter(path -> !Files.isDirectory(path))
.filter(path -> Files.isReadable(path) && Files.isWritable(path))
.map(path -> path.toFile())
.map(file -> file.absolutePath)
.filter(path -> path.endsWith(".java"))
.filter(path -> !path.endsWith("_template.java"))
.collect(Collectors.toSet())
} catch (Throwable error) {
getLogger().error("Could not walk directory for java sources: ${dir.toString()}", error)
return Collections.emptySet()
}
}
boolean doJavaFormat(Formatter formatter, String path) {
try {
final def file = Paths.get(path)
final def content = Files.readAllBytes(file)
final def utf8Decoded = new String(content, StandardCharsets.UTF_8.name())
final def formatted = formatter.formatSource(utf8Decoded)
if (utf8Decoded == formatted) {
getLogger().info("{}: UP-TO-DATE", file)
return true
}
byte[] utf8Encoded = formatted.getBytes(StandardCharsets.UTF_8.name())
Files.write(file, utf8Encoded)
println("${path}: Formatted successfully!")
return true
} catch (Throwable th) {
getLogger().error("Failed to format ${path}", th)
return false
}
}
task formatJavaSources() {
doLast {
def jarName = "google-java-format-1.15.0-all-deps"
def jar = rootProject.file(".tools/${jarName}.jar")
if (!jar.exists()) {
getLogger().info("google-java-format JAR file not found. Skipping format task...")
return
}
boolean formattingChanges = false
rootProject.subprojects.forEach(sub -> {
def sources = walkForSources(sub.file("src/main/java").toPath())
sources.addAll(walkForSources(sub.file("src/test/java").toPath()))
final def formatter = new Formatter(
JavaFormatterOptions
.builder()
.style(JavaFormatterOptions.Style.AOSP)
.formatJavadoc(true)
.build()
)
for (def path : sources) {
if (doJavaFormat(formatter, path)) {
formattingChanges = true
}
}
})
if (!formattingChanges) {
getLogger().info("No formatting changes.")
return
}
// ---------------- Stage Changes -------------------
def out = new ByteArrayOutputStream()
def exit = rootProject.exec {
ignoreExitValue true
standardOutput out
errorOutput standardOutput
commandLine "git", "add", "."
}
if (exit.exitValue != 0) {
getLogger().error("Unable to stage changes. Process terminated with exit code ${exit.exitValue}")
getLogger().error(out.toString())
return
}
// ----------------- Commit Changes --------------------
out = new ByteArrayOutputStream()
exit = rootProject.exec {
ignoreExitValue true
standardOutput out
errorOutput standardOutput
commandLine "git", "commit", "-m", "[Gradle] Format java source code"
}
if (exit.exitValue == 0) {
getLogger().info("Changes committed successfully.")
} else {
getLogger().error("Failed to commit changes. Process terminated with exit code ${exit.exitValue}")
getLogger().error(out.toString())
}
}
}