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..ebd8a5914 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 @@ -33,17 +33,11 @@ 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 @@ + +- Hello App Display Name ++ ProjectName + " `; exports[`should edit template 3`] = ` @@ -51,16 +45,24 @@ exports[`should edit template 3`] = ` - 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\\", @@ -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\\"; - }" -`; diff --git a/packages/cli/src/commands/init/__tests__/editTemplate.test.ts b/packages/cli/src/commands/init/__tests__/editTemplate.test.ts index 1e5c89530..db2d2f2c3 100644 --- a/packages/cli/src/commands/init/__tests__/editTemplate.test.ts +++ b/packages/cli/src/commands/init/__tests__/editTemplate.test.ts @@ -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', ); @@ -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), { @@ -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( + `${PROJECT_TITLE}`, + ); }); describe('changePlaceholderInTemplate', () => { @@ -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, @@ -216,6 +188,7 @@ describe('replacePlaceholderWithPackageName', () => { path.resolve( testPath, 'android', + 'android-java', 'com', PACKAGE_NAME, 'MainActivity.java', @@ -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', () => { 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}`,