diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d71d5..11d180b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,9 @@ _None_ ### Breaking Changes -_None_ +* Templates are now organized in subdirectories for each command. + [@djbe](https://github.com/djbe) + [#13](https://github.com/SwiftGen/templates/issues/13) ### New Features diff --git a/Tests/TemplatesTests/ColorsTests.swift b/Tests/TemplatesTests/ColorsTests.swift index cfbfc0a..ad5aaf8 100644 --- a/Tests/TemplatesTests/ColorsTests.swift +++ b/Tests/TemplatesTests/ColorsTests.swift @@ -27,25 +27,22 @@ class ColorsTests: XCTestCase { } func testDefault() { - test(template: "colors-default", + test(template: "default", contextNames: Contexts.all, - outputPrefix: "default", directory: .colors, contextVariations: variations) } func testRawValue() { - test(template: "colors-rawValue", + test(template: "rawValue", contextNames: Contexts.rawOnly, - outputPrefix: "rawValue", directory: .colors, contextVariations: variations) } func testSwift3() { - test(template: "colors-swift3", + test(template: "swift3", contextNames: Contexts.all, - outputPrefix: "swift3", directory: .colors, contextVariations: variations) } diff --git a/Tests/TemplatesTests/FontsTests.swift b/Tests/TemplatesTests/FontsTests.swift index c3bc558..02da6a9 100644 --- a/Tests/TemplatesTests/FontsTests.swift +++ b/Tests/TemplatesTests/FontsTests.swift @@ -28,17 +28,15 @@ class FontsTests: XCTestCase { } func testDefault() { - test(template: "fonts-default", + test(template: "default", contextNames: Contexts.all, - outputPrefix: "default", directory: .fonts, contextVariations: variations) } func testSwift3() { - test(template: "fonts-swift3", + test(template: "swift3", contextNames: Contexts.all, - outputPrefix: "swift3", directory: .fonts, contextVariations: variations) } diff --git a/Tests/TemplatesTests/ImagesTests.swift b/Tests/TemplatesTests/ImagesTests.swift index 90aa7c7..eb30f7a 100644 --- a/Tests/TemplatesTests/ImagesTests.swift +++ b/Tests/TemplatesTests/ImagesTests.swift @@ -26,41 +26,36 @@ class ImagesTests: XCTestCase { } func testDefault() { - test(template: "images-default", + test(template: "default", contextNames: Contexts.all, - outputPrefix: "default", directory: .images, contextVariations: variations) } func testAllValues() { - test(template: "images-allvalues", + test(template: "allvalues", contextNames: Contexts.all, - outputPrefix: "allvalues", directory: .images, contextVariations: variations) } func testSwift3() { - test(template: "images-swift3", + test(template: "swift3", contextNames: Contexts.all, - outputPrefix: "swift3", directory: .images, contextVariations: variations) } func testDotSyntax() { - test(template: "images-dot-syntax", + test(template: "dot-syntax", contextNames: Contexts.all, - outputPrefix: "dot-syntax", directory: .images, contextVariations: variations) } func testDotSyntaxSwift3() { - test(template: "images-dot-syntax-swift3", + test(template: "dot-syntax-swift3", contextNames: Contexts.all, - outputPrefix: "dot-syntax-swift3", directory: .images, contextVariations: variations) } diff --git a/Tests/TemplatesTests/StoryboardsMacOSTests.swift b/Tests/TemplatesTests/StoryboardsMacOSTests.swift index a35fda7..3cc097a 100644 --- a/Tests/TemplatesTests/StoryboardsMacOSTests.swift +++ b/Tests/TemplatesTests/StoryboardsMacOSTests.swift @@ -46,26 +46,29 @@ class StoryboardsMacOSTests: XCTestCase { } func testDefault() { - test(template: "storyboards-osx-default", + test(template: "osx-default", contextNames: Contexts.all, outputPrefix: "default", - directory: .storyboardsMacOS, + directory: .storyboards, + resourceDirectory: .storyboardsMacOS, contextVariations: variations) } func testSwift3() { - test(template: "storyboards-osx-swift3", + test(template: "osx-swift3", contextNames: Contexts.all, outputPrefix: "swift3", - directory: .storyboardsMacOS, + directory: .storyboards, + resourceDirectory: .storyboardsMacOS, contextVariations: variations) } func testLowercase() { - test(template: "storyboards-osx-lowercase", + test(template: "osx-lowercase", contextNames: Contexts.all, outputPrefix: "lowercase", - directory: .storyboardsMacOS, + directory: .storyboards, + resourceDirectory: .storyboardsMacOS, contextVariations: variations) } } diff --git a/Tests/TemplatesTests/StoryboardsiOSTests.swift b/Tests/TemplatesTests/StoryboardsiOSTests.swift index 5834099..f5177a1 100644 --- a/Tests/TemplatesTests/StoryboardsiOSTests.swift +++ b/Tests/TemplatesTests/StoryboardsiOSTests.swift @@ -41,34 +41,34 @@ class StoryboardsiOSTests: XCTestCase { } func testDefault() { - test(template: "storyboards-default", + test(template: "default", contextNames: Contexts.all, - outputPrefix: "default", - directory: .storyboardsiOS, + directory: .storyboards, + resourceDirectory: .storyboardsiOS, contextVariations: variations) } func testSwift3() { - test(template: "storyboards-swift3", + test(template: "swift3", contextNames: Contexts.all, - outputPrefix: "swift3", - directory: .storyboardsiOS, + directory: .storyboards, + resourceDirectory: .storyboardsiOS, contextVariations: variations) } func testLowercase() { - test(template: "storyboards-lowercase", + test(template: "lowercase", contextNames: Contexts.all, - outputPrefix: "lowercase", - directory: .storyboardsiOS, + directory: .storyboards, + resourceDirectory: .storyboardsiOS, contextVariations: variations) } func testUppercase() { - test(template: "storyboards-uppercase", + test(template: "uppercase", contextNames: Contexts.all, - outputPrefix: "uppercase", - directory: .storyboardsiOS, + directory: .storyboards, + resourceDirectory: .storyboardsiOS, contextVariations: variations) } } diff --git a/Tests/TemplatesTests/StringsTests.swift b/Tests/TemplatesTests/StringsTests.swift index dda71df..2dd0785 100644 --- a/Tests/TemplatesTests/StringsTests.swift +++ b/Tests/TemplatesTests/StringsTests.swift @@ -26,57 +26,50 @@ class StringsTests: XCTestCase { } func testDefault() { - test(template: "strings-default", + test(template: "default", contextNames: Contexts.all, - outputPrefix: "default", directory: .strings, contextVariations: variations) } func testSwift3() { - test(template: "strings-swift3", + test(template: "swift3", contextNames: Contexts.all, - outputPrefix: "swift3", directory: .strings, contextVariations: variations) } func testNoCommentsSwift3() { - test(template: "strings-no-comments-swift3", + test(template: "no-comments-swift3", contextNames: Contexts.all, - outputPrefix: "no-comments-swift3", directory: .strings, contextVariations: variations) } func testGenstrings() { - test(template: "strings-genstrings", + test(template: "genstrings", contextNames: Contexts.all, - outputPrefix: "genstrings", directory: .strings, contextVariations: variations) } func testStructured() { - test(template: "strings-structured", + test(template: "structured", contextNames: Contexts.all, - outputPrefix: "structured", directory: .strings, contextVariations: variations) } func testDotSyntax() { - test(template: "strings-dot-syntax", + test(template: "dot-syntax", contextNames: Contexts.all, - outputPrefix: "dot-syntax", directory: .strings, contextVariations: variations) } func testDotSyntaxSwift3() { - test(template: "strings-dot-syntax-swift3", + test(template: "dot-syntax-swift3", contextNames: Contexts.all, - outputPrefix: "dot-syntax-swift3", directory: .strings, contextVariations: variations) } diff --git a/Tests/TemplatesTests/TestsHelper.swift b/Tests/TemplatesTests/TestsHelper.swift index a951bbe..e8b533f 100644 --- a/Tests/TemplatesTests/TestsHelper.swift +++ b/Tests/TemplatesTests/TestsHelper.swift @@ -71,6 +71,7 @@ class Fixtures { case colors = "Colors" case fonts = "Fonts" case images = "Images" + case storyboards = "Storyboards" case storyboardsiOS = "Storyboards-iOS" case storyboardsMacOS = "Storyboards-macOS" case strings = "Strings" @@ -110,8 +111,8 @@ class Fixtures { return data } - static func template(for name: String) -> String { - return string(for: name, subDirectory: "templates") + static func template(for name: String, sub: Directory) -> String { + return string(for: name, subDirectory: "templates/\(sub.rawValue.lowercased())") } static func output(for name: String, sub: Directory) -> String { @@ -142,24 +143,32 @@ extension XCTestCase { - Parameter template: The name of the template (without the `stencil` extension) - Parameter contextNames: A list of context names (without the `plist` extension) - - Parameter outputPrefix: Prefix for the output files, becomes "{outputPrefix}-context-{contextName}.swift" + - Parameter outputPrefix: Prefix for the output files, becomes "{outputPrefix}-context-{contextName}.swift". + Defaults to template name. - Parameter directory: The directory to look for files in (correspons to de command) + - Parameter resourceDirectory: The directory to look for files in (corresponds to de command) - Parameter contextVariations: Optional closure to generate context variations. */ func test(template templateName: String, contextNames: [String], - outputPrefix: String, + outputPrefix: String? = nil, directory: Fixtures.Directory, + resourceDirectory: Fixtures.Directory? = nil, file: StaticString = #file, line: UInt = #line, contextVariations: VariationGenerator? = nil) { - let template = StencilSwiftTemplate(templateString: Fixtures.template(for: "\(templateName).stencil"), + let templateString = Fixtures.template(for: "\(templateName).stencil", sub: directory) + let template = StencilSwiftTemplate(templateString: templateString, environment: stencilSwiftEnvironment()) + + // default values let contextVariations = contextVariations ?? { [(context: $1, suffix: "")] } + let prefix = outputPrefix ?? templateName + let resourceDir = resourceDirectory ?? directory for contextName in contextNames { print("Testing context '\(contextName)'...") - let context = Fixtures.context(for: "\(contextName).plist", sub: directory) + let context = Fixtures.context(for: "\(contextName).plist", sub: resourceDir) // generate context variations guard let variations = try? contextVariations(contextName, context) else { @@ -167,13 +176,13 @@ extension XCTestCase { } for (index, (context: context, suffix: suffix)) in variations.enumerated() { - let outputFile = "\(outputPrefix)-context-\(contextName)\(suffix).swift" + let outputFile = "\(prefix)-context-\(contextName)\(suffix).swift" if variations.count > 1 { print(" - Variation #\(index)... (expecting: \(outputFile))") } guard let result = try? template.render(context) else { fatalError("Unable to render template") } - let expected = Fixtures.output(for: outputFile, sub: directory) + let expected = Fixtures.output(for: outputFile, sub: resourceDir) XCTDiffStrings(result, expected, file: file, line: line) } } diff --git a/templates/colors-default.stencil b/templates/colors/default.stencil similarity index 100% rename from templates/colors-default.stencil rename to templates/colors/default.stencil diff --git a/templates/colors-rawValue.stencil b/templates/colors/rawValue.stencil similarity index 100% rename from templates/colors-rawValue.stencil rename to templates/colors/rawValue.stencil diff --git a/templates/colors-swift3.stencil b/templates/colors/swift3.stencil similarity index 100% rename from templates/colors-swift3.stencil rename to templates/colors/swift3.stencil diff --git a/templates/fonts-default.stencil b/templates/fonts/default.stencil similarity index 100% rename from templates/fonts-default.stencil rename to templates/fonts/default.stencil diff --git a/templates/fonts-swift3.stencil b/templates/fonts/swift3.stencil similarity index 100% rename from templates/fonts-swift3.stencil rename to templates/fonts/swift3.stencil diff --git a/templates/images-allvalues.stencil b/templates/images/allvalues.stencil similarity index 100% rename from templates/images-allvalues.stencil rename to templates/images/allvalues.stencil diff --git a/templates/images-default.stencil b/templates/images/default.stencil similarity index 100% rename from templates/images-default.stencil rename to templates/images/default.stencil diff --git a/templates/images-dot-syntax-swift3.stencil b/templates/images/dot-syntax-swift3.stencil similarity index 100% rename from templates/images-dot-syntax-swift3.stencil rename to templates/images/dot-syntax-swift3.stencil diff --git a/templates/images-dot-syntax.stencil b/templates/images/dot-syntax.stencil similarity index 100% rename from templates/images-dot-syntax.stencil rename to templates/images/dot-syntax.stencil diff --git a/templates/images-swift3.stencil b/templates/images/swift3.stencil similarity index 100% rename from templates/images-swift3.stencil rename to templates/images/swift3.stencil diff --git a/templates/storyboards-default.stencil b/templates/storyboards/default.stencil similarity index 100% rename from templates/storyboards-default.stencil rename to templates/storyboards/default.stencil diff --git a/templates/storyboards-lowercase.stencil b/templates/storyboards/lowercase.stencil similarity index 100% rename from templates/storyboards-lowercase.stencil rename to templates/storyboards/lowercase.stencil diff --git a/templates/storyboards-osx-default.stencil b/templates/storyboards/osx-default.stencil similarity index 100% rename from templates/storyboards-osx-default.stencil rename to templates/storyboards/osx-default.stencil diff --git a/templates/storyboards-osx-lowercase.stencil b/templates/storyboards/osx-lowercase.stencil similarity index 100% rename from templates/storyboards-osx-lowercase.stencil rename to templates/storyboards/osx-lowercase.stencil diff --git a/templates/storyboards-osx-swift3.stencil b/templates/storyboards/osx-swift3.stencil similarity index 100% rename from templates/storyboards-osx-swift3.stencil rename to templates/storyboards/osx-swift3.stencil diff --git a/templates/storyboards-swift3.stencil b/templates/storyboards/swift3.stencil similarity index 100% rename from templates/storyboards-swift3.stencil rename to templates/storyboards/swift3.stencil diff --git a/templates/storyboards-uppercase.stencil b/templates/storyboards/uppercase.stencil similarity index 100% rename from templates/storyboards-uppercase.stencil rename to templates/storyboards/uppercase.stencil diff --git a/templates/strings-default.stencil b/templates/strings/default.stencil similarity index 100% rename from templates/strings-default.stencil rename to templates/strings/default.stencil diff --git a/templates/strings-dot-syntax-swift3.stencil b/templates/strings/dot-syntax-swift3.stencil similarity index 100% rename from templates/strings-dot-syntax-swift3.stencil rename to templates/strings/dot-syntax-swift3.stencil diff --git a/templates/strings-dot-syntax.stencil b/templates/strings/dot-syntax.stencil similarity index 100% rename from templates/strings-dot-syntax.stencil rename to templates/strings/dot-syntax.stencil diff --git a/templates/strings-genstrings.stencil b/templates/strings/genstrings.stencil similarity index 100% rename from templates/strings-genstrings.stencil rename to templates/strings/genstrings.stencil diff --git a/templates/strings-no-comments-swift3.stencil b/templates/strings/no-comments-swift3.stencil similarity index 100% rename from templates/strings-no-comments-swift3.stencil rename to templates/strings/no-comments-swift3.stencil diff --git a/templates/strings-structured.stencil b/templates/strings/structured.stencil similarity index 100% rename from templates/strings-structured.stencil rename to templates/strings/structured.stencil diff --git a/templates/strings-swift3.stencil b/templates/strings/swift3.stencil similarity index 100% rename from templates/strings-swift3.stencil rename to templates/strings/swift3.stencil