Skip to content

Commit

Permalink
Merge pull request #118 from angelolloqui/feature/UIKitTransform
Browse files Browse the repository at this point in the history
Implemented basic UIKit -> Android widget transform plugins
  • Loading branch information
angelolloqui authored May 17, 2020
2 parents 6cb381e + 4186d60 commit ba9f70b
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Assets/Tests/plugins/UIKitTransformPlugin.kt
Original file line number Diff line number Diff line change
@@ -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() {}
}
21 changes: 21 additions & 0 deletions Assets/Tests/plugins/UIKitTransformPlugin.swift
Original file line number Diff line number Diff line change
@@ -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() {
}
}
1 change: 1 addition & 0 deletions Sources/SwiftKotlinApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ViewController: NSViewController {
tokenTransformPlugins: [
XCTTestToJUnitTokenTransformPlugin(),
FoundationMethodsTransformPlugin(),
UIKitTransformPlugin(),
CommentsAdditionTransformPlugin()
]
)
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftKotlinCommandLine/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 50 additions & 0 deletions Sources/SwiftKotlinFramework/plugins/UIKitTransformPlugin.swift
Original file line number Diff line number Diff line change
@@ -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()"
]

}
4 changes: 2 additions & 2 deletions SwiftKotlinApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftKotlinFrameworkTests/KotlinTokenizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class KotlinTokenizerTests: XCTestCase {
let kotlinTokenizer = KotlinTokenizer(
tokenTransformPlugins: [
XCTTestToJUnitTokenTransformPlugin(),
FoundationMethodsTransformPlugin()
FoundationMethodsTransformPlugin()
]
)

Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftKotlinFrameworkTests/TransformPluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class TransformPluginTests: XCTestCase {
file: "CommentsAdditionTransformPlugin")
}

func testUIKitTransformPlugin() {
try! testTokenTransformPlugin(
plugin: UIKitTransformPlugin(),
file: "UIKitTransformPlugin")
}

}

extension TransformPluginTests {
Expand Down

0 comments on commit ba9f70b

Please sign in to comment.