Skip to content

Commit

Permalink
fix: replacing name in Kotlin template
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Dec 15, 2023
1 parent 085b045 commit 053028a
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.helloworld

import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate

class MainActivity : ReactActivity() {

/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "PlaceholderName"

/**
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Hello App Display Name</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,36 @@ exports[`should edit template 2`] = `
- First value
+ Second value
@@ -1,2 +1,2 @@
- com.placeholdername;
+ com.projectname;
@@ -4,4 +4,4 @@
public static void run() {
- String name = \\"PlaceholderName\\";
- String title = \\"Hello App Display Name\\";
+ String name = \\"ProjectName\\";
+ String title = \\"ProjectName\\";
}"
@@ -1,3 +1,3 @@
<resources>
- <string name=\\"app_name\\">Hello App Display Name</string>
+ <string name=\\"app_name\\">ProjectName</string>
</resources>"
`;
exports[`should edit template 3`] = `
"Snapshot Diff:
- First value
+ Second value
@@ -4,14 +4,14 @@
\\"/android/com\\",
- \\"/android/com/placeholdername\\",
- \\"/android/com/placeholdername/Main.java\\",
- \\"/android/com/placeholdername/MainActivity.java\\",
- \\"/android/com/placeholdername/PlaceholderName.java\\",
+ \\"/android/com/projectname\\",
+ \\"/android/com/projectname/Main.java\\",
+ \\"/android/com/projectname/MainActivity.java\\",
+ \\"/android/com/projectname/ProjectName.java\\",
@@ -9,5 +9,5 @@
\\"/android/android-java/com/.DS_Store\\",
- \\"/android/android-java/com/placeholdername\\",
- \\"/android/android-java/com/placeholdername/MainActivity.java\\",
- \\"/android/android-java/com/placeholdername/PlaceholderName.java\\",
+ \\"/android/android-java/com/projectname\\",
+ \\"/android/android-java/com/projectname/MainActivity.java\\",
+ \\"/android/android-java/com/projectname/ProjectName.java\\",
\\"/android/android-kotlin\\",
@@ -16,14 +16,14 @@
\\"/android/android-kotlin/com/.DS_Store\\",
- \\"/android/android-kotlin/com/placeholdername\\",
- \\"/android/android-kotlin/com/placeholdername/MainActivity.kt\\",
- \\"/android/android-kotlin/com/placeholdername/PlaceholderName.kt\\",
+ \\"/android/android-kotlin/com/projectname\\",
+ \\"/android/android-kotlin/com/projectname/MainActivity.kt\\",
+ \\"/android/android-kotlin/com/projectname/ProjectName.kt\\",
\\"/android/strings.xml\\",
\\"/ios\\",
- \\"/ios/PlaceholderName\\",
- \\"/ios/PlaceholderName/AppDelegate.m\\",
Expand All @@ -78,21 +80,3 @@ exports[`should edit template 3`] = `
+ \\"/ios/ProjectNameTests/.gitkeep\\",
\\"/node_modules\\","
`;
exports[`should edit template with custom title 1`] = `
"Snapshot Diff:
- First value
+ Second value
@@ -1,2 +1,2 @@
- com.placeholdername;
+ com.projectname;
@@ -4,4 +4,4 @@
public static void run() {
- String name = \\"PlaceholderName\\";
- String title = \\"Hello App Display Name\\";
+ String name = \\"ProjectName\\";
+ String title = \\"ProjectTitle\\";
}"
`;
74 changes: 35 additions & 39 deletions packages/cli/src/commands/init/__tests__/editTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,12 @@ test('should edit template', async () => {
const transformedTree = walk(testPath).map((e) => e.replace(testPath, ''));
const fixtureTree = walk(FIXTURE_DIR).map((e) => e.replace(FIXTURE_DIR, ''));

const oldJavaFile = fs.readFileSync(
path.resolve(
FIXTURE_DIR,
'android',
'com',
PLACEHOLDER_NAME.toLowerCase(),
'Main.java',
),
const oldXmlFile = fs.readFileSync(
path.resolve(FIXTURE_DIR, 'android', 'strings.xml'),
'utf8',
);
const newJavaFile = fs.readFileSync(
path.resolve(
testPath,
'android',
'com',
PROJECT_NAME.toLowerCase(),
'Main.java',
),
const newXmlFile = fs.readFileSync(
path.resolve(testPath, 'android', 'strings.xml'),
'utf8',
);

Expand All @@ -87,7 +75,7 @@ test('should edit template', async () => {

expect(snapshotDiff(oldCFile, newCFile, {contextLines: 1})).toMatchSnapshot();
expect(
snapshotDiff(oldJavaFile, newJavaFile, {contextLines: 1}),
snapshotDiff(oldXmlFile, newXmlFile, {contextLines: 1}),
).toMatchSnapshot();
expect(
snapshotDiff(fixtureTree.map(slash), transformedTree.map(slash), {
Expand All @@ -105,30 +93,14 @@ test('should edit template with custom title', async () => {
projectTitle: PROJECT_TITLE,
});

const oldJavaFile = fs.readFileSync(
path.resolve(
FIXTURE_DIR,
'android',
'com',
PLACEHOLDER_NAME.toLowerCase(),
'Main.java',
),
'utf8',
);
const newJavaFile = fs.readFileSync(
path.resolve(
testPath,
'android',
'com',
PROJECT_NAME.toLowerCase(),
'Main.java',
),
const replacedFile = fs.readFileSync(
path.resolve(testPath, 'android', 'strings.xml'),
'utf8',
);

expect(
snapshotDiff(oldJavaFile, newJavaFile, {contextLines: 1}),
).toMatchSnapshot();
expect(replacedFile).toContain(
`<string name="app_name">${PROJECT_TITLE}</string>`,
);
});

describe('changePlaceholderInTemplate', () => {
Expand Down Expand Up @@ -204,7 +176,7 @@ describe('replacePlaceholderWithPackageName', () => {
).toBeTruthy();
});

test(`should rename Main component name for Android with ${PROJECT_NAME}`, async () => {
test(`should rename Main component name for Android with ${PROJECT_NAME} in Java template`, async () => {
await replacePlaceholderWithPackageName({
projectName: PROJECT_NAME,
placeholderName: PLACEHOLDER_NAME,
Expand All @@ -216,6 +188,7 @@ describe('replacePlaceholderWithPackageName', () => {
path.resolve(
testPath,
'android',
'android-java',
'com',
PACKAGE_NAME,
'MainActivity.java',
Expand All @@ -225,6 +198,29 @@ describe('replacePlaceholderWithPackageName', () => {

expect(mainActivityFile.includes(`return "${PROJECT_NAME}"`)).toBeTruthy();
});

test(`should rename Main component name for Android with ${PROJECT_NAME} in Kotlin template`, async () => {
await replacePlaceholderWithPackageName({
projectName: PROJECT_NAME,
placeholderName: PLACEHOLDER_NAME,
placeholderTitle: 'Test',
packageName: PACKAGE_NAME,
});

const mainActivityFile = fs.readFileSync(
path.resolve(
testPath,
'android',
'android-kotlin',
'com',
PACKAGE_NAME,
'MainActivity.kt',
),
'utf8',
);

expect(mainActivityFile.includes(`= "${PROJECT_NAME}"`)).toBeTruthy();
});
});

describe('validatePackageName', () => {
Expand Down
22 changes: 16 additions & 6 deletions packages/cli/src/commands/init/editTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,22 @@ export async function replacePlaceholderWithPackageName({
if (filePath.includes('app.json')) {
await replaceNameInUTF8File(filePath, projectName, placeholderName);
} else {
// replace main component name for Android package
await replaceNameInUTF8File(
filePath,
`return "${projectName}"`,
`return "${placeholderName}"`,
);
const fileExtension = path.extname(filePath);

if (fileExtension === '.java') {
await replaceNameInUTF8File(
filePath,
`return "${projectName}"`,
`return "${placeholderName}"`,
);
} else if (fileExtension === '.kt') {
await replaceNameInUTF8File(
filePath,
`= "${projectName}"`,
`= "${placeholderName}"`,
);
}

await replaceNameInUTF8File(
filePath,
`<string name="app_name">${projectName}</string>`,
Expand Down

0 comments on commit 053028a

Please sign in to comment.