Skip to content

Digipolitan/swift-code-writer

SwiftCodeWriter

Swift Version Swift Package Manager Twitter

Library to write swift source code

Installation

SPM

To install SwiftCodeWriter with SwiftPackageManager, add the following lines to your Package.swift.

let package = Package(
    name: "XXX",
    products: [
        .library(
            name: "XXX",
            targets: ["XXX"]),
    ],
    dependencies: [
        .package(url: "https://github.com/Digipolitan/swift-code-writer.git", from: "1.0.0")
    ],
    targets: [
        .target(
            name: "XXX",
            dependencies: ["SwiftCodeWriter"])
    ]
)

The Basics

The you must create a file description that represent the swift output file

let fileDescription = FileDescription()

A file description can contains :

  • classes [ClassDescription]
  • enums [EnumDescription]
  • protocols [ProtocolDescription]
  • extensions [ExtensionDescription]
  • methods [MethodDescription]
  • properties [PropertyDescription]
  • documentation

The following example will show how to register a structure inside the file description

// Add a User struct with 2 properties
var cd = ClassDescription(name: "User", options: .init(visibility: .public, isReferenceType: true))
cd.properties.append(.init(name: "lastName", type: "String?"))
cd.properties.append(.init(name: "firstName", type: "String?"))
fileDescription.classes.append(cd)

After that use the FileWriter to transform the given FileDescription to string as follow

let res = FileWriter.default.write(description: fileDescription)

Contributing

See CONTRIBUTING.md for more details!

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to contact@digipolitan.com.

License

SwiftCodeWriter is licensed under the BSD 3-Clause license.