Skip to content

Commit a71fd58

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

11 files changed

Lines changed: 586 additions & 887 deletions

File tree

packages/app/android/template.config.mjs

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,139 @@
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";
5+
import { copyFrom } from "../scripts/template.mjs";
56
import { generateAndroidManifest } from "./android-manifest.js";
67
import { configureGradleWrapper } from "./gradle-wrapper.js";
78

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

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

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)