Skip to content

Instantly share code, notes, and snippets.

@regularberry
Created June 29, 2018 14:35
Show Gist options
  • Save regularberry/7b4618d553f64b09079c2b631e8b9187 to your computer and use it in GitHub Desktop.
Save regularberry/7b4618d553f64b09079c2b631e8b9187 to your computer and use it in GitHub Desktop.
let methodBuilder = MethodSpec.builder(for: "doTheThing")
methodBuilder.add(modifier: .Public)
let code = """
for _ in 0..<howManyTimes {
print("I'm doing the thing!")
}
"""
methodBuilder.add(codeBlock: code.toCodeBlock())
let parameterBuilder = ParameterSpec.builder(for: "howManyTimes", type: .IntegerType)
parameterBuilder.add(initializer: "3".toCodeBlock())
methodBuilder.add(parameter: parameterBuilder.build())
let fieldBuilder = FieldSpec.builder(for: "myField", type: .StringType, construct: .mutableField)
fieldBuilder.add(initializer: "\"mine\"".toCodeBlock())
let structBuilder = StructSpec.builder(for: "TestStruct")
structBuilder.add(field: fieldBuilder.build())
structBuilder.add(method: methodBuilder.build())
let poet = PoetFile(list: [structBuilder.build()], generatorInfo: nil)
print(poet.fileContents)
/// OUTPUT
------------
//
// TestStruct.swift
//
// Contains:
// struct TestStruct
//
// Generated by SwiftPoet on 6/29/18
//
struct TestStruct {
var myField: String = "mine"
/**
:param: howManyTimes
*/
public func doTheThing(howManyTimes: Int = 3) {
for _ in 0..<howManyTimes {
print("I'm doing the thing!")
}
}
/**
:param: myField
*/
internal init(myField: String) {
self.myField = myField
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment