-
Notifications
You must be signed in to change notification settings - Fork 115
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 #10 from modestman/feature/export-colors-as-code
Export colors as swift code without assets
- Loading branch information
Showing
10 changed files
with
181 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,26 @@ Additionally the `Color.swift` file will be created to use colors from the code. | |
|
||
``` | ||
|
||
If you set option `useColorAssets: False` in the configuration file, then will be generated code like this: | ||
```swift | ||
import UIKit | ||
|
||
extension UIColor { | ||
static var primaryText: UIColor { | ||
UIColor { traitCollection -> UIColor in | ||
if traitCollection.userInterfaceStyle == .dark { | ||
return UIColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.000) | ||
} else { | ||
return UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000) | ||
} | ||
} | ||
} | ||
static var backgroundVideo: UIColor { | ||
return UIColor(red: 0.467, green: 0.012, blue: 1.000, alpha: 0.500) | ||
} | ||
} | ||
``` | ||
|
||
#### Icons | ||
|
||
Icons will be exported as PDF files with `Template Image` render mode. | ||
|
@@ -156,6 +176,8 @@ ios: | |
|
||
# Parameters for exporting colors | ||
colors: | ||
# Should be generate color assets instead of pure swift code | ||
useColorAssets: True | ||
# Name of the folder inside Assets.xcassets where to place colors (.colorset directories) | ||
assetsFolder: Colors | ||
# Path to Color.swift file where to export colors for accessing colors from the code (e.g. UIColor.backgroundPrimary) | ||
|
@@ -194,7 +216,8 @@ android: | |
|
||
### iOS properties | ||
* `ios.xcassetsPath` — Relative or absolute path to directory `Assets.xcassets` where to export colors, icons and images. | ||
* `ios.colors.assetsFolder` — Name of the folder inside `Assets.xcassets` where colors will be exported. | ||
* `ios.colors.useColorAssets` — How to export colors - as assets or as swift UIColor initializers only. | ||
* `ios.colors.assetsFolder` — Name of the folder inside `Assets.xcassets` where colors will be exported. Used only if `useColorAssets == true`. | ||
* `ios.colors.colorSwift` — Relative or absolute path to `Color.swift` file. | ||
* `ios.colors.nameStyle` — Color name style: camelCase or snake_case | ||
* `ios.icons.assetsFolder` — Name of the folder inside `Assets.xcassets` where icons will be exported. | ||
|
@@ -245,4 +268,4 @@ If you have any issues with the FigmaExport or you want some new features feel f | |
|
||
## Authors | ||
|
||
Daniil Subbotin - [email protected] | ||
Daniil Subbotin - [email protected] |
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
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
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,93 @@ | ||
import XCTest | ||
import FigmaExportCore | ||
@testable import XcodeExport | ||
|
||
final class XcodeColorExporterTests: XCTestCase { | ||
|
||
// MARK: - Properties | ||
|
||
private let fileManager = FileManager.default | ||
private var colorsFile: URL! | ||
private var colorsAsssetCatalog: URL! | ||
|
||
private let colorPair1 = AssetPair<Color>( | ||
light: Color(name: "colorPair1", r: 1, g: 1, b: 1, a: 1), | ||
dark: Color(name: "colorPair1", r: 0, g: 0, b: 0, a: 1)) | ||
|
||
private let colorPair2 = AssetPair<Color>( | ||
light: Color(name: "colorPair2", r: 119.0/255.0, g: 3.0/255.0, b: 1.0, a: 0.5), | ||
dark: nil) | ||
|
||
// MARK: - Setup | ||
|
||
override func setUp() { | ||
super.setUp() | ||
colorsFile = fileManager.temporaryDirectory.appendingPathComponent("Colors.swift") | ||
colorsAsssetCatalog = fileManager.temporaryDirectory.appendingPathComponent("Assets.xcassets/Colors") | ||
} | ||
|
||
// MARK: - Tests | ||
|
||
func testExport_without_assets() { | ||
let output = XcodeColorsOutput(assetsColorsURL: nil, colorSwiftURL: colorsFile) | ||
let exporter = XcodeColorExporter(output: output) | ||
|
||
let result = exporter.export(colorPairs: [colorPair1, colorPair2]) | ||
XCTAssertEqual(result.count, 1) | ||
|
||
let content = result[0].data | ||
XCTAssertNotNil(content) | ||
|
||
let generatedCode = String(data: content!, encoding: .utf8) | ||
let referenceCode = """ | ||
import UIKit | ||
extension UIColor { | ||
static var colorPair1: UIColor { | ||
UIColor { traitCollection -> UIColor in | ||
if traitCollection.userInterfaceStyle == .dark { | ||
return UIColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.000) | ||
} else { | ||
return UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000) | ||
} | ||
} | ||
} | ||
static var colorPair2: UIColor { | ||
return UIColor(red: 0.467, green: 0.012, blue: 1.000, alpha: 0.500) | ||
} | ||
} | ||
""" | ||
XCTAssertEqual(generatedCode, referenceCode) | ||
} | ||
|
||
func testExport_with_assets() { | ||
let output = XcodeColorsOutput(assetsColorsURL: colorsAsssetCatalog, colorSwiftURL: colorsFile) | ||
let exporter = XcodeColorExporter(output: output) | ||
let result = exporter.export(colorPairs: [colorPair1, colorPair2]) | ||
|
||
XCTAssertEqual(result.count, 4) | ||
XCTAssertTrue(result[0].destination.url.absoluteString.hasSuffix("Colors.swift")) | ||
XCTAssertTrue(result[1].destination.url.absoluteString.hasSuffix("Assets.xcassets/Colors/Contents.json")) | ||
XCTAssertTrue(result[2].destination.url.absoluteString.hasSuffix("colorPair1.colorset/Contents.json")) | ||
XCTAssertTrue(result[3].destination.url.absoluteString.hasSuffix("colorPair2.colorset/Contents.json")) | ||
|
||
let content = result[0].data | ||
XCTAssertNotNil(content) | ||
|
||
let generatedCode = String(data: content!, encoding: .utf8) | ||
let referenceCode = """ | ||
import UIKit | ||
extension UIColor { | ||
static var colorPair1: UIColor { return UIColor(named: #function)! } | ||
static var colorPair2: UIColor { return UIColor(named: #function)! } | ||
} | ||
""" | ||
XCTAssertEqual(generatedCode, referenceCode) | ||
} | ||
|
||
} |
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