forked from exercism/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Package.swift
83 lines (72 loc) · 2.4 KB
/
Package.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// swift-tools-version:4.2
import PackageDescription
import Foundation
extension String {
var pascalCased: String {
let items = self.components(separatedBy: "-")
return items.reduce("", { $0 + $1.capitalized })
}
}
let currentDirectory = FileManager.default.currentDirectoryPath
let configPath = currentDirectory + "/config.json"
var allProblems = [String]()
if
let jsonData = try? Data(contentsOf: URL(fileURLWithPath: configPath), options: Data.ReadingOptions.mappedIfSafe),
let json = try? JSONSerialization.jsonObject(with: jsonData, options: []),
let jsonDict = json as? [String: Any],
let exercisesDict = jsonDict["exercises"] as? [[String: Any]],
let exercises = exercisesDict.map({ $0["slug"] }) as? [String] {
allProblems += exercises
} else {
print("Could not parse config.json at \(configPath)")
}
let allProblemsPascalCase = allProblems.map { $0.pascalCased }
#if os(Linux)
// Create ./Tests/LinuxMain.swift
let allTestCases = allProblemsPascalCase.map { "testCase(\($0)Tests.allTests)," }
let linuxMainText =
"""
import XCTest
@testable import xswiftTests
XCTMain([
\(allTestCases.joined(separator: "\n"))
])
"""
let linuxMainFilePath = currentDirectory + "/LinuxMain.swift"
do {
if FileManager.default.fileExists(atPath: linuxMainFilePath) {
try FileManager.default.removeItem(atPath: linuxMainFilePath)
}
try linuxMainText.write(to: URL(fileURLWithPath: linuxMainFilePath), atomically: false, encoding: .utf8)
} catch let fileError {
print("Could not write file. \(fileError)")
}
#endif
let packageDependencies: [Package.Dependency] = allProblems.map { .package(path: "./exercises/\($0)/") }
let targetDependencies: [Target.Dependency] = allProblemsPascalCase.map { .byName(name:"\($0)") }
let sources = allProblems.map { "./\($0)/Sources" }
let testSources = allProblems.map { "./\($0)/Tests" }
let package = Package(
name: "xswift",
products: [
.library(
name: "xswift",
targets: ["xswift"]
)
],
dependencies: packageDependencies,
targets: [
.target(
name: "xswift",
dependencies: targetDependencies,
path: "./exercises",
sources: sources
),
.testTarget(
name: "xswiftTests",
dependencies: ["xswift"],
path: "./exercises",
sources: testSources
),
]
)