Skip to content

Commit

Permalink
Refactor minimumXcodeGenVersion to be less pervasive
Browse files Browse the repository at this point in the history
  • Loading branch information
brentleyjones committed Jul 30, 2018
1 parent 7d41f9e commit 67c87d7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 24 deletions.
7 changes: 1 addition & 6 deletions Sources/ProjectSpec/SpecValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ import PathKit

extension Project {

public func validate(xcodeGenVersion: Version) throws {
public func validate() throws {

var errors: [SpecValidationError.ValidationError] = []

if let minimumXcodeGenVersion = options.minimumXcodeGenVersion, xcodeGenVersion < minimumXcodeGenVersion {
errors.append(.invalidXcodeGenVersion(version: xcodeGenVersion, minimumVersion: minimumXcodeGenVersion))
}

func validateSettings(_ settings: Settings) -> [SpecValidationError.ValidationError] {
var errors: [SpecValidationError.ValidationError] = []
for group in settings.groups {
Expand Down
4 changes: 2 additions & 2 deletions Sources/ProjectSpec/SpecValidationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public struct SpecValidationError: Error, CustomStringConvertible {
public var errors: [ValidationError]

public enum ValidationError: Error, CustomStringConvertible {
case invalidXcodeGenVersion(version: Version, minimumVersion: Version)
case invalidXcodeGenVersion(minimumVersion: Version, version: Version)
case invalidTargetDependency(target: String, dependency: String)
case invalidTargetSource(target: String, source: String)
case invalidTargetConfigFile(target: String, configFile: String, config: String)
Expand All @@ -24,7 +24,7 @@ public struct SpecValidationError: Error, CustomStringConvertible {

public var description: String {
switch self {
case let .invalidXcodeGenVersion(version, minimumVersion):
case let .invalidXcodeGenVersion(minimumVersion, version):
return "XcodeGen version is \(version), but minimum required version specified as \(minimumVersion)"
case let .invalidTargetDependency(target, dependency):
return "Target \(target.quoted) has invalid dependency: \(dependency.quoted)"
Expand Down
5 changes: 4 additions & 1 deletion Sources/XcodeGen/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ func generate(spec: String, project: String, isQuiet: Bool, justVersion: Bool) {

do {
logger.info("⚙️ Generating project...")

try project.validateMinimumXcodeGenVersion(version)

let projectGenerator = ProjectGenerator(project: project)
let xcodeProject = try projectGenerator.generateXcodeProject(xcodeGenVersion: version)
let xcodeProject = try projectGenerator.generateXcodeProject()

logger.info("⚙️ Writing project...")

Expand Down
4 changes: 2 additions & 2 deletions Sources/XcodeGenKit/ProjectGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class ProjectGenerator {
return project.configs.first { $0.type == .release }!
}

public func generateXcodeProject(xcodeGenVersion: Version) throws -> XcodeProj {
try project.validate(xcodeGenVersion: xcodeGenVersion)
public func generateXcodeProject() throws -> XcodeProj {
try project.validate()
let pbxProjGenerator = PBXProjGenerator(project: project)
let pbxProject = try pbxProjGenerator.generate()
let workspace = try generateWorkspace()
Expand Down
6 changes: 6 additions & 0 deletions Sources/XcodeGenKit/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ extension Project {
var schemeVersion: String {
return "1.3"
}

public func validateMinimumXcodeGenVersion(_ xcodeGenVersion: Version) throws {
if let minimumXcodeGenVersion = options.minimumXcodeGenVersion, xcodeGenVersion < minimumXcodeGenVersion {
throw SpecValidationError.ValidationError.invalidXcodeGenVersion(minimumVersion: minimumXcodeGenVersion, version: xcodeGenVersion)
}
}
}

public struct XCodeVersion {
Expand Down
8 changes: 4 additions & 4 deletions Tests/XcodeGenKitTests/GeneratorHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import Yams

extension Project {

func generateXcodeProject(xcodeGenVersion: Version = try! Version("1.11.0"), file: String = #file, line: Int = #line) throws -> XcodeProj {
func generateXcodeProject(file: String = #file, line: Int = #line) throws -> XcodeProj {
return try doThrowing(file: file, line: line) {
let generator = ProjectGenerator(project: self)
return try generator.generateXcodeProject(xcodeGenVersion: xcodeGenVersion)
return try generator.generateXcodeProject()
}
}

func generatePbxProj(xcodeGenVersion: Version = try! Version("1.11.0"), file: String = #file, line: Int = #line) throws -> PBXProj {
func generatePbxProj(file: String = #file, line: Int = #line) throws -> PBXProj {
return try doThrowing(file: file, line: line) {
let xcodeProject = try generateXcodeProject(xcodeGenVersion: xcodeGenVersion).pbxproj
let xcodeProject = try generateXcodeProject().pbxproj
try xcodeProject.validate()
return xcodeProject
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/XcodeGenKitTests/ProjectFixtureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class ProjectFixtureTests: XCTestCase {
}
}

fileprivate func generateXcodeProject(xcodeGenVersion: Version = try! Version("1.11.0"), specPath: Path, projectPath: Path, file: String = #file, line: Int = #line) throws -> XcodeProj {
fileprivate func generateXcodeProject(specPath: Path, projectPath: Path, file: String = #file, line: Int = #line) throws -> XcodeProj {
let project = try Project(path: specPath)
let generator = ProjectGenerator(project: project)
let xcodeProject = try generator.generateXcodeProject(xcodeGenVersion: xcodeGenVersion)
let xcodeProject = try generator.generateXcodeProject()
let oldProject = try XcodeProj(path: projectPath)
let pbxProjPath = projectPath + XcodeProj.pbxprojPath(projectPath)
let oldProjectString: String = try pbxProjPath.read()
Expand Down
20 changes: 13 additions & 7 deletions Tests/XcodeGenKitTests/ProjectSpecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class ProjectSpecTests: XCTestCase {
var project = baseProject
project.options = SpecOptions(minimumXcodeGenVersion: minimumVersion)

try expectValidationError(xcodeGenVersion: try! Version("1.11.0"), project, .invalidXcodeGenVersion(version: try! Version("1.11.0"), minimumVersion: minimumVersion))
try expectValidationError(xcodeGenVersion: try! Version("1.10.99"), project, .invalidXcodeGenVersion(version: try! Version("1.10.99"), minimumVersion: minimumVersion))
try expectValidationError(xcodeGenVersion: try! Version("0.99"), project, .invalidXcodeGenVersion(version: try! Version("0.99"), minimumVersion: minimumVersion))
try expectMinimumXcodeGenVersionError(project, minimumVersion: minimumVersion, xcodeGenVersion: try! Version("1.11.0"))
try expectMinimumXcodeGenVersionError(project, minimumVersion: minimumVersion, xcodeGenVersion: try! Version("1.10.99"))
try expectMinimumXcodeGenVersionError(project, minimumVersion: minimumVersion, xcodeGenVersion: try! Version("0.99"))
}

$0.it("fails with invalid project") {
Expand All @@ -109,7 +109,7 @@ class ProjectSpecTests: XCTestCase {
project.options = SpecOptions(disabledValidations: [.missingConfigs])
let configPath = fixturePath + "test.xcconfig"
project.configFiles = ["missingConfiguration": configPath.string]
try project.validate(xcodeGenVersion: try Version("1.11.0"))
try project.validate()
}

$0.it("fails with invalid target") {
Expand Down Expand Up @@ -190,7 +190,7 @@ class ProjectSpecTests: XCTestCase {
platform: .iOS,
sources: [.init(path: "generated.swift", optional: true)]
)]
try project.validate(xcodeGenVersion: try Version("1.11.0"))
try project.validate()
}

$0.it("validates missing default configurations") {
Expand All @@ -202,9 +202,9 @@ class ProjectSpecTests: XCTestCase {
}
}

fileprivate func expectValidationError(xcodeGenVersion: Version = try! Version("1.11.0"), _ project: Project, _ expectedError: SpecValidationError.ValidationError, file: String = #file, line: Int = #line) throws {
fileprivate func expectValidationError(_ project: Project, _ expectedError: SpecValidationError.ValidationError, file: String = #file, line: Int = #line) throws {
do {
try project.validate(xcodeGenVersion: xcodeGenVersion)
try project.validate()
} catch let error as SpecValidationError {
if !error.errors
.contains(where: { $0.description == expectedError.description }) {
Expand All @@ -216,3 +216,9 @@ fileprivate func expectValidationError(xcodeGenVersion: Version = try! Version("
}
throw failure("Supposed to fail with \"\(expectedError)\"", file: file, line: line)
}

fileprivate func expectMinimumXcodeGenVersionError(_ project: Project, minimumVersion: Version, xcodeGenVersion: Version, file: String = #file, line: Int = #line) throws {
try expectError(SpecValidationError.ValidationError.invalidXcodeGenVersion(minimumVersion: minimumVersion, version: xcodeGenVersion), file: file, line: line) {
try project.validateMinimumXcodeGenVersion(xcodeGenVersion)
}
}

0 comments on commit 67c87d7

Please sign in to comment.