Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherry pick "fix: replacing package name in Kotlin template (#2209)" #2255

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -21,78 +21,91 @@ exports[`should edit template 1`] = `
- First value
+ Second value

@@ -15,3 +15,3 @@
protected String getMainComponentName() {
- return \\"PlaceholderName\\";
+ return \\"ProjectName\\";
}"
`;

exports[`should edit template 2`] = `
"Snapshot Diff:
- First value
+ Second value

@@ -12,3 +12,3 @@
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
- moduleName:@\\"PlaceholderName\\"
+ moduleName:@\\"ProjectName\\"
initialProperties:nil];"
`;

exports[`should edit template 2`] = `
exports[`should edit template 3`] = `
"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 = \\"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`] = `
exports[`should edit template 4`] = `
"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\\",
Array [
\\"\\",
\\"/android\\",
\\"/android/android-java\\",
\\"/android/android-java/com\\",
- \\"/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\\",
\\"/android/android-kotlin/com\\",
- \\"/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\\",
- \\"/ios/PlaceholderName/project.pbxproj\\",
- \\"/ios/PlaceholderName-tvOS\\",
- \\"/ios/PlaceholderName-tvOS/.gitkeep\\",
- \\"/ios/PlaceholderName/AppDelegate.m\\",
- \\"/ios/PlaceholderName/project.pbxproj\\",
- \\"/ios/PlaceholderNameTests\\",
- \\"/ios/PlaceholderNameTests/.gitkeep\\",
+ \\"/ios/ProjectName\\",
+ \\"/ios/ProjectName/AppDelegate.m\\",
+ \\"/ios/ProjectName/project.pbxproj\\",
+ \\"/ios/ProjectName-tvOS\\",
+ \\"/ios/ProjectName-tvOS/.gitkeep\\",
+ \\"/ios/ProjectName/AppDelegate.m\\",
+ \\"/ios/ProjectName/project.pbxproj\\",
+ \\"/ios/ProjectNameTests\\",
+ \\"/ios/ProjectNameTests/.gitkeep\\",
\\"/node_modules\\","
\\"/node_modules\\",
\\"/node_modules/PlaceholderName\\",
\\"/package.json\\",
]"
`;

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\\";
@@ -15,3 +15,3 @@
protected String getMainComponentName() {
- return \\"PlaceholderName\\";
+ return \\"ProjectName\\";
}"
`;
75 changes: 66 additions & 9 deletions packages/cli/src/commands/init/__tests__/editTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,35 @@ test('should edit template', async () => {
path.resolve(
FIXTURE_DIR,
'android',
'android-java',
'com',
PLACEHOLDER_NAME.toLowerCase(),
'Main.java',
'MainActivity.java',
),
'utf8',
);

const newJavaFile = fs.readFileSync(
path.resolve(
testPath,
'android',
'android-java',
'com',
PROJECT_NAME.toLowerCase(),
'Main.java',
'MainActivity.java',
),
'utf8',
);

const oldXmlFile = fs.readFileSync(
path.resolve(FIXTURE_DIR, 'android', 'strings.xml'),
'utf8',
);
const newXmlFile = fs.readFileSync(
path.resolve(testPath, 'android', 'strings.xml'),
'utf8',
);

const oldCFile = fs.readFileSync(
path.resolve(FIXTURE_DIR, 'ios', PLACEHOLDER_NAME, 'AppDelegate.m'),
'utf8',
Expand All @@ -85,14 +97,23 @@ test('should edit template', async () => {
'utf8',
);

expect(snapshotDiff(oldCFile, newCFile, {contextLines: 1})).toMatchSnapshot();
expect(
snapshotDiff(oldJavaFile, newJavaFile, {contextLines: 1}),
).toMatchSnapshot();

expect(snapshotDiff(oldCFile, newCFile, {contextLines: 1})).toMatchSnapshot();
expect(
snapshotDiff(fixtureTree.map(slash), transformedTree.map(slash), {
contextLines: 1,
}),
snapshotDiff(oldXmlFile, newXmlFile, {contextLines: 1}),
).toMatchSnapshot();

expect(
snapshotDiff(
fixtureTree.map(slash).sort(),
transformedTree.map(slash).sort(),
{
contextLines: 5,
},
),
).toMatchSnapshot();
});

Expand All @@ -109,26 +130,38 @@ test('should edit template with custom title', async () => {
path.resolve(
FIXTURE_DIR,
'android',
'android-java',
'com',
PLACEHOLDER_NAME.toLowerCase(),
'Main.java',
'MainActivity.java',
),
'utf8',
);

const newJavaFile = fs.readFileSync(
path.resolve(
testPath,
'android',
'android-java',
'com',
PROJECT_NAME.toLowerCase(),
'Main.java',
'MainActivity.java',
),
'utf8',
);

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 +237,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 +249,7 @@ describe('replacePlaceholderWithPackageName', () => {
path.resolve(
testPath,
'android',
'android-java',
'com',
PACKAGE_NAME,
'MainActivity.java',
Expand All @@ -225,6 +259,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