Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --swift language option #353

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/cli-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ ${postCreateInstructions(createOptions)}`);
command: '--platforms <platforms>',
description: 'Platforms the library module will be created for - comma separated',
default: 'ios,android',
}, {
command: '--swift',
description: 'EXPERIMENAL: Generate the iOS native module in Swift',
}, {
command: '--tvos-enabled',
description: 'Generate the module with tvOS build enabled (requires react-native-tvos fork, with minimum version of 0.60, and iOS platform to be enabled)',
Expand Down
3 changes: 3 additions & 0 deletions lib/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const generateWithNormalizedOptions = ({
authorEmail = DEFAULT_AUTHOR_EMAIL,
license = DEFAULT_LICENSE,
isView = false,
swift = false,
useAppleNetworking = false,
generateExample = DEFAULT_GENERATE_EXAMPLE,
exampleFileLinkage = false,
Expand Down Expand Up @@ -109,6 +110,7 @@ const generateWithNormalizedOptions = ({
object class name: ${objectClassName}
Android nativePackageId: ${nativePackageId}
platforms: ${platforms}
use swift [iOS]: ${swift}
Apple tvosEnabled: ${tvosEnabled}
authorName: ${authorName}
authorEmail: ${authorEmail}
Expand Down Expand Up @@ -170,6 +172,7 @@ const generateWithNormalizedOptions = ({
authorEmail,
license,
isView,
swift,
useAppleNetworking,
};

Expand Down
41 changes: 38 additions & 3 deletions templates/ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ end

`,
}, {
// header for module without view:
name: ({ objectClassName, isView }) => !isView && `${platform}/${objectClassName}.h`,
// header for Objective-C module without view:
name: ({ objectClassName, swift, isView }) => !swift && !isView && `${platform}/${objectClassName}.h`,
content: ({ objectClassName }) => `// ${objectClassName}.h

#import <React/RCTBridgeModule.h>
Expand All @@ -45,7 +45,7 @@ end
`,
}, {
// implementation of module without view:
name: ({ objectClassName, isView }) => !isView && `${platform}/${objectClassName}.m`,
name: ({ objectClassName, swift, isView }) => !swift && !isView && `${platform}/${objectClassName}.m`,
content: ({ objectClassName, useAppleNetworking }) => `// ${objectClassName}.m

#import "${objectClassName}.h"
Expand Down Expand Up @@ -77,6 +77,41 @@ RCT_EXPORT_METHOD(sampleMethod:(NSString *)stringArgument numberParameter:(nonnu

@end
`,
}, {
// bridge header for Swift module without view:
name: ({ objectClassName, swift, view }) => swift && !view && `${platform}/${objectClassName}-Bridging-Header.h`,
content: ({ objectClassName }) => `// ${platform}/${objectClassName}-Bridging-Header.h
#import <React/RCTBridgeModule.h>
`,
}, {
// bridge for Swift module without view:
name: ({ objectClassName, swift, view }) => swift && !view && `${platform}/${objectClassName}.m`,
content: ({ objectClassName, useAppleNetworking }) => `// ${objectClassName}.m

#import <React/RCTBridge.h>

@interface RCT_EXTERN_MODULE(${objectClassName}, NSObject)

RCT_EXTERN_METHOD(sampleMethod:(NSString *)stringArgument numberParameter:(nonnull NSNumber *)numberArgument callback:(RCTResponseSenderBlock)callback)

@end
`,
}, {
// implementation of Swift module without view:
name: ({ objectClassName, swift, view }) => swift && !view && `${platform}/${objectClassName}.swift`,
content: ({ objectClassName, useAppleNetworking }) => `// ${objectClassName}.swift

import Foundation

@objc(${objectClassName})
class ${objectClassName} : NSObject {
@objc(sampleMethod:numberParameter:callback:)
func sampleMethod(stringArgument: String, numberArgument: NSNumber, callback: RCTResponseSenderBlock) -> Void {
// TODO: Implement some actually useful functionality
callback([String(format: "numberArgument: %@ stringArgument: %@", numberArgument, stringArgument)])
}
}
`
}, {
// header for module with view:
name: ({ objectClassName, isView }) => isView && `${platform}/${objectClassName}.h`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Options:
--object-class-name [objectClassName] The name of the object class to be exported by both JavaScript and native code. Default: (name in PascalCase)
--native-package-id [nativePackageId] [Android] The native Java package identifier used for Android (default: \\"com.reactlibrary\\")
--platforms <platforms> Platforms the library module will be created for - comma separated (default: \\"ios,android\\")
--swift EXPERIMENAL: Generate the iOS native module in Swift
--tvos-enabled Generate the module with tvOS build enabled (requires react-native-tvos fork, with minimum version of 0.60, and iOS platform to be enabled)
--github-account [githubAccount] The github account where the library module is hosted (default: \\"github_account\\")
--author-name [authorName] The author's name (default: \\"Your Name\\")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Options:
--object-class-name [objectClassName] The name of the object class to be exported by both JavaScript and native code. Default: (name in PascalCase)
--native-package-id [nativePackageId] [Android] The native Java package identifier used for Android (default: \\"com.reactlibrary\\")
--platforms <platforms> Platforms the library module will be created for - comma separated (default: \\"ios,android\\")
--swift EXPERIMENAL: Generate the iOS native module in Swift
--tvos-enabled Generate the module with tvOS build enabled (requires react-native-tvos fork, with minimum version of 0.60, and iOS platform to be enabled)
--github-account [githubAccount] The github account where the library module is hosted (default: \\"github_account\\")
--author-name [authorName] The author's name (default: \\"Your Name\\")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Object {
"default": "ios,android",
"description": "Platforms the library module will be created for - comma separated",
},
Object {
"command": "--swift",
"description": "EXPERIMENAL: Generate the iOS native module in Swift",
},
Object {
"command": "--tvos-enabled",
"description": "Generate the module with tvOS build enabled (requires react-native-tvos fork, with minimum version of 0.60, and iOS platform to be enabled)",
Expand Down
Loading