-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from jjotaum/release/0.4
Release 0.4 - Bump swift-tool-version to 5.5, package & project main structure clean up. - Refactor chroma arguments to receive asset path and exported file path to make it work on a more standard and less risky way. - Clean up on unit tests setup. - Update swift-argument-parser version to 1.1.2. - Update folder file creation method to throw an error instead of causing a fatal error. - Update Chroma description on Readme file.
- Loading branch information
Showing
16 changed files
with
195 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// Chroma.swift | ||
// Chroma | ||
// | ||
// Created by Jota Uribe on 7/06/20. | ||
// Copyright © 2020 Jota Uribe. All rights reserved. | ||
// | ||
|
||
import ArgumentParser | ||
import Files | ||
import Foundation | ||
|
||
public struct Chroma: ParsableCommand { | ||
@Option(name: .shortAndLong, help: "The path of .xcasset file.") | ||
private var asset: String | ||
|
||
@Option(name: .shortAndLong, help: "The path of the generated .swift file.") | ||
private var path: String | ||
|
||
@Option(name: .shortAndLong, help: OutputType.help) | ||
private var type: OutputType = .extension | ||
|
||
@Option(name: .long, help: "Specifies the platform compatibility of the exported file.\niOS, macOS, swiftUI") | ||
private var platform: Platform = .iOS | ||
|
||
public init() {} | ||
|
||
public func run() throws { | ||
try generate() | ||
} | ||
|
||
} | ||
|
||
extension Chroma { | ||
|
||
private func generate() throws { | ||
let outputFile = try outputFile() | ||
let content = try getContentFromAssetsFile(outputFile: outputFile) | ||
try outputFile.write(content) | ||
print( | ||
""" | ||
\(outputFile.name) was generated successfully. | ||
Can be found at \(outputFile.path) | ||
""" | ||
) | ||
} | ||
|
||
private func outputFile() throws -> File { | ||
guard let pathURL = URL(string: path), !pathURL.hasDirectoryPath, pathURL.pathExtension == "swift" else { | ||
throw ChromaError.invalidPath(path: path) | ||
} | ||
|
||
|
||
let folder = try Folder(path: pathURL.deletingLastPathComponent().path) | ||
return try File(named: pathURL.lastPathComponent, at: folder) | ||
} | ||
|
||
private func getContentFromAssetsFile(outputFile: File) throws -> String { | ||
let folder = try Folder(path: asset) | ||
let body = folder.colorDefinitions(for: platform).sorted().joined(separator: "\n") | ||
return platform.fileContent(header: header(file: outputFile), body: body) | ||
} | ||
|
||
private func header(file: File) -> String { | ||
switch type { | ||
case .extension: | ||
return "\(type.rawValue) \(platform.variableType)" | ||
case .struct: | ||
return "\(type.rawValue) \(file.nameExcludingExtension)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// ChromaError.swift | ||
// Chroma | ||
// | ||
// Created by Jota Uribe on 9/06/22. | ||
// | ||
|
||
import Foundation | ||
|
||
enum ChromaError: LocalizedError { | ||
case fileCreationFailed(path: String) | ||
case fileCreationFailedAtFolder(path: String, fileName: String) | ||
case invalidPath(path: String) | ||
} | ||
|
||
extension ChromaError { | ||
var errorDescription: String? { | ||
switch self { | ||
case .fileCreationFailed(let path): | ||
return "Could not create file at \(path)" | ||
case .fileCreationFailedAtFolder(let path, let fileName): | ||
return "Could not create file '\(fileName)' at \(path)" | ||
case .invalidPath(let path): | ||
return "Invalid path: \(path)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
// Copyright © 2020 Jota Uribe. All rights reserved. | ||
// | ||
|
||
import ChromaLibrary | ||
import Foundation | ||
|
||
Chroma.main() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ChromaErrorTests.swift | ||
// Chroma | ||
// | ||
// Created by Jota Uribe on 9/06/22. | ||
// | ||
|
||
import XCTest | ||
@testable import Chroma | ||
|
||
final class ChromaErrorTests: XCTestCase { | ||
let mockPath = "/path/file.ext" | ||
|
||
func testFileCreationFailedDescription() throws { | ||
let error = ChromaError.fileCreationFailed(path: mockPath) | ||
let expectedDesc = "Could not create file at /path/file.ext" | ||
XCTAssertEqual(error.localizedDescription, expectedDesc) | ||
} | ||
|
||
func testFileCreationFailedAtFolder() throws { | ||
let error = ChromaError.fileCreationFailedAtFolder(path: "/path", fileName: "file.ext") | ||
let expectedDesc = "Could not create file 'file.ext' at /path" | ||
XCTAssertEqual(error.localizedDescription, expectedDesc) | ||
} | ||
|
||
func testInvalidPathDescription() throws { | ||
let error = ChromaError.invalidPath(path: mockPath) | ||
let expectedDesc = "Invalid path: /path/file.ext" | ||
XCTAssertEqual(error.localizedDescription, expectedDesc) | ||
} | ||
|
||
|
||
static var allTests = [ | ||
("testFileCreationFailedDescription", testFileCreationFailedDescription), | ||
("testFileCreationFailedAtFolder", testFileCreationFailedAtFolder), | ||
("testInvalidPathDescription", testInvalidPathDescription) | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// FileTests.swift | ||
// Chroma | ||
// | ||
// Created by Jota Uribe on 9/06/22. | ||
// | ||
|
||
import XCTest | ||
import Files | ||
@testable import Chroma | ||
|
||
final class FileTests: XCTestCase { | ||
|
||
func testNamedAtInitWithOutExtension() throws { | ||
let path = Bundle.module.bundlePath | ||
let folder = try Folder(path: path) | ||
let fileName = "File" | ||
let file = try File(named: fileName, at: folder) | ||
XCTAssertEqual(file.path, "\(path)/\(fileName)") | ||
} | ||
|
||
static var allTests = [ | ||
("testNamedAtInitWithOutExtension", testNamedAtInitWithOutExtension) | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters