Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Context generator #14

Merged
merged 14 commits into from
May 7, 2017
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ _None_
[David Jennes](https://github.com/djbe)
[#10](https://github.com/SwiftGen/SwiftGenKit/issues/10)
[#28](https://github.com/SwiftGen/SwiftGenKit/issues/28)
* We can now re-generate the contexts used for testing by using the "Generate Contexts" Xcode scheme.
[David Jennes](https://github.com/djbe)
[#14](https://github.com/SwiftGen/SwiftGenKit/issues/14)
* Documented the input & output of each parser.
[David Jennes](https://github.com/djbe)
[#24](https://github.com/SwiftGen/SwiftGenKit/issues/24)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ Each parser provided by this framework has a corresponding documentation file ex
* [Fonts](Documentation/Fonts.md)
* [Storyboards](Documentation/Storyboards.md)
* [Strings](Documentation/Strings.md)

## Contributing

Please check the [CONTRIBUTING file](https://github.com/SwiftGen/SwiftGen/blob/master/CONTRIBUTING.md) for guidelines on how to contribute to this repository.

During development, should you make changes to the code generating context, you can re-generate all the context files instead of modifying them manually. Use either of these methods:
- In Xcode, select the "Generate Contexts" scheme and run the tests
- From Terminal, execute `rake generate_contexts`
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ CONFIGURATION = 'Debug'
POD_NAME = 'SwiftGenKit'


desc 'Generate Test Contexts'
task :generate_contexts => "xcode:build" do |task|
Utils.print_header 'Generating contexts...'
Utils.run(%Q(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "Generate Contexts" -configuration "#{CONFIGURATION}" test-without-building), task, xcrun: true, formatter: :xcpretty)
end

task :default => 'xcode:test'
2 changes: 1 addition & 1 deletion Sources/Parsers/AssetsCatalogParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ extension AssetsCatalogParser {
- Returns: An array of dictionaries, representing the tree of nodes in the catalog.
*/
fileprivate func loadAssetCatalog(at path: Path) -> [[String: Any]]? {
let command = Command("xcrun", arguments: "actool", "--print-contents", path.description)
let command = Command("xcrun", arguments: "actool", "--print-contents", path.string)
let output = command.execute() as Data

// try to parse plist
Expand Down
2 changes: 1 addition & 1 deletion Sources/Parsers/ColorsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public final class ColorsCLRFileParser: ColorsFileParser {
public init() {}

public func parseFile(at path: Path) throws {
if let colorsList = NSColorList(name: "UserColors", fromFile: path.description) {
if let colorsList = NSColorList(name: "UserColors", fromFile: path.string) {
for colorName in colorsList.allKeys {
colors[colorName] = colorsList.color(withKey: colorName)?.rgbColor?.hexValue
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Parsers/StringsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import Foundation
import PathKit

public enum StringsFileParserError: Error, CustomStringConvertible {
case FailureOnLoading(path: String)
case InvalidFormat
case failureOnLoading(path: String)
case invalidFormat

public var description: String {
switch self {
case .FailureOnLoading(let path):
case .failureOnLoading(let path):
return "Failed to load a file at \"\(path)\""
case .InvalidFormat:
case .invalidFormat:
return "Invalid strings file"
}
}
Expand All @@ -33,14 +33,14 @@ public final class StringsFileParser {
// Localizable.strings files are generally UTF16, not UTF8!
public func parseFile(at path: Path) throws {
guard let data = try? path.read() else {
throw StringsFileParserError.FailureOnLoading(path: path.description)
throw StringsFileParserError.failureOnLoading(path: path.string)
}

let plist = try PropertyListSerialization
.propertyList(from: data, format: nil)

guard let dict = plist as? [String: String] else {
throw StringsFileParserError.InvalidFormat
throw StringsFileParserError.invalidFormat
}

for (key, translation) in dict {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "09A87B4F1BCCA2C600D9B9F5"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:SwiftGenKit.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<EnvironmentVariables>
<EnvironmentVariable
key = "GENERATE_CONTEXTS"
value = "YES"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
12 changes: 3 additions & 9 deletions Tests/SwiftGenKitTests/ColorsCLRFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,23 @@ class ColorsCLRFileTests: XCTestCase {
let parser = ColorsCLRFileParser()

let result = parser.stencilContext()
let expected = Fixtures.context(for: "empty.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "empty.plist", sub: .colors)
}

func testFileWithDefaults() throws {
let parser = ColorsCLRFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.clr", sub: .colors))

let result = parser.stencilContext()
let expected = Fixtures.context(for: "defaults.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "defaults.plist", sub: .colors)
}

func testFileWithCustomName() throws {
let parser = ColorsCLRFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.clr", sub: .colors))

let result = parser.stencilContext(enumName: "XCTColors")
let expected = Fixtures.context(for: "customname.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "customname.plist", sub: .colors)
}

func testFileWithBadFile() {
Expand Down
12 changes: 3 additions & 9 deletions Tests/SwiftGenKitTests/ColorsJSONFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,23 @@ class ColorsJSONFileTests: XCTestCase {
let parser = ColorsJSONFileParser()

let result = parser.stencilContext()
let expected = Fixtures.context(for: "empty.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "empty.plist", sub: .colors)
}

func testFileWithDefaults() throws {
let parser = ColorsJSONFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.json", sub: .colors))

let result = parser.stencilContext()
let expected = Fixtures.context(for: "defaults.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "defaults.plist", sub: .colors)
}

func testFileWithCustomName() throws {
let parser = ColorsJSONFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.json", sub: .colors))

let result = parser.stencilContext(enumName: "XCTColors")
let expected = Fixtures.context(for: "customname.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "customname.plist", sub: .colors)
}

func testFileWithBadSyntax() {
Expand Down
16 changes: 4 additions & 12 deletions Tests/SwiftGenKitTests/ColorsTextFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class ColorsTextFileTests: XCTestCase {
let parser = ColorsTextFileParser()

let result = parser.stencilContext()
let expected = Fixtures.context(for: "empty.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "empty.plist", sub: .colors)
}

func testListWithDefaults() throws {
Expand All @@ -26,29 +24,23 @@ class ColorsTextFileTests: XCTestCase {
try parser.addColor(named: "ArticleBackground", value: "#ffcc0099")

let result = parser.stencilContext()
let expected = Fixtures.context(for: "entries.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "entries.plist", sub: .colors)
}

func testFileWithDefaults() throws {
let parser = ColorsTextFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.txt", sub: .colors))

let result = parser.stencilContext()
let expected = Fixtures.context(for: "text-defaults.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "text-defaults.plist", sub: .colors)
}

func testFileWithCustomName() throws {
let parser = ColorsTextFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.txt", sub: .colors))

let result = parser.stencilContext(enumName: "XCTColors")
let expected = Fixtures.context(for: "text-customname.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "text-customname.plist", sub: .colors)
}

func testFileWithBadSyntax() {
Expand Down
12 changes: 3 additions & 9 deletions Tests/SwiftGenKitTests/ColorsXMLFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,23 @@ class ColorsXMLFileTests: XCTestCase {
let parser = ColorsXMLFileParser()

let result = parser.stencilContext()
let expected = Fixtures.context(for: "empty.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "empty.plist", sub: .colors)
}

func testFileWithDefaults() throws {
let parser = ColorsXMLFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.xml", sub: .colors))

let result = parser.stencilContext()
let expected = Fixtures.context(for: "defaults.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "defaults.plist", sub: .colors)
}

func testFileWithCustomName() throws {
let parser = ColorsXMLFileParser()
try parser.parseFile(at: Fixtures.path(for: "colors.xml", sub: .colors))

let result = parser.stencilContext(enumName: "XCTColors")
let expected = Fixtures.context(for: "customname.plist", sub: .colors)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "customname.plist", sub: .colors)
}

func testFileWithBadSyntax() {
Expand Down
12 changes: 3 additions & 9 deletions Tests/SwiftGenKitTests/FontsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,22 @@ class FontsTests: XCTestCase {
let parser = FontsFileParser()

let result = parser.stencilContext()
let expected = Fixtures.context(for: "empty.plist", sub: .fonts)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "empty.plist", sub: .fonts)
}

func testDefaults() {
let parser = FontsFileParser()
parser.parseFile(at: Fixtures.directory(sub: .fonts))

let result = parser.stencilContext()
let expected = Fixtures.context(for: "defaults.plist", sub: .fonts)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "defaults.plist", sub: .fonts)
}

func testCustomName() {
let parser = FontsFileParser()
parser.parseFile(at: Fixtures.directory(sub: .fonts))

let result = parser.stencilContext(enumName: "CustomFamily")
let expected = Fixtures.context(for: "customname.plist", sub: .fonts)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "customname.plist", sub: .fonts)
}
}
12 changes: 3 additions & 9 deletions Tests/SwiftGenKitTests/ImagesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,22 @@ class ImagesTests: XCTestCase {
let parser = AssetsCatalogParser()

let result = parser.stencilContext()
let expected = Fixtures.context(for: "empty.plist", sub: .images)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "empty.plist", sub: .images)
}

func testFileWithDefaults() {
let parser = AssetsCatalogParser()
parser.parseCatalog(at: Fixtures.path(for: "Images.xcassets", sub: .images))

let result = parser.stencilContext()
let expected = Fixtures.context(for: "defaults.plist", sub: .images)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "defaults.plist", sub: .images)
}

func testFileWithCustomName() {
let parser = AssetsCatalogParser()
parser.parseCatalog(at: Fixtures.path(for: "Images.xcassets", sub: .images))

let result = parser.stencilContext(enumName: "XCTImages")
let expected = Fixtures.context(for: "customname.plist", sub: .images)

XCTDiffContexts(result, expected)
XCTDiffContexts(result, expected: "customname.plist", sub: .images)
}
}
Loading