From b805ce9d52b822e09bc5e8a2b15e5d16ae6d5e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Gu=CC=88ner?= Date: Wed, 12 Jun 2024 14:00:06 +0300 Subject: [PATCH] refactor: rename files --- .../create-react-native-library/src/index.ts | 88 ++++++++++--------- .../{%- project.name %}Module.kt | 25 ++++++ .../{%- project.name %}Package.kt | 17 ++++ .../{%- project.name %}Module.kt | 24 +++++ .../{%- project.name %}Package.kt | 35 ++++++++ .../src/newarch/{%- project.name %}Spec.kt | 7 ++ .../src/oldarch/{%- project.name %}Spec.kt | 11 +++ .../{%- project.name %}Module.kt | 23 +++++ .../{%- project.name %}Package.kt | 34 +++++++ .../{%- project.name %}Package.kt | 17 ++++ .../{%- project.name %}ViewManager.kt | 20 +++++ 11 files changed, 260 insertions(+), 41 deletions(-) create mode 100644 packages/create-react-native-library/templates/kotlin-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt create mode 100644 packages/create-react-native-library/templates/kotlin-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt create mode 100644 packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt create mode 100644 packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt create mode 100644 packages/create-react-native-library/templates/kotlin-library-mixed/android/src/newarch/{%- project.name %}Spec.kt create mode 100644 packages/create-react-native-library/templates/kotlin-library-mixed/android/src/oldarch/{%- project.name %}Spec.kt create mode 100644 packages/create-react-native-library/templates/kotlin-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt create mode 100644 packages/create-react-native-library/templates/kotlin-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt create mode 100644 packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt create mode 100644 packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt diff --git a/packages/create-react-native-library/src/index.ts b/packages/create-react-native-library/src/index.ts index af3a74d53..2aa0767bd 100644 --- a/packages/create-react-native-library/src/index.ts +++ b/packages/create-react-native-library/src/index.ts @@ -38,54 +38,70 @@ const NATIVE_COMMON_EXAMPLE_FILES = path.resolve( ); const NATIVE_FILES = { - view_module_legacy: path.resolve( + '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( + 'view-module-mixed': path.resolve( __dirname, '../templates/native-view-library-mixed' ), - view_module_new: path.resolve( + '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_module_legacy: path.resolve( + '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( + 'view-module-mixed': path.resolve( __dirname, '../templates/objc-view-library-mixed' ), - view_module_new: path.resolve( + 'view-module-new': path.resolve( __dirname, '../templates/objc-view-library-new' ), } as const; const KOTLIN_FILES = { - view_module_legacy: path.resolve( + 'view-module-legacy': path.resolve( __dirname, '../templates/kotlin-view-library-legacy' ), - view_module_mixed: path.resolve( + 'view-module-mixed': path.resolve( __dirname, '../templates/kotlin-view-library-mixed' ), - view_module_new: path.resolve( + 'view-module-new': path.resolve( __dirname, '../templates/kotlin-view-library-new' ), + 'view-legacy': path.resolve(__dirname, '../templates/kotlin-view-legacy'), + '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'), } 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 = @@ -549,9 +565,9 @@ async function create(argv: yargs.Arguments) { version = FALLBACK_BOB_VERSION; } - const moduleType = type.startsWith('view-module') - ? 'view_module' - : type.startsWith('view') + const moduleType = type.startsWith('view-module-') + ? 'view-module' + : type.startsWith('view-') ? 'view' : 'module'; const arch = @@ -603,8 +619,8 @@ async function create(argv: yargs.Arguments) { arch, cpp: languages === 'cpp', swift: languages === 'kotlin-swift', - view: moduleType === 'view' || moduleType === 'view_module', - module: moduleType === 'module' || moduleType === 'view_module', + view: moduleType === 'view' || moduleType === 'view-module', + module: moduleType === 'module' || moduleType === 'view-module', }, author: { name: authorName, @@ -688,7 +704,7 @@ async function create(argv: yargs.Arguments) { } } - if (languages === 'js') { + if (type === 'library') { await copyDir(JS_FILES, folder); await copyDir(EXPO_FILES, folder); } else { @@ -705,32 +721,22 @@ async function create(argv: yargs.Arguments) { await copyDir(NATIVE_COMMON_EXAMPLE_FILES, folder); } - if (arch === 'mixed') { - // View module doesn't have legacy or a new arch only version. - await copyDir(NATIVE_FILES['view_module_mixed'], folder); - await copyDir(OBJC_FILES['view_module_mixed'], folder); - await copyDir(KOTLIN_FILES['view_module_mixed'], folder); - } else { - await copyDir(NATIVE_FILES[`${moduleType}_${arch}`], folder); - - if (options.project.swift) { - await copyDir(SWIFT_FILES[`${moduleType}_legacy`], folder); - } else { - if (moduleType === 'module') { - await copyDir(OBJC_FILES[`${moduleType}_common`], folder); - } else { - await copyDir(OBJC_FILES[`view_${arch}`], folder); - } - } + await copyDir(NATIVE_FILES[type], folder); - const templateType = `${moduleType}_${arch}` as const; + if ( + type === 'view-legacy' || + (type === 'module-legacy' && options.project.swift) + ) { + await copyDir(SWIFT_FILES[type], folder); + } else { + await copyDir(OBJC_FILES[type], folder); + } - await copyDir(KOTLIN_FILES[templateType], folder); + await copyDir(KOTLIN_FILES[type], folder); - if (options.project.cpp) { - await copyDir(CPP_FILES, folder); - await fs.remove(path.join(folder, 'ios', `${options.project.name}.m`)); - } + if (options.project.cpp) { + await copyDir(CPP_FILES, folder); + await fs.remove(path.join(folder, 'ios', `${options.project.name}.m`)); } } diff --git a/packages/create-react-native-library/templates/kotlin-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt b/packages/create-react-native-library/templates/kotlin-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-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-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt new file mode 100644 index 000000000..d34649aee --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-library-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- 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 listOf(<%- project.name -%>Module(reactContext)) + } + + override fun createViewManagers(reactContext: ReactApplicationContext): List> { + return emptyList() + } +} diff --git a/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Module.kt b/packages/create-react-native-library/templates/kotlin-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-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-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt b/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt new file mode 100644 index 000000000..93368013b --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt @@ -0,0 +1,35 @@ +package com.<%- project.package %> + +import com.facebook.react.TurboReactPackage +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 java.util.HashMap + +class <%- project.name -%>Package : TurboReactPackage() { + 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 + } + } +} diff --git a/packages/create-react-native-library/templates/kotlin-library-mixed/android/src/newarch/{%- project.name %}Spec.kt b/packages/create-react-native-library/templates/kotlin-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-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-library-mixed/android/src/oldarch/{%- project.name %}Spec.kt b/packages/create-react-native-library/templates/kotlin-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-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-library-new/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 new file mode 100644 index 000000000..5da133031 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-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-library-new/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 new file mode 100644 index 000000000..599cf307c --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-library-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}Package.kt @@ -0,0 +1,34 @@ +package com.<%- project.package %> + +import com.facebook.react.TurboReactPackage +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 java.util.HashMap + +class <%- project.name -%>Package : TurboReactPackage() { + 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 + true // isTurboModule + ) + moduleInfos + } + } +} diff --git a/packages/create-react-native-library/templates/kotlin-view-legacy/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 new file mode 100644 index 000000000..67e778677 --- /dev/null +++ b/packages/create-react-native-library/templates/kotlin-view-legacy/android/src/main/java/com/{%- project.package_dir %}/{%- 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-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..f52229f97 --- /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)) + } +}