Skip to content

Commit

Permalink
chore(rnmbxcodegen): add codegen to generate rn boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas committed Jun 27, 2024
1 parent 54ae81d commit 1c7f310
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 38 deletions.
14 changes: 13 additions & 1 deletion ios/RNMBX/RNMBXLocation.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/***
to: ios/rnmbx/RNMBXLocation.swift
userEditable: true
***/

@objc(RNMBXLocation)
open class RNMBXLocation : RNMBXMapComponentBase {
}


@objc
public static func someMethod(_ view: RNMBXLocation, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
// TODO implement
}

}
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"@sinonjs/fake-timers": "^8.0.1",
"@testing-library/react-native": "^12.4.0",
"@types/debounce": "^1.2.1",
"@types/ejs": "^3.1.5",
"@types/mapbox-gl": "^2.7.5",
"@typescript-eslint/eslint-plugin": "^5.37.0",
"@typescript-eslint/parser": "^5.37.0",
Expand All @@ -105,6 +106,7 @@
"eslint-plugin-jest": "^27.0.1",
"expo": "^47.0.0",
"expo-module-scripts": "^3.0.4",
"gray-matter": "^4.0.2",
"husky": "^8.0.1",
"jest": "29.7.0",
"jest-cli": "29.7.0",
Expand All @@ -115,17 +117,21 @@
"prettier": "2.7.1",
"react": "18.2.0",
"react-docgen": "rnmapbox/react-docgen#rnmapbox-dist-react-docgen-v6",
"react-native": "0.73.0-rc.4",
"react-native": "0.74.2",
"react-native-builder-bob": "^0.23.1",
"react-test-renderer": "18.2.0",
"ts-node": "10.9.1",
"typescript": "5.1.3",
"@mdx-js/mdx": "^3.0.0"
"typescript": "5.1.3"
},
"codegenConfig": {
"name": "rnmapbox_maps_specs",
"type": "all",
"jsSrcsDir": "src/specs",
"includesGeneratedCode": true,
"outputDir": {
"android": "android/src/main/codegen",
"ios": "ios/codegen"
},
"android": {
"javaPackageName": "com.rnmapbox.rnmbx"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/***
to: ios/rnmbx/generated/<%= Name %>ComponentView.h
***/
#ifdef RCT_NEW_ARCH_ENABLED

#import <UIKit/UIKit.h>
Expand All @@ -7,8 +10,7 @@

NS_ASSUME_NONNULL_BEGIN


@interface RNMBXLocationComponentView : RCTViewComponentView
@interface <%= Name %>ComponentView : RCTViewComponentView

@end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/***
to: ios/rnmbx/generated/<%= Name %>ComponentView.mm
***/
#ifdef RCT_NEW_ARCH_ENABLED

#import "RNMBXLocationComponentView.h"
#import "<%= Name %>ComponentView.h"

#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
Expand All @@ -17,16 +20,16 @@
using namespace facebook::react;


@implementation RNMBXLocationComponentView {
RNMBXLocation *_view;
@implementation <%= Name %>ComponentView {
<%= Name %> *_view;
}

- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNMBXViewportProps>();
static const auto defaultProps = std::make_shared<const <%= Name %>Props>();
_props = defaultProps;
_view = [[RNMBXLocation alloc] init];
_view = [[<%= Name %> alloc] init];
[self prepareView];

self.contentView = _view;
Expand All @@ -45,7 +48,7 @@ - (void)prepareView
if (strongSelf != nullptr && strongSelf->_eventEmitter != nullptr) {
auto type = std::string([[event objectForKey:@"type"] UTF8String]);
auto payload = convertIdToFollyDynamic([event objectForKey:@"payload"]);
RNMBXLocationEventEmitter::OnStatusChanged event = {type, payload};
<%= Name %>EventEmitter::OnStatusChanged event = {type, payload};
strongSelf->_eventEmitter->onStatusChanged(event);
}
}];
Expand All @@ -56,13 +59,13 @@ - (void)prepareView

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RNMBXViewportComponentDescriptor>();
return concreteComponentDescriptorProvider<<%= Name %>ComponentDescriptor>();
}

- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{
const auto &oldViewProps = static_cast<const RNMBXLocationProps &>(*oldProps);
const auto &newViewProps = static_cast<const RNMBXLocationProps &>(*props);
const auto &oldViewProps = static_cast<const <%= Name %>Props &>(*oldProps);
const auto &newViewProps = static_cast<const <%= Name %>Props &>(*props);

if (!oldProps.get() || oldViewProps.transitionsToIdleUponUserInteraction != newViewProps.transitionsToIdleUponUserInteraction) {
_view.transitionsToIdleUponUserInteraction = convertDynamicToOptional_boolean(newViewProps.transitionsToIdleUponUserInteraction, @"transitionsToIdleUponUserInteraction");
Expand All @@ -76,9 +79,9 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
}
@end

Class<RCTComponentViewProtocol> RNMBXViewportCls(void)
Class<RCTComponentViewProtocol> <%= Name %>Cls(void)
{
return RNMBXLocationComponentView.class;
return <%= Name %>ComponentView.class;
}

#endif // RCT_NEW_ARCH_ENABLED
15 changes: 15 additions & 0 deletions scripts/rnmbxcodegen/component/ios/component.swift.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/***
to: ios/rnmbx/<%= Name %>.swift
userEditable: true
***/

@objc(<%= Name %>)
open class <%= Name %> : RNMBXMapComponentBase {

<% module.spec.properties.forEach(function (property) { %>
@objc
public static func <%= property.name %>(_ view: <%= ComponentName %>, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
// TODO implement
}
<% }) %>
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/***
to: ios/RNMBX/generated/<%= Name %>.h
***/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

Expand All @@ -7,9 +10,9 @@
#import <React/RCTBridge.h>
#endif

@interface RNMBXLocationComponentModule : NSObject
@interface <%= Name %> : NSObject
#ifdef RCT_NEW_ARCH_ENABLED
<NativeRNMBXLocationModuleSpec>
<Native<%= Name %>ModuleSpec>
#else
<RCTBridgeModule>
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/***
to: ios/RNMBX/generated/<%= Name %>.mm
***/
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import <React/RCTUIManagerUtils.h>

#import "RNMBXLocationComponentModule.h"
#import "<%= Name %>.h"

#import "<%= ComponentName %>ComponentView.h"
#ifdef RCT_NEW_ARCH_ENABLED
#import "RNMBXLocationComponentView.h"
#import "<%= ComponentName %>ComponentView.h"
#endif // RCT_NEW_ARCH_ENABLED

#import "rnmapbox_maps-Swift.pre.h"

@class RNMBXLocation;

@implementation RNMBXLocationComponentModule
@implementation <%= Name %>

RCT_EXPORT_MODULE();

Expand All @@ -32,39 +35,39 @@ - (dispatch_queue_t)methodQueue
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeRNMBXLocationModuleSpecJSI>(params);
return std::make_shared<facebook::react::Native<%= Name %>ModuleSpecJSI>(params);
}
#endif // RCT_NEW_ARCH_ENABLED

- (void)withLocation:(nonnull NSNumber*)viewRef block:(void (^)(RNMBXLocation *))block reject:(RCTPromiseRejectBlock)reject methodName:(NSString *)methodName
- (void)with<%= Name %>:(nonnull NSNumber*)viewRef block:(void (^)(<%= ComponentName %> *))block reject:(RCTPromiseRejectBlock)reject methodName:(NSString *)methodName
{
#ifdef RCT_NEW_ARCH_ENABLED
[self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) {
RNMBXLocationComponentView *componentView = [self.viewRegistry_DEPRECATED viewForReactTag:viewRef];
RNMBXLocation *view = componentView.contentView;
<%= ComponentName %>ComponentView *componentView = [self.viewRegistry_DEPRECATED viewForReactTag:viewRef];
<%= ComponentName %> *view = componentView.contentView;

#else
[self.bridge.uiManager
addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RNMBXLocation *view = [uiManager viewForReactTag:viewRef];
<%= ComponentName %> *view = [uiManager viewForReactTag:viewRef];
#endif // RCT_NEW_ARCH_ENABLED
if (view != nil) {
block(view);
block(view);
} else {
reject(methodName, [NSString stringWithFormat:@"Unknown reactTag: %@", viewRef], nil);
}
}];
}

#if false
RCT_EXPORT_METHOD(someMethod:(nonnull NSNumber *)viewRef
<% module.spec.properties.forEach(function (property) { %>
RCT_EXPORT_METHOD(<%= property.name %>:(nonnull NSNumber *)viewRef
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
[self withLocation:viewRef block:^(RNMBXCamera *view) {
[RNMBXLocationManager someMethod:view resolve:resolve reject:reject];
} reject:reject methodName:@"someMethod"];
[self with<%= Name %>:viewRef block:^(<%= ComponentName %> *view) {
[<%= ComponentName %> <%= property.name %>:view resolve:resolve reject:reject];
} reject:reject methodName:@"<%= property.name %>"];
}
#endif
<% }) %>

@end
Loading

0 comments on commit 1c7f310

Please sign in to comment.