Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial internal support for wasmJS target #345

Merged
merged 24 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions OrganiseStrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

TODO_COMMENT = "<!--TODO-->"

ASSETS_DIR = "shared/src/commonMain/resources/assets/"
BASE_STRINGS_XML_PATH = "shared/src/commonMain/resources/assets/values/strings.xml"
ASSETS_DIR = "shared/src/commonMain/composeResources/"
BASE_STRINGS_XML_PATH = "shared/src/commonMain/composeResources/values/strings.xml"

UNTRANSLATABLE_MATCH = "translatable=\"false\""
STRING_START = " <string name=\""
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ The app is currently being developed for both English and Japanese. Contribution
- [KizzyRPC](https://github.com/dead8309/KizzyRPC): Discord status functionality
- [ComposeReorderable](https://github.com/aclassen/ComposeReorderable): Reorderable lists like the song queue
- [compose-color-picker](https://github.com/godaddy/compose-color-picker): Theme editor colour wheel
- [Catppuccin](https://github.com/catppuccin/java): Themes included as options in-app
- [Catppuccin](https://github.com/d1snin/catppuccin-kotlin): Themes included as options in-app

## Disclaimer
This project and its contents are not affiliated with, funded, authorized, endorsed by, or in any way associated with YouTube, Google LLC or any of its affiliates and subsidiaries.
Expand Down
69 changes: 69 additions & 0 deletions ReplaceStrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
import re

STRING_REGEX = r"getString\(\"([^\"]+)\"\)"
STRING_REGEX_REPLACEMENT = r"stringResource(Res.string.\1)"

def formatStringKey(key: str) -> str:
if "$" in key:
return f"`{key}`"

return key

def getImportsForReplacements(replacements: list[re.Match]) -> list[str]:
imports = [
"org.jetbrains.compose.resources.stringResource",
"spmp.shared.generated.resources.Res"
]

for replacement in replacements:
call = replacement.string[replacement.start() : replacement.end()]
string_key = call[11:-2]

imports.append(f"spmp.shared.generated.resources.{formatStringKey(string_key)}")

return [f"import {imp}" for imp in imports]

def processKotlinFile(path: str):
f = open(path, "r")
content = f.read()
f.close()

replacements = list(re.finditer(STRING_REGEX, content))
if len(replacements) == 0:
return

content = re.sub(STRING_REGEX, STRING_REGEX_REPLACEMENT, content)

imports_start_line = None
package_line = None

lines = content.split("\n")

for index, line in enumerate(lines):
line = line.strip()
if line.startswith("package "):
package_line = index
elif line.startswith("import "):
imports_start_line = index

if imports_start_line is None:
imports_start_line = package_line + 1
else:
imports_start_line += 1

for index, import_line in enumerate(getImportsForReplacements(replacements)):
lines.insert(imports_start_line + index, import_line)

f = open(path, "w")
f.write("\n".join(lines))
f.close()

def main():
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".kt"):
processKotlinFile(os.path.join(root, file))

if __name__ == "__main__":
main()
5 changes: 3 additions & 2 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import plugin.spmp.getDeps

plugins {
kotlin("multiplatform")
kotlin("plugin.compose")
id("com.android.application")
id("org.jetbrains.compose")
}

val strings_file: File = rootProject.file("shared/src/commonMain/resources/assets/values/strings.xml")
val strings_file: File = rootProject.file("shared/src/commonMain/composeResources/values/strings.xml")
var keystore_props_file: File = rootProject.file("androidApp/keystore.properties")
if (!keystore_props_file.isFile) {
keystore_props_file = rootProject.file("androidApp/keystore.properties.debug")
Expand Down Expand Up @@ -56,7 +57,7 @@ kotlin {
val androidMain by getting {
dependencies {
implementation(project(":shared"))
implementation(deps.get("dev.toastbits.composekit:library-android"))
implementation(deps.get("dev.toastbits.composekit:library"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dev.toastbits.composekit.platform.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.runBlocking

class MainActivity: ComponentActivity() {
private val coroutine_scope = CoroutineScope(Job())
Expand All @@ -40,7 +41,7 @@ class MainActivity: ComponentActivity() {
if (
error is java.nio.channels.UnresolvedAddressException // Thrown by Kizzy
) {
SpMp.Log.warning("Skipping error: ${error.stackTraceToString()}")
println("WARNING: Skipping error: ${error.stackTraceToString()}")
return@setDefaultUncaughtExceptionHandler
}

Expand All @@ -62,7 +63,7 @@ class MainActivity: ComponentActivity() {
}

val shortcut_state: ShortcutState = ShortcutState()
val context: AppContext = AppContext(this, coroutine_scope, ApplicationContext(this))
val context: AppContext = runBlocking { AppContext.create(this@MainActivity, coroutine_scope, ApplicationContext(this@MainActivity)) }
SpMp.init(context)

WindowCompat.setDecorFitsSystemWindows(window, false)
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
// this is necessary to avoid the plugins to be loaded multiple times
// in each subproject's classloader
kotlin("multiplatform") apply false
kotlin("plugin.compose") apply false
id("com.android.application") apply false
id("com.android.library") apply false
id("org.jetbrains.compose") apply false
Expand Down
1 change: 0 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies {
implementation("com.github.kobjects:kxml2:2.4.1")
}


tasks.withType(JavaCompile::class) {
options.release.set(21)
}
73 changes: 29 additions & 44 deletions buildSrc/src/main/kotlin/plugins/generate-build-config.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.github.gmazzo.buildconfig.BuildConfigClassSpec
import plugin.shared.Command
import plugin.spmp.ProjectConfigValues
import java.util.Properties
Expand All @@ -7,23 +8,45 @@ plugins {
id("com.github.gmazzo.buildconfig")
}

val release_source_sets: List<String> =
listOf(
"androidRelease",
"desktopMain",
"wasmJsMain"
)

val debug_source_sets: List<String> =
listOf(
"androidDebug"
)

buildConfig {
className = "ProjectBuildConfig"
packageName = "com.toasterofbread.${project.parent!!.name}"

useKotlinOutput {
internalVisibility = false
}

// TODO
buildConfig(debug_mode = false)

// sourceSets.all {
// for ((source_set, debug) in release_source_sets.map { it to false } + debug_source_sets.map { it to true }) {
// if (name == source_set) {
// }
// }
// }
}

fun loadKeys(
private fun BuildConfigClassSpec.loadKeys(
file: File,
getType: (key: String) -> String,
key_names: Collection<String>,
include_values: Boolean = true
) {
fun addBuildConfigField(key: String, value: String?) {
buildConfig.buildConfigField(key) {
buildConfigField(key) {
type(getType(key))
expression(value ?: null.toString())
}
Expand Down Expand Up @@ -64,7 +87,7 @@ fun loadKeys(
}
}

fun Task.buildConfig(debug_mode: Boolean) {
private fun BuildConfigClassSpec.buildConfig(debug_mode: Boolean) {
loadKeys(
rootProject.file("buildSrc/project.properties"),
{ key ->
Expand All @@ -83,45 +106,7 @@ fun Task.buildConfig(debug_mode: Boolean) {
include_values = debug_mode
)

buildConfig {
val tag_override: String? = project.properties["GIT_TAG_OVERRIDE"] as String?
val git_tag: String? = (tag_override ?: Command.getCurrentGitTag())?.ifBlank { null }
buildConfigField("GIT_TAG", git_tag)
buildConfigField("GIT_COMMIT_HASH", if (git_tag != null) null else Command.getCurrentGitCommitHash())
buildConfigField("IS_DEBUG", debug_mode)
}
}

val buildConfigDebug = tasks.register("buildConfigDebug") {
outputs.upToDateWhen { false }
doFirst {
buildConfig(debug_mode = true)
}
}
val buildConfigRelease = tasks.register("buildConfigRelease") {
outputs.upToDateWhen { false }
doFirst {
buildConfig(debug_mode = false)
}
}
val buildConfigDesktop = tasks.register("buildConfigDesktop") {
outputs.upToDateWhen { false }
doFirst {
var debug: Boolean = gradle.taskGraph.getAllTasks().none { task ->
task.name.startsWith("packageRelease") || task.name == "runRelease"
}

println("buildConfigDesktop will be configured with debug_mode=$debug")
buildConfig(debug_mode = debug)
}
}

afterEvaluate {
tasks.all {
when (name) {
"compileDebugKotlinAndroid" -> dependsOn(buildConfigDebug)
"compileReleaseKotlinAndroid" -> dependsOn(buildConfigRelease)
"compileKotlinDesktop" -> dependsOn(buildConfigDesktop)
}
}
buildConfigField("GIT_COMMIT_HASH", Command.getCurrentGitCommitHash())
buildConfigField("GIT_TAG", Command.getCurrentGitTag())
buildConfigField("IS_DEBUG", debug_mode)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import plugin.spmp.SpMpDeps
import java.io.File
import plugin.spmp.getDeps

Expand All @@ -9,7 +10,7 @@ plugins {
}

fun Task.generateDependencyList() {
val output_directory: File = project.layout.buildDirectory.dir("generated/sources/buildConfig/main/com/toasterofbread/spmp").get().getAsFile()
val output_directory: File = project.layout.buildDirectory.dir(SpMpDeps.OUTPUT_DIR).get().asFile
output_directory.mkdirs()

val dependency_info_file_content: String =
Expand Down Expand Up @@ -53,12 +54,6 @@ val generateDependencyList by tasks.registering {
}
}

afterEvaluate {
tasks.all {
when (name) {
"compileDebugKotlinAndroid" -> dependsOn(generateDependencyList)
"compileReleaseKotlinAndroid" -> dependsOn(generateDependencyList)
"compileKotlinDesktop" -> dependsOn(generateDependencyList)
}
}
tasks.generateBuildConfig {
dependsOn(generateDependencyList)
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/plugins/shared/DesktopUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object DesktopUtils {
)

const val LOCAL_PROPERTIES_PATH: String = "local.properties"
val Project.strings_file: File get() = rootProject.file("shared/src/commonMain/resources/assets/values/strings.xml")
val Project.strings_file: File get() = rootProject.file("shared/src/commonMain/composeResources/values/strings.xml")

fun Project.getString(key: String): String {
val reader: InputStreamReader = strings_file.reader()
Expand Down
Loading
Loading