Skip to content

Commit 4c5b73c

Browse files
cleanup
1 parent 44c37c0 commit 4c5b73c

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

app/src/main/java/org/godotengine/godot_gradle_build_environment/BuildEnvironment.kt

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,50 @@ class BuildEnvironment(
165165
return exitCode
166166
}
167167

168+
private fun setupProject(projectPath: String, outputHandler: (Int, String) -> Unit): File {
169+
val projectTreeUri: Uri
170+
val hash = Integer.toHexString(projectPath.hashCode())
171+
val workDir = File(projectRoot, hash)
172+
if (workDir.exists()) {
173+
val info = ProjectInfo.readFromDirectory(workDir)
174+
if (info != null) {
175+
projectTreeUri = info.projectTreeUri.toUri()
176+
} else {
177+
throw Exception("Could not get projectTreeUri.")
178+
}
179+
} else {
180+
outputHandler(OUTPUT_STDERR, "Project path \"$projectPath\" is not accessible. Please give GABE app access to this directory.")
181+
Utils.showDirectoryAccessNotification(context, projectPath)
182+
// 2 minute wait should be ideal?
183+
val uri = waitForDirectoryAccess(2 * 60 * 1000)
184+
?: throw Exception("Directory access not granted in time. Build canceled.")
185+
projectTreeUri = uri
186+
outputHandler(OUTPUT_STDOUT, "Access granted for $projectPath. Starting Gradle build...")
187+
}
188+
189+
outputHandler(OUTPUT_INFO, "> Importing project files...")
190+
FileUtils.importAndroidProject(context, projectTreeUri, workDir)
191+
return workDir
192+
}
193+
194+
private fun fixGradleArgs(projectPath: String, rawGradleArgs: List<String>): List<String> {
195+
return rawGradleArgs.map { arg ->
196+
when {
197+
arg.startsWith("-Pdebug_keystore_file=") -> "-Pdebug_keystore_file=/project/.android/debug.keystore"
198+
arg.startsWith("-Prelease_keystore_file=") -> "-Prelease_keystore_file=/project/.android/release.keystore"
199+
arg.startsWith("-Paddons_directory=") -> "-Paddons_directory=/project/addons"
200+
201+
arg.startsWith("-Pplugins_local_binaries=") -> {
202+
val prefix = "-Pplugins_local_binaries="
203+
val value = arg.removePrefix(prefix)
204+
val updated = value.replace("$projectPath/addons", "/project/addons")
205+
prefix + updated
206+
}
207+
else -> arg
208+
}
209+
}
210+
}
211+
168212
fun cleanProject(projectPath: String) {
169213
val normalizeProjectPath = projectPath.trimEnd('/')
170214
val hash = Integer.toHexString(normalizeProjectPath.hashCode())
@@ -338,33 +382,9 @@ class BuildEnvironment(
338382
return 255
339383
}
340384

341-
val projectTreeUri: Uri
342385
val projectPath = rawProjectPath.trimEnd('/')
343-
val hash = Integer.toHexString(projectPath.hashCode())
344-
val workDir = File(projectRoot, hash)
345-
if (workDir.exists()) {
346-
val info = ProjectInfo.readFromDirectory(workDir)
347-
if (info != null) {
348-
projectTreeUri = info.projectTreeUri.toUri()
349-
} else {
350-
outputHandler(OUTPUT_STDERR, "Unable to setup project: could not get projectTreeUri.")
351-
return 255
352-
}
353-
} else {
354-
outputHandler(OUTPUT_STDERR, "Unable to setup project: $projectPath is not accessible. Please give GABE app access to this directory.")
355-
Utils.showDirectoryAccessNotification(context, projectPath)
356-
val uri = waitForDirectoryAccess(2 * 60 * 1000) // 2 minutes should be ideal?
357-
if (uri == null) {
358-
outputHandler(OUTPUT_STDERR, "Directory access not granted in time. Build canceled.")
359-
return 255
360-
}
361-
projectTreeUri = uri
362-
outputHandler(OUTPUT_STDOUT, "Access granted for $projectPath. Starting Gradle build...")
363-
}
364-
365-
try {
366-
outputHandler(OUTPUT_INFO, "> Importing project files...")
367-
FileUtils.importAndroidProject(context, projectTreeUri, workDir)
386+
val workDir = try {
387+
setupProject(projectPath, outputHandler)
368388
} catch (e: Exception) {
369389
outputHandler(OUTPUT_STDERR, "Unable to setup project: ${e.message}")
370390
return 255
@@ -380,25 +400,7 @@ class BuildEnvironment(
380400
outputHandler(type, line)
381401
}
382402

383-
val gradleArgs = rawGradleArgs.map { arg ->
384-
when {
385-
arg.startsWith("-Pdebug_keystore_file=") -> "-Pdebug_keystore_file=/project/.android/debug.keystore"
386-
arg.startsWith("-Prelease_keystore_file=") -> "-Prelease_keystore_file=/project/.android/release.keystore"
387-
arg.startsWith("-Paddons_directory=") -> "-Paddons_directory=/project/addons"
388-
389-
arg.startsWith("-Pplugins_local_binaries=") -> {
390-
val prefix = "-Pplugins_local_binaries="
391-
val value = arg.removePrefix(prefix)
392-
val normalizeProjectPath = projectPath.trimEnd('/')
393-
val updated = value.replace(
394-
"$normalizeProjectPath/addons",
395-
"/project/addons"
396-
)
397-
prefix + updated
398-
}
399-
else -> arg
400-
}
401-
}
403+
val gradleArgs = fixGradleArgs(projectPath, rawGradleArgs)
402404

403405
var result = executeGradleInternal(gradleArgs, workDir, captureOutputHandler)
404406

0 commit comments

Comments
 (0)