Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Filename Template #108

Merged
merged 6 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Docs/ConfigOptions.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions Sources/CreateAPI/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ struct Generate: ParsableCommand {
@Option(help: "Specifies what to generate", completion: .list(["paths", "entities"]))
var generate = ["paths", "entities"]

@Option(help: "Example: \"%0.generated.swift\" will produce files with the following names: \"Paths.generated.swift\".")
var filenameTemplate: String = "%0.swift"

@Option(help: "Example: \"%0Generated\" will produce entities with the following names: \"EntityGenerated\".")
var entitynameTemplate: String = "%0"

Expand Down Expand Up @@ -109,8 +106,8 @@ struct Generate: ParsableCommand {
try generator.makePackageFile(name: package).write(to: packageURL.appending(path: "Package.swift"))
}
try sourceURL.createDirectoryIfNeeded()
try write(output: paths, name: "Paths", outputURL: sourceURL, options: options)
try write(output: schemas, name: "Entities", outputURL: sourceURL, options: options)
try write(output: paths, name: "Paths", outputURL: sourceURL, filenameTemplate: options.paths.filenameTemplate, options: options)
try write(output: schemas, name: "Entities", outputURL: sourceURL, filenameTemplate: options.entities.filenameTemplate, options: options)
benchmark.stop()
}

Expand Down Expand Up @@ -192,7 +189,7 @@ struct Generate: ParsableCommand {
return spec
}

private func write(output: GeneratorOutput?, name: String, outputURL: URL, options: GenerateOptions) throws {
private func write(output: GeneratorOutput?, name: String, outputURL: URL, filenameTemplate: String, options: GenerateOptions) throws {
guard let output = output else {
return
}
Expand All @@ -205,22 +202,22 @@ struct Generate: ParsableCommand {
let contents = ([output.header] + output.files.map(\.contents))
.compactMap { $0 }
.joined(separator: "\n\n")
try process(contents).write(to: outputURL.appending(path: makeFilename(for: name)))
try process(contents).write(to: outputURL.appending(path: makeFilename(for: name, template: filenameTemplate)))
} else {
let outputURL = outputURL.appending(path: name)
try outputURL.createDirectoryIfNeeded()
for file in output.files {
try process(output.header + "\n\n" + file.contents).write(to: outputURL.appending(path: makeFilename(for: file.name)))
try process(output.header + "\n\n" + file.contents).write(to: outputURL.appending(path: makeFilename(for: file.name, template: filenameTemplate)))
}
}

for file in output.extensions {
try process(output.header + "\n\n" + file.contents).write(to: outputURL.appendingPathComponent(makeFilename(for: file.name)))
try process(output.header + "\n\n" + file.contents).write(to: outputURL.appendingPathComponent(makeFilename(for: file.name, template: "%0.swift")))
}
}

private func makeFilename(for name: String) -> String {
Template(filenameTemplate).substitute(name)
private func makeFilename(for name: String, template: String) -> String {
Template(template).substitute(name)
}

private var arguments: GenerateArguments {
Expand Down
12 changes: 12 additions & 0 deletions Sources/CreateOptions/ConfigOptions+DecodableWithDefault.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Sources/CreateOptions/ConfigOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ public struct ConfigOptions: Encodable {
/// When set to a non-empty value, only entities matching the given names will be generated.
/// This cannot be used in conjunction with [`exclude`](#entitiesexclude).
public var include: Set<String> = []

/// Template to use for Entity file generation
///
/// <details>
/// <summary>Examples</summary>
///
/// ```yaml
/// entities:
/// filenameTemplate: "%0Model.swift"
/// ```
///
/// </details>
public var filenameTemplate: String = "%0.swift"
}

public var paths: Paths = .init()
Expand Down Expand Up @@ -328,6 +341,19 @@ public struct ConfigOptions: Encodable {
/// When set to a non-empty value, only the given paths will be generated.
/// This cannot be used in conjunction with [`exclude`](#pathsexclude).
public var include: Set<String> = []

/// Template to use for Paths file generation.
///
/// <details>
/// <summary>Examples</summary>
///
/// ```yaml
/// paths:
/// filenameTemplate: "%0API.swift"
/// ```
///
/// </details>
public var filenameTemplate: String = "%0.swift"
}

public var rename: Rename = .init()
Expand Down
4 changes: 2 additions & 2 deletions Tests/CreateAPITests/AllPackages/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ let package = Package(
.package(path: "../Expected/OctoKit"),
.package(path: "../Expected/petstore-base-class"),
.package(path: "../Expected/petstore-change-entityname"),
.package(path: "../Expected/petstore-change-filename"),
.package(path: "../Expected/petstore-change-namespace-when-operations-style"),
.package(path: "../Expected/petstore-change-namespace-when-rest-style"),
.package(path: "../Expected/petstore-custom-imports"),
Expand All @@ -42,6 +41,7 @@ let package = Package(
.package(path: "../Expected/petstore-disable-inlining"),
.package(path: "../Expected/petstore-disable-mutable-properties"),
.package(path: "../Expected/petstore-enable-mutable-properties"),
.package(path: "../Expected/petstore-filename-template"),
.package(path: "../Expected/petstore-generate-classes"),
.package(path: "../Expected/petstore-identifiable"),
.package(path: "../Expected/petstore-only-schemas"),
Expand Down Expand Up @@ -74,7 +74,6 @@ let package = Package(
"OctoKit",
"petstore-base-class",
"petstore-change-entityname",
"petstore-change-filename",
"petstore-change-namespace-when-operations-style",
"petstore-change-namespace-when-rest-style",
.byName(name: "petstore-custom-imports", condition: .when(platforms: [.iOS, .macOS])),
Expand All @@ -84,6 +83,7 @@ let package = Package(
"petstore-disable-inlining",
"petstore-disable-mutable-properties",
"petstore-enable-mutable-properties",
"petstore-filename-template",
"petstore-generate-classes",
"petstore-identifiable",
"petstore-only-schemas",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "petstore-entity-filename-template",
platforms: [.iOS(.v13), .macCatalyst(.v13), .macOS(.v10_15), .watchOS(.v6), .tvOS(.v13)],
products: [
.library(name: "petstore-entity-filename-template", targets: ["petstore-entity-filename-template"]),
],
dependencies: [
.package(url: "https://github.com/kean/Get", from: "1.0.2")
],
targets: [
.target(name: "petstore-entity-filename-template", dependencies: [
.product(name: "Get", package: "Get")
], path: "Sources")
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import PackageDescription

let package = Package(
name: "petstore-change-filename",
name: "petstore-filename-template",
platforms: [.iOS(.v13), .macCatalyst(.v13), .macOS(.v10_15), .watchOS(.v6), .tvOS(.v13)],
products: [
.library(name: "petstore-change-filename", targets: ["petstore-change-filename"]),
.library(name: "petstore-filename-template", targets: ["petstore-filename-template"]),
],
dependencies: [
.package(url: "https://github.com/kean/Get", from: "1.0.2"),
.package(url: "https://github.com/CreateAPI/HTTPHeaders", from: "0.1.0"),
.package(url: "https://github.com/CreateAPI/URLQueryEncoder", from: "0.2.0")
],
targets: [
.target(name: "petstore-change-filename", dependencies: [
.target(name: "petstore-filename-template", dependencies: [
.product(name: "Get", package: "Get"),
.product(name: "HTTPHeaders", package: "HTTPHeaders"),
.product(name: "URLQueryEncoder", package: "URLQueryEncoder")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Generated by Create API
// https://github.com/CreateAPI/CreateAPI

import Foundation

struct StringCodingKey: CodingKey, ExpressibleByStringLiteral {
private let string: String
private var int: Int?

var stringValue: String { return string }

init(string: String) {
self.string = string
}

init?(stringValue: String) {
self.string = stringValue
}

var intValue: Int? { return int }

init?(intValue: Int) {
self.string = String(describing: intValue)
self.int = intValue
}

init(stringLiteral value: String) {
self.string = value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Generated by Create API
// https://github.com/CreateAPI/CreateAPI

import Foundation

public struct Error: Codable {
public var code: Int
public var message: String

public init(code: Int, message: String) {
self.code = code
self.message = message
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Generated by Create API
// https://github.com/CreateAPI/CreateAPI

import Foundation

/// A pet title
///
/// A pet description
public struct Pet: Codable {
public var id: Int
/// Example: "Buddy"
public var name: String
public var tag: String?

public init(id: Int, name: String, tag: String? = nil) {
self.id = id
self.name = name
self.tag = tag
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Generated by Create API
// https://github.com/CreateAPI/CreateAPI

import Foundation

public struct Store: Codable {
public var pets: [Pet]

public init(pets: [Pet]) {
self.pets = pets
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Paths {
public let path: String

/// List all pets
public func get(limit: Int? = nil) -> Request<[petstore_change_filename.Pet]> {
public func get(limit: Int? = nil) -> Request<[petstore_filename_template.Pet]> {
Request(method: "GET", url: path, query: makeGetQuery(limit), id: "listPets")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Paths.Pets {
public let path: String

/// Info for a specific pet
public var get: Request<petstore_change_filename.Pet> {
public var get: Request<petstore_filename_template.Pet> {
Request(method: "GET", url: path, id: "showPetById")
}
}
Expand Down
Loading