Skip to content

Commit b847253

Browse files
committed
refactor: move platform specific template config
1 parent ca2ca0d commit b847253

11 files changed

Lines changed: 601 additions & 888 deletions

File tree

packages/app/android/template.config.mjs

Lines changed: 177 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,146 @@
11
// @ts-check
22
import * as nodefs from "node:fs";
33
import * as path from "node:path";
4-
import { findFile } from "../scripts/helpers.js";
4+
import { findFile, toVersionNumber } from "../scripts/helpers.js";
55
import { generateAndroidManifest } from "./android-manifest.js";
66
import { configureGradleWrapper } from "./gradle-wrapper.js";
77

8-
/** @import { ProjectConfig, ProjectParams } from "../scripts/types.js"; */
8+
/**
9+
* @import {
10+
* Configuration,
11+
* ConfigureParams,
12+
* ProjectConfig,
13+
* ProjectParams,
14+
* } from "../scripts/types.js";
15+
*/
16+
17+
/**
18+
* @returns {string}
19+
*/
20+
export function buildGradle() {
21+
return `buildscript {
22+
apply(from: {
23+
def searchDir = rootDir.toPath()
24+
do {
25+
def p = searchDir.resolve("node_modules/react-native-test-app/android/dependencies.gradle")
26+
if (p.toFile().exists()) {
27+
return p.toRealPath().toString()
28+
}
29+
} while (searchDir = searchDir.getParent())
30+
throw new GradleException("Could not find \`react-native-test-app\`");
31+
}())
32+
33+
repositories {
34+
mavenCentral()
35+
google()
36+
}
37+
38+
dependencies {
39+
getReactNativeDependencies().each { dependency ->
40+
classpath(dependency)
41+
}
42+
}
43+
}
44+
`;
45+
}
46+
47+
/**
48+
* @param {number} _targetVersion Target React Native version
49+
* @returns {string}
50+
*/
51+
export function gradleProperties(_targetVersion) {
52+
return `# Project-wide Gradle settings.
53+
54+
# IDE (e.g. Android Studio) users:
55+
# Gradle settings configured through the IDE *will override*
56+
# any settings specified in this file.
57+
58+
# For more details on how to configure your build environment visit
59+
# http://www.gradle.org/docs/current/userguide/build_environment.html
60+
61+
# Specifies the JVM arguments used for the Gradle Daemon. The setting is
62+
# particularly useful for configuring JVM memory settings for build performance.
63+
# This does not affect the JVM settings for the Gradle client VM.
64+
# The default is \`-Xmx512m -XX:MaxMetaspaceSize=256m\`.
65+
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
66+
67+
# When configured, Gradle will fork up to org.gradle.workers.max JVMs to execute
68+
# projects in parallel. To learn more about parallel task execution, see the
69+
# section on Gradle build performance:
70+
# https://docs.gradle.org/current/userguide/performance.html#parallel_execution.
71+
# Default is \`false\`.
72+
#org.gradle.parallel=true
73+
74+
# AndroidX package structure to make it clearer which packages are bundled with the
75+
# Android operating system, and which are packaged with your app's APK
76+
# https://developer.android.com/topic/libraries/support-library/androidx-rn
77+
android.useAndroidX=true
78+
# Automatically convert third-party libraries to use AndroidX
79+
#android.enableJetifier=true
80+
# Jetifier randomly fails on these libraries
81+
#android.jetifier.ignorelist=hermes-android,react-android
82+
83+
# Use this property to specify which architecture you want to build.
84+
# You can also override it from the CLI using
85+
# ./gradlew <task> -PreactNativeArchitectures=x86_64
86+
reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
87+
88+
# Use this property to enable support to the new architecture.
89+
# This will allow you to use TurboModules and the Fabric render in
90+
# your application. You should enable this flag either if you want
91+
# to write custom TurboModules/Fabric components OR use libraries that
92+
# are providing them.
93+
# Note that this is incompatible with web debugging.
94+
newArchEnabled=true
95+
#bridgelessEnabled=true
96+
97+
# Uncomment the line below to build React Native from source.
98+
#react.buildFromSource=true
99+
100+
# Version of Android NDK to build against.
101+
#ANDROID_NDK_VERSION=26.1.10909125
102+
103+
# Version of Kotlin to build against.
104+
#KOTLIN_VERSION=1.8.22
105+
`;
106+
}
107+
108+
/**
109+
* @param {string} name Root project name
110+
* @returns {string}
111+
*/
112+
export function settingsGradle(name) {
113+
return `pluginManagement {
114+
repositories {
115+
gradlePluginPortal()
116+
mavenCentral()
117+
google()
118+
}
119+
}
120+
121+
rootProject.name = "${name}"
122+
123+
apply(from: {
124+
def searchDir = rootDir.toPath()
125+
do {
126+
def p = searchDir.resolve("node_modules/react-native-test-app/test-app.gradle")
127+
if (p.toFile().exists()) {
128+
return p.toRealPath().toString()
129+
}
130+
} while (searchDir = searchDir.getParent())
131+
throw new GradleException("Could not find \`react-native-test-app\`");
132+
}())
133+
applyTestAppSettings(settings)
134+
`;
135+
}
136+
137+
/**
138+
* @param {...string} paths
139+
* @returns {{ source: string; }}
140+
*/
141+
function copyFrom(...paths) {
142+
return { source: path.join(...paths) };
143+
}
9144

