Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 594c67f

Browse files
authored
Add enable option for include to enable optional including for addtional spec (yonaskolb#1242)
* add new option enable for include of spec * fix to see the environment variable when parsing include * add test for include with environment variable * fix how to parse boolean value * add spec about enable for include * add Change Log * fix the number of PR in changelog * fix include test to make more clear * fix test to focus enable option more * fix english error * fix to expand variable only one time * add new test case by setting environment object as NO
1 parent e9295f1 commit 594c67f

File tree

8 files changed

+90
-22
lines changed

8 files changed

+90
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
### Added
66

77
- Add support for `mlmodelc` files #1236 @antonsergeev88
8+
- Add `enable` option for `include` #1242 @freddi-kit
9+
10+
### Fixed
11+
- Fix checking environment variable in `include` #1242 @freddi-kit
812

913
### Fixed
1014

Docs/ProjectSpec.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ An include can be provided via a string (the path) or an object of the form:
6464

6565
- [x] **path**: **String** - The path to the included file.
6666
- [ ] **relativePaths**: **Bool** - Dictates whether the included spec specifies paths relative to itself (the default) or the root spec file.
67-
67+
- [ ] **enable**: **Bool** - Dictates whether the specified spec should be included or not. You can also specify it by environment variable.
6868
```yaml
6969
include:
7070
- includedFile.yml
7171
- path: path/to/includedFile.yml
7272
relativePaths: false
73+
enable: ${INCLUDE_ADDITIONAL_YAML}
7374
```
7475
7576
By default specs are merged additively. That is for every value:

Sources/ProjectSpec/SpecFile.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import JSONUtilities
33
import PathKit
4+
import Yams
45

56
public struct SpecFile {
67
public let basePath: Path
@@ -13,17 +14,20 @@ public struct SpecFile {
1314
fileprivate struct Include {
1415
let path: Path
1516
let relativePaths: Bool
17+
let enable: Bool
1618

1719
static let defaultRelativePaths = true
20+
static let defaultEnable = true
1821

1922
init?(any: Any) {
2023
if let string = any as? String {
2124
path = Path(string)
2225
relativePaths = Include.defaultRelativePaths
23-
} else if let dictionary = any as? JSONDictionary,
24-
let path = dictionary["path"] as? String {
26+
enable = Include.defaultEnable
27+
} else if let dictionary = any as? JSONDictionary, let path = dictionary["path"] as? String {
2528
self.path = Path(path)
26-
relativePaths = dictionary["relativePaths"] as? Bool ?? Include.defaultRelativePaths
29+
relativePaths = Self.resolveBoolean(dictionary, key: "relativePaths") ?? Include.defaultRelativePaths
30+
enable = Self.resolveBoolean(dictionary, key: "enable") ?? Include.defaultEnable
2731
} else {
2832
return nil
2933
}
@@ -38,10 +42,14 @@ public struct SpecFile {
3842
return []
3943
}
4044
}
45+
46+
private static func resolveBoolean(_ dictionary: [String: Any], key: String) -> Bool? {
47+
dictionary[key] as? Bool ?? (dictionary[key] as? NSString)?.boolValue
48+
}
4149
}
4250

43-
public init(path: Path) throws {
44-
try self.init(filePath: path, basePath: path.parent())
51+
public init(path: Path, variables: [String: String] = [:]) throws {
52+
try self.init(filePath: path, basePath: path.parent(), variables: variables)
4553
}
4654

4755
public init(filePath: Path, jsonDictionary: JSONDictionary, basePath: Path = "", relativePath: Path = "", subSpecs: [SpecFile] = []) {
@@ -52,21 +60,23 @@ public struct SpecFile {
5260
self.filePath = filePath
5361
}
5462

55-
private init(include: Include, basePath: Path, relativePath: Path) throws {
63+
private init(include: Include, basePath: Path, relativePath: Path, variables: [String: String]) throws {
5664
let basePath = include.relativePaths ? (basePath + relativePath) : (basePath + relativePath + include.path.parent())
5765
let relativePath = include.relativePaths ? include.path.parent() : Path()
5866

59-
try self.init(filePath: include.path, basePath: basePath, relativePath: relativePath)
67+
try self.init(filePath: include.path, basePath: basePath, variables: variables, relativePath: relativePath)
6068
}
6169

62-
private init(filePath: Path, basePath: Path, relativePath: Path = "") throws {
70+
private init(filePath: Path, basePath: Path, variables: [String: String], relativePath: Path = "") throws {
6371
let path = basePath + relativePath + filePath.lastComponent
64-
let jsonDictionary = try SpecFile.loadDictionary(path: path)
72+
let jsonDictionary = try SpecFile.loadDictionary(path: path).expand(variables: variables)
6573

6674
let includes = Include.parse(json: jsonDictionary["include"])
67-
let subSpecs: [SpecFile] = try includes.map { include in
68-
try SpecFile(include: include, basePath: basePath, relativePath: relativePath)
69-
}
75+
let subSpecs: [SpecFile] = try includes
76+
.filter(\.enable)
77+
.map { include in
78+
try SpecFile(include: include, basePath: basePath, relativePath: relativePath, variables: variables)
79+
}
7080

7181
self.init(filePath: filePath, jsonDictionary: jsonDictionary, basePath: basePath, relativePath: relativePath, subSpecs: subSpecs)
7282
}
@@ -85,8 +95,8 @@ public struct SpecFile {
8595
}
8696
}
8797

88-
public func resolvedDictionary(variables: [String: String] = [:]) -> JSONDictionary {
89-
resolvedDictionaryWithUniqueTargets().expand(variables: variables)
98+
public func resolvedDictionary() -> JSONDictionary {
99+
resolvedDictionaryWithUniqueTargets()
90100
}
91101

92102
private func resolvedDictionaryWithUniqueTargets() -> JSONDictionary {

Sources/ProjectSpec/SpecLoader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class SpecLoader {
1616
}
1717

1818
public func loadProject(path: Path, projectRoot: Path? = nil, variables: [String: String] = [:]) throws -> Project {
19-
let spec = try SpecFile(path: path)
20-
let resolvedDictionary = spec.resolvedDictionary(variables: variables)
19+
let spec = try SpecFile(path: path, variables: variables)
20+
let resolvedDictionary = spec.resolvedDictionary()
2121
let project = try Project(basePath: projectRoot ?? spec.basePath, jsonDictionary: resolvedDictionary)
2222

2323
self.project = project

Tests/Fixtures/include_test.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
include: [included.yml]
1+
include:
2+
- included.yml
3+
- path: included_addtional.yml
4+
enable: ${INCLUDE_ADDTIONAL_YAML}
5+
packages:
6+
Yams:
7+
url: https://github.com/jpsim/Yams
8+
majorVersion: 2.0.0
29
name: NewName
310
settingGroups:
411
test:
@@ -19,3 +26,5 @@ targets:
1926
name: IncludedTargetNew
2027
platform: tvOS
2128
sources:REPLACE: NewSource
29+
dependencies:
30+
- package: Yams
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Included_Addtional
2+
settingGroups:
3+
test:
4+
MY_SETTING5: ADDTIONAL
5+
packages:
6+
SwiftPM:
7+
url: https://github.com/apple/swift-package-manager
8+
branch: swift-5.0-branch
9+
targets:
10+
IncludedTarget:
11+
dependencies:
12+
- package: SwiftPM

Tests/PerformanceTests/PerformanceTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class GeneratedPerformanceTests: XCTestCase {
1515
try dumpYamlDictionary(project.toJSONDictionary(), path: specPath)
1616

1717
measure {
18-
let spec = try! SpecFile(path: specPath)
19-
_ = spec.resolvedDictionary(variables: ProcessInfo.processInfo.environment)
18+
let spec = try! SpecFile(path: specPath, variables: ProcessInfo.processInfo.environment)
19+
_ = spec.resolvedDictionary()
2020
}
2121
}
2222

Tests/ProjectSpecTests/SpecLoadingTests.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SpecLoadingTests: XCTestCase {
2929
describe {
3030
$0.it("merges includes") {
3131
let path = fixturePath + "include_test.yml"
32-
let project = try loadSpec(path: path)
32+
let project = try loadSpec(path: path, variables: [:])
3333

3434
try expect(project.name) == "NewName"
3535
try expect(project.settingGroups) == [
@@ -38,7 +38,39 @@ class SpecLoadingTests: XCTestCase {
3838
"toReplace": Settings(dictionary: ["MY_SETTING2": "VALUE2"]),
3939
]
4040
try expect(project.targets) == [
41-
Target(name: "IncludedTargetNew", type: .application, platform: .tvOS, sources: ["NewSource"]),
41+
Target(name: "IncludedTargetNew", type: .application, platform: .tvOS, sources: ["NewSource"], dependencies: [Dependency(type: .package(product: nil), reference: "Yams")]),
42+
Target(name: "NewTarget", type: .application, platform: .iOS, sources: ["template", "target"]),
43+
]
44+
}
45+
46+
$0.it("merges includes with addtional one") {
47+
let path = fixturePath + "include_test.yml"
48+
let project = try loadSpec(path: path, variables: ["INCLUDE_ADDTIONAL_YAML": "YES"])
49+
50+
try expect(project.name) == "NewName"
51+
try expect(project.settingGroups) == [
52+
"test": Settings(dictionary: ["MY_SETTING1": "NEW VALUE", "MY_SETTING2": "VALUE2", "MY_SETTING3": "VALUE3", "MY_SETTING4": "${SETTING4}", "MY_SETTING5": "ADDTIONAL"]),
53+
"new": Settings(dictionary: ["MY_SETTING": "VALUE"]),
54+
"toReplace": Settings(dictionary: ["MY_SETTING2": "VALUE2"]),
55+
]
56+
try expect(project.targets) == [
57+
Target(name: "IncludedTargetNew", type: .application, platform: .tvOS, sources: ["NewSource"], dependencies: [Dependency(type: .package(product: nil), reference: "SwiftPM"), Dependency(type: .package(product: nil), reference: "Yams")]),
58+
Target(name: "NewTarget", type: .application, platform: .iOS, sources: ["template", "target"]),
59+
]
60+
}
61+
62+
$0.it("merges includes without addtional one by environemnt variable") {
63+
let path = fixturePath + "include_test.yml"
64+
let project = try loadSpec(path: path, variables: ["INCLUDE_ADDTIONAL_YAML": "NO"])
65+
66+
try expect(project.name) == "NewName"
67+
try expect(project.settingGroups) == [
68+
"test": Settings(dictionary: ["MY_SETTING1": "NEW VALUE", "MY_SETTING2": "VALUE2", "MY_SETTING3": "VALUE3", "MY_SETTING4": "${SETTING4}"]),
69+
"new": Settings(dictionary: ["MY_SETTING": "VALUE"]),
70+
"toReplace": Settings(dictionary: ["MY_SETTING2": "VALUE2"]),
71+
]
72+
try expect(project.targets) == [
73+
Target(name: "IncludedTargetNew", type: .application, platform: .tvOS, sources: ["NewSource"], dependencies: [Dependency(type: .package(product: nil), reference: "Yams")]),
4274
Target(name: "NewTarget", type: .application, platform: .iOS, sources: ["template", "target"]),
4375
]
4476
}

0 commit comments

Comments
 (0)