From a75cf32890804bcfc20819df4fdfe5bd4fafb7f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCner?= Date: Thu, 20 Jun 2024 13:15:22 +0300 Subject: [PATCH] feat: merge view+module native templates (#562) ## Summary This adds new templates that use `Kotlin` and `Objective-C`. The templates each have a native view and a native module included. ### Other Changes / Side Effects 1. With 993d5cd2d624c9e6de15554d3c676b3dcf4f42e9, the new arch and mixed arch views will no longer have the `View` suffix in their codegen names. This doesn't break codegen as the name field is arbitrary as it isn't stated otherwise [in the new arch working group documents](https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-libraries-prerequisites.md#configure-codegen). One important matter to remember here is to make sure we pass the `RN` prefix since headers can conflict. 2. No more Java templates! I've removed them all for the sake of Kotlin. 3. I've removed templates like view-new (new arch-only view template) since we now have view+module templates and it would be hard to maintain the old model. 4. I kept `module-legacy`, `module-mixed`, and `module-new` since they are used by cpp. 5. I kept `module-legacy`, and `view-legacy` since they are used by Swift. ## Test plan 1. Go to `packages/create-react-native-library`. 7. Call `yarn prepare` to build the CLI. 8. Run `./bin/create-react-native-library` to run the CLI you built. 9. Fill in the name, email, etc., and make sure to select ` Fabric view and Turbo module with backward compat`. 10. Go to the library you created and make sure to build the following: a. Android with old architecture b. iOS with old architecture c. Android with new architecture d. iOS with new architecture --- .github/workflows/build-templates.yml | 70 +++--- .../create-react-native-library/src/index.ts | 202 +++++++++--------- .../common-example/example/src/App.tsx | 32 ++- .../templates/common/$package.json | 4 +- .../templates/common/CONTRIBUTING.md | 2 +- .../templates/common/README.md | 5 +- .../{%- project.name %}Module.java | 43 ---- .../{%- project.name %}Package.java | 28 --- .../{%- project.name %}Module.java | 40 ---- .../{%- project.name %}Package.java | 45 ---- .../src/newarch/{%- project.name %}Spec.java | 9 - .../src/oldarch/{%- project.name %}Spec.java | 13 -- .../{%- project.name %}Module.java | 40 ---- .../{%- project.name %}Package.java | 44 ---- .../{%- project.name %}Package.java | 22 -- .../{%- project.name %}ViewManager.java | 31 --- .../{%- project.name %}View.java | 24 --- .../{%- project.name %}ViewManager.java | 31 --- .../{%- project.name %}ViewPackage.java | 25 --- .../{%- project.name %}ViewManagerSpec.java | 24 --- .../{%- project.name %}ViewManagerSpec.java | 12 -- .../{%- project.name %}View.java | 24 --- .../{%- project.name %}ViewManager.java | 47 ---- .../{%- project.name %}ViewPackage.java | 25 --- .../{%- project.name %}Module.kt | 25 +++ .../{%- project.name %}Package.kt | 39 ++++ .../{%- project.name %}ViewManager.kt | 20 ++ .../{%- project.name %}Module.kt | 24 +++ .../{%- project.name %}Package.kt | 40 ++++ .../{%- project.name %}View.kt | 0 .../{%- project.name %}ViewManager.kt | 0 .../src/newarch/{%- project.name %}Spec.kt | 7 + .../{%- project.name %}ViewManagerSpec.kt | 0 .../src/oldarch/{%- project.name %}Spec.kt | 11 + .../{%- project.name %}ViewManagerSpec.kt | 0 .../{%- project.name %}Module.kt | 23 ++ .../{%- project.name %}Package.kt | 40 ++++ .../{%- project.name %}View.kt | 0 .../{%- project.name %}ViewManager.kt | 0 .../{%- project.name %}Package.kt | 19 -- .../{%- project.name %}Package.kt | 19 -- .../native-common/android/build.gradle | 10 - .../native-view-library-legacy/src/index.tsx | 7 + .../src/{%- project.name %}.ts | 18 ++ .../src/{%- project.name %}View.ts | 26 +++ .../src/Native{%- project.name %}.ts | 8 + .../native-view-library-mixed/src/index.tsx | 32 +++ .../{%- project.name %}NativeComponent.ts} | 0 .../src/Native{%- project.name %}.ts | 8 + .../native-view-library-new/src/index.tsx | 8 + .../{%- project.name %}NativeComponent.ts} | 0 .../templates/native-view-mixed/src/index.tsx | 2 - .../templates/native-view-new/src/index.tsx | 2 - .../ios/{%- project.name %}.h | 6 + .../ios/{%- project.name %}.mm | 18 ++ .../ios/{%- project.name %}ViewManager.m | 0 .../ios/Utils.h | 0 .../ios/Utils.m | 0 .../ios/{%- project.name %}.h | 17 ++ .../ios/{%- project.name %}.mm | 48 +++++ .../ios/{%- project.name %}View.h | 0 .../ios/{%- project.name %}View.mm | 8 +- .../ios/{%- project.name %}ViewManager.mm | 0 .../ios/{%- project.name %}.h | 17 ++ .../ios/{%- project.name %}.mm | 48 +++++ .../ios/{%- project.name %}View.h | 0 .../ios/{%- project.name %}View.mm | 8 +- .../ios/{%- project.name %}ViewManager.mm | 0 68 files changed, 638 insertions(+), 762 deletions(-) delete mode 100644 packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java delete mode 100644 packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java delete mode 100644 packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java delete mode 100644 packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java delete mode 100644 packages/create-react-native-library/templates/java-library-mixed/android/src/newarch/{%- project.name %}Spec.java delete mode 100644 packages/create-react-native-library/templates/java-library-mixed/android/src/oldarch/{%- project.name %}Spec.java delete mode 100644 packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java delete mode 100644 packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java delete mode 100644 packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java delete mode 100644 packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java delete mode 100644 packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java delete mode 100644 packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java delete mode 100644 packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java delete mode 100644 packages/create-react-native-library/templates/java-view-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.java delete mode 100644 packages/create-react-native-library/templates/java-view-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.java delete mode 100644 packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java delete mode 100644 packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java delete mode 100644 packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt rename packages/create-react-native-library/templates/{kotlin-view-mixed => kotlin-view-library-mixed}/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt (100%) rename packages/create-react-native-library/templates/{kotlin-view-mixed => kotlin-view-library-mixed}/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt (100%) create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/newarch/{%- project.name %}Spec.kt rename packages/create-react-native-library/templates/{kotlin-view-mixed => kotlin-view-library-mixed}/android/src/newarch/{%- project.name %}ViewManagerSpec.kt (100%) create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/oldarch/{%- project.name %}Spec.kt rename packages/create-react-native-library/templates/{kotlin-view-mixed => kotlin-view-library-mixed}/android/src/oldarch/{%- project.name %}ViewManagerSpec.kt (100%) create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt create mode 100644 packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt rename packages/create-react-native-library/templates/{kotlin-view-new => kotlin-view-library-new}/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt (100%) rename packages/create-react-native-library/templates/{kotlin-view-new => kotlin-view-library-new}/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt (100%) delete mode 100644 packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt delete mode 100644 packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt create mode 100644 packages/create-react-native-library/templates/native-view-library-legacy/src/index.tsx create mode 100644 packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}.ts create mode 100644 packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}View.ts create mode 100644 packages/create-react-native-library/templates/native-view-library-mixed/src/Native{%- project.name %}.ts create mode 100644 packages/create-react-native-library/templates/native-view-library-mixed/src/index.tsx rename packages/create-react-native-library/templates/{native-view-mixed/src/{%- project.name %}ViewNativeComponent.ts => native-view-library-mixed/src/{%- project.name %}NativeComponent.ts} (100%) create mode 100644 packages/create-react-native-library/templates/native-view-library-new/src/Native{%- project.name %}.ts create mode 100644 packages/create-react-native-library/templates/native-view-library-new/src/index.tsx rename packages/create-react-native-library/templates/{native-view-new/src/{%- project.name %}ViewNativeComponent.ts => native-view-library-new/src/{%- project.name %}NativeComponent.ts} (100%) delete mode 100644 packages/create-react-native-library/templates/native-view-mixed/src/index.tsx delete mode 100644 packages/create-react-native-library/templates/native-view-new/src/index.tsx create mode 100644 packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.h create mode 100644 packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.mm rename packages/create-react-native-library/templates/{objc-view-legacy => objc-view-library-legacy}/ios/{%- project.name %}ViewManager.m (100%) rename packages/create-react-native-library/templates/{objc-view-mixed => objc-view-library-mixed}/ios/Utils.h (100%) rename packages/create-react-native-library/templates/{objc-view-mixed => objc-view-library-mixed}/ios/Utils.m (100%) create mode 100644 packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.h create mode 100644 packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.mm rename packages/create-react-native-library/templates/{objc-view-mixed => objc-view-library-mixed}/ios/{%- project.name %}View.h (100%) rename packages/create-react-native-library/templates/{objc-view-mixed => objc-view-library-mixed}/ios/{%- project.name %}View.mm (81%) rename packages/create-react-native-library/templates/{objc-view-mixed => objc-view-library-mixed}/ios/{%- project.name %}ViewManager.mm (100%) create mode 100644 packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.h create mode 100644 packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.mm rename packages/create-react-native-library/templates/{objc-view-new => objc-view-library-new}/ios/{%- project.name %}View.h (100%) rename packages/create-react-native-library/templates/{objc-view-new => objc-view-library-new}/ios/{%- project.name %}View.mm (84%) rename packages/create-react-native-library/templates/{objc-view-new => objc-view-library-new}/ios/{%- project.name %}ViewManager.mm (100%) diff --git a/.github/workflows/build-templates.yml b/.github/workflows/build-templates.yml index c7d2be9b0..e21002541 100644 --- a/.github/workflows/build-templates.yml +++ b/.github/workflows/build-templates.yml @@ -28,57 +28,41 @@ jobs: - module-legacy - module-mixed - module-new - - view-legacy - - view-mixed - - view-new + - view-module-legacy + - view-module-mixed + - view-module-new language: - - java-objc - - java-swift - kotlin-objc - - kotlin-swift + - cpp exclude: - - os: macos-14 + - type: view-module-legacy + language: cpp + - type: view-module-mixed + language: cpp + - type: view-module-new + language: cpp + - type: module-legacy + language: kotlin-objc + - type: module-mixed language: kotlin-objc - - os: macos-14 - language: kotlin-swift - - type: module-new - language: java-swift - type: module-new + language: kotlin-objc + include: + - os: ubuntu-latest + type: view-legacy language: kotlin-swift - - type: module-mixed - language: java-swift - - type: module-mixed + - os: macos-14 + type: view-legacy language: kotlin-swift - - type: view-new - language: java-swift - - type: view-new + - os: ubuntu-latest + type: module-legacy language: kotlin-swift - - type: view-mixed - language: java-swift - - type: view-mixed + - os: macos-14 + type: module-legacy language: kotlin-swift - include: - os: ubuntu-latest type: library language: js - - os: ubuntu-latest - type: module-legacy - language: cpp - - os: ubuntu-latest - type: module-mixed - language: cpp - - os: ubuntu-latest - type: module-new - language: cpp - - os: macos-14 - type: module-legacy - language: cpp - - os: macos-14 - type: module-mixed - language: cpp - - os: macos-14 - type: module-new - language: cpp concurrency: group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}-${{ matrix.type }}-${{ matrix.language }} @@ -138,17 +122,13 @@ jobs: working-directory: ${{ env.work_dir }} run: | # Build Android for only some matrices to skip redundant builds - if [[ ${{ matrix.os }} == ubuntu-latest ]]; then - if [[ ${{ matrix.type }} == view-* && ${{ matrix.language }} == *-objc ]] || [[ ${{ matrix.type }} == module-* && ${{ matrix.language }} == *-objc ]] || [[ ${{ matrix.type }} == module-* && ${{ matrix.language }} == cpp ]]; then + if [[ ${{ matrix.os }} == ubuntu-latest ]] && [[ ${{ matrix.language }} != js ]] ; then echo "android_build=1" >> $GITHUB_ENV - fi fi # Build iOS for only some matrices to skip redundant builds - if [[ ${{ matrix.os }} == macos-14 ]]; then - if [[ ${{ matrix.type }} == view-* && ${{ matrix.language }} == java-* ]] || [[ ${{ matrix.type }} == module-* && ${{ matrix.language }} == java-* ]] || [[ ${{ matrix.type }} == module-* && ${{ matrix.language }} == cpp ]]; then + if [[ ${{ matrix.os }} == macos-14 ]] && [[ ${{ matrix.language }} != js ]]; then echo "ios_build=1" >> $GITHUB_ENV - fi fi - name: Cache turborepo diff --git a/packages/create-react-native-library/src/index.ts b/packages/create-react-native-library/src/index.ts index 18350a22e..ae39abb47 100644 --- a/packages/create-react-native-library/src/index.ts +++ b/packages/create-react-native-library/src/index.ts @@ -38,42 +38,70 @@ const NATIVE_COMMON_EXAMPLE_FILES = path.resolve( ); const NATIVE_FILES = { - module_legacy: path.resolve(__dirname, '../templates/native-library-legacy'), - module_new: path.resolve(__dirname, '../templates/native-library-new'), - module_mixed: path.resolve(__dirname, '../templates/native-library-mixed'), - view_legacy: path.resolve(__dirname, '../templates/native-view-legacy'), - view_mixed: path.resolve(__dirname, '../templates/native-view-mixed'), - view_new: path.resolve(__dirname, '../templates/native-view-new'), -} as const; - -const JAVA_FILES = { - module_legacy: path.resolve(__dirname, '../templates/java-library-legacy'), - module_new: path.resolve(__dirname, '../templates/java-library-new'), - module_mixed: path.resolve(__dirname, '../templates/java-library-mixed'), - view_legacy: path.resolve(__dirname, '../templates/java-view-legacy'), - view_mixed: path.resolve(__dirname, '../templates/java-view-mixed'), - view_new: path.resolve(__dirname, '../templates/java-view-new'), + 'module-legacy': path.resolve( + __dirname, + '../templates/native-library-legacy' + ), + 'module-new': path.resolve(__dirname, '../templates/native-library-new'), + 'module-mixed': path.resolve(__dirname, '../templates/native-library-mixed'), + 'view-legacy': path.resolve(__dirname, '../templates/native-view-legacy'), + 'view-module-legacy': path.resolve( + __dirname, + '../templates/native-view-library-legacy' + ), + 'view-module-mixed': path.resolve( + __dirname, + '../templates/native-view-library-mixed' + ), + 'view-module-new': path.resolve( + __dirname, + '../templates/native-view-library-new' + ), } as const; const OBJC_FILES = { - module_common: path.resolve(__dirname, '../templates/objc-library'), - view_legacy: path.resolve(__dirname, '../templates/objc-view-legacy'), - view_mixed: path.resolve(__dirname, '../templates/objc-view-mixed'), - view_new: path.resolve(__dirname, '../templates/objc-view-new'), + 'module-legacy': path.resolve(__dirname, '../templates/objc-library'), + 'module-mixed': path.resolve(__dirname, '../templates/objc-library'), + 'module-new': path.resolve(__dirname, '../templates/objc-library'), + 'view-module-legacy': path.resolve( + __dirname, + '../templates/objc-view-library-legacy' + ), + 'view-module-mixed': path.resolve( + __dirname, + '../templates/objc-view-library-mixed' + ), + 'view-module-new': path.resolve( + __dirname, + '../templates/objc-view-library-new' + ), } as const; const KOTLIN_FILES = { - module_legacy: path.resolve(__dirname, '../templates/kotlin-library-legacy'), - module_new: path.resolve(__dirname, '../templates/kotlin-library-new'), - module_mixed: path.resolve(__dirname, '../templates/kotlin-library-mixed'), - view_legacy: path.resolve(__dirname, '../templates/kotlin-view-legacy'), - view_mixed: path.resolve(__dirname, '../templates/kotlin-view-mixed'), - view_new: path.resolve(__dirname, '../templates/kotlin-view-new'), + 'module-legacy': path.resolve( + __dirname, + '../templates/kotlin-library-legacy' + ), + 'module-new': path.resolve(__dirname, '../templates/kotlin-library-new'), + 'module-mixed': path.resolve(__dirname, '../templates/kotlin-library-mixed'), + 'view-legacy': path.resolve(__dirname, '../templates/kotlin-view-legacy'), + 'view-module-legacy': path.resolve( + __dirname, + '../templates/kotlin-view-library-legacy' + ), + 'view-module-mixed': path.resolve( + __dirname, + '../templates/kotlin-view-library-mixed' + ), + 'view-module-new': path.resolve( + __dirname, + '../templates/kotlin-view-library-new' + ), } as const; const SWIFT_FILES = { - module_legacy: path.resolve(__dirname, '../templates/swift-library-legacy'), - view_legacy: path.resolve(__dirname, '../templates/swift-view-legacy'), + 'module-legacy': path.resolve(__dirname, '../templates/swift-library-legacy'), + 'view-legacy': path.resolve(__dirname, '../templates/swift-view-legacy'), } as const; type ArgName = @@ -89,22 +117,17 @@ type ArgName = | 'example' | 'react-native-version'; -type ProjectLanguages = - | 'java-objc' - | 'java-swift' - | 'kotlin-objc' - | 'kotlin-swift' - | 'cpp' - | 'js'; +type ProjectLanguages = 'kotlin-objc' | 'kotlin-swift' | 'cpp' | 'js'; type ProjectType = + | 'library' | 'module-legacy' - | 'module-new' | 'module-mixed' - | 'view-mixed' - | 'view-new' + | 'module-new' | 'view-legacy' - | 'library'; + | 'view-module-legacy' + | 'view-module-mixed' + | 'view-module-new'; type Answers = { slug: string; @@ -127,37 +150,13 @@ const LANGUAGE_CHOICES: { { title: 'Kotlin & Objective-C', value: 'kotlin-objc', - types: [ - 'module-legacy', - 'module-new', - 'module-mixed', - 'view-mixed', - 'view-new', - 'view-legacy', - ], - }, - { - title: 'Java & Objective-C', - value: 'java-objc', - types: [ - 'module-legacy', - 'module-new', - 'module-mixed', - 'view-mixed', - 'view-new', - 'view-legacy', - ], + types: ['view-module-legacy', 'view-module-mixed', 'view-module-new'], }, { title: 'Kotlin & Swift', value: 'kotlin-swift', types: ['module-legacy', 'view-legacy'], }, - { - title: 'Java & Swift', - value: 'java-swift', - types: ['module-legacy', 'view-legacy'], - }, { title: 'C++ for Android & iOS', value: 'cpp', @@ -178,6 +177,21 @@ const TYPE_CHOICES: { value: ProjectType; description: string; }[] = [ + { + title: 'Fabric view and Turbo module with backward compat', + value: 'view-module-mixed', + description: BACKCOMPAT_DESCRIPTION, + }, + { + title: 'Fabric view and Turbo module', + value: 'view-module-new', + description: NEWARCH_DESCRIPTION, + }, + { + title: 'Native module and Native view', + value: 'view-module-legacy', + description: 'bridge for native APIs and views to JS', + }, { title: 'JavaScript library', value: 'library', @@ -203,16 +217,6 @@ const TYPE_CHOICES: { value: 'module-new', description: NEWARCH_DESCRIPTION, }, - { - title: 'Fabric view with backward compat', - value: 'view-mixed', - description: BACKCOMPAT_DESCRIPTION, - }, - { - title: 'Fabric view', - value: 'view-new', - description: NEWARCH_DESCRIPTION, - }, ]; const args: Record = { @@ -501,8 +505,8 @@ async function create(argv: yargs.Arguments) { authorEmail, authorUrl, repoUrl, - type = 'module-mixed', - languages = type === 'library' ? 'js' : 'java-objc', + type = 'view-module-mixed', + languages = type === 'library' ? 'js' : 'kotlin-objc', example: hasExample, reactNativeVersion, } = { @@ -561,13 +565,11 @@ async function create(argv: yargs.Arguments) { version = FALLBACK_BOB_VERSION; } - const moduleType = type.startsWith('view-') ? 'view' : 'module'; - const arch = - type === 'module-new' || type === 'view-new' - ? 'new' - : type === 'module-mixed' || type === 'view-mixed' - ? 'mixed' - : 'legacy'; + const arch = type.endsWith('new') + ? 'new' + : type.endsWith('mixed') + ? 'mixed' + : 'legacy'; const example = hasExample && !local ? (type === 'library' ? 'expo' : 'native') : 'none'; @@ -610,10 +612,9 @@ async function create(argv: yargs.Arguments) { native: languages !== 'js', arch, cpp: languages === 'cpp', - kotlin: languages === 'kotlin-objc' || languages === 'kotlin-swift', - swift: languages === 'java-swift' || languages === 'kotlin-swift', - view: moduleType === 'view', - module: moduleType === 'module', + swift: languages === 'kotlin-swift', + view: type.includes('view'), + module: type.includes('module'), }, author: { name: authorName, @@ -697,7 +698,7 @@ async function create(argv: yargs.Arguments) { } } - if (languages === 'js') { + if (type === 'library') { await copyDir(JS_FILES, folder); await copyDir(EXPO_FILES, folder); } else { @@ -714,29 +715,18 @@ async function create(argv: yargs.Arguments) { await copyDir(NATIVE_COMMON_EXAMPLE_FILES, folder); } - if (moduleType === 'module') { - await copyDir(NATIVE_FILES[`${moduleType}_${arch}`], folder); - } else { - await copyDir(NATIVE_FILES[`${moduleType}_${arch}`], folder); - } + await copyDir(NATIVE_FILES[type], folder); - if (options.project.swift) { - await copyDir(SWIFT_FILES[`${moduleType}_legacy`], folder); + if ( + type === 'view-legacy' || + (type === 'module-legacy' && options.project.swift) + ) { + await copyDir(SWIFT_FILES[type], folder); } else { - if (moduleType === 'module') { - await copyDir(OBJC_FILES[`${moduleType}_common`], folder); - } else { - await copyDir(OBJC_FILES[`view_${arch}`], folder); - } + await copyDir(OBJC_FILES[type], folder); } - const templateType = `${moduleType}_${arch}` as const; - - if (options.project.kotlin) { - await copyDir(KOTLIN_FILES[templateType], folder); - } else { - await copyDir(JAVA_FILES[templateType], folder); - } + await copyDir(KOTLIN_FILES[type], folder); if (options.project.cpp) { await copyDir(CPP_FILES, folder); diff --git a/packages/create-react-native-library/templates/common-example/example/src/App.tsx b/packages/create-react-native-library/templates/common-example/example/src/App.tsx index 38db6fe50..55ebf1f82 100644 --- a/packages/create-react-native-library/templates/common-example/example/src/App.tsx +++ b/packages/create-react-native-library/templates/common-example/example/src/App.tsx @@ -1,6 +1,9 @@ import * as React from 'react'; -<% if (project.view) { -%> +<% if (project.view && project.module) { -%> +import { StyleSheet, View, Text } from 'react-native'; +import { <%- project.name -%>View, multiply } from '<%- project.slug -%>'; +<% } else if (project.view) { -%> import { StyleSheet, View } from 'react-native'; import { <%- project.name -%>View } from '<%- project.slug -%>'; <% } else { -%> @@ -8,39 +11,30 @@ import { StyleSheet, View, Text } from 'react-native'; import { multiply } from '<%- project.slug -%>'; <% } -%> -<% if (project.view) { -%> -export default function App() { - return ( - - <<%- project.name -%>View color="#32a852" style={styles.box} /> - - ); -} -<% } else if (project.arch === 'new' && project.module) { -%> +<% if (project.arch === 'new' && project.module) { -%> const result = multiply(3, 7); +<% } -%> export default function App() { - return ( - - Result: {result} - - ); -} -<% } else { -%> -export default function App() { +<% if ((project.arch !== 'new' && project.module) || !project.native) { -%> const [result, setResult] = React.useState(); React.useEffect(() => { multiply(3, 7).then(setResult); }, []); +<% } -%> return ( +<% if (project.module || !project.native) { -%> Result: {result} +<% } -%> +<% if (project.view) { -%> + <<%- project.name -%>View color="#32a852" style={styles.box} /> +<% } -%> ); } -<% } -%> const styles = StyleSheet.create({ container: { diff --git a/packages/create-react-native-library/templates/common/$package.json b/packages/create-react-native-library/templates/common/$package.json index 5cf2ca060..e86a80224 100644 --- a/packages/create-react-native-library/templates/common/$package.json +++ b/packages/create-react-native-library/templates/common/$package.json @@ -169,8 +169,8 @@ <% if (project.arch !== 'legacy') { -%> }, "codegenConfig": { - "name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec", - "type": <%- project.view ? '"components"': '"modules"' %>, + "name": "RN<%- project.name -%>Spec", + "type": <%- project.view && project.module ? '"all"' : project.view ? '"components"': '"modules"' %>, "jsSrcsDir": "src" <% } -%> } diff --git a/packages/create-react-native-library/templates/common/CONTRIBUTING.md b/packages/create-react-native-library/templates/common/CONTRIBUTING.md index 6d2908914..303a6b326 100644 --- a/packages/create-react-native-library/templates/common/CONTRIBUTING.md +++ b/packages/create-react-native-library/templates/common/CONTRIBUTING.md @@ -26,7 +26,7 @@ It is configured to use the local version of the library, so any changes you mak <% if (project.native) { -%> If you want to use Android Studio or XCode to edit the native code, you can open the `example/android` or `example/ios` directories respectively in those editors. To edit the Objective-C or Swift files, open `example/ios/<%- project.name -%>Example.xcworkspace` in XCode and find the source files at `Pods > Development Pods > <%- project.slug -%>`. -To edit the Java or Kotlin files, open `example/android` in Android studio and find the source files at `<%- project.identifier -%>` under `Android`. +To edit the the Kotlin files, open `example/android` in Android studio and find the source files at `<%- project.identifier -%>` under `Android`. <% } -%> You can use various commands from the root directory to work with the project. diff --git a/packages/create-react-native-library/templates/common/README.md b/packages/create-react-native-library/templates/common/README.md index ea2b491f0..d42a79cb3 100644 --- a/packages/create-react-native-library/templates/common/README.md +++ b/packages/create-react-native-library/templates/common/README.md @@ -18,7 +18,8 @@ import { <%- project.name -%>View } from "<%- project.slug -%>"; <<%- project.name -%>View color="tomato" /> ``` -<% } else if (project.arch === 'new' && project.module) { -%> +<% } -%> +<% if (project.module && project.arch === 'new') { -%> ```js import { multiply } from '<%- project.slug -%>'; @@ -27,7 +28,7 @@ import { multiply } from '<%- project.slug -%>'; const result = multiply(3, 7); ``` -<% } else { -%> +<% } else if (project.module) { -%> ```js import { multiply } from '<%- project.slug -%>'; diff --git a/packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java b/packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java deleted file mode 100644 index 798e50429..000000000 --- a/packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.NonNull; - -import com.facebook.react.bridge.Promise; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; -import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.module.annotations.ReactModule; - -@ReactModule(name = <%- project.name -%>Module.NAME) -public class <%- project.name -%>Module extends ReactContextBaseJavaModule { - public static final String NAME = "<%- project.name -%>"; - - public <%- project.name -%>Module(ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - @NonNull - public String getName() { - return NAME; - } - -<% if (project.cpp) { -%> - static { - System.loadLibrary("<%- project.identifier -%>"); - } - - private static native double nativeMultiply(double a, double b); -<% } -%> - - // Example method - // See https://reactnative.dev/docs/native-modules-android - @ReactMethod - public void multiply(double a, double b, Promise promise) { -<% if (project.cpp) { -%> - promise.resolve(nativeMultiply(a, b)); -<% } else { -%> - promise.resolve(a * b); -<% } -%> - } -} diff --git a/packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java b/packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java deleted file mode 100644 index 0a7e05207..000000000 --- a/packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.NonNull; - -import com.facebook.react.ReactPackage; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.ViewManager; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class <%- project.name -%>Package implements ReactPackage { - @NonNull - @Override - public List createNativeModules(@NonNull ReactApplicationContext reactContext) { - List modules = new ArrayList<>(); - modules.add(new <%- project.name -%>Module(reactContext)); - return modules; - } - - @NonNull - @Override - public List createViewManagers(@NonNull ReactApplicationContext reactContext) { - return Collections.emptyList(); - } -} diff --git a/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java b/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java deleted file mode 100644 index afd27c5c5..000000000 --- a/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.NonNull; - -import com.facebook.react.bridge.Promise; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactMethod; - -public class <%- project.name -%>Module extends <%- project.name -%>Spec { - public static final String NAME = "<%- project.name -%>"; - - <%- project.name -%>Module(ReactApplicationContext context) { - super(context); - } - - @Override - @NonNull - public String getName() { - return NAME; - } - -<% if (project.cpp) { -%> - static { - System.loadLibrary("<%- project.identifier -%>"); - } - - public static native double nativeMultiply(double a, double b); -<% } -%> - - // Example method - // See https://reactnative.dev/docs/native-modules-android - @ReactMethod - public void multiply(double a, double b, Promise promise) { -<% if (project.cpp) { -%> - promise.resolve(nativeMultiply(a, b)); -<% } else { -%> - promise.resolve(a * b); -<% } -%> - } -} diff --git a/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java b/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java deleted file mode 100644 index 89e8c71b2..000000000 --- a/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.Nullable; - -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.module.model.ReactModuleInfo; -import com.facebook.react.module.model.ReactModuleInfoProvider; -import com.facebook.react.TurboReactPackage; - -import java.util.HashMap; -import java.util.Map; - -public class <%- project.name -%>Package extends TurboReactPackage { - - @Nullable - @Override - public NativeModule getModule(String name, ReactApplicationContext reactContext) { - if (name.equals(<%- project.name -%>Module.NAME)) { - return new <%- project.name -%>Module(reactContext); - } else { - return null; - } - } - - @Override - public ReactModuleInfoProvider getReactModuleInfoProvider() { - return () -> { - final Map moduleInfos = new HashMap<>(); - boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED; - moduleInfos.put( - <%- project.name -%>Module.NAME, - new ReactModuleInfo( - <%- project.name -%>Module.NAME, - <%- project.name -%>Module.NAME, - false, // canOverrideExistingModule - false, // needsEagerInit - true, // hasConstants - false, // isCxxModule - isTurboModule // isTurboModule - )); - return moduleInfos; - }; - } -} diff --git a/packages/create-react-native-library/templates/java-library-mixed/android/src/newarch/{%- project.name %}Spec.java b/packages/create-react-native-library/templates/java-library-mixed/android/src/newarch/{%- project.name %}Spec.java deleted file mode 100644 index 4edc2ec82..000000000 --- a/packages/create-react-native-library/templates/java-library-mixed/android/src/newarch/{%- project.name %}Spec.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.<%- project.package %>; - -import com.facebook.react.bridge.ReactApplicationContext; - -abstract class <%- project.name -%>Spec extends Native<%- project.name -%>Spec { - <%- project.name -%>Spec(ReactApplicationContext context) { - super(context); - } -} diff --git a/packages/create-react-native-library/templates/java-library-mixed/android/src/oldarch/{%- project.name %}Spec.java b/packages/create-react-native-library/templates/java-library-mixed/android/src/oldarch/{%- project.name %}Spec.java deleted file mode 100644 index 350d05a34..000000000 --- a/packages/create-react-native-library/templates/java-library-mixed/android/src/oldarch/{%- project.name %}Spec.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.<%- project.package %>; - -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; -import com.facebook.react.bridge.Promise; - -abstract class <%- project.name -%>Spec extends ReactContextBaseJavaModule { - <%- project.name -%>Spec(ReactApplicationContext context) { - super(context); - } - - public abstract void multiply(double a, double b, Promise promise); -} diff --git a/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java b/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java deleted file mode 100644 index e9a2e6aaf..000000000 --- a/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.NonNull; - -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.module.annotations.ReactModule; - -@ReactModule(name = <%- project.name -%>Module.NAME) -public class <%- project.name -%>Module extends Native<%- project.name -%>Spec { - public static final String NAME = "<%- project.name -%>"; - - public <%- project.name -%>Module(ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - @NonNull - public String getName() { - return NAME; - } - -<% if (project.cpp) { -%> - static { - System.loadLibrary("<%- project.identifier -%>"); - } - - private static native double nativeMultiply(double a, double b); -<% } -%> - - // Example method - // See https://reactnative.dev/docs/native-modules-android - @Override - public double multiply(double a, double b) { -<% if (project.cpp) { -%> - return nativeMultiply(a, b); -<% } else { -%> - return a * b; -<% } -%> - } -} diff --git a/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java b/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java deleted file mode 100644 index bcc5307ba..000000000 --- a/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.Nullable; - -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.module.model.ReactModuleInfo; -import com.facebook.react.module.model.ReactModuleInfoProvider; -import com.facebook.react.TurboReactPackage; - -import java.util.HashMap; -import java.util.Map; - -public class <%- project.name -%>Package extends TurboReactPackage { - - @Nullable - @Override - public NativeModule getModule(String name, ReactApplicationContext reactContext) { - if (name.equals(<%- project.name -%>Module.NAME)) { - return new <%- project.name -%>Module(reactContext); - } else { - return null; - } - } - - @Override - public ReactModuleInfoProvider getReactModuleInfoProvider() { - return () -> { - final Map moduleInfos = new HashMap<>(); - moduleInfos.put( - <%- project.name -%>Module.NAME, - new ReactModuleInfo( - <%- project.name -%>Module.NAME, - <%- project.name -%>Module.NAME, - false, // canOverrideExistingModule - false, // needsEagerInit - true, // hasConstants - false, // isCxxModule - true // isTurboModule - )); - return moduleInfos; - }; - } -} diff --git a/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java b/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java deleted file mode 100644 index f8626aab1..000000000 --- a/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.<%- project.package %>; - -import com.facebook.react.ReactPackage; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.ViewManager; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public class <%- project.name -%>Package implements ReactPackage { - @Override - public List createNativeModules(ReactApplicationContext reactContext) { - return Collections.emptyList(); - } - - @Override - public List createViewManagers(ReactApplicationContext reactContext) { - return Arrays.asList(new <%- project.name -%>ViewManager()); - } -} diff --git a/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java b/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java deleted file mode 100644 index 9fa0a42bc..000000000 --- a/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.<%- project.package %>; - -import android.graphics.Color; -import android.view.View; - -import androidx.annotation.NonNull; - -import com.facebook.react.uimanager.SimpleViewManager; -import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.uimanager.annotations.ReactProp; - -public class <%- project.name -%>ViewManager extends SimpleViewManager { - public static final String REACT_CLASS = "<%- project.name -%>View"; - - @Override - @NonNull - public String getName() { - return REACT_CLASS; - } - - @Override - @NonNull - public View createViewInstance(ThemedReactContext reactContext) { - return new View(reactContext); - } - - @ReactProp(name = "color") - public void setColor(View view, String color) { - view.setBackgroundColor(Color.parseColor(color)); - } -} diff --git a/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java deleted file mode 100644 index d053791f8..000000000 --- a/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.Nullable; - -import android.content.Context; -import android.util.AttributeSet; - -import android.view.View; - -public class <%- project.name -%>View extends View { - - public <%- project.name -%>View(Context context) { - super(context); - } - - public <%- project.name -%>View(Context context, @Nullable AttributeSet attrs) { - super(context, attrs); - } - - public <%- project.name -%>View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - } - -} diff --git a/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java deleted file mode 100644 index 7442a7b44..000000000 --- a/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.<%- project.package %>; - -import android.graphics.Color; - -import androidx.annotation.Nullable; - -import com.facebook.react.module.annotations.ReactModule; -import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.uimanager.annotations.ReactProp; - -@ReactModule(name = <%- project.name -%>ViewManager.NAME) -public class <%- project.name -%>ViewManager extends <%- project.name -%>ViewManagerSpec<<%- project.name -%>View> { - - public static final String NAME = "<%- project.name -%>View"; - - @Override - public String getName() { - return NAME; - } - - @Override - public <%- project.name -%>View createViewInstance(ThemedReactContext context) { - return new <%- project.name -%>View(context); - } - - @Override - @ReactProp(name = "color") - public void setColor(<%- project.name -%>View view, @Nullable String color) { - view.setBackgroundColor(Color.parseColor(color)); - } -} diff --git a/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java deleted file mode 100644 index c46da2710..000000000 --- a/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java +++ /dev/null @@ -1,25 +0,0 @@ -<% /* TODO: try to migrate it to TurboReactPackage */ %> -package com.<%- project.package %>; - -import com.facebook.react.ReactPackage; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.ViewManager; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class <%- project.name -%>ViewPackage implements ReactPackage { - @Override - public List createViewManagers(ReactApplicationContext reactContext) { - List viewManagers = new ArrayList<>(); - viewManagers.add(new <%- project.name -%>ViewManager()); - return viewManagers; - } - - @Override - public List createNativeModules(ReactApplicationContext reactContext) { - return Collections.emptyList(); - } -} diff --git a/packages/create-react-native-library/templates/java-view-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.java deleted file mode 100644 index 763f28eaa..000000000 --- a/packages/create-react-native-library/templates/java-view-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.<%- project.package %>; - -import android.view.View; - -import androidx.annotation.Nullable; - -import com.facebook.react.uimanager.SimpleViewManager; -import com.facebook.react.uimanager.ViewManagerDelegate; -import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerDelegate; -import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerInterface; - -public abstract class <%- project.name -%>ViewManagerSpec extends SimpleViewManager implements <%- project.name -%>ViewManagerInterface { - private final ViewManagerDelegate mDelegate; - - public <%- project.name -%>ViewManagerSpec() { - mDelegate = new <%- project.name -%>ViewManagerDelegate(this); - } - - @Nullable - @Override - protected ViewManagerDelegate getDelegate() { - return mDelegate; - } -} diff --git a/packages/create-react-native-library/templates/java-view-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.java deleted file mode 100644 index 0962fb3d1..000000000 --- a/packages/create-react-native-library/templates/java-view-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.<%- project.package %>; - -import android.view.View; - -import androidx.annotation.Nullable; - -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.SimpleViewManager; - -public abstract class <%- project.name -%>ViewManagerSpec extends SimpleViewManager { - public abstract void setColor(T view, @Nullable String value); -} diff --git a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java deleted file mode 100644 index d053791f8..000000000 --- a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.<%- project.package %>; - -import androidx.annotation.Nullable; - -import android.content.Context; -import android.util.AttributeSet; - -import android.view.View; - -public class <%- project.name -%>View extends View { - - public <%- project.name -%>View(Context context) { - super(context); - } - - public <%- project.name -%>View(Context context, @Nullable AttributeSet attrs) { - super(context, attrs); - } - - public <%- project.name -%>View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - } - -} diff --git a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java deleted file mode 100644 index 6360c1940..000000000 --- a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.<%- project.package %>; - -import android.graphics.Color; - -import androidx.annotation.Nullable; - -import com.facebook.react.module.annotations.ReactModule; -import com.facebook.react.uimanager.SimpleViewManager; -import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.uimanager.ViewManagerDelegate; -import com.facebook.react.uimanager.annotations.ReactProp; -import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerDelegate; -import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerInterface; - -@ReactModule(name = <%- project.name -%>ViewManager.NAME) -public class <%- project.name -%>ViewManager extends SimpleViewManager<<%- project.name -%>View> implements <%- project.name -%>ViewManagerInterface<<%- project.name -%>View> { - - public static final String NAME = "<%- project.name -%>View"; - - private final ViewManagerDelegate<<%- project.name -%>View> mDelegate; - - public <%- project.name -%>ViewManager() { - mDelegate = new <%- project.name -%>ViewManagerDelegate(this); - } - - @Nullable - @Override - protected ViewManagerDelegate<<%- project.name -%>View> getDelegate() { - return mDelegate; - } - - @Override - public String getName() { - return NAME; - } - - @Override - public <%- project.name -%>View createViewInstance(ThemedReactContext context) { - return new <%- project.name -%>View(context); - } - - @Override - @ReactProp(name = "color") - public void setColor(<%- project.name -%>View view, String color) { - view.setBackgroundColor(Color.parseColor(color)); - } -} diff --git a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java deleted file mode 100644 index c46da2710..000000000 --- a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewPackage.java +++ /dev/null @@ -1,25 +0,0 @@ -<% /* TODO: try to migrate it to TurboReactPackage */ %> -package com.<%- project.package %>; - -import com.facebook.react.ReactPackage; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.ViewManager; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class <%- project.name -%>ViewPackage implements ReactPackage { - @Override - public List createViewManagers(ReactApplicationContext reactContext) { - List viewManagers = new ArrayList<>(); - viewManagers.add(new <%- project.name -%>ViewManager()); - return viewManagers; - } - - @Override - public List createNativeModules(ReactApplicationContext reactContext) { - return Collections.emptyList(); - } -} diff --git a/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt b/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt new file mode 100644 index 000000000..392956228 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt @@ -0,0 +1,25 @@ +package com.<%- project.package %> + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactContextBaseJavaModule +import com.facebook.react.bridge.ReactMethod +import com.facebook.react.bridge.Promise + +class <%- project.name -%>Module(reactContext: ReactApplicationContext) : + ReactContextBaseJavaModule(reactContext) { + + override fun getName(): String { + return NAME + } + + // Example method + // See https://reactnative.dev/docs/native-modules-android + @ReactMethod + fun multiply(a: Double, b: Double, promise: Promise) { + promise.resolve(a * b) + } + + companion object { + const val NAME = "<%- project.name -%>" + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt new file mode 100644 index 000000000..dbd7eec4e --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt @@ -0,0 +1,39 @@ +package com.<%- project.package %> + +import com.facebook.react.BaseReactPackage +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.NativeModule +import com.facebook.react.module.model.ReactModuleInfoProvider +import com.facebook.react.module.model.ReactModuleInfo +import com.facebook.react.uimanager.ViewManager +import java.util.HashMap + +class <%- project.name -%>Package : BaseReactPackage() { + override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? { + return if (name == <%- project.name -%>Module.NAME) { + <%- project.name -%>Module(reactContext) + } else { + null + } + } + + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider { + return ReactModuleInfoProvider { + val moduleInfos: MutableMap = HashMap() + moduleInfos[<%- project.name -%>Module.NAME] = ReactModuleInfo( + <%- project.name -%>Module.NAME, + <%- project.name -%>Module.NAME, + false, // canOverrideExistingModule + false, // needsEagerInit + true, // hasConstants + false, // isCxxModule + false // isTurboModule + ) + moduleInfos + } + } + + override fun createViewManagers(reactContext: ReactApplicationContext): List> { + return mutableListOf(<%- project.name -%>ViewManager()); + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt b/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt new file mode 100644 index 000000000..f52229f97 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt @@ -0,0 +1,20 @@ +package com.<%- project.package %> + +import android.graphics.Color +import android.view.View +import com.facebook.react.uimanager.SimpleViewManager +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.uimanager.annotations.ReactProp + +class <%- project.name -%>ViewManager : SimpleViewManager() { + override fun getName() = "<%- project.name -%>View" + + override fun createViewInstance(reactContext: ThemedReactContext): View { + return View(reactContext) + } + + @ReactProp(name = "color") + fun setColor(view: View, color: String) { + view.setBackgroundColor(Color.parseColor(color)) + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt new file mode 100644 index 000000000..2cf84d752 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt @@ -0,0 +1,24 @@ +package com.<%- project.package %> + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactMethod +import com.facebook.react.bridge.Promise + +class <%- project.name -%>Module internal constructor(context: ReactApplicationContext) : + <%- project.name -%>Spec(context) { + + override fun getName(): String { + return NAME + } + + // Example method + // See https://reactnative.dev/docs/native-modules-android + @ReactMethod + override fun multiply(a: Double, b: Double, promise: Promise) { + promise.resolve(a * b) + } + + companion object { + const val NAME = "<%- project.name -%>" + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt new file mode 100644 index 000000000..fae83afe0 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt @@ -0,0 +1,40 @@ +package com.<%- project.package %> + +import com.facebook.react.BaseReactPackage +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.NativeModule +import com.facebook.react.module.model.ReactModuleInfoProvider +import com.facebook.react.module.model.ReactModuleInfo +import com.facebook.react.uimanager.ViewManager +import java.util.HashMap + +class <%- project.name -%>Package : BaseReactPackage() { + override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? { + return if (name == <%- project.name -%>Module.NAME) { + <%- project.name -%>Module(reactContext) + } else { + null + } + } + + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider { + return ReactModuleInfoProvider { + val moduleInfos: MutableMap = HashMap() + val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED + moduleInfos[<%- project.name -%>Module.NAME] = ReactModuleInfo( + <%- project.name -%>Module.NAME, + <%- project.name -%>Module.NAME, + false, // canOverrideExistingModule + false, // needsEagerInit + true, // hasConstants + false, // isCxxModule + isTurboModule // isTurboModule + ) + moduleInfos + } + } + + override fun createViewManagers(reactContext: ReactApplicationContext): List> { + return mutableListOf(<%- project.name -%>ViewManager()); + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt rename to packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt rename to packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/newarch/{%- project.name %}Spec.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/newarch/{%- project.name %}Spec.kt new file mode 100644 index 000000000..3711dacb2 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/newarch/{%- project.name %}Spec.kt @@ -0,0 +1,7 @@ +package com.<%- project.package %> + +import com.facebook.react.bridge.ReactApplicationContext + +abstract class <%- project.name -%>Spec internal constructor(context: ReactApplicationContext) : + Native<%- project.name -%>Spec(context) { +} diff --git a/packages/create-react-native-library/templates/kotlin-view-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.kt rename to packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/newarch/{%- project.name %}ViewManagerSpec.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/oldarch/{%- project.name %}Spec.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/oldarch/{%- project.name %}Spec.kt new file mode 100644 index 000000000..ef0259618 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/oldarch/{%- project.name %}Spec.kt @@ -0,0 +1,11 @@ +package com.<%- project.package %> + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactContextBaseJavaModule +import com.facebook.react.bridge.Promise + +abstract class <%- project.name -%>Spec internal constructor(context: ReactApplicationContext) : + ReactContextBaseJavaModule(context) { + + abstract fun multiply(a: Double, b: Double, promise: Promise) +} diff --git a/packages/create-react-native-library/templates/kotlin-view-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.kt b/packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.kt rename to packages/create-react-native-library/templates/kotlin-view-library-mixed/android/src/oldarch/{%- project.name %}ViewManagerSpec.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt b/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt new file mode 100644 index 000000000..5da133031 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt @@ -0,0 +1,23 @@ +package com.<%- project.package %> + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.module.annotations.ReactModule + +@ReactModule(name = <%- project.name -%>Module.NAME) +class <%- project.name -%>Module(reactContext: ReactApplicationContext) : + Native<%- project.name -%>Spec(reactContext) { + + override fun getName(): String { + return NAME + } + + // Example method + // See https://reactnative.dev/docs/native-modules-android + override fun multiply(a: Double, b: Double): Double { + return a * b + } + + companion object { + const val NAME = "<%- project.name -%>" + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt new file mode 100644 index 000000000..fae83afe0 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt @@ -0,0 +1,40 @@ +package com.<%- project.package %> + +import com.facebook.react.BaseReactPackage +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.NativeModule +import com.facebook.react.module.model.ReactModuleInfoProvider +import com.facebook.react.module.model.ReactModuleInfo +import com.facebook.react.uimanager.ViewManager +import java.util.HashMap + +class <%- project.name -%>Package : BaseReactPackage() { + override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? { + return if (name == <%- project.name -%>Module.NAME) { + <%- project.name -%>Module(reactContext) + } else { + null + } + } + + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider { + return ReactModuleInfoProvider { + val moduleInfos: MutableMap = HashMap() + val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED + moduleInfos[<%- project.name -%>Module.NAME] = ReactModuleInfo( + <%- project.name -%>Module.NAME, + <%- project.name -%>Module.NAME, + false, // canOverrideExistingModule + false, // needsEagerInit + true, // hasConstants + false, // isCxxModule + isTurboModule // isTurboModule + ) + moduleInfos + } + } + + override fun createViewManagers(reactContext: ReactApplicationContext): List> { + return mutableListOf(<%- project.name -%>ViewManager()); + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt b/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt rename to packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}View.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt b/packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt rename to packages/create-react-native-library/templates/kotlin-view-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt deleted file mode 100644 index 9f3ab9124..000000000 --- a/packages/create-react-native-library/templates/kotlin-view-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.<%- project.package %> - -import com.facebook.react.ReactPackage -import com.facebook.react.bridge.NativeModule -import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.uimanager.ViewManager -import java.util.ArrayList - -class <%- project.name -%>ViewPackage : ReactPackage { - override fun createViewManagers(reactContext: ReactApplicationContext): List> { - val viewManagers: MutableList> = ArrayList() - viewManagers.add(<%- project.name -%>ViewManager()) - return viewManagers - } - - override fun createNativeModules(reactContext: ReactApplicationContext): List { - return emptyList() - } -} diff --git a/packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt deleted file mode 100644 index 9f3ab9124..000000000 --- a/packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.<%- project.package %> - -import com.facebook.react.ReactPackage -import com.facebook.react.bridge.NativeModule -import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.uimanager.ViewManager -import java.util.ArrayList - -class <%- project.name -%>ViewPackage : ReactPackage { - override fun createViewManagers(reactContext: ReactApplicationContext): List> { - val viewManagers: MutableList> = ArrayList() - viewManagers.add(<%- project.name -%>ViewManager()) - return viewManagers - } - - override fun createNativeModules(reactContext: ReactApplicationContext): List { - return emptyList() - } -} diff --git a/packages/create-react-native-library/templates/native-common/android/build.gradle b/packages/create-react-native-library/templates/native-common/android/build.gradle index e0e3a6428..fb68eaefe 100644 --- a/packages/create-react-native-library/templates/native-common/android/build.gradle +++ b/packages/create-react-native-library/templates/native-common/android/build.gradle @@ -1,9 +1,7 @@ buildscript { -<% if (project.kotlin) { -%> // Buildscript is evaluated before everything else so we can't use getExtOrDefault def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["<%- project.name -%>_kotlinVersion"] -<% } -%> repositories { google() mavenCentral() @@ -11,10 +9,8 @@ buildscript { dependencies { classpath "com.android.tools.build:gradle:7.2.1" -<% if (project.kotlin) { -%> // noinspection DifferentKotlinGradleVersion classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" -<% } -%> } } @@ -28,9 +24,7 @@ def isNewArchitectureEnabled() { } apply plugin: "com.android.library" -<% if (project.kotlin) { -%> apply plugin: "kotlin-android" -<% } -%> if (isNewArchitectureEnabled()) { apply plugin: "com.facebook.react" @@ -149,18 +143,14 @@ repositories { google() } -<% if (project.kotlin) { -%> def kotlin_version = getExtOrDefault("kotlinVersion") -<% } -%> dependencies { // For < 0.71, this will be from the local maven repo // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" -<% if (project.kotlin) { -%> implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" -<% } -%> } <% if (project.arch === "new" || project.arch === "mixed") { -%> diff --git a/packages/create-react-native-library/templates/native-view-library-legacy/src/index.tsx b/packages/create-react-native-library/templates/native-view-library-legacy/src/index.tsx new file mode 100644 index 000000000..0619f4994 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-library-legacy/src/index.tsx @@ -0,0 +1,7 @@ +import { <%- project.name -%> } from './<%- project.name -%>'; + +export function multiply(a: number, b: number): Promise { + return <%- project.name -%>.multiply(a, b); +} + +export * from './<%- project.name -%>View'; diff --git a/packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}.ts b/packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}.ts new file mode 100644 index 000000000..220e70cd2 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}.ts @@ -0,0 +1,18 @@ +import { NativeModules, Platform } from 'react-native'; + +const LINKING_ERROR = + `The package '<%- project.slug -%>' doesn't seem to be linked. Make sure: \n\n` + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' + + '- You are not using Expo Go\n'; + +export const <%- project.name -%> = NativeModules.<%- project.name %> + ? NativeModules.<%- project.name %> + : new Proxy( + {}, + { + get() { + throw new Error(LINKING_ERROR); + }, + } + ); diff --git a/packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}View.ts b/packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}View.ts new file mode 100644 index 000000000..055016821 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-library-legacy/src/{%- project.name %}View.ts @@ -0,0 +1,26 @@ +import { + requireNativeComponent, + UIManager, + Platform, + type ViewStyle, +} from 'react-native'; + +const LINKING_ERROR = + `The package '<%- project.slug -%>' doesn't seem to be linked. Make sure: \n\n` + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' + + '- You are not using Expo Go\n'; + +type <%- project.name -%>Props = { + color: string; + style: ViewStyle; +}; + +const ComponentName = '<%- project.name -%>View'; + +export const <%- project.name -%>View = + UIManager.getViewManagerConfig(ComponentName) != null + ? requireNativeComponent<<%- project.name -%>Props>(ComponentName) + : () => { + throw new Error(LINKING_ERROR); + }; diff --git a/packages/create-react-native-library/templates/native-view-library-mixed/src/Native{%- project.name %}.ts b/packages/create-react-native-library/templates/native-view-library-mixed/src/Native{%- project.name %}.ts new file mode 100644 index 000000000..166b44ed7 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-library-mixed/src/Native{%- project.name %}.ts @@ -0,0 +1,8 @@ +import type { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; + +export interface Spec extends TurboModule { + multiply(a: number, b: number): Promise; +} + +export default TurboModuleRegistry.getEnforcing('<%- project.name -%>'); diff --git a/packages/create-react-native-library/templates/native-view-library-mixed/src/index.tsx b/packages/create-react-native-library/templates/native-view-library-mixed/src/index.tsx new file mode 100644 index 000000000..d4979707a --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-library-mixed/src/index.tsx @@ -0,0 +1,32 @@ +import { NativeModules, Platform } from 'react-native'; + +const LINKING_ERROR = + `The package '<%- project.slug -%>' doesn't seem to be linked. Make sure: \n\n` + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' + + '- You are not using Expo Go\n'; + +// @ts-expect-error +const isTurboModuleEnabled = global.__turboModuleProxy != null; + +const <%- project.name -%>Module = isTurboModuleEnabled + ? require('./Native<%- project.name -%>').default + : NativeModules.<%- project.name -%>; + +const <%- project.name -%> = <%- project.name -%>Module + ? <%- project.name -%>Module + : new Proxy( + {}, + { + get() { + throw new Error(LINKING_ERROR); + }, + } + ); + +export function multiply(a: number, b: number): Promise { + return <%- project.name -%>.multiply(a, b); +} + +export { default as <%- project.name -%>View } from './<%- project.name -%>NativeComponent'; +export * from './<%- project.name -%>NativeComponent'; diff --git a/packages/create-react-native-library/templates/native-view-mixed/src/{%- project.name %}ViewNativeComponent.ts b/packages/create-react-native-library/templates/native-view-library-mixed/src/{%- project.name %}NativeComponent.ts similarity index 100% rename from packages/create-react-native-library/templates/native-view-mixed/src/{%- project.name %}ViewNativeComponent.ts rename to packages/create-react-native-library/templates/native-view-library-mixed/src/{%- project.name %}NativeComponent.ts diff --git a/packages/create-react-native-library/templates/native-view-library-new/src/Native{%- project.name %}.ts b/packages/create-react-native-library/templates/native-view-library-new/src/Native{%- project.name %}.ts new file mode 100644 index 000000000..b6f84feb8 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-library-new/src/Native{%- project.name %}.ts @@ -0,0 +1,8 @@ +import type { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; + +export interface Spec extends TurboModule { + multiply(a: number, b: number): number; +} + +export default TurboModuleRegistry.getEnforcing('<%- project.name -%>'); diff --git a/packages/create-react-native-library/templates/native-view-library-new/src/index.tsx b/packages/create-react-native-library/templates/native-view-library-new/src/index.tsx new file mode 100644 index 000000000..d98e51a06 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-library-new/src/index.tsx @@ -0,0 +1,8 @@ +const <%- project.name -%> = require('./Native<%- project.name -%>').default; + +export function multiply(a: number, b: number): number { + return <%- project.name -%>.multiply(a, b); +} + +export { default as <%- project.name -%>View } from './<%- project.name -%>NativeComponent'; +export * from './<%- project.name -%>NativeComponent'; diff --git a/packages/create-react-native-library/templates/native-view-new/src/{%- project.name %}ViewNativeComponent.ts b/packages/create-react-native-library/templates/native-view-library-new/src/{%- project.name %}NativeComponent.ts similarity index 100% rename from packages/create-react-native-library/templates/native-view-new/src/{%- project.name %}ViewNativeComponent.ts rename to packages/create-react-native-library/templates/native-view-library-new/src/{%- project.name %}NativeComponent.ts diff --git a/packages/create-react-native-library/templates/native-view-mixed/src/index.tsx b/packages/create-react-native-library/templates/native-view-mixed/src/index.tsx deleted file mode 100644 index 7a2bfbdc2..000000000 --- a/packages/create-react-native-library/templates/native-view-mixed/src/index.tsx +++ /dev/null @@ -1,2 +0,0 @@ -export { default as <%- project.name -%>View } from './<%- project.name -%>ViewNativeComponent'; -export * from './<%- project.name -%>ViewNativeComponent'; diff --git a/packages/create-react-native-library/templates/native-view-new/src/index.tsx b/packages/create-react-native-library/templates/native-view-new/src/index.tsx deleted file mode 100644 index 7a2bfbdc2..000000000 --- a/packages/create-react-native-library/templates/native-view-new/src/index.tsx +++ /dev/null @@ -1,2 +0,0 @@ -export { default as <%- project.name -%>View } from './<%- project.name -%>ViewNativeComponent'; -export * from './<%- project.name -%>ViewNativeComponent'; diff --git a/packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.h b/packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.h new file mode 100644 index 000000000..99f7c846b --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.h @@ -0,0 +1,6 @@ +#import + +@interface <%- project.name -%> : NSObject +#endif + +@end diff --git a/packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.mm b/packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.mm new file mode 100644 index 000000000..5289baf7b --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}.mm @@ -0,0 +1,18 @@ +#import "<%- project.name -%>.h" + +@implementation <%- project.name -%> + +RCT_EXPORT_MODULE() + +// Example method +// See // https://reactnative.dev/docs/native-modules-ios +RCT_EXPORT_METHOD(multiply:(double)a + b:(double)b + resolve:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) +{ + NSNumber *result = @(a * b); + resolve(result); +} + +@end diff --git a/packages/create-react-native-library/templates/objc-view-legacy/ios/{%- project.name %}ViewManager.m b/packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}ViewManager.m similarity index 100% rename from packages/create-react-native-library/templates/objc-view-legacy/ios/{%- project.name %}ViewManager.m rename to packages/create-react-native-library/templates/objc-view-library-legacy/ios/{%- project.name %}ViewManager.m diff --git a/packages/create-react-native-library/templates/objc-view-mixed/ios/Utils.h b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/Utils.h similarity index 100% rename from packages/create-react-native-library/templates/objc-view-mixed/ios/Utils.h rename to packages/create-react-native-library/templates/objc-view-library-mixed/ios/Utils.h diff --git a/packages/create-react-native-library/templates/objc-view-mixed/ios/Utils.m b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/Utils.m similarity index 100% rename from packages/create-react-native-library/templates/objc-view-mixed/ios/Utils.m rename to packages/create-react-native-library/templates/objc-view-library-mixed/ios/Utils.m diff --git a/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.h b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.h new file mode 100644 index 000000000..32ed3c4b5 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.h @@ -0,0 +1,17 @@ +<% if (project.cpp) { -%> +#ifdef __cplusplus +#import "<%- project.identifier -%>.h" +#endif +<% } -%> + +#ifdef RCT_NEW_ARCH_ENABLED +#import "RN<%- project.name -%>Spec.h" + +@interface <%- project.name -%> : NSObject Spec> +#else +#import + +@interface <%- project.name -%> : NSObject +#endif + +@end diff --git a/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.mm b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.mm new file mode 100644 index 000000000..7650a9fe4 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}.mm @@ -0,0 +1,48 @@ +#import "<%- project.name -%>.h" + +@implementation <%- project.name -%> + +RCT_EXPORT_MODULE() + +<% if (project.arch === 'legacy' || project.arch === 'mixed') { -%> +// Example method +// See // https://reactnative.dev/docs/native-modules-ios +RCT_EXPORT_METHOD(multiply:(double)a + b:(double)b + resolve:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) +{ +<% if (project.cpp) { -%> + NSNumber *result = @(<%- project.package_cpp -%>::multiply(a, b)); +<% } else { -%> + NSNumber *result = @(a * b); +<% } -%> + + resolve(result); +} + +<% } -%> +<% if (project.arch === 'new' || project.arch === 'mixed') { -%> +// Don't compile this code when we build for the old architecture. +#ifdef RCT_NEW_ARCH_ENABLED +<% if (project.arch === 'new') { -%> +- (NSNumber *)multiply:(double)a b:(double)b { +<% if (project.cpp) { -%> + NSNumber *result = @(<%- project.package_cpp -%>::multiply(a, b)); +<% } else { -%> + NSNumber *result = @(a * b); +<% } -%> + + return result; +} + +<% } -%> +- (std::shared_ptr)getTurboModule: + (const facebook::react::ObjCTurboModule::InitParams &)params +{ + return std::make_sharedSpecJSI>(params); +} +#endif +<% } -%> + +@end diff --git a/packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}View.h b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}View.h similarity index 100% rename from packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}View.h rename to packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}View.h diff --git a/packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}View.mm b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}View.mm similarity index 81% rename from packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}View.mm rename to packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}View.mm index ff8ae754c..fb815ff30 100644 --- a/packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}View.mm +++ b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}View.mm @@ -1,10 +1,10 @@ #ifdef RCT_NEW_ARCH_ENABLED #import "<%- project.name -%>View.h" -#import ViewSpec/ComponentDescriptors.h> -#import ViewSpec/EventEmitters.h> -#import ViewSpec/Props.h> -#import ViewSpec/RCTComponentViewHelpers.h> +#import Spec/ComponentDescriptors.h> +#import Spec/EventEmitters.h> +#import Spec/Props.h> +#import Spec/RCTComponentViewHelpers.h> #import "RCTFabricComponentsPlugins.h" #import "Utils.h" diff --git a/packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}ViewManager.mm b/packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}ViewManager.mm similarity index 100% rename from packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}ViewManager.mm rename to packages/create-react-native-library/templates/objc-view-library-mixed/ios/{%- project.name %}ViewManager.mm diff --git a/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.h b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.h new file mode 100644 index 000000000..32ed3c4b5 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.h @@ -0,0 +1,17 @@ +<% if (project.cpp) { -%> +#ifdef __cplusplus +#import "<%- project.identifier -%>.h" +#endif +<% } -%> + +#ifdef RCT_NEW_ARCH_ENABLED +#import "RN<%- project.name -%>Spec.h" + +@interface <%- project.name -%> : NSObject Spec> +#else +#import + +@interface <%- project.name -%> : NSObject +#endif + +@end diff --git a/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.mm b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.mm new file mode 100644 index 000000000..7650a9fe4 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}.mm @@ -0,0 +1,48 @@ +#import "<%- project.name -%>.h" + +@implementation <%- project.name -%> + +RCT_EXPORT_MODULE() + +<% if (project.arch === 'legacy' || project.arch === 'mixed') { -%> +// Example method +// See // https://reactnative.dev/docs/native-modules-ios +RCT_EXPORT_METHOD(multiply:(double)a + b:(double)b + resolve:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) +{ +<% if (project.cpp) { -%> + NSNumber *result = @(<%- project.package_cpp -%>::multiply(a, b)); +<% } else { -%> + NSNumber *result = @(a * b); +<% } -%> + + resolve(result); +} + +<% } -%> +<% if (project.arch === 'new' || project.arch === 'mixed') { -%> +// Don't compile this code when we build for the old architecture. +#ifdef RCT_NEW_ARCH_ENABLED +<% if (project.arch === 'new') { -%> +- (NSNumber *)multiply:(double)a b:(double)b { +<% if (project.cpp) { -%> + NSNumber *result = @(<%- project.package_cpp -%>::multiply(a, b)); +<% } else { -%> + NSNumber *result = @(a * b); +<% } -%> + + return result; +} + +<% } -%> +- (std::shared_ptr)getTurboModule: + (const facebook::react::ObjCTurboModule::InitParams &)params +{ + return std::make_sharedSpecJSI>(params); +} +#endif +<% } -%> + +@end diff --git a/packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}View.h b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}View.h similarity index 100% rename from packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}View.h rename to packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}View.h diff --git a/packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}View.mm b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}View.mm similarity index 84% rename from packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}View.mm rename to packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}View.mm index 46e92936f..0b73c7b1d 100644 --- a/packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}View.mm +++ b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}View.mm @@ -1,9 +1,9 @@ #import "<%- project.name -%>View.h" -#import ViewSpec/ComponentDescriptors.h> -#import ViewSpec/EventEmitters.h> -#import ViewSpec/Props.h> -#import ViewSpec/RCTComponentViewHelpers.h> +#import Spec/ComponentDescriptors.h> +#import Spec/EventEmitters.h> +#import Spec/Props.h> +#import Spec/RCTComponentViewHelpers.h> #import "RCTFabricComponentsPlugins.h" diff --git a/packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}ViewManager.mm b/packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}ViewManager.mm similarity index 100% rename from packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}ViewManager.mm rename to packages/create-react-native-library/templates/objc-view-library-new/ios/{%- project.name %}ViewManager.mm