diff --git a/Sources/ProjectSpec/ProjectSpec.swift b/Sources/ProjectSpec/ProjectSpec.swift index 92480f69..769f5e38 100644 --- a/Sources/ProjectSpec/ProjectSpec.swift +++ b/Sources/ProjectSpec/ProjectSpec.swift @@ -19,7 +19,7 @@ public struct ProjectSpec { public var configFiles: [String: String] public var include: [String] = [] - public struct Options { + public struct Options: Equatable { public var carthageBuildPath: String? public var createIntermediateGroups: Bool public var bundleIdPrefix: String? @@ -28,6 +28,7 @@ public struct ProjectSpec { public var usesTabs: Bool? public var tabWidth: Int? public var indentWidth: Int? + public var xcodeVersion: String? public enum SettingPresets: String { case all @@ -50,7 +51,7 @@ public struct ProjectSpec { } } - public init(carthageBuildPath: String? = nil, createIntermediateGroups: Bool = false, bundleIdPrefix: String? = nil, settingPresets: SettingPresets = .all, developmentLanguage: String? = nil, indentWidth: Int? = nil, tabWidth: Int? = nil, usesTabs: Bool? = nil) { + public init(carthageBuildPath: String? = nil, createIntermediateGroups: Bool = false, bundleIdPrefix: String? = nil, settingPresets: SettingPresets = .all, developmentLanguage: String? = nil, indentWidth: Int? = nil, tabWidth: Int? = nil, usesTabs: Bool? = nil, xcodeVersion: String? = nil) { self.carthageBuildPath = carthageBuildPath self.createIntermediateGroups = createIntermediateGroups self.bundleIdPrefix = bundleIdPrefix @@ -59,6 +60,19 @@ public struct ProjectSpec { self.tabWidth = tabWidth self.indentWidth = indentWidth self.usesTabs = usesTabs + self.xcodeVersion = xcodeVersion + } + + public static func == (lhs: ProjectSpec.Options, rhs: ProjectSpec.Options) -> Bool { + return lhs.carthageBuildPath == rhs.carthageBuildPath && + lhs.bundleIdPrefix == rhs.bundleIdPrefix && + lhs.settingPresets == rhs.settingPresets && + lhs.createIntermediateGroups == rhs.createIntermediateGroups && + lhs.developmentLanguage == rhs.developmentLanguage && + lhs.tabWidth == rhs.tabWidth && + lhs.indentWidth == rhs.indentWidth && + lhs.usesTabs == rhs.usesTabs && + lhs.xcodeVersion == rhs.xcodeVersion } } @@ -122,17 +136,6 @@ extension ProjectSpec: Equatable { } } -extension ProjectSpec.Options: Equatable { - - public static func == (lhs: ProjectSpec.Options, rhs: ProjectSpec.Options) -> Bool { - return lhs.carthageBuildPath == rhs.carthageBuildPath && - lhs.bundleIdPrefix == rhs.bundleIdPrefix && - lhs.settingPresets == rhs.settingPresets && - lhs.createIntermediateGroups == rhs.createIntermediateGroups && - lhs.developmentLanguage == rhs.developmentLanguage - } -} - extension ProjectSpec { public init(basePath: Path, jsonDictionary: JSONDictionary) throws { diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index 64d13766..1c5db15c 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -8,7 +8,6 @@ import ProjectSpec public class PBXProjGenerator { let spec: ProjectSpec - let currentXcodeVersion: String let proj: PBXProj let sourceGenerator: SourceGenerator @@ -27,8 +26,7 @@ public class PBXProjGenerator { return spec.options.carthageBuildPath ?? "Carthage/Build" } - public init(spec: ProjectSpec, currentXcodeVersion: String) { - self.currentXcodeVersion = currentXcodeVersion + public init(spec: ProjectSpec) { self.spec = spec proj = PBXProj(objectVersion: 46, rootObject: referenceGenerator.generate(PBXProject.self, spec.name)) sourceGenerator = SourceGenerator(spec: spec, referenceGenerator: referenceGenerator) { _ in } @@ -111,7 +109,7 @@ public class PBXProjGenerator { sortGroups(group: mainGroup) - let projectAttributes: [String: Any] = ["LastUpgradeCheck": currentXcodeVersion].merged(spec.attributes) + let projectAttributes: [String: Any] = ["LastUpgradeCheck": spec.xcodeVersion].merged(spec.attributes) let root = PBXProject(name: spec.name, reference: proj.rootObject, buildConfigurationList: buildConfigList.reference, diff --git a/Sources/XcodeGenKit/ProjectGenerator.swift b/Sources/XcodeGenKit/ProjectGenerator.swift index 565bb6cc..0e2b4e7f 100644 --- a/Sources/XcodeGenKit/ProjectGenerator.swift +++ b/Sources/XcodeGenKit/ProjectGenerator.swift @@ -8,7 +8,6 @@ import ProjectSpec public class ProjectGenerator { let spec: ProjectSpec - let currentXcodeVersion = "0900" public init(spec: ProjectSpec) { self.spec = spec @@ -24,7 +23,7 @@ public class ProjectGenerator { public func generateProject() throws -> XcodeProj { try spec.validate() - let pbxProjGenerator = PBXProjGenerator(spec: spec, currentXcodeVersion: currentXcodeVersion) + let pbxProjGenerator = PBXProjGenerator(spec: spec) let pbxProject = try pbxProjGenerator.generate() let workspace = try generateWorkspace() let sharedData = try generateSharedData(pbxProject: pbxProject) @@ -87,7 +86,7 @@ public class ProjectGenerator { let archiveAction = XCScheme.ArchiveAction(buildConfiguration: scheme.archive?.config ?? defaultReleaseConfig.name, revealArchiveInOrganizer: true) return XCScheme(name: scheme.name, - lastUpgradeVersion: currentXcodeVersion, + lastUpgradeVersion: spec.xcodeVersion, version: "1.3", buildAction: buildAction, testAction: testAction, diff --git a/Sources/XcodeGenKit/Version.swift b/Sources/XcodeGenKit/Version.swift new file mode 100644 index 00000000..fcf7c3f9 --- /dev/null +++ b/Sources/XcodeGenKit/Version.swift @@ -0,0 +1,42 @@ +import Foundation +import ProjectSpec + +extension ProjectSpec { + + var xcodeVersion: String { + return XCodeVersion.parse(options.xcodeVersion ?? "9.0") + } +} + +public struct XCodeVersion { + + public static func parse(_ version: String) -> String { + if version.contains(".") { + let parts = version.split(separator: ".").map(String.init) + var string = "" + let major = parts[0] + if major.count == 1 { + string = "0\(major)" + } else { + string = major + } + + let minor = parts[1] + string += minor + + if parts.count > 2 { + let patch = parts[2] + string += patch + } else { + string += "0" + } + return string + } else if version.count == 2 { + return "\(version)00" + } else if version.count == 1 { + return "0\(version)00" + } else { + return version + } + } +} diff --git a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift index 1f72c7d6..428255b9 100644 --- a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift @@ -62,6 +62,24 @@ func projectGeneratorTests() { } try expect(pbxProject.developmentRegion) == "de" } + + $0.it("formats xcode version") { + let versions: [String: String] = [ + "0900": "0900", + "1010": "1010", + "9": "0900", + "9.0": "0900", + "9.1": "0910", + "9.1.1": "0911", + "10": "1000", + "10.1": "1010", + "10.1.2": "1012", + ] + + for (version, expected) in versions { + try expect(XCodeVersion.parse(version)) == expected + } + } } $0.describe("Config") {