From d5649a1bd31a5de484e9b521dbbcf45effe3dd2d Mon Sep 17 00:00:00 2001 From: ryohey Date: Thu, 4 Apr 2019 23:42:06 +0900 Subject: [PATCH] Allow nil values --- Sources/ProjectSpec/AggregateTarget.swift | 13 +-- Sources/ProjectSpec/BuildRule.swift | 7 +- Sources/ProjectSpec/BuildScript.swift | 16 ++-- Sources/ProjectSpec/Dependency.swift | 15 ++-- Sources/ProjectSpec/DeploymentTarget.swift | 22 ++--- Sources/ProjectSpec/Encoding.swift | 2 +- Sources/ProjectSpec/Scheme.swift | 93 ++++++---------------- Sources/ProjectSpec/SpecOptions.swift | 42 +++------- Sources/ProjectSpec/Target.swift | 52 ++++-------- Sources/ProjectSpec/TargetSource.swift | 22 ++--- 10 files changed, 80 insertions(+), 204 deletions(-) diff --git a/Sources/ProjectSpec/AggregateTarget.swift b/Sources/ProjectSpec/AggregateTarget.swift index 68e23a32..d861958f 100644 --- a/Sources/ProjectSpec/AggregateTarget.swift +++ b/Sources/ProjectSpec/AggregateTarget.swift @@ -64,19 +64,14 @@ extension AggregateTarget: NamedJSONDictionaryConvertible { extension AggregateTarget: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + return [ "settings": settings.toJSONValue(), "targets": targets, "configFiles": configFiles, "attributes": attributes, - "buildScripts": buildScripts.map { $0.toJSONValue() } - ] - - if let scheme = scheme { - dict["scheme"] = scheme.toJSONValue() - } - - return dict + "buildScripts": buildScripts.map { $0.toJSONValue() }, + "scheme": scheme?.toJSONValue() + ] as [String: Any?] } } diff --git a/Sources/ProjectSpec/BuildRule.swift b/Sources/ProjectSpec/BuildRule.swift index 9c4e3cea..3f75e75f 100644 --- a/Sources/ProjectSpec/BuildRule.swift +++ b/Sources/ProjectSpec/BuildRule.swift @@ -83,15 +83,12 @@ extension BuildRule: JSONObjectConvertible { extension BuildRule: JSONEncodable { public func toJSONValue() -> Any { - var dict: [String: Any] = [ + var dict: [String: Any?] = [ "outputFiles": outputFiles, "outputFilesCompilerFlags": outputFilesCompilerFlags, + "name": name ] - if let name = name { - dict["name"] = name - } - switch fileType { case .pattern(let string): dict["filePattern"] = string diff --git a/Sources/ProjectSpec/BuildScript.swift b/Sources/ProjectSpec/BuildScript.swift index d485ea77..14cfce0d 100644 --- a/Sources/ProjectSpec/BuildScript.swift +++ b/Sources/ProjectSpec/BuildScript.swift @@ -63,25 +63,19 @@ extension BuildScript: JSONObjectConvertible { } extension BuildScript: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + var dict: [String: Any?] = [ "inputFiles": inputFiles, "inputFileLists": inputFileLists, "outputFiles": outputFiles, - "outputFileLists": outputFileLists + "outputFileLists": outputFileLists, + "runOnlyWhenInstalling": runOnlyWhenInstalling, + "name": name, + "shell": shell ] - if runOnlyWhenInstalling { - dict["runOnlyWhenInstalling"] = runOnlyWhenInstalling - } if !showEnvVars { dict["showEnvVars"] = showEnvVars } - if let name = name { - dict["name"] = name - } - if let shell = shell { - dict["shell"] = shell - } switch script { case .path(let string): diff --git a/Sources/ProjectSpec/Dependency.swift b/Sources/ProjectSpec/Dependency.swift index 6ea575b6..121fff2d 100644 --- a/Sources/ProjectSpec/Dependency.swift +++ b/Sources/ProjectSpec/Dependency.swift @@ -83,7 +83,11 @@ extension Dependency: JSONObjectConvertible { extension Dependency: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [:] + var dict: [String: Any?] = [ + "embed": embed, + "codeSign": codeSign, + "link": link + ] if !removeHeaders { dict["removeHeaders"] = removeHeaders @@ -94,15 +98,6 @@ extension Dependency: JSONEncodable { if weakLink { dict["weak"] = weakLink } - if let embed = embed { - dict["embed"] = embed - } - if let codeSign = codeSign { - dict["codeSign"] = codeSign - } - if let link = link { - dict["link"] = link - } switch type { case .target: diff --git a/Sources/ProjectSpec/DeploymentTarget.swift b/Sources/ProjectSpec/DeploymentTarget.swift index 0968cfee..7cba891a 100644 --- a/Sources/ProjectSpec/DeploymentTarget.swift +++ b/Sources/ProjectSpec/DeploymentTarget.swift @@ -81,21 +81,11 @@ extension DeploymentTarget: JSONObjectConvertible { extension DeploymentTarget: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [:] - - if let iOS = iOS { - dict["iOS"] = iOS.string - } - if let tvOS = tvOS { - dict["tvOS"] = tvOS.string - } - if let watchOS = watchOS { - dict["watchOS"] = watchOS.string - } - if let macOS = macOS { - dict["macOS"] = macOS.string - } - - return dict + return [ + "iOS": iOS?.string, + "tvOS": tvOS?.string, + "watchOS": watchOS?.string, + "macOS": macOS?.string, + ] } } diff --git a/Sources/ProjectSpec/Encoding.swift b/Sources/ProjectSpec/Encoding.swift index 46e1ad52..6934f0b2 100644 --- a/Sources/ProjectSpec/Encoding.swift +++ b/Sources/ProjectSpec/Encoding.swift @@ -2,6 +2,6 @@ import Foundation import JSONUtilities public protocol JSONEncodable { - // returns JSONDictionary or JSONArray or JSONRawType + // returns JSONDictionary or JSONArray or JSONRawType or nil func toJSONValue() -> Any } diff --git a/Sources/ProjectSpec/Scheme.swift b/Sources/ProjectSpec/Scheme.swift index 1f497931..67c8ffba 100644 --- a/Sources/ProjectSpec/Scheme.swift +++ b/Sources/ProjectSpec/Scheme.swift @@ -220,16 +220,11 @@ extension Scheme.ExecutionAction: JSONObjectConvertible { extension Scheme.ExecutionAction: JSONEncodable { public func toJSONValue() -> Any { - var dict = [ + return [ "script": script, "name": name, + "settingsTarget": settingsTarget ] - - if let settingsTarget = settingsTarget { - dict["settingsTarget"] = settingsTarget - } - - return dict } } @@ -246,18 +241,13 @@ extension Scheme.Run: JSONObjectConvertible { extension Scheme.Run: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + return [ "commandLineArguments": commandLineArguments, "preActions": preActions.map { $0.toJSONValue() }, "postActions": postActions.map { $0.toJSONValue() }, "environmentVariables": environmentVariables.map { $0.toJSONValue() }, - ] - - if let config = config { - dict["config"] = config - } - - return dict + "config": config + ] as [String: Any?] } } @@ -288,20 +278,15 @@ extension Scheme.Test: JSONObjectConvertible { extension Scheme.Test: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + return [ "gatherCoverageData": gatherCoverageData, "commandLineArguments": commandLineArguments, "targets": targets.map { $0.toJSONValue() }, "preActions": preActions.map { $0.toJSONValue() }, "postActions": postActions.map { $0.toJSONValue() }, "environmentVariables": environmentVariables.map { $0.toJSONValue() }, - ] - - if let config = config { - dict["config"] = config - } - - return dict + "config": config + ] as [String: Any?] } } @@ -348,18 +333,13 @@ extension Scheme.Profile: JSONObjectConvertible { extension Scheme.Profile: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + return [ "commandLineArguments": commandLineArguments, "preActions": preActions.map { $0.toJSONValue() }, "postActions": postActions.map { $0.toJSONValue() }, "environmentVariables": environmentVariables.map { $0.toJSONValue() }, - ] - - if let config = config { - dict["config"] = config - } - - return dict + "config": config + ] as [String: Any?] } } @@ -372,13 +352,9 @@ extension Scheme.Analyze: JSONObjectConvertible { extension Scheme.Analyze: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [:] - - if let config = config { - dict["config"] = config - } - - return dict + return [ + "config": config + ] } } @@ -395,20 +371,16 @@ extension Scheme.Archive: JSONObjectConvertible { extension Scheme.Archive: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + var dict: [String: Any?] = [ "preActions": preActions.map { $0.toJSONValue() }, - "postActions": postActions.map { $0.toJSONValue() } + "postActions": postActions.map { $0.toJSONValue() }, + "config": config, + "customArchiveName": customArchiveName, ] if !revealArchiveInOrganizer { dict["revealArchiveInOrganizer"] = revealArchiveInOrganizer } - if let config = config { - dict["config"] = config - } - if let customArchiveName = customArchiveName { - dict["customArchiveName"] = customArchiveName - } return dict } @@ -429,27 +401,14 @@ extension Scheme: NamedJSONDictionaryConvertible { extension Scheme: JSONEncodable { public func toJSONValue() -> Any { - var dict = [ - "build": build.toJSONValue() - ] - - if let run = run { - dict["run"] = run.toJSONValue() - } - if let test = test { - dict["test"] = test.toJSONValue() - } - if let analyze = analyze { - dict["analyze"] = analyze.toJSONValue() - } - if let profile = profile { - dict["profile"] = profile.toJSONValue() - } - if let archive = archive { - dict["archive"] = archive.toJSONValue() - } - - return dict + return [ + "build": build.toJSONValue(), + "run": run?.toJSONValue(), + "test": test?.toJSONValue(), + "analyze": analyze?.toJSONValue(), + "profile": profile?.toJSONValue(), + "archive": archive?.toJSONValue(), + ] as [String: Any?] } } diff --git a/Sources/ProjectSpec/SpecOptions.swift b/Sources/ProjectSpec/SpecOptions.swift index e8471650..020b0494 100644 --- a/Sources/ProjectSpec/SpecOptions.swift +++ b/Sources/ProjectSpec/SpecOptions.swift @@ -128,11 +128,21 @@ extension SpecOptions: JSONObjectConvertible { extension SpecOptions: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + var dict: [String: Any?] = [ "deploymentTarget": deploymentTarget.toJSONValue(), "transitivelyLinkDependencies": transitivelyLinkDependencies, "groupSortPosition": groupSortPosition.rawValue, "disabledValidations": disabledValidations.map { $0.rawValue }, + "minimumXcodeGenVersion": minimumXcodeGenVersion?.string, + "carthageBuildPath": carthageBuildPath, + "carthageExecutablePath": carthageExecutablePath, + "bundleIdPrefix": bundleIdPrefix, + "developmentLanguage": developmentLanguage, + "usesTabs": usesTabs, + "xcodeVersion": xcodeVersion, + "indentWidth": indentWidth.flatMap { Int($0) }, + "tabWidth": tabWidth.flatMap { Int($0) }, + "defaultConfig": defaultConfig, ] if settingPresets != .all { @@ -147,36 +157,6 @@ extension SpecOptions: JSONEncodable { if findCarthageFrameworks { dict["findCarthageFrameworks"] = findCarthageFrameworks } - if let minimumXcodeGenVersion = minimumXcodeGenVersion { - dict["minimumXcodeGenVersion"] = minimumXcodeGenVersion.string - } - if let carthageBuildPath = carthageBuildPath { - dict["carthageBuildPath"] = carthageBuildPath - } - if let carthageExecutablePath = carthageExecutablePath { - dict["carthageExecutablePath"] = carthageExecutablePath - } - if let bundleIdPrefix = bundleIdPrefix { - dict["bundleIdPrefix"] = bundleIdPrefix - } - if let developmentLanguage = developmentLanguage { - dict["developmentLanguage"] = developmentLanguage - } - if let usesTabs = usesTabs { - dict["usesTabs"] = usesTabs - } - if let xcodeVersion = xcodeVersion { - dict["xcodeVersion"] = xcodeVersion - } - if let indentWidth = indentWidth { - dict["indentWidth"] = Int(indentWidth) - } - if let tabWidth = tabWidth { - dict["tabWidth"] = Int(tabWidth) - } - if let defaultConfig = defaultConfig { - dict["defaultConfig"] = defaultConfig - } return dict } diff --git a/Sources/ProjectSpec/Target.swift b/Sources/ProjectSpec/Target.swift index e2a9f00e..c529c554 100644 --- a/Sources/ProjectSpec/Target.swift +++ b/Sources/ProjectSpec/Target.swift @@ -274,19 +274,12 @@ extension LegacyTarget: JSONObjectConvertible { extension LegacyTarget: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + return [ "toolPath": toolPath, "passSettings": passSettings, - ] - - if let arguments = arguments { - dict["arguments"] = arguments - } - if let workingDirectory = workingDirectory { - dict["workingDirectory"] = workingDirectory - } - - return dict + "arguments": arguments, + "workingDirectory": workingDirectory, + ] as [String: Any?] } } @@ -364,7 +357,7 @@ extension Target: NamedJSONDictionaryConvertible { extension Target: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + var dict: [String: Any?] = [ "type": type.name, "platform": platform.rawValue, "settings": settings.toJSONValue(), @@ -375,38 +368,21 @@ extension Target: JSONEncodable { "postCompileScripts": postCompileScripts.map{ $0.toJSONValue() }, "prebuildScripts": preBuildScripts.map{ $0.toJSONValue() }, "postbuildScripts": postBuildScripts.map{ $0.toJSONValue() }, - "buildRules": buildRules.map{ $0.toJSONValue() } + "buildRules": buildRules.map{ $0.toJSONValue() }, + "deploymentTarget": deploymentTarget?.deploymentTarget, + "info": info?.toJSONValue(), + "entitlements": entitlements?.toJSONValue(), + "transitivelyLinkDependencies": transitivelyLinkDependencies, + "directlyEmbedCarthageDependencies": directlyEmbedCarthageDependencies, + "requiresObjCLinking": requiresObjCLinking, + "scheme": scheme?.toJSONValue(), + "legacy": legacy?.toJSONValue(), ] if productName != name { dict["productName"] = productName } - if let deploymentTarget = deploymentTarget { - dict["deploymentTarget"] = deploymentTarget.deploymentTarget - } - if let info = info { - dict["info"] = info.toJSONValue() - } - if let entitlements = entitlements { - dict["entitlements"] = entitlements.toJSONValue() - } - if let transitivelyLinkDependencies = transitivelyLinkDependencies { - dict["transitivelyLinkDependencies"] = transitivelyLinkDependencies - } - if let directlyEmbedCarthageDependencies = directlyEmbedCarthageDependencies { - dict["directlyEmbedCarthageDependencies"] = directlyEmbedCarthageDependencies - } - if let requiresObjCLinking = requiresObjCLinking { - dict["requiresObjCLinking"] = requiresObjCLinking - } - if let scheme = scheme { - dict["scheme"] = scheme.toJSONValue() - } - if let legacy = legacy { - dict["legacy"] = legacy.toJSONValue() - } - return dict } } diff --git a/Sources/ProjectSpec/TargetSource.swift b/Sources/ProjectSpec/TargetSource.swift index b8964513..8669094a 100644 --- a/Sources/ProjectSpec/TargetSource.swift +++ b/Sources/ProjectSpec/TargetSource.swift @@ -183,26 +183,16 @@ extension TargetSource: JSONObjectConvertible { extension TargetSource: JSONEncodable { public func toJSONValue() -> Any { - var dict: JSONDictionary = [ + var dict: [String: Any?] = [ "compilerFlags": compilerFlags, "excludes": excludes, + "name": name, + "headerVisibility": headerVisibility?.rawValue, + "type": type?.rawValue, + "buildPhase": buildPhase?.toJSONValue(), + "createIntermediateGroups": createIntermediateGroups, ] - if let name = name { - dict["name"] = name - } - if let headerVisibility = headerVisibility { - dict["headerVisibility"] = headerVisibility.rawValue - } - if let type = type { - dict["type"] = type.rawValue - } - if let buildPhase = buildPhase { - dict["buildPhase"] = buildPhase.toJSONValue() - } - if let createIntermediateGroups = createIntermediateGroups { - dict["createIntermediateGroups"] = createIntermediateGroups - } if optional { dict["optional"] = true }