diff --git a/packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/MainActivity.java b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-java/com/placeholdername/MainActivity.java similarity index 100% rename from packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/MainActivity.java rename to packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-java/com/placeholdername/MainActivity.java diff --git a/packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/PlaceholderName.java b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-java/com/placeholdername/PlaceholderName.java similarity index 100% rename from packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/PlaceholderName.java rename to packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-java/com/placeholdername/PlaceholderName.java diff --git a/packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-kotlin/com/placeholdername/MainActivity.kt b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-kotlin/com/placeholdername/MainActivity.kt new file mode 100644 index 000000000..b4c083da1 --- /dev/null +++ b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-kotlin/com/placeholdername/MainActivity.kt @@ -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) +} diff --git a/packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-kotlin/com/placeholdername/PlaceholderName.kt b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/android-kotlin/com/placeholdername/PlaceholderName.kt new file mode 100644 index 000000000..e69de29bb diff --git a/packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/Main.java b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/Main.java deleted file mode 100644 index adf8b4b60..000000000 --- a/packages/cli/src/commands/init/__fixtures__/editTemplate/android/com/placeholdername/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -com.placeholdername; - -public static class Main { - public static void run() { - String name = "PlaceholderName"; - String title = "Hello App Display Name"; - } -} diff --git a/packages/cli/src/commands/init/__fixtures__/editTemplate/android/strings.xml b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/strings.xml new file mode 100644 index 000000000..0c79c4bad --- /dev/null +++ b/packages/cli/src/commands/init/__fixtures__/editTemplate/android/strings.xml @@ -0,0 +1,3 @@ + + Hello App Display Name + diff --git a/packages/cli/src/commands/init/__tests__/__snapshots__/editTemplate.test.ts.snap b/packages/cli/src/commands/init/__tests__/__snapshots__/editTemplate.test.ts.snap index d25fc014b..66cca4b77 100644 --- a/packages/cli/src/commands/init/__tests__/__snapshots__/editTemplate.test.ts.snap +++ b/packages/cli/src/commands/init/__tests__/__snapshots__/editTemplate.test.ts.snap @@ -21,6 +21,18 @@ 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\\" @@ -28,55 +40,62 @@ exports[`should edit template 1`] = ` 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 @@ + +- Hello App Display Name ++ ProjectName + " `; -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`] = ` @@ -84,15 +103,9 @@ exports[`should edit template with custom title 1`] = ` - 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\\"; }" `; diff --git a/packages/cli/src/commands/init/__tests__/editTemplate.test.ts b/packages/cli/src/commands/init/__tests__/editTemplate.test.ts index 1e5c89530..32214f1ee 100644 --- a/packages/cli/src/commands/init/__tests__/editTemplate.test.ts +++ b/packages/cli/src/commands/init/__tests__/editTemplate.test.ts @@ -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', @@ -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(); }); @@ -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( + `${PROJECT_TITLE}`, + ); }); describe('changePlaceholderInTemplate', () => { @@ -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, @@ -216,6 +249,7 @@ describe('replacePlaceholderWithPackageName', () => { path.resolve( testPath, 'android', + 'android-java', 'com', PACKAGE_NAME, 'MainActivity.java', @@ -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', () => { diff --git a/packages/cli/src/commands/init/editTemplate.ts b/packages/cli/src/commands/init/editTemplate.ts index 1702aa453..d3bb39f23 100644 --- a/packages/cli/src/commands/init/editTemplate.ts +++ b/packages/cli/src/commands/init/editTemplate.ts @@ -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, `${projectName}`,