10145
/**
11146
* @returns {string | undefined}
@@ -47,3 +182,43 @@ export function configure(
47182
packageName: packageName || getAndroidPackageName(),
48183
};
49184
}
185+
186+
/**
187+
* @param {ConfigureParams} params
188+
* @returns {Configuration}
189+
*/
190+
export function getTemplate({ name, testAppPath, targetVersion }) {
191+
const targetVersionNum = toVersionNumber(targetVersion);
192+
return {
193+
files: {
194+
"build.gradle": buildGradle(),
195+
"gradle/wrapper/gradle-wrapper.jar": copyFrom(
196+
testAppPath,
197+
"example",
198+
"android",
199+
"gradle",
200+
"wrapper",
201+
"gradle-wrapper.jar"
202+
),
203+
"gradle/wrapper/gradle-wrapper.properties": copyFrom(
204+
testAppPath,
205+
"example",
206+
"android",
207+
"gradle",
208+
"wrapper",
209+
"gradle-wrapper.properties"
210+
),
211+
"gradle.properties": gradleProperties(targetVersionNum),
212+
gradlew: copyFrom(testAppPath, "example", "android", "gradlew"),
213+
"gradlew.bat": copyFrom(testAppPath, "example", "android", "gradlew.bat"),
214+
"settings.gradle": settingsGradle(name),
215+
},
216+
oldFiles: [],
217+
scripts: {
218+
android: "react-native run-android",
219+
"build:android":
220+
"react-native bundle --entry-file index.js --platform android --dev true --bundle-output dist/main.android.jsbundle --assets-dest dist/res",
221+
},
222+
dependencies: {},
223+
};
224+
}

packages/app/ios/template.config.mjs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
// @ts-check
22
import * as nodefs from "node:fs";
3+
import { toVersionNumber, v } from "../scripts/helpers.js";
34

4-
/** @import { ProjectConfig, ProjectParams } from "../scripts/types.js"; */
5+
/**
6+
* @import {
7+
* Configuration,
8+
* ConfigureParams,
9+
* ProjectConfig,
10+
* ProjectParams,
11+
* } from "../scripts/types.js";
12+
*/
13+
14+
/**
15+
* @param {string} name Root project name
16+
* @param {"" | "macos/" | "visionos/"} prefix Platform prefix
17+
* @param {number} targetVersion Target React Native version
18+
* @returns {string}
19+
*/
20+
export function podfile(name, prefix, targetVersion) {
21+
// https://reactnative.dev/blog/2024/10/23/the-new-architecture-is-here
22+
/** @type {Record<typeof prefix, number>} */
23+
const newArchMatrix = {
24+
"": v(0, 76, 0),
25+
"macos/": v(1000, 0, 0),
26+
"visionos/": v(0, 76, 0),
27+
};
28+
const newArchEnabled = targetVersion >= newArchMatrix[prefix];
29+
return `ws_dir = Pathname.new(__dir__)
30+
ws_dir = ws_dir.parent until
31+
File.exist?("#{ws_dir}/node_modules/react-native-test-app/${prefix}test_app.rb") ||
32+
ws_dir.expand_path.to_s == '/'
33+
require "#{ws_dir}/node_modules/react-native-test-app/${prefix}test_app.rb"
34+
35+
workspace '${name}.xcworkspace'
36+
37+
use_test_app! :hermes_enabled => true, :fabric_enabled => ${newArchEnabled}
38+
`;
39+
}
540

641
/**
742
* @param {string} _projectRoot
@@ -11,3 +46,28 @@ import * as nodefs from "node:fs";
1146
export function configure(_projectRoot, config, _fs = nodefs) {
1247
return config;
1348
}
49+
50+
/**
51+
* @param {ConfigureParams} params
52+
* @returns {Configuration}
53+
*/
54+
export function getTemplate({ name, targetVersion }) {
55+
const targetVersionNum = toVersionNumber(targetVersion);
56+
return {
57+
files: {
58+
Podfile: podfile(name, "", targetVersionNum),
59+
},
60+
oldFiles: [
61+
"Podfile.lock",
62+
"Pods",
63+
`${name}.xcodeproj`,
64+
`${name}.xcworkspace`,
65+
],
66+
scripts: {
67+
"build:ios":
68+
"react-native bundle --entry-file index.js --platform ios --dev true --bundle-output dist/main.ios.jsbundle --assets-dest dist",
69+
ios: "react-native run-ios",
70+
},
71+
dependencies: {},
72+
};
73+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// @ts-check
2+
import * as nodefs from "node:fs";
3+
import { podfile } from "../ios/template.config.mjs";
4+
import { toVersionNumber } from "../scripts/helpers.js";
5+
6+
/** @import { Configuration, ConfigureParams } from "../scripts/types.js"; */
7+
8+
/**
9+
* @param {string} _projectRoot
10+
* @param {unknown} _config
11+
* @returns {undefined}
12+
*/
13+
export function configure(_projectRoot, _config, _fs = nodefs) {
14+
return undefined;
15+
}
16+
17+
/**
18+
* @param {ConfigureParams} params
19+
* @returns {Configuration}
20+
*/
21+
export function getTemplate({ name, targetVersion }) {
22+
const targetVersionNum = toVersionNumber(targetVersion);
23+
return {
24+
files: {
25+
Podfile: podfile(name, "macos/", targetVersionNum),
26+
},
27+
oldFiles: [
28+
"Podfile.lock",
29+
"Pods",
30+
`${name}.xcodeproj`,
31+
`${name}.xcworkspace`,
32+
],
33+
scripts: {
34+
"build:macos":
35+
"react-native bundle --entry-file index.js --platform macos --dev true --bundle-output dist/main.macos.jsbundle --assets-dest dist",
36+
macos: `react-native run-macos --scheme ${name}`,
37+
},
38+
dependencies: {},
39+
};
40+
}

0 commit comments

Comments
 (0)