Skip to content
Permalink
Browse files

Push code

  • Loading branch information...
alexito4 committed Apr 22, 2019
1 parent 9ed614c commit eecfef764dcb319c907229647a419b4f085221a1
@@ -66,3 +66,4 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output
SwiftUsing.xcodeproj
@@ -0,0 +1,50 @@

struct User {
var name: String
var age: Int
}

struct Address {
var street: String
let city: String
}

struct Friend {
// using
let user: User

// using
var home: Address

var friendshipDate: Date
}

// generated
extension Friend {
var name: String {
get {
return user.name
}
}
var age: Int {
get {
return user.age
}
}
}
// generated
extension Friend {
var street: String {
get {
return home.street
}
set {
home.street = newValue
}
}
var city: String {
get {
return home.city
}
}
}
@@ -0,0 +1,18 @@
INSTALL_PATH = /usr/local/bin/swiftusing

build:
swift package update
swift build -c release -Xswiftc -static-stdlib

install: build
cp -f .build/release/swiftusing $(INSTALL_PATH)

clean:
rm -rf .build

uninstall:
rm -f $(INSTALL_PATH)

xcode:
swift package generate-xcodeproj
xed .
@@ -0,0 +1,34 @@
{
"object": {
"pins": [
{
"package": "SnapshotTesting",
"repositoryURL": "https://github.com/pointfreeco/swift-snapshot-testing.git",
"state": {
"branch": null,
"revision": "883f0b0623ae55f41f42e2399dab428017896448",
"version": "1.5.0"
}
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax.git",
"state": {
"branch": null,
"revision": "43aa4a19b8105a803d8149ad2a86aa53a77efef3",
"version": "0.50000.0"
}
},
{
"package": "Yaap",
"repositoryURL": "https://github.com/hartbit/Yaap.git",
"state": {
"branch": "master",
"revision": "7e205f99ad86ac5eac50253d2d5848cf7c9c579a",
"version": null
}
}
]
},
"version": 1
}
@@ -0,0 +1,23 @@
// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: "SwiftUsing",
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", .exact("0.50000.0")),
.package(url: "https://github.com/hartbit/Yaap.git", .branch("master")),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.5.0"),
],
targets: [
.target(
name: "swiftusing",
dependencies: ["SwiftUsingCore", "Yaap"]),
.target(
name: "SwiftUsingCore",
dependencies: ["SwiftSyntax"]),
.testTarget(
name: "SwiftUsingTests",
dependencies: ["SwiftUsingCore", "SnapshotTesting"])
]
)
@@ -0,0 +1,26 @@
/**
* SwiftUsing
* Copyright (c) Alejandro Martínez 2019
* Licensed under the MIT license. See LICENSE file.
*/

import SwiftUsingCore
import Foundation
import Yaap

class GenerateCommand: Command {
let name = "Generate"

let path = Argument<String>(documentation: "Path to the file to execute the code generation on.")

func run(outputStream: inout TextOutputStream, errorStream: inout TextOutputStream) throws {
outputStream.write("Generating code...\n")

let file = URL(fileURLWithPath: path.value)

let generator = SwiftUsing(file: file)
try generator.generate()
}
}

GenerateCommand().parseAndRun()
@@ -0,0 +1,56 @@
import SwiftSyntax
import Foundation

public class SwiftUsing {

let file: URL

public init(file: URL) {
self.file = file
}

public func generate() throws {
// Parse file
let tree = try SyntaxTreeParser.parse(file)

// File -> UsingFinder Visitor -> (types + using)
let (types, using) = find(tree)

// (types + using) -> Maker -> [ExtensionDeclSyntax]
let generated = make(types: types, using: using)

// Tree + [ExtensionDeclSyntax] -> Rewritter -> new Tree
let newTree = rewritte(tree, generated: generated)

// Overwrite file
try "\(newTree)".write(to: file, atomically: true, encoding: .utf8)
}

func find(_ tree: SourceFileSyntax) -> (
types: [StructDeclSyntax],
using: [VariableDeclSyntax]
) {
let visitor = UsingFinder()
tree.walk(visitor)

return (
visitor.types,
visitor.using
)
}

func make(
types: [StructDeclSyntax],
using: [VariableDeclSyntax]
) -> [ExtensionDeclSyntax] {
let maker = UsingMaker(types: types, using: using)
return maker.generateUsingCode()
}

func rewritte(_ tree: SourceFileSyntax, generated: [ExtensionDeclSyntax]) -> SourceFileSyntax {
let rewriter = UsingRewriter(generated: generated)
let final = rewriter.rewrite(tree)
return final
}

}
Oops, something went wrong.

0 comments on commit eecfef7

Please sign in to comment.
You can’t perform that action at this time.