Giter Site home page Giter Site logo

swiftpoet's Introduction

SwiftPoet

SwiftPoet is a library that is inspired from https://github.com/square/javapoet to help Swift developers easy to generate Swift source code.

Instruction

Cocoapods:

pod 'SwiftPoet', :git => '[email protected]:jupitervn/SwiftPoet.git'

Swift file:

In order to generate swift file, you can use SwiftFile.newFile() or SwiftFile.newSingleClass() method. Example:

let swiftFile = SwiftFile
        .newFile({file_name})
        .addType(classSpec)
        .addMethod(operatorMethod)
        .build()
        .writeTo(testOutputDir)

SwiftFileBuilder will contains other methods to add variable or class/protocol/extension/enum or method or even codeblock

TypeSpec

Please use these static methods to construct a class/protocol/struct/enum/extension

public static func newClass(name: String) -> TypeSpecBuilder
public static func newProtocol(name: String) -> TypeSpecBuilder
public static func newStruct(name: String) -> TypeSpecBuilder
public static func newEnum(name: String, indirect: Bool = false) -> TypeSpecBuilder
public static func newExtension(ofType: String) -> TypeSpecBuilder

Example

 let classSpec = try? TypeSpec.newClass("TestClass")
      .addProperty(FieldSpecBuilder(name: "field01", fieldType: "Int").initWith("1").build())
      .addProperty(FieldSpecBuilder(name: "field02", fieldType: "String").initWith("\"abc\"").build())
      .addProperty(FieldSpecBuilder(name: "field03").modifiable().initWith("\"abc\"").build())
      .addMethod(MethodSpec.initBuilder().addParam(ParameterSpec(name: "name", paramType: "String")).build())
      .build()
    
    let swiftFile = SwiftFile
      .newSingleClass("sample_class01_output", classSpec: classSpec!)
      .build()
      .writeTo(testOutputDir)

will generate this file

class TestClass {
  let field01: Int = 1
  let field02: String = "abc"
  var field03 = "abc"

  init(name: String) {
  }

}

CodeBlock

In order to construct a codeblock, please use: CodeBlock.newCodeBlock() {} .statement() is used to insert a new statement inside codeblock .beginControlFlow() will start a block of control flow and handle all the indents for you .nextControlFlow() is used to generate else clause '.endControlFlow()` will close the current block of control flow.

Example:

let codeBlock = CodeBlock.newCodeBlock() { codeBlock in
      codeBlock.beginControlFlow("if (param == \"test\")")
        .statement("return 123")
        .nextControlFlow("else")
        .statement("return 456")
        .endControlFlow()
    }

will generate this piece of code

if (param == "test") {
      return 123
    } else {
      return 456
    }

Please find more examples inside SwiftFile_Tests.swift to learn more about the usage of this library.

swiftpoet's People

Contributors

jupiter2359media avatar jupitervn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.