Skip to content

Commit

Permalink
Merge pull request #6 from jjotaum/feature/output-option
Browse files Browse the repository at this point in the history
Added output option to support file generation as extension or struct
  • Loading branch information
Jota Uribe authored Jul 17, 2020
2 parents 4f6ef56 + 461b56b commit 5365935
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
15 changes: 13 additions & 2 deletions Application/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ struct Generator: ParsableCommand {
@Option(name: .shortAndLong, default: "Chroma", help: "Defines the name of the generated file.")
private var name: String

@Option(name: .shortAndLong, default: .extension, help: OutputType.help)
private var output: OutputType

@Option(name: .shortAndLong, default: .iOS, help: "Specifies the platform compatibility of the exported file.")
private var platform: Platform

private var header: String {
switch output {
case .extension:
return "\(output.rawValue) \(platform.variableType)"
case .struct:
return "\(output.rawValue) \(name)"
}
}

func run() throws {
generate()
}
Expand All @@ -30,8 +42,7 @@ extension Generator {
let folder = Folder.root
let file = File(named: name, at: folder)
let body = folder.colorDefinitions(for: platform).joined(separator: "\n\n")
let content = platform.fileContent(body: body)
let content = platform.fileContent(header: header, body: body)
try? file.write(content)
}

}
31 changes: 31 additions & 0 deletions Application/Output.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Output.swift
// Chroma
//
// Created by Jota Uribe on 8/06/20.
// Copyright © 2020 Jota Uribe. All rights reserved.
//

import Foundation
import ArgumentParser

enum OutputType: String, CaseIterable, ExpressibleByArgument {

case `extension`
case `struct`

}

extension OutputType {

private static var formattedValues: String {
return OutputType.allCases.map { "\"\($0.rawValue)\"" }.joined(separator: ",")
}

static var help: ArgumentHelp {
"""
Specifies generated file type.
Supported values: \(formattedValues).
"""
}
}
4 changes: 2 additions & 2 deletions Application/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ extension Platform {
}
}

func fileContent(body: String) -> String {
func fileContent(header: String, body: String) -> String {
"""
import \(framework)
extension \(variableType) {
\(header) {
\(body)
Expand Down
4 changes: 4 additions & 0 deletions Chroma.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
B221DAE8248D905700BE201A /* Files in Frameworks */ = {isa = PBXBuildFile; productRef = B221DAE7248D905700BE201A /* Files */; };
B221DAEB248D90A900BE201A /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = B221DAEA248D90A900BE201A /* ArgumentParser */; };
B221DAED248D90D700BE201A /* Generator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B221DAEC248D90D700BE201A /* Generator.swift */; };
B2E7DD3E248F274400F5F211 /* Output.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2E7DD3D248F274400F5F211 /* Output.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -35,6 +36,7 @@
B221DADC248D901200BE201A /* Chroma */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Chroma; sourceTree = BUILT_PRODUCTS_DIR; };
B221DADF248D901200BE201A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
B221DAEC248D90D700BE201A /* Generator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Generator.swift; sourceTree = "<group>"; };
B2E7DD3D248F274400F5F211 /* Output.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Output.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -74,6 +76,7 @@
5E0FAA6F248DC3F200B57DC1 /* Platform.swift */,
5E0FAA71248DC5A800B57DC1 /* Folder.swift */,
5E0FAA74248DC92800B57DC1 /* File.swift */,
B2E7DD3D248F274400F5F211 /* Output.swift */,
);
path = Application;
sourceTree = "<group>";
Expand Down Expand Up @@ -166,6 +169,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B2E7DD3E248F274400F5F211 /* Output.swift in Sources */,
5E0FAA75248DC92800B57DC1 /* File.swift in Sources */,
5E0FAA70248DC3F200B57DC1 /* Platform.swift in Sources */,
B221DAE0248D901200BE201A /* main.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Chroma.xcodeproj/xcshareddata/xcschemes/Chroma.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "-n Chroma -p iOS"
argument = "-o struct"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
Expand Down

0 comments on commit 5365935

Please sign in to comment.