diff --git a/Assets/Tests/plugins/UIKitTransformPlugin.kt b/Assets/Tests/plugins/UIKitTransformPlugin.kt new file mode 100644 index 0000000..cf293be --- /dev/null +++ b/Assets/Tests/plugins/UIKitTransformPlugin.kt @@ -0,0 +1,20 @@ + +class ViewController: UIViewController { + @BindView() lateinit var view: View + @BindView() lateinit var label: TextView + @BindView() lateinit var textField: EditText + @BindView() lateinit var imageView: ImageView + @BindView() lateinit var button: Button + @BindView() lateinit var tableView: RecyclerView + @BindView() lateinit var stackView: LinearLayout + @BindView() lateinit var scrollView: ScrollView + @BindView() lateinit var aSwitch: Switch + + fun method(label: TextView) : View { + val view = View() + view.addSubview(view) + return view + } + + @OnClick() fun onTap() {} +} diff --git a/Assets/Tests/plugins/UIKitTransformPlugin.swift b/Assets/Tests/plugins/UIKitTransformPlugin.swift new file mode 100644 index 0000000..bfee53b --- /dev/null +++ b/Assets/Tests/plugins/UIKitTransformPlugin.swift @@ -0,0 +1,21 @@ + +class ViewController: UIViewController { + @IBOutlet weak var view: UIView! + @IBOutlet weak var label: UILabel! + @IBOutlet weak var textField: UITextField! + @IBOutlet weak var imageView: UIImageView! + @IBOutlet weak var button: UIButton! + @IBOutlet weak var tableView: UITableView! + @IBOutlet weak var stackView: UIStackView! + @IBOutlet weak var scrollView: UIScrollView! + @IBOutlet weak var aSwitch: UISwitch! + + func method(label: UILabel) -> UIView { + let view = UIView() + view.addSubview(view) + return view + } + + @IBAction func onTap() { + } +} diff --git a/Sources/SwiftKotlinApp/ViewController.swift b/Sources/SwiftKotlinApp/ViewController.swift index 93fc863..29fa16a 100644 --- a/Sources/SwiftKotlinApp/ViewController.swift +++ b/Sources/SwiftKotlinApp/ViewController.swift @@ -21,6 +21,7 @@ class ViewController: NSViewController { tokenTransformPlugins: [ XCTTestToJUnitTokenTransformPlugin(), FoundationMethodsTransformPlugin(), + UIKitTransformPlugin(), CommentsAdditionTransformPlugin() ] ) diff --git a/Sources/SwiftKotlinCommandLine/main.swift b/Sources/SwiftKotlinCommandLine/main.swift index 4d10b0d..0c7b616 100644 --- a/Sources/SwiftKotlinCommandLine/main.swift +++ b/Sources/SwiftKotlinCommandLine/main.swift @@ -13,10 +13,11 @@ let kotlinTokenizer = KotlinTokenizer( tokenTransformPlugins: [ XCTTestToJUnitTokenTransformPlugin(), FoundationMethodsTransformPlugin(), + UIKitTransformPlugin(), CommentsAdditionTransformPlugin() ] ) -let version = "0.2.4" +let version = "0.2.5" let arguments = [ "output", "help", diff --git a/Sources/SwiftKotlinFramework/plugins/UIKitTransformPlugin.swift b/Sources/SwiftKotlinFramework/plugins/UIKitTransformPlugin.swift new file mode 100644 index 0000000..b659026 --- /dev/null +++ b/Sources/SwiftKotlinFramework/plugins/UIKitTransformPlugin.swift @@ -0,0 +1,50 @@ +// +// UIKitTransformPlugin.swift +// SwiftKotlinFramework +// +// Created by Angel Luis Garcia on 17/05/2020. +// + +import Foundation +import Transform +import AST + +public class UIKitTransformPlugin: TokenTransformPlugin { + public var name: String { + return "UIKit transformations" + } + + public var description: String { + return "Transforms classes like UIView, UILabel,... to their Android counterpart" + } + + public init() {} + + public func transform(tokens: [Token], topDeclaration: TopLevelDeclaration) throws -> [Token] { + var newTokens = [Token]() + + for token in tokens { + if token.kind == .identifier, let mapping = classMappings[token.value], let node = token.node { + newTokens.append(node.newToken(.identifier, mapping)) + } else { + newTokens.append(token) + } + } + return newTokens + } + + let classMappings = [ + "UIView": "View", + "UILabel": "TextView", + "UITextField": "EditText", + "UIImageView": "ImageView", + "UIButton": "Button", + "UITableView": "RecyclerView", + "UIStackView": "LinearLayout", + "UIScrollView": "ScrollView", + "UISwitch": "Switch", + "IBOutlet": "BindView()", + "IBAction": "OnClick()" + ] + +} diff --git a/SwiftKotlinApp.xcodeproj/project.pbxproj b/SwiftKotlinApp.xcodeproj/project.pbxproj index 6ff4139..50cfde4 100644 --- a/SwiftKotlinApp.xcodeproj/project.pbxproj +++ b/SwiftKotlinApp.xcodeproj/project.pbxproj @@ -297,7 +297,7 @@ INFOPLIST_FILE = "$(SRCROOT)/Sources/SwiftKotlinApp/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.12; - MARKETING_VERSION = 0.2.4; + MARKETING_VERSION = 0.2.5; MTL_ENABLE_DEBUG_INFO = YES; PRODUCT_BUNDLE_IDENTIFIER = com.angelolloqui.SwiftKotlinApp; PRODUCT_NAME = SwiftKotlin; @@ -352,7 +352,7 @@ INFOPLIST_FILE = "$(SRCROOT)/Sources/SwiftKotlinApp/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.12; - MARKETING_VERSION = 0.2.4; + MARKETING_VERSION = 0.2.5; MTL_ENABLE_DEBUG_INFO = NO; PRODUCT_BUNDLE_IDENTIFIER = com.angelolloqui.SwiftKotlinApp; PRODUCT_NAME = SwiftKotlin; diff --git a/Tests/SwiftKotlinFrameworkTests/KotlinTokenizerTests.swift b/Tests/SwiftKotlinFrameworkTests/KotlinTokenizerTests.swift index d4c8e4e..9ab2152 100644 --- a/Tests/SwiftKotlinFrameworkTests/KotlinTokenizerTests.swift +++ b/Tests/SwiftKotlinFrameworkTests/KotlinTokenizerTests.swift @@ -12,7 +12,7 @@ class KotlinTokenizerTests: XCTestCase { let kotlinTokenizer = KotlinTokenizer( tokenTransformPlugins: [ XCTTestToJUnitTokenTransformPlugin(), - FoundationMethodsTransformPlugin() + FoundationMethodsTransformPlugin() ] ) diff --git a/Tests/SwiftKotlinFrameworkTests/TransformPluginTests.swift b/Tests/SwiftKotlinFrameworkTests/TransformPluginTests.swift index 68df347..87a41e4 100644 --- a/Tests/SwiftKotlinFrameworkTests/TransformPluginTests.swift +++ b/Tests/SwiftKotlinFrameworkTests/TransformPluginTests.swift @@ -28,6 +28,12 @@ class TransformPluginTests: XCTestCase { file: "CommentsAdditionTransformPlugin") } + func testUIKitTransformPlugin() { + try! testTokenTransformPlugin( + plugin: UIKitTransformPlugin(), + file: "UIKitTransformPlugin") + } + } extension TransformPluginTests {