diff --git a/packages/create-react-native-library/src/index.ts b/packages/create-react-native-library/src/index.ts index f80577d86..cfb766514 100644 --- a/packages/create-react-native-library/src/index.ts +++ b/packages/create-react-native-library/src/index.ts @@ -19,11 +19,7 @@ const COMMON_FILES = path.resolve(__dirname, '../templates/common'); const JS_FILES = path.resolve(__dirname, '../templates/js-library'); const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library'); const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library'); -const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example'); -const EXAMPLE_TURBO_FILES = path.resolve( - __dirname, - '../templates/example-turbo' -); +const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example-legacy'); const NATIVE_COMMON_FILES = path.resolve( __dirname, '../templates/native-common' @@ -31,33 +27,39 @@ const NATIVE_COMMON_FILES = path.resolve( const NATIVE_FILES = { module_legacy: path.resolve(__dirname, '../templates/native-library-legacy'), - module_turbo: path.resolve(__dirname, '../templates/native-library-turbo'), + module_new: path.resolve(__dirname, '../templates/native-library-new'), module_mixed: path.resolve(__dirname, '../templates/native-library-mixed'), - view: path.resolve(__dirname, '../templates/native-view-library'), + 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'), }; const JAVA_FILES = { module_legacy: path.resolve(__dirname, '../templates/java-library-legacy'), - module_turbo: path.resolve(__dirname, '../templates/java-library-turbo'), + module_new: path.resolve(__dirname, '../templates/java-library-new'), module_mixed: path.resolve(__dirname, '../templates/java-library-mixed'), - view: path.resolve(__dirname, '../templates/java-view-library'), + 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'), }; const OBJC_FILES = { module: path.resolve(__dirname, '../templates/objc-library'), - view: path.resolve(__dirname, '../templates/objc-view-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'), }; const KOTLIN_FILES = { module_legacy: path.resolve(__dirname, '../templates/kotlin-library-legacy'), - module_turbo: path.resolve(__dirname, '../templates/kotlin-library-turbo'), + module_new: path.resolve(__dirname, '../templates/kotlin-library-new'), module_mixed: path.resolve(__dirname, '../templates/kotlin-library-mixed'), - view: path.resolve(__dirname, '../templates/kotlin-view-library'), + view_legacy: path.resolve(__dirname, '../templates/kotlin-view-legacy'), }; const SWIFT_FILES = { - module: path.resolve(__dirname, '../templates/swift-library'), - view: path.resolve(__dirname, '../templates/swift-view-library'), + module: path.resolve(__dirname, '../templates/swift-library-legacy'), + view: path.resolve(__dirname, '../templates/swift-view-legacy'), }; type ArgName = @@ -84,7 +86,14 @@ type Answers = { | 'kotlin-objc' | 'kotlin-swift' | 'cpp'; - type?: 'module-legacy' | 'module-turbo' | 'module-mixed' | 'view' | 'library'; + type?: + | 'module-legacy' + | 'module-new' + | 'module-mixed' + | 'view' + | 'view-mixed' + | 'view-new' + | 'library'; example?: 'expo' | 'native'; }; @@ -131,6 +140,8 @@ const args: Record = { 'module-turbo', 'module-mixed', 'view', + 'view-mixed', + 'view-new', 'library', ], }, @@ -278,18 +289,31 @@ async function create(argv: yargs.Arguments) { }, { title: 'Turbo module (experimental)', - value: 'module-turbo', + value: 'module-new', }, { title: 'Native module', value: 'module-legacy', }, { title: 'Native view', value: 'view' }, + { + title: 'Native fabric view with backward compat (experimental)', + value: 'view-mixed', + }, + { + title: 'Native fabric view (experimental)', + value: 'view-new', + }, { title: 'JavaScript library', value: 'library' }, ], }, 'languages': { - type: (_, values) => (values.type !== 'library' ? 'select' : null), + type: (_, values) => + values.type === 'library' || + values.type === 'view-new' || + values.type === 'view-mixed' + ? null + : 'select', name: 'languages', message: 'Which languages do you want to use?', choices: (_, values) => { @@ -298,7 +322,7 @@ async function create(argv: yargs.Arguments) { { title: 'Kotlin & Objective-C', value: 'kotlin-objc' }, ]; - if (values.type !== 'module-turbo' && values.type !== 'module-mixed') { + if (values.type !== 'module-new' && values.type !== 'module-mixed') { languages.push( { title: 'Java & Swift', value: 'java-swift' }, { title: 'Kotlin & Swift', value: 'kotlin-swift' } @@ -376,17 +400,14 @@ async function create(argv: yargs.Arguments) { version = FALLBACK_BOB_VERSION; } - const moduleType = type === 'view' ? 'view' : 'module'; - + const moduleType = type.startsWith('view') ? 'view' : 'module'; const architecture = - type === 'module-turbo' - ? 'turbo' - : type === 'module-mixed' + type === 'module-new' || type === 'view-new' + ? 'new' + : type === 'module-mixed' || type === 'view-mixed' ? 'mixed' : 'legacy'; - const turbomodule = architecture === 'turbo' || architecture === 'mixed'; - const project = slug.replace(/^(react-native-|@[^/]+\/)/, ''); let namespace: string | undefined; @@ -424,11 +445,11 @@ async function create(argv: yargs.Arguments) { identifier: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''), native: languages !== 'js', architecture, - turbomodule, cpp: languages === 'cpp', kotlin: languages === 'kotlin-objc' || languages === 'kotlin-swift', swift: languages === 'java-swift' || languages === 'kotlin-swift', - view: type === 'view', + view: moduleType === 'view', + module: moduleType === 'module', }, author: { name: authorName, @@ -500,33 +521,45 @@ async function create(argv: yargs.Arguments) { path.join(folder, 'example') ); - if (turbomodule) { - await copyDir( - path.join(EXAMPLE_TURBO_FILES, 'example'), - path.join(folder, 'example') - ); - } - await copyDir(NATIVE_COMMON_FILES, folder); if (moduleType === 'module') { await copyDir(NATIVE_FILES[`${moduleType}_${architecture}`], folder); } else { - await copyDir(NATIVE_FILES[moduleType], folder); + await copyDir(NATIVE_FILES[`${moduleType}_${architecture}`], folder); } if (options.project.swift) { await copyDir(SWIFT_FILES[moduleType], folder); } else { - await copyDir(OBJC_FILES[moduleType], folder); + if (moduleType === 'module') { + await copyDir(OBJC_FILES[moduleType], folder); + } else { + await copyDir(OBJC_FILES[`view_${architecture}`], folder); + } } - const android_files = options.project.kotlin ? KOTLIN_FILES : JAVA_FILES; - - if (moduleType === 'module') { - await copyDir(android_files[`${moduleType}_${architecture}`], folder); + if (options.project.kotlin) { + switch (`${moduleType}_${architecture}`) { + case 'module_legacy': + await copyDir(KOTLIN_FILES['module_legacy'], folder); + break; + case 'module_mixed': + await copyDir(KOTLIN_FILES['module_mixed'], folder); + break; + case 'module_new': + await copyDir(KOTLIN_FILES['module_new'], folder); + break; + case 'view_legacy': + await copyDir(KOTLIN_FILES['view_legacy'], folder); + break; + default: + console.log( + `Kotlin template for ${moduleType}_${architecture} has not been implemented` + ); + } } else { - await copyDir(android_files[moduleType], folder); + await copyDir(JAVA_FILES[`${moduleType}_${architecture}`], folder); } if (options.project.cpp) { diff --git a/packages/create-react-native-library/src/utils/generateExampleApp.ts b/packages/create-react-native-library/src/utils/generateExampleApp.ts index 92f3b9dd9..9bc629028 100644 --- a/packages/create-react-native-library/src/utils/generateExampleApp.ts +++ b/packages/create-react-native-library/src/utils/generateExampleApp.ts @@ -40,7 +40,7 @@ export default async function generateExampleApp({ type: 'expo' | 'native'; dest: string; projectName: string; - architecture: 'turbo' | 'mixed' | 'legacy'; + architecture: 'new' | 'mixed' | 'legacy'; }) { const directory = path.join(dest, 'example'); const args = @@ -97,7 +97,7 @@ export default async function generateExampleApp({ // Temporary until autolinking is fixed on iOS // https://github.com/facebook/react-native/commit/a5622165c198ac6e7ffff5883d4f269a2c974f2e - if (architecture === 'turbo' || architecture === 'mixed') { + if (architecture === 'new' || architecture === 'mixed') { scripts.postinstall = 'patch-package'; Object.assign(devDependencies, PACKAGES_TO_ADD_NEW_ARCH); @@ -109,7 +109,7 @@ export default async function generateExampleApp({ ); // If the library is on new architecture, enable new arch for iOS and Android - if (architecture === 'turbo') { + if (architecture === 'new') { // Android // Change newArchEnabled=false to newArchEnabled=true in example/android/gradle.properties const gradleProperties = await fs.readFile( diff --git a/packages/create-react-native-library/templates/common/$package.json b/packages/create-react-native-library/templates/common/$package.json index d56f2d991..3d213cdf1 100644 --- a/packages/create-react-native-library/templates/common/$package.json +++ b/packages/create-react-native-library/templates/common/$package.json @@ -61,7 +61,7 @@ "@release-it/conventional-changelog": "^5.0.0", "@types/jest": "^28.1.2", "@types/react": "~17.0.21", - "@types/react-native": "0.68.0", + "@types/react-native": "0.70.0", "commitlint": "^17.0.2", "eslint": "^8.4.1", "eslint-config-prettier": "^8.5.0", @@ -70,7 +70,7 @@ "pod-install": "^0.1.0", "prettier": "^2.0.5", "react": "17.0.2", - "react-native": "0.68.2", + "react-native": "0.70.0", "react-native-builder-bob": "^<%- bob.version %>", "release-it": "^15.0.0", "typescript": "^4.5.2" @@ -154,11 +154,11 @@ } ] ] -<% if (project.turbomodule) { -%> +<% if (project.architecture === "new") { -%> }, "codegenConfig": { - "name": "RN<%- project.name -%>Spec", - "type": "modules", + "name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec", + "type": <%- 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 542bc32c8..2321846c5 100644 --- a/packages/create-react-native-library/templates/common/CONTRIBUTING.md +++ b/packages/create-react-native-library/templates/common/CONTRIBUTING.md @@ -51,7 +51,7 @@ By default, the example is configured to build with the old architecture. To run ``` <% } -%> -<% if (project.architecture === 'turbo' || project.architecture == 'mixed') { -%> +<% if (project.architecture === 'new' || project.architecture == 'mixed') { -%> To confirm that the app is running with the new architecture, you can check the Metro logs for a message like this: ```sh diff --git a/packages/create-react-native-library/templates/common/README.md b/packages/create-react-native-library/templates/common/README.md index 5c8789cb4..03f0a23d6 100644 --- a/packages/create-react-native-library/templates/common/README.md +++ b/packages/create-react-native-library/templates/common/README.md @@ -11,6 +11,7 @@ npm install <%- project.slug %> ## Usage <% if (project.view) { -%> + ```js import { <%- project.name -%>View } from "<%- project.slug -%>"; @@ -18,22 +19,27 @@ import { <%- project.name -%>View } from "<%- project.slug -%>"; <<%- project.name -%>View color="tomato" /> ``` -<% } else if (project.architecture == 'turbo') { -%> + +<% } else if (project.architecture === 'new' && project.module) { -%> + ```js -import { multiply } from "<%- project.slug -%>"; +import { multiply } from '<%- project.slug -%>'; // ... const result = multiply(3, 7); ``` + <% } else { -%> + ```js -import { multiply } from "<%- project.slug -%>"; +import { multiply } from '<%- project.slug -%>'; // ... const result = await multiply(3, 7); ``` + <% } -%> ## Contributing diff --git a/packages/create-react-native-library/templates/common/example/src/App.tsx b/packages/create-react-native-library/templates/common/example/src/App.tsx index 10cf7c7d5..be0e8a752 100644 --- a/packages/create-react-native-library/templates/common/example/src/App.tsx +++ b/packages/create-react-native-library/templates/common/example/src/App.tsx @@ -16,7 +16,7 @@ export default function App() { ); } -<% } else if (project.architecture == 'turbo') { -%> +<% } else if (project.architecture === 'new' && project.module) { -%> const result = multiply(3, 7); export default function App() { diff --git a/packages/create-react-native-library/templates/example/example/babel.config.js b/packages/create-react-native-library/templates/example-legacy/example/babel.config.js similarity index 100% rename from packages/create-react-native-library/templates/example/example/babel.config.js rename to packages/create-react-native-library/templates/example-legacy/example/babel.config.js diff --git a/packages/create-react-native-library/templates/example/example/index.js b/packages/create-react-native-library/templates/example-legacy/example/index.js similarity index 100% rename from packages/create-react-native-library/templates/example/example/index.js rename to packages/create-react-native-library/templates/example-legacy/example/index.js diff --git a/packages/create-react-native-library/templates/example/example/ios/File.swift b/packages/create-react-native-library/templates/example-legacy/example/ios/File.swift similarity index 100% rename from packages/create-react-native-library/templates/example/example/ios/File.swift rename to packages/create-react-native-library/templates/example-legacy/example/ios/File.swift diff --git a/packages/create-react-native-library/templates/example/example/ios/{%- project.name %}Example-Bridging-Header.h b/packages/create-react-native-library/templates/example-legacy/example/ios/{%- project.name %}Example-Bridging-Header.h similarity index 100% rename from packages/create-react-native-library/templates/example/example/ios/{%- project.name %}Example-Bridging-Header.h rename to packages/create-react-native-library/templates/example-legacy/example/ios/{%- project.name %}Example-Bridging-Header.h diff --git a/packages/create-react-native-library/templates/example-legacy/example/react-native.config.js b/packages/create-react-native-library/templates/example-legacy/example/react-native.config.js new file mode 100644 index 000000000..a5166956f --- /dev/null +++ b/packages/create-react-native-library/templates/example-legacy/example/react-native.config.js @@ -0,0 +1,10 @@ +const path = require('path'); +const pak = require('../package.json'); + +module.exports = { + dependencies: { + [pak.name]: { + root: path.join(__dirname, '..'), + }, + }, +}; diff --git a/packages/create-react-native-library/templates/example-turbo/example/patches/react-native+0.70.0.patch b/packages/create-react-native-library/templates/example-turbo/example/patches/react-native+0.70.0.patch deleted file mode 100644 index f0077bdde..000000000 --- a/packages/create-react-native-library/templates/example-turbo/example/patches/react-native+0.70.0.patch +++ /dev/null @@ -1,134 +0,0 @@ -diff --git a/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js b/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js -index 458495b..6fc398a 100644 ---- a/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js -+++ b/node_modules/react-native/scripts/codegen/generate-artifacts-executor.js -@@ -204,6 +204,55 @@ function handleThirdPartyLibraries( - }); - } - -+function handleLibrariesFromReactNativeConfig( -+ libraries, -+ codegenConfigKey, -+ codegenConfigFilename, -+ appRootDir, -+) { -+ const rnConfigFileName = 'react-native.config.js'; -+ -+ console.log( -+ `\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${rnConfigFileName}`, -+ ); -+ -+ const rnConfigFilePath = path.join(appRootDir, rnConfigFileName); -+ -+ if (fs.existsSync(rnConfigFilePath)) { -+ const rnConfig = require(rnConfigFilePath); -+ -+ if (rnConfig.dependencies != null) { -+ Object.keys(rnConfig.dependencies).forEach(name => { -+ const dependencyConfig = rnConfig.dependencies[name]; -+ -+ if (dependencyConfig.root) { -+ const codegenConfigFileDir = path.resolve( -+ appRootDir, -+ dependencyConfig.root, -+ ); -+ const configFilePath = path.join( -+ codegenConfigFileDir, -+ codegenConfigFilename, -+ ); -+ const pkgJsonPath = path.join(codegenConfigFileDir, 'package.json'); -+ -+ if (fs.existsSync(configFilePath)) { -+ const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath)); -+ const configFile = JSON.parse(fs.readFileSync(configFilePath)); -+ extractLibrariesFromJSON( -+ configFile, -+ libraries, -+ codegenConfigKey, -+ pkgJson.name, -+ codegenConfigFileDir, -+ ); -+ } -+ } -+ }); -+ } -+ } -+} -+ - function handleInAppLibraries( - libraries, - pkgJson, -@@ -362,6 +411,39 @@ function createComponentProvider( - } - } - -+function findCodegenEnabledLibraries( -+ appRootDir, -+ baseCodegenConfigFileDir, -+ codegenConfigFilename, -+ codegenConfigKey, -+) { -+ const pkgJson = readPackageJSON(appRootDir); -+ const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies}; -+ const libraries = []; -+ -+ handleReactNativeCodeLibraries( -+ libraries, -+ codegenConfigFilename, -+ codegenConfigKey, -+ ); -+ handleThirdPartyLibraries( -+ libraries, -+ baseCodegenConfigFileDir, -+ dependencies, -+ codegenConfigFilename, -+ codegenConfigKey, -+ ); -+ handleLibrariesFromReactNativeConfig( -+ libraries, -+ codegenConfigKey, -+ codegenConfigFilename, -+ appRootDir, -+ ); -+ handleInAppLibraries(libraries, pkgJson, codegenConfigKey, appRootDir); -+ -+ return libraries; -+} -+ - // It removes all the empty files and empty folders - // it finds, starting from `filepath`, recursively. - // -@@ -429,23 +511,12 @@ function execute( - } - - try { -- const pkgJson = readPackageJSON(appRootDir); -- const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies}; -- const libraries = []; -- -- handleReactNativeCodeLibraries( -- libraries, -- codegenConfigFilename, -- codegenConfigKey, -- ); -- handleThirdPartyLibraries( -- libraries, -+ const libraries = findCodegenEnabledLibraries( -+ appRootDir, - baseCodegenConfigFileDir, -- dependencies, - codegenConfigFilename, - codegenConfigKey, - ); -- handleInAppLibraries(libraries, pkgJson, codegenConfigKey, appRootDir); - - if (libraries.length === 0) { - console.log('[Codegen] No codegen-enabled libraries found.'); -@@ -482,6 +553,7 @@ module.exports = { - execute: execute, - // exported for testing purposes only: - _extractLibrariesFromJSON: extractLibrariesFromJSON, -+ _findCodegenEnabledLibraries: findCodegenEnabledLibraries, - _executeNodeScript: executeNodeScript, - _generateCode: generateCode, - _cleanupEmptyFilesAndFolders: cleanupEmptyFilesAndFolders, diff --git a/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.java b/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.java index b27f82559..0dedb3e08 100644 --- a/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.java +++ b/packages/create-react-native-library/templates/java-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.java @@ -2,7 +2,7 @@ /** * This is where the module implementation lives - * The exposed methods can be defined in the `turbo` and `legacy` folders + * The exposed methods can be defined in the `new` and `legacy` folders */ public class <%- project.name -%>ModuleImpl { public static final String NAME = "<%- project.name -%>"; diff --git a/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}Module.java b/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}Module.java new file mode 100644 index 000000000..e736f7bac --- /dev/null +++ b/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}Module.java @@ -0,0 +1,30 @@ +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; +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; + } + + // Example method + // See https://reactnative.dev/docs/native-modules-android + @Override + public double multiply(double a, double b) { + return a * b; + } +} diff --git a/packages/create-react-native-library/templates/java-library-turbo/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 %}/{%- project.name %}Package.java similarity index 100% rename from packages/create-react-native-library/templates/java-library-turbo/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java rename to packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}Package.java diff --git a/packages/create-react-native-library/templates/java-library-turbo/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 similarity index 100% rename from packages/create-react-native-library/templates/java-library-turbo/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java rename to packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.java 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 new file mode 100644 index 000000000..004811bda --- /dev/null +++ b/packages/create-react-native-library/templates/java-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java @@ -0,0 +1,43 @@ +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-library/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 %}/{%- project.name %}Package.java similarity index 100% rename from packages/create-react-native-library/templates/java-view-library/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java rename to packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}Package.java diff --git a/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManager.java b/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManager.java new file mode 100644 index 000000000..9fe212bc4 --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManager.java @@ -0,0 +1,31 @@ +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-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 new file mode 100644 index 000000000..b4f8be306 --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.java @@ -0,0 +1,22 @@ +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-library/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 similarity index 100% rename from packages/create-react-native-library/templates/java-view-library/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java rename to packages/create-react-native-library/templates/java-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.java diff --git a/packages/create-react-native-library/templates/java-view-mixed/android/src/legacy/{%- project.name %}ViewManager.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/legacy/{%- project.name %}ViewManager.java new file mode 100644 index 000000000..448c954db --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-mixed/android/src/legacy/{%- project.name %}ViewManager.java @@ -0,0 +1,38 @@ +package com.<%- project.package -%>; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.common.MapBuilder; +import com.facebook.react.uimanager.SimpleViewManager; +import com.facebook.react.uimanager.ThemedReactContext; +import com.facebook.react.uimanager.annotations.ReactProp; +import com.facebook.react.bridge.ReactApplicationContext; + +import java.util.Map; + + +public class <%- project.name -%>ViewManager extends SimpleViewManager<<%- project.name -%>View> { + + ReactApplicationContext mCallerContext; + + public <%- project.name -%>ViewManager(ReactApplicationContext reactContext) { + mCallerContext = reactContext; + } + + @Override + public String getName() { + return <%- project.name -%>ViewManagerImpl.NAME; + } + + @Override + public <%- project.name -%>View createViewInstance(ThemedReactContext context) { + return <%- project.name -%>ViewManagerImpl.createViewInstance(context); + } + + @ReactProp(name = "color") + public void setColor(<%- project.name -%>View view, String color) { + <%- project.name -%>ViewManagerImpl.setColor(view, color); + } +} diff --git a/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package %}/{%- project.name %}View.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package %}/{%- project.name %}View.java new file mode 100644 index 000000000..693bfd4af --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package %}/{%- project.name %}View.java @@ -0,0 +1,23 @@ +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 %}/{%- project.name %}ViewManagerImpl.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManagerImpl.java new file mode 100644 index 000000000..c7e07fbda --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManagerImpl.java @@ -0,0 +1,17 @@ +package com.<%- project.package -%>; + +import com.facebook.react.uimanager.ThemedReactContext; +import android.graphics.Color; + +public class <%- project.name -%>ViewManagerImpl { + + public static final String NAME = "<%- project.name -%>View"; + + public static <%- project.name -%>View createViewInstance(ThemedReactContext context) { + return new <%- project.name -%>View(context); + } + + public static void setColor(<%- project.name -%>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 %}/{%- project.name %}ViewPackage.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewPackage.java new file mode 100644 index 000000000..aadb187ab --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-mixed/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewPackage.java @@ -0,0 +1,25 @@ +<% /* 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(reactContext)); + 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/turbo/{%- project.name %}ViewManager.java b/packages/create-react-native-library/templates/java-view-mixed/android/src/turbo/{%- project.name %}ViewManager.java new file mode 100644 index 000000000..f5f500b95 --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-mixed/android/src/turbo/{%- project.name %}ViewManager.java @@ -0,0 +1,43 @@ +package com.<%- project.package -%>; + +import com.facebook.react.bridge.ReadableArray; +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.bridge.ReactApplicationContext; +import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerDelegate; +import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerInterface; + +import androidx.annotation.Nullable; + +@ReactModule(name = <%- project.name -%>ViewManagerImpl.NAME) +public class <%- project.name -%>ViewManager extends SimpleViewManager<<%- project.name -%>View> implements <%- project.name -%>ViewManagerInterface<<%- project.name -%>View> { + private final ViewManagerDelegate<<%- project.name -%>View> mDelegate; + + public <%- project.name -%>ViewManager(ReactApplicationContext context) { + mDelegate = new <%- project.name -%>ViewManagerDelegate(this); + } + + @Nullable + @Override + protected ViewManagerDelegate<<%- project.name -%>View> getDelegate() { + return mDelegate; + } + + @Override + public String getName() { + return <%- project.name -%>ViewManagerImpl.NAME; + } + + @Override + public <%- project.name -%>View createViewInstance(ThemedReactContext context) { + return <%- project.name -%>ViewManagerImpl.createViewInstance(context); + } + + @ReactProp(name = "color") + public void setColor(<%- project.name -%>View view, String color) { + <%- project.name -%>ViewManagerImpl.setColor(view, color); + } +} diff --git a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}View.java b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}View.java new file mode 100644 index 000000000..693bfd4af --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}View.java @@ -0,0 +1,23 @@ +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 %}/{%- project.name %}ViewManager.java b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManager.java new file mode 100644 index 000000000..f5f500b95 --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManager.java @@ -0,0 +1,43 @@ +package com.<%- project.package -%>; + +import com.facebook.react.bridge.ReadableArray; +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.bridge.ReactApplicationContext; +import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerDelegate; +import com.facebook.react.viewmanagers.<%- project.name -%>ViewManagerInterface; + +import androidx.annotation.Nullable; + +@ReactModule(name = <%- project.name -%>ViewManagerImpl.NAME) +public class <%- project.name -%>ViewManager extends SimpleViewManager<<%- project.name -%>View> implements <%- project.name -%>ViewManagerInterface<<%- project.name -%>View> { + private final ViewManagerDelegate<<%- project.name -%>View> mDelegate; + + public <%- project.name -%>ViewManager(ReactApplicationContext context) { + mDelegate = new <%- project.name -%>ViewManagerDelegate(this); + } + + @Nullable + @Override + protected ViewManagerDelegate<<%- project.name -%>View> getDelegate() { + return mDelegate; + } + + @Override + public String getName() { + return <%- project.name -%>ViewManagerImpl.NAME; + } + + @Override + public <%- project.name -%>View createViewInstance(ThemedReactContext context) { + return <%- project.name -%>ViewManagerImpl.createViewInstance(context); + } + + @ReactProp(name = "color") + public void setColor(<%- project.name -%>View view, String color) { + <%- project.name -%>ViewManagerImpl.setColor(view, color); + } +} diff --git a/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManagerImpl.java b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManagerImpl.java new file mode 100644 index 000000000..c7e07fbda --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManagerImpl.java @@ -0,0 +1,17 @@ +package com.<%- project.package -%>; + +import com.facebook.react.uimanager.ThemedReactContext; +import android.graphics.Color; + +public class <%- project.name -%>ViewManagerImpl { + + public static final String NAME = "<%- project.name -%>View"; + + public static <%- project.name -%>View createViewInstance(ThemedReactContext context) { + return new <%- project.name -%>View(context); + } + + public static 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 %}/{%- project.name %}ViewPackage.java b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewPackage.java new file mode 100644 index 000000000..aadb187ab --- /dev/null +++ b/packages/create-react-native-library/templates/java-view-new/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewPackage.java @@ -0,0 +1,25 @@ +<% /* 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(reactContext)); + return viewManagers; + } + + @Override + public List createNativeModules(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } +} diff --git a/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.kt b/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.kt index 310c8a6f9..695aac44e 100644 --- a/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.kt +++ b/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ModuleImpl.kt @@ -4,7 +4,7 @@ import com.facebook.react.bridge.Promise /** * This is where the module implementation lives - * The exposed methods can be defined in the `turbo` and `legacy` folders + * The exposed methods can be defined in the `new` and `legacy` folders */ object <%- project.name -%>ModuleImpl { const val NAME = "<%- project.name -%>" diff --git a/packages/create-react-native-library/templates/kotlin-library-turbo/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt b/packages/create-react-native-library/templates/kotlin-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-library-turbo/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt rename to packages/create-react-native-library/templates/kotlin-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt diff --git a/packages/create-react-native-library/templates/kotlin-library-turbo/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-library-turbo/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt rename to packages/create-react-native-library/templates/kotlin-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}Package.kt new file mode 100644 index 000000000..be91f7679 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}Package.kt @@ -0,0 +1,17 @@ +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 + + +class <%- project.name -%>Package : ReactPackage { + override fun createNativeModules(reactContext: ReactApplicationContext): List { + return emptyList() + } + + override fun createViewManagers(reactContext: ReactApplicationContext): List> { + return listOf(<%- project.name -%>ViewManager()) + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-library/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt b/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManager.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-library/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt rename to packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}ViewManager.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-library/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt similarity index 100% rename from packages/create-react-native-library/templates/kotlin-view-library/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt rename to packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt diff --git a/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt b/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt new file mode 100644 index 000000000..c2222db91 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-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/native-common/android/build.gradle b/packages/create-react-native-library/templates/native-common/android/build.gradle index 1e42d9de3..52379962e 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 @@ -80,12 +80,12 @@ android { targetCompatibility JavaVersion.VERSION_1_8 } -<% if (project.architecture == 'turbo') { -%> +<% if (project.architecture == 'new') { -%> sourceSets { main { if (isNewArchitectureEnabled()) { java.srcDirs += [ - // This is needed to build Kotlin project with TurboModules enabled + // This is needed to build Kotlin project with NewArch enabled "${project.buildDir}/generated/source/codegen/java" ] } @@ -97,7 +97,7 @@ android { if (isNewArchitectureEnabled()) { java.srcDirs += [ "src/turbo", - // This is needed to build Kotlin project with TurboModules enabled + // This is needed to build Kotlin project with NewArch enabled "${project.buildDir}/generated/source/codegen/java" ] } else { @@ -193,7 +193,7 @@ dependencies { if (isNewArchitectureEnabled()) { react { jsRootDir = file("../src/") - libraryName = "<%- project.name -%>" + libraryName = "<%- project.view ? project.name+'View' : project.name -%>" codegenJavaPackageName = "com.<%- project.package -%>" } } diff --git a/packages/create-react-native-library/templates/native-common/{%- project.identifier %}.podspec b/packages/create-react-native-library/templates/native-common/{%- project.identifier %}.podspec index 88d919b64..d5d223289 100644 --- a/packages/create-react-native-library/templates/native-common/{%- project.identifier %}.podspec +++ b/packages/create-react-native-library/templates/native-common/{%- project.identifier %}.podspec @@ -11,7 +11,7 @@ Pod::Spec.new do |s| s.license = package["license"] s.authors = package["author"] - s.platforms = { :ios => "10.0" } + s.platforms = { :ios => <%- project.architecture === "new" ? '"11.0"': '"10.0"' -%> } s.source = { :git => "<%- repo -%>.git", :tag => "#{s.version}" } <% if (project.cpp) { -%> @@ -29,9 +29,12 @@ Pod::Spec.new do |s| s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1" s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"", + "OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1", "CLANG_CXX_LANGUAGE_STANDARD" => "c++17" } - +<% if(project.architecture === "new" && project.view) { -%> + s.dependency "React-RCTFabric" +<% } -%> s.dependency "React-Codegen" s.dependency "RCT-Folly" s.dependency "RCTRequired" diff --git a/packages/create-react-native-library/templates/native-library-turbo/src/Native{%- project.name %}.ts b/packages/create-react-native-library/templates/native-library-new/src/Native{%- project.name %}.ts similarity index 100% rename from packages/create-react-native-library/templates/native-library-turbo/src/Native{%- project.name %}.ts rename to packages/create-react-native-library/templates/native-library-new/src/Native{%- project.name %}.ts diff --git a/packages/create-react-native-library/templates/native-library-turbo/src/index.tsx b/packages/create-react-native-library/templates/native-library-new/src/index.tsx similarity index 100% rename from packages/create-react-native-library/templates/native-library-turbo/src/index.tsx rename to packages/create-react-native-library/templates/native-library-new/src/index.tsx diff --git a/packages/create-react-native-library/templates/native-view-library/src/index.tsx b/packages/create-react-native-library/templates/native-view-legacy/src/index.tsx similarity index 100% rename from packages/create-react-native-library/templates/native-view-library/src/index.tsx rename to packages/create-react-native-library/templates/native-view-legacy/src/index.tsx 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 new file mode 100644 index 000000000..1807b66d0 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-mixed/src/index.tsx @@ -0,0 +1,2 @@ +export { default as <%- project.name -%>View } from './<%- project.name -%>ViewNativeComponent'; +export * from './<%- project.name -%>ViewNativeComponent'; \ No newline at end of file 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-mixed/src/{%- project.name %}ViewNativeComponent.ts new file mode 100644 index 000000000..db1ff171e --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-mixed/src/{%- project.name %}ViewNativeComponent.ts @@ -0,0 +1,8 @@ +import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent'; +import type { ViewProps } from 'react-native'; + +interface NativeProps extends ViewProps { + color?: string; +} + +export default codegenNativeComponent('<%- project.name -%>View'); 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 new file mode 100644 index 000000000..1807b66d0 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-new/src/index.tsx @@ -0,0 +1,2 @@ +export { default as <%- project.name -%>View } from './<%- project.name -%>ViewNativeComponent'; +export * from './<%- project.name -%>ViewNativeComponent'; \ No newline at end of file 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-new/src/{%- project.name %}ViewNativeComponent.ts new file mode 100644 index 000000000..db1ff171e --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-new/src/{%- project.name %}ViewNativeComponent.ts @@ -0,0 +1,8 @@ +import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent'; +import type { ViewProps } from 'react-native'; + +interface NativeProps extends ViewProps { + color?: string; +} + +export default codegenNativeComponent('<%- project.name -%>View'); diff --git a/packages/create-react-native-library/templates/objc-library/ios/{%- project.name %}.mm b/packages/create-react-native-library/templates/objc-library/ios/{%- project.name %}.mm index 1bbff1f01..8f9ef5750 100644 --- a/packages/create-react-native-library/templates/objc-library/ios/{%- project.name %}.mm +++ b/packages/create-react-native-library/templates/objc-library/ios/{%- project.name %}.mm @@ -4,7 +4,7 @@ @implementation <%- project.name -%> RCT_EXPORT_MODULE() -<% if (project.architecture == 'turbo') { -%> +<% if (project.architecture === 'new') { -%> - (NSNumber *)multiply:(double)a b:(double)b { <% if (project.cpp) { -%> NSNumber *result = @(<%- project.package_cpp -%>::multiply(a, b)); diff --git a/packages/create-react-native-library/templates/objc-view-library/ios/{%- project.name %}ViewManager.m b/packages/create-react-native-library/templates/objc-view-legacy/ios/{%- project.name %}ViewManager.m similarity index 100% rename from packages/create-react-native-library/templates/objc-view-library/ios/{%- project.name %}ViewManager.m rename to packages/create-react-native-library/templates/objc-view-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-mixed/ios/Utils.h new file mode 100644 index 000000000..1be34dba4 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-mixed/ios/Utils.h @@ -0,0 +1,8 @@ +#ifndef Utils_h +#define Utils_h + +@interface Utils : NSObject ++ hexStringToColor:(NSString *)stringToConvert; +@end + +#endif /* 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-mixed/ios/Utils.m new file mode 100644 index 000000000..bc9f8b9bd --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-mixed/ios/Utils.m @@ -0,0 +1,26 @@ +#import +#import "Utils.h" +#import + +@implementation Utils + ++ hexStringToColor:(NSString *)stringToConvert +{ + NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""]; + NSScanner *stringScanner = [NSScanner scannerWithString:noHashString]; + + unsigned hex; + if (![stringScanner scanHexInt:&hex]) return nil; + int r = (hex >> 16) & 0xFF; + int g = (hex >> 8) & 0xFF; + int b = (hex) & 0xFF; + + return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f]; +} + ++ (id)alloc { + [NSException raise:@"Cannot be instantiated!" format:@"Static class 'Utils' cannot be instantiated!"]; + return nil; +} + +@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-mixed/ios/{%- project.name %}View.h new file mode 100644 index 000000000..0760d8a54 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}View.h @@ -0,0 +1,17 @@ +// This guard prevent this file to be compiled in the old architecture. +#ifdef RCT_NEW_ARCH_ENABLED +#import +#import + +#ifndef <%- project.name -%>ViewNativeComponent_h +#define <%- project.name -%>ViewNativeComponent_h + +NS_ASSUME_NONNULL_BEGIN + +@interface <%- project.name -%>View : RCTViewComponentView +@end + +NS_ASSUME_NONNULL_END + +#endif /* <%- project.name -%>ViewNativeComponent_h */ +#endif /* RCT_NEW_ARCH_ENABLED */ 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-mixed/ios/{%- project.name %}View.mm new file mode 100644 index 000000000..ff8ae754c --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}View.mm @@ -0,0 +1,60 @@ +#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 "RCTFabricComponentsPlugins.h" +#import "Utils.h" + +using namespace facebook::react; + +@interface <%- project.name -%>View () ViewViewProtocol> + +@end + +@implementation <%- project.name -%>View { + UIView * _view; +} + ++ (ComponentDescriptorProvider)componentDescriptorProvider +{ + return concreteComponentDescriptorProvider<<%- project.name -%>ViewComponentDescriptor>(); +} + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) { + static const auto defaultProps = std::make_sharedViewProps>(); + _props = defaultProps; + + _view = [[UIView alloc] init]; + + self.contentView = _view; + } + + return self; +} + +- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps +{ + const auto &oldViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(_props); + const auto &newViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(props); + + if (oldViewProps.color != newViewProps.color) { + NSString * colorToConvert = [[NSString alloc] initWithUTF8String: newViewProps.color.c_str()]; + [_view setBackgroundColor: [Utils hexStringToColor:colorToConvert]]; + } + + [super updateProps:props oldProps:oldProps]; +} + +Class <%- project.name -%>ViewCls(void) +{ + return <%- project.name -%>View.class; +} + +@end +#endif 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-mixed/ios/{%- project.name %}ViewManager.mm new file mode 100644 index 000000000..fc935bd80 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-mixed/ios/{%- project.name %}ViewManager.mm @@ -0,0 +1,23 @@ +#import +#import +#import "RCTBridge.h" +#import "Utils.h" + +@interface <%- project.name -%>ViewManager : RCTViewManager +@end + +@implementation <%- project.name -%>ViewManager + +RCT_EXPORT_MODULE(<%- project.name -%>View) + +- (UIView *)view +{ + return [[UIView alloc] init]; +} + +RCT_CUSTOM_VIEW_PROPERTY(color, NSString, UIView) +{ + [view setBackgroundColor: [Utils hexStringToColor:colorToConvert]]; +} + +@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-new/ios/{%- project.name %}View.h new file mode 100644 index 000000000..0760d8a54 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}View.h @@ -0,0 +1,17 @@ +// This guard prevent this file to be compiled in the old architecture. +#ifdef RCT_NEW_ARCH_ENABLED +#import +#import + +#ifndef <%- project.name -%>ViewNativeComponent_h +#define <%- project.name -%>ViewNativeComponent_h + +NS_ASSUME_NONNULL_BEGIN + +@interface <%- project.name -%>View : RCTViewComponentView +@end + +NS_ASSUME_NONNULL_END + +#endif /* <%- project.name -%>ViewNativeComponent_h */ +#endif /* RCT_NEW_ARCH_ENABLED */ 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-new/ios/{%- project.name %}View.mm new file mode 100644 index 000000000..46e92936f --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}View.mm @@ -0,0 +1,71 @@ +#import "<%- project.name -%>View.h" + +#import ViewSpec/ComponentDescriptors.h> +#import ViewSpec/EventEmitters.h> +#import ViewSpec/Props.h> +#import ViewSpec/RCTComponentViewHelpers.h> + +#import "RCTFabricComponentsPlugins.h" + +using namespace facebook::react; + +@interface <%- project.name -%>View () ViewViewProtocol> + +@end + +@implementation <%- project.name -%>View { + UIView * _view; +} + ++ (ComponentDescriptorProvider)componentDescriptorProvider +{ + return concreteComponentDescriptorProvider<<%- project.name -%>ViewComponentDescriptor>(); +} + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) { + static const auto defaultProps = std::make_sharedViewProps>(); + _props = defaultProps; + + _view = [[UIView alloc] init]; + + self.contentView = _view; + } + + return self; +} + +- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps +{ + const auto &oldViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(_props); + const auto &newViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(props); + + if (oldViewProps.color != newViewProps.color) { + NSString * colorToConvert = [[NSString alloc] initWithUTF8String: newViewProps.color.c_str()]; + [_view setBackgroundColor:[self hexStringToColor:colorToConvert]]; + } + + [super updateProps:props oldProps:oldProps]; +} + +Class <%- project.name -%>ViewCls(void) +{ + return <%- project.name -%>View.class; +} + +- hexStringToColor:(NSString *)stringToConvert +{ + NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""]; + NSScanner *stringScanner = [NSScanner scannerWithString:noHashString]; + + unsigned hex; + if (![stringScanner scanHexInt:&hex]) return nil; + int r = (hex >> 16) & 0xFF; + int g = (hex >> 8) & 0xFF; + int b = (hex) & 0xFF; + + return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f]; +} + +@end 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-new/ios/{%- project.name %}ViewManager.mm new file mode 100644 index 000000000..9cd397b97 --- /dev/null +++ b/packages/create-react-native-library/templates/objc-view-new/ios/{%- project.name %}ViewManager.mm @@ -0,0 +1,19 @@ +#import +#import +#import "RCTBridge.h" + +@interface <%- project.name -%>ViewManager : RCTViewManager +@end + +@implementation <%- project.name -%>ViewManager + +RCT_EXPORT_MODULE(<%- project.name -%>View) + +- (UIView *)view +{ + return [[UIView alloc] init]; +} + +RCT_EXPORT_VIEW_PROPERTY(color, NSString) + +@end diff --git a/packages/create-react-native-library/templates/swift-library/ios/{%- project.name %}-Bridging-Header.h b/packages/create-react-native-library/templates/swift-library-legacy/ios/{%- project.name %}-Bridging-Header.h similarity index 100% rename from packages/create-react-native-library/templates/swift-library/ios/{%- project.name %}-Bridging-Header.h rename to packages/create-react-native-library/templates/swift-library-legacy/ios/{%- project.name %}-Bridging-Header.h diff --git a/packages/create-react-native-library/templates/swift-library/ios/{%- project.name %}.m b/packages/create-react-native-library/templates/swift-library-legacy/ios/{%- project.name %}.m similarity index 100% rename from packages/create-react-native-library/templates/swift-library/ios/{%- project.name %}.m rename to packages/create-react-native-library/templates/swift-library-legacy/ios/{%- project.name %}.m diff --git a/packages/create-react-native-library/templates/swift-library/ios/{%- project.name %}.swift b/packages/create-react-native-library/templates/swift-library-legacy/ios/{%- project.name %}.swift similarity index 100% rename from packages/create-react-native-library/templates/swift-library/ios/{%- project.name %}.swift rename to packages/create-react-native-library/templates/swift-library-legacy/ios/{%- project.name %}.swift diff --git a/packages/create-react-native-library/templates/swift-view-library/ios/{%- project.name %}-Bridging-Header.h b/packages/create-react-native-library/templates/swift-view-legacy/ios/{%- project.name %}-Bridging-Header.h similarity index 100% rename from packages/create-react-native-library/templates/swift-view-library/ios/{%- project.name %}-Bridging-Header.h rename to packages/create-react-native-library/templates/swift-view-legacy/ios/{%- project.name %}-Bridging-Header.h diff --git a/packages/create-react-native-library/templates/swift-view-library/ios/{%- project.name %}ViewManager.m b/packages/create-react-native-library/templates/swift-view-legacy/ios/{%- project.name %}ViewManager.m similarity index 100% rename from packages/create-react-native-library/templates/swift-view-library/ios/{%- project.name %}ViewManager.m rename to packages/create-react-native-library/templates/swift-view-legacy/ios/{%- project.name %}ViewManager.m diff --git a/packages/create-react-native-library/templates/swift-view-library/ios/{%- project.name %}ViewManager.swift b/packages/create-react-native-library/templates/swift-view-legacy/ios/{%- project.name %}ViewManager.swift similarity index 100% rename from packages/create-react-native-library/templates/swift-view-library/ios/{%- project.name %}ViewManager.swift rename to packages/create-react-native-library/templates/swift-view-legacy/ios/{%- project.name %}ViewManager.swift