Skip to content

Commit 2561740

Browse files
authoredJan 10, 2024
fix: replacing package name in Kotlin template (#2209)
* fix: replacing name in Kotlin template * fix(test): update snapshots * test: update `contextLines` * fix: use `utf-8` when reading files
1 parent c34df46 commit 2561740

File tree

9 files changed

+161
-61
lines changed

9 files changed

+161
-61
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.helloworld
2+
3+
import com.facebook.react.ReactActivity
4+
import com.facebook.react.ReactActivityDelegate
5+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
6+
import com.facebook.react.defaults.DefaultReactActivityDelegate
7+
8+
class MainActivity : ReactActivity() {
9+
10+
/**
11+
* Returns the name of the main component registered from JavaScript. This is used to schedule
12+
* rendering of the component.
13+
*/
14+
override fun getMainComponentName(): String = "PlaceholderName"
15+
16+
/**
17+
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
18+
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
19+
*/
20+
override fun createReactActivityDelegate(): ReactActivityDelegate =
21+
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
22+
}

‎packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-kotlin/com/placeholdername/PlaceholderName.kt

Whitespace-only changes.

‎packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/Main.java

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Hello App Display Name</string>
3+
</resources>

‎packages/cli/src/commands/init/__tests__/__snapshots__/editTemplate.test.ts.snap

+51-38
Original file line numberDiff line numberDiff line change
@@ -21,78 +21,91 @@ exports[`should edit template 1`] = `
2121
- First value
2222
+ Second value
2323
24+
@@ -15,3 +15,3 @@
25+
protected String getMainComponentName() {
26+
- return \\"PlaceholderName\\";
27+
+ return \\"ProjectName\\";
28+
}"
29+
`;
30+
31+
exports[`should edit template 2`] = `
32+
"Snapshot Diff:
33+
- First value
34+
+ Second value
35+
2436
@@ -12,3 +12,3 @@
2537
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
2638
- moduleName:@\\"PlaceholderName\\"
2739
+ moduleName:@\\"ProjectName\\"
2840
initialProperties:nil];"
2941
`;
3042
31-
exports[`should edit template 2`] = `
43+
exports[`should edit template 3`] = `
3244
"Snapshot Diff:
3345
- First value
3446
+ Second value
3547
36-
@@ -1,2 +1,2 @@
37-
- com.placeholdername;
38-
+ com.projectname;
39-
40-
@@ -4,4 +4,4 @@
41-
public static void run() {
42-
- String name = \\"PlaceholderName\\";
43-
- String title = \\"Hello App Display Name\\";
44-
+ String name = \\"ProjectName\\";
45-
+ String title = \\"ProjectName\\";
46-
}"
48+
@@ -1,3 +1,3 @@
49+
<resources>
50+
- <string name=\\"app_name\\">Hello App Display Name</string>
51+
+ <string name=\\"app_name\\">ProjectName</string>
52+
</resources>"
4753
`;
4854
49-
exports[`should edit template 3`] = `
55+
exports[`should edit template 4`] = `
5056
"Snapshot Diff:
5157
- First value
5258
+ Second value
5359
54-
@@ -4,14 +4,14 @@
55-
\\"/android/com\\",
56-
- \\"/android/com/placeholdername\\",
57-
- \\"/android/com/placeholdername/Main.java\\",
58-
- \\"/android/com/placeholdername/MainActivity.java\\",
59-
- \\"/android/com/placeholdername/PlaceholderName.java\\",
60-
+ \\"/android/com/projectname\\",
61-
+ \\"/android/com/projectname/Main.java\\",
62-
+ \\"/android/com/projectname/MainActivity.java\\",
63-
+ \\"/android/com/projectname/ProjectName.java\\",
60+
Array [
61+
\\"\\",
62+
\\"/android\\",
63+
\\"/android/android-java\\",
64+
\\"/android/android-java/com\\",
65+
- \\"/android/android-java/com/placeholdername\\",
66+
- \\"/android/android-java/com/placeholdername/MainActivity.java\\",
67+
- \\"/android/android-java/com/placeholdername/PlaceholderName.java\\",
68+
+ \\"/android/android-java/com/projectname\\",
69+
+ \\"/android/android-java/com/projectname/MainActivity.java\\",
70+
+ \\"/android/android-java/com/projectname/ProjectName.java\\",
71+
\\"/android/android-kotlin\\",
72+
\\"/android/android-kotlin/com\\",
73+
- \\"/android/android-kotlin/com/placeholdername\\",
74+
- \\"/android/android-kotlin/com/placeholdername/MainActivity.kt\\",
75+
- \\"/android/android-kotlin/com/placeholdername/PlaceholderName.kt\\",
76+
+ \\"/android/android-kotlin/com/projectname\\",
77+
+ \\"/android/android-kotlin/com/projectname/MainActivity.kt\\",
78+
+ \\"/android/android-kotlin/com/projectname/ProjectName.kt\\",
79+
\\"/android/strings.xml\\",
6480
\\"/ios\\",
6581
- \\"/ios/PlaceholderName\\",
66-
- \\"/ios/PlaceholderName/AppDelegate.m\\",
67-
- \\"/ios/PlaceholderName/project.pbxproj\\",
6882
- \\"/ios/PlaceholderName-tvOS\\",
6983
- \\"/ios/PlaceholderName-tvOS/.gitkeep\\",
84+
- \\"/ios/PlaceholderName/AppDelegate.m\\",
85+
- \\"/ios/PlaceholderName/project.pbxproj\\",
7086
- \\"/ios/PlaceholderNameTests\\",
7187
- \\"/ios/PlaceholderNameTests/.gitkeep\\",
7288
+ \\"/ios/ProjectName\\",
73-
+ \\"/ios/ProjectName/AppDelegate.m\\",
74-
+ \\"/ios/ProjectName/project.pbxproj\\",
7589
+ \\"/ios/ProjectName-tvOS\\",
7690
+ \\"/ios/ProjectName-tvOS/.gitkeep\\",
91+
+ \\"/ios/ProjectName/AppDelegate.m\\",
92+
+ \\"/ios/ProjectName/project.pbxproj\\",
7793
+ \\"/ios/ProjectNameTests\\",
7894
+ \\"/ios/ProjectNameTests/.gitkeep\\",
79-
\\"/node_modules\\","
95+
\\"/node_modules\\",
96+
\\"/node_modules/PlaceholderName\\",
97+
\\"/package.json\\",
98+
]"
8099
`;
81100
82101
exports[`should edit template with custom title 1`] = `
83102
"Snapshot Diff:
84103
- First value
85104
+ Second value
86105
87-
@@ -1,2 +1,2 @@
88-
- com.placeholdername;
89-
+ com.projectname;
90-
91-
@@ -4,4 +4,4 @@
92-
public static void run() {
93-
- String name = \\"PlaceholderName\\";
94-
- String title = \\"Hello App Display Name\\";
95-
+ String name = \\"ProjectName\\";
96-
+ String title = \\"ProjectTitle\\";
106+
@@ -15,3 +15,3 @@
107+
protected String getMainComponentName() {
108+
- return \\"PlaceholderName\\";
109+
+ return \\"ProjectName\\";
97110
}"
98111
`;

‎packages/cli/src/commands/init/__tests__/editTemplate.test.ts

+69-9
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,35 @@ skipIfNode20('should edit template', async () => {
6262
path.resolve(
6363
FIXTURE_DIR,
6464
'android',
65+
'android-java',
6566
'com',
6667
PLACEHOLDER_NAME.toLowerCase(),
67-
'Main.java',
68+
'MainActivity.java',
6869
),
6970
'utf8',
7071
);
72+
7173
const newJavaFile = fs.readFileSync(
7274
path.resolve(
7375
testPath,
7476
'android',
77+
'android-java',
7578
'com',
7679
PROJECT_NAME.toLowerCase(),
77-
'Main.java',
80+
'MainActivity.java',
7881
),
7982
'utf8',
8083
);
8184

85+
const oldXmlFile = fs.readFileSync(
86+
path.resolve(FIXTURE_DIR, 'android', 'strings.xml'),
87+
'utf8',
88+
);
89+
const newXmlFile = fs.readFileSync(
90+
path.resolve(testPath, 'android', 'strings.xml'),
91+
'utf8',
92+
);
93+
8294
const oldCFile = fs.readFileSync(
8395
path.resolve(FIXTURE_DIR, 'ios', PLACEHOLDER_NAME, 'AppDelegate.m'),
8496
'utf8',
@@ -88,14 +100,23 @@ skipIfNode20('should edit template', async () => {
88100
'utf8',
89101
);
90102

91-
expect(snapshotDiff(oldCFile, newCFile, {contextLines: 1})).toMatchSnapshot();
92103
expect(
93104
snapshotDiff(oldJavaFile, newJavaFile, {contextLines: 1}),
94105
).toMatchSnapshot();
106+
107+
expect(snapshotDiff(oldCFile, newCFile, {contextLines: 1})).toMatchSnapshot();
95108
expect(
96-
snapshotDiff(fixtureTree.map(slash), transformedTree.map(slash), {
97-
contextLines: 1,
98-
}),
109+
snapshotDiff(oldXmlFile, newXmlFile, {contextLines: 1}),
110+
).toMatchSnapshot();
111+
112+
expect(
113+
snapshotDiff(
114+
fixtureTree.map(slash).sort(),
115+
transformedTree.map(slash).sort(),
116+
{
117+
contextLines: 5,
118+
},
119+
),
99120
).toMatchSnapshot();
100121
});
101122

@@ -112,26 +133,38 @@ skipIfNode20('should edit template with custom title', async () => {
112133
path.resolve(
113134
FIXTURE_DIR,
114135
'android',
136+
'android-java',
115137
'com',
116138
PLACEHOLDER_NAME.toLowerCase(),
117-
'Main.java',
139+
'MainActivity.java',
118140
),
119141
'utf8',
120142
);
143+
121144
const newJavaFile = fs.readFileSync(
122145
path.resolve(
123146
testPath,
124147
'android',
148+
'android-java',
125149
'com',
126150
PROJECT_NAME.toLowerCase(),
127-
'Main.java',
151+
'MainActivity.java',
128152
),
129153
'utf8',
130154
);
131155

156+
const replacedFile = fs.readFileSync(
157+
path.resolve(testPath, 'android', 'strings.xml'),
158+
'utf8',
159+
);
160+
132161
expect(
133162
snapshotDiff(oldJavaFile, newJavaFile, {contextLines: 1}),
134163
).toMatchSnapshot();
164+
165+
expect(replacedFile).toContain(
166+
`<string name="app_name">${PROJECT_TITLE}</string>`,
167+
);
135168
});
136169

137170
describe('changePlaceholderInTemplate', () => {
@@ -214,7 +247,7 @@ describe('replacePlaceholderWithPackageName', () => {
214247
});
215248

216249
skipIfNode20(
217-
`should rename Main component name for Android with ${PROJECT_NAME}`,
250+
`should rename Main component name for Android with ${PROJECT_NAME} in Java template`,
218251
async () => {
219252
await replacePlaceholderWithPackageName({
220253
projectName: PROJECT_NAME,
@@ -227,6 +260,7 @@ describe('replacePlaceholderWithPackageName', () => {
227260
path.resolve(
228261
testPath,
229262
'android',
263+
'android-java',
230264
'com',
231265
PACKAGE_NAME,
232266
'MainActivity.java',
@@ -239,6 +273,32 @@ describe('replacePlaceholderWithPackageName', () => {
239273
).toBeTruthy();
240274
},
241275
);
276+
277+
skipIfNode20(
278+
`should rename Main component name for Android with ${PROJECT_NAME} in Kotlin template`,
279+
async () => {
280+
await replacePlaceholderWithPackageName({
281+
projectName: PROJECT_NAME,
282+
placeholderName: PLACEHOLDER_NAME,
283+
placeholderTitle: 'Test',
284+
packageName: PACKAGE_NAME,
285+
});
286+
287+
const mainActivityFile = fs.readFileSync(
288+
path.resolve(
289+
testPath,
290+
'android',
291+
'android-kotlin',
292+
'com',
293+
PACKAGE_NAME,
294+
'MainActivity.kt',
295+
),
296+
'utf8',
297+
);
298+
299+
expect(mainActivityFile.includes(`= "${PROJECT_NAME}"`)).toBeTruthy();
300+
},
301+
);
242302
});
243303

244304
describe('validatePackageName', () => {

‎packages/cli/src/commands/init/editTemplate.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,22 @@ export async function replacePlaceholderWithPackageName({
169169
if (filePath.includes('app.json')) {
170170
await replaceNameInUTF8File(filePath, projectName, placeholderName);
171171
} else {
172-
// replace main component name for Android package
173-
await replaceNameInUTF8File(
174-
filePath,
175-
`return "${projectName}"`,
176-
`return "${placeholderName}"`,
177-
);
172+
const fileExtension = path.extname(filePath);
173+
174+
if (fileExtension === '.java') {
175+
await replaceNameInUTF8File(
176+
filePath,
177+
`return "${projectName}"`,
178+
`return "${placeholderName}"`,
179+
);
180+
} else if (fileExtension === '.kt') {
181+
await replaceNameInUTF8File(
182+
filePath,
183+
`= "${projectName}"`,
184+
`= "${placeholderName}"`,
185+
);
186+
}
187+
178188
await replaceNameInUTF8File(
179189
filePath,
180190
`<string name="app_name">${projectName}</string>`,

0 commit comments

Comments
 (0)
Please sign in to comment.