File size: 8,042 Bytes
b450976 | 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 | import {execSync} from "child_process"
import path from "path"
import {
AndroidConfig,
type ConfigPlugin,
withAndroidManifest,
withGradleProperties,
withProjectBuildGradle,
withSettingsGradle,
} from "expo/config-plugins"
import {type BluetoothSdkPluginProps} from "./index"
import {resolveAnalyticsProps} from "./analyticsProps"
import {withKotlinVersion} from "./withKotlinVersion"
const META_ANALYTICS_DISABLED = "com.mentra.bluetoothsdk.analytics.disabled"
const META_ANALYTICS_ENVIRONMENT = "com.mentra.bluetoothsdk.analytics.environment"
const STALE_META_POSTHOG_API_KEY = "com.mentra.bluetoothsdk.analytics.posthog_api_key"
const STALE_META_POSTHOG_HOST = "com.mentra.bluetoothsdk.analytics.posthog_host"
const ANALYTICS_META_NAMES = [
META_ANALYTICS_DISABLED,
META_ANALYTICS_ENVIRONMENT,
STALE_META_POSTHOG_API_KEY,
STALE_META_POSTHOG_HOST,
]
function getBluetoothSdkRoot(): string {
return path.dirname(require.resolve("../../package.json"))
}
function toGroovyString(value: string): string {
return JSON.stringify(value)
}
/**
* Modify settings.gradle to include the lc3Lib module
*/
function withSettingsGradleModifications(config: any) {
return withSettingsGradle(config, (config) => {
let settingsGradle = config.modResults.contents
const bluetoothSdkRoot = getBluetoothSdkRoot()
const bluetoothSdkRootExpression = `System.getenv("MENTRA_BLUETOOTH_SDK_PACKAGE_PATH") ?: ${toGroovyString(
bluetoothSdkRoot,
)}`
if (!settingsGradle.includes("project(':mentra-bluetooth-sdk').projectDir")) {
const bluetoothSdkProjectBlock = `
if (findProject(':mentra-bluetooth-sdk') != null) {
project(':mentra-bluetooth-sdk').projectDir = new File(${bluetoothSdkRootExpression}, 'android')
}
`
const lc3Include = " include ':lc3Lib'"
settingsGradle = settingsGradle.includes(lc3Include)
? settingsGradle.replace(lc3Include, `${bluetoothSdkProjectBlock}\n${lc3Include}`)
: `${settingsGradle}${bluetoothSdkProjectBlock}`
}
if (!settingsGradle.includes("include ':lc3Lib'")) {
settingsGradle += `
include ':lc3Lib'
`
}
if (!settingsGradle.includes("project(':lc3Lib').projectDir")) {
settingsGradle += `
project(':lc3Lib').projectDir = new File(${bluetoothSdkRootExpression}, 'android/lc3Lib')
`
}
// Earlier versions of this plugin added a `:silero` module, which no longer exists. Prebuild
// runs without --clean, so remove those lines from an already generated settings.gradle.
settingsGradle = settingsGradle
.split("\n")
.filter((line) => !line.includes("include ':silero'") && !line.includes("project(':silero')"))
.join("\n")
config.modResults.contents = settingsGradle
return config
})
}
/**
* Modify gradle.properties to add node path for native builds launched outside a shell.
*/
function withGradlePropertiesModifications(config: any) {
return withGradleProperties(config, (config) => {
let props = config.modResults
// Get node path and add to org.gradle.jvmargs
try {
const nodeExecutable = execSync("which node", {encoding: "utf-8"}).trim()
// Get parent directory of bin (e.g., /path/to/node/bin/node -> /path/to/node)
const nodePath = path.dirname(nodeExecutable)
// Find existing org.gradle.jvmargs property
const jvmArgsIndex = props.findIndex((p) => p.type === "property" && p.key === "org.gradle.jvmargs")
if (jvmArgsIndex !== -1) {
// Append nodePath to existing jvmargs if not already present
const jvmArgsProp = props[jvmArgsIndex]
if (jvmArgsProp.type === "property" && "value" in jvmArgsProp) {
const currentValue = jvmArgsProp.value
if (!currentValue.includes("-Dorg.gradle.project.nodePath=")) {
jvmArgsProp.value = `${currentValue} -Dorg.gradle.project.nodePath=${nodePath}`
}
}
} else {
// Create new jvmargs property with nodePath
props.push({
type: "property",
key: "org.gradle.jvmargs",
value: `-Dorg.gradle.project.nodePath=${nodePath}`,
})
}
} catch (error) {
console.warn("Failed to get node path:", error)
}
config.modResults = props
return config
})
}
/**
* Inject a local Maven repository into the root project's allprojects block so
* that `:app` (and any other module that transitively pulls our deps) can
* resolve `com.k2fsa.sherpa.onnx:sherpa-onnx`. The AAR is downloaded at
* configure-time by bluetooth-sdk's own build.gradle into
* `android/libs/maven/`; we just need that directory exposed as a maven repo
* to consumers as well. AGP rejects raw local-.aar deps from library modules,
* so the Maven layout is the supported workaround.
*/
function withSherpaOnnxLocalMavenRepo(config: any) {
return withProjectBuildGradle(config, (config) => {
if (config.modResults.language !== "groovy") {
return config
}
let contents = config.modResults.contents
const fallbackRoot = toGroovyString(getBluetoothSdkRoot())
const repoDirExpression = `new File(System.getenv("MENTRA_BLUETOOTH_SDK_PACKAGE_PATH") ?: ${fallbackRoot}, "android/libs/maven")`
const marker = "// bluetooth-sdk: sherpa-onnx local maven repo"
if (contents.includes(marker)) {
return config
}
const repoBlock = ` maven {\n ${marker}\n url = uri(${repoDirExpression})\n }`
const allprojectsMatch = contents.match(/allprojects\s*\{[\s\S]*?repositories\s*\{/)
if (allprojectsMatch) {
const insertIdx = allprojectsMatch.index! + allprojectsMatch[0].length
contents = contents.slice(0, insertIdx) + "\n" + repoBlock + contents.slice(insertIdx)
} else {
// Fallback: append an allprojects block. Older Expo templates that don't
// emit allprojects {} put repositories in settings.gradle instead — but
// this codebase's prebuild has historically emitted allprojects, so this
// branch is just a safety net.
contents += `\nallprojects {\n repositories {\n${repoBlock}\n }\n}\n`
}
config.modResults.contents = contents
return config
})
}
function upsertMetaData(application: any, name: string, value: string) {
application["meta-data"] ??= []
const existing = application["meta-data"].find((item: any) => item.$?.["android:name"] === name)
const entry = existing ?? {$: {"android:name": name}}
entry.$["android:value"] = value
if (!existing) {
application["meta-data"].push(entry)
}
}
function removeMetaData(application: any, name: string) {
application["meta-data"] = (application["meta-data"] ?? []).filter((item: any) => item.$?.["android:name"] !== name)
}
function withAnalyticsManifestMetadata(config: any, props: BluetoothSdkPluginProps | undefined) {
return withAndroidManifest(config, (config) => {
const analytics = resolveAnalyticsProps(props)
const application = AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults)
for (const name of ANALYTICS_META_NAMES) {
removeMetaData(application, name)
}
if (analytics.disabled !== undefined) {
upsertMetaData(application, META_ANALYTICS_DISABLED, analytics.disabled ? "true" : "false")
}
if (analytics.environment !== undefined) {
upsertMetaData(application, META_ANALYTICS_ENVIRONMENT, analytics.environment)
}
return config
})
}
export const withAndroidConfiguration: ConfigPlugin<BluetoothSdkPluginProps> = (config, props) => {
config = withKotlinVersion(config)
config = withSettingsGradleModifications(config)
config = withSherpaOnnxLocalMavenRepo(config)
config = withAnalyticsManifestMetadata(config, props)
if (props?.node) {
config = withGradlePropertiesModifications(config)
}
return config
}
|