mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
221 lines
7.5 KiB
Groovy
Executable File
221 lines
7.5 KiB
Groovy
Executable File
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.20'
|
|
|
|
project.ext {
|
|
compileSdk = 31
|
|
buildTools = "31.0.0"
|
|
minSdk = 26
|
|
targetSdk = 28
|
|
versionCode = 202
|
|
versionName = "2.0.2-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.3'
|
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
classpath '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())
|
|
}
|
|
}
|
|
} |