Skip to content

Commit 398b8f8

Browse files
authored
Feature/swift format (traderepublic#24)
* Added swiftformat script + rules config file * Apply SwiftFormat rules
1 parent f5a16e3 commit 398b8f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+526
-436
lines changed

.swiftformat

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--swiftversion 5.8
2+
--xcodeindentation enabled
3+
--disable unusedArguments, redundantReturn, redundantSelf, trailingCommas, trailingClosures, wrapArguments, wrapMultilineStatementBraces
4+
--enable blockComments, isEmpty
5+
--emptybraces spaced
6+
--ifdef no-indent
7+
--importgrouping testable-first
8+
--maxwidth 150
9+
--wraparguments before-first
10+
--wrapcollections before-first
11+
--exclude Carthage, vendor, **/*.generated.swift

Cilicon.xcodeproj/project.pbxproj

+23
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
isa = PBXNativeTarget;
297297
buildConfigurationList = A9728DE02918F79100342A77 /* Build configuration list for PBXNativeTarget "Cilicon" */;
298298
buildPhases = (
299+
622F3F2C2A3CB696007506AF /* SwiftFormat */,
299300
A9728DCD2918F79000342A77 /* Sources */,
300301
A9728DCE2918F79000342A77 /* Frameworks */,
301302
A9728DCF2918F79000342A77 /* Resources */,
@@ -393,6 +394,28 @@
393394
};
394395
/* End PBXResourcesBuildPhase section */
395396

397+
/* Begin PBXShellScriptBuildPhase section */
398+
622F3F2C2A3CB696007506AF /* SwiftFormat */ = {
399+
isa = PBXShellScriptBuildPhase;
400+
alwaysOutOfDate = 1;
401+
buildActionMask = 2147483647;
402+
files = (
403+
);
404+
inputFileListPaths = (
405+
);
406+
inputPaths = (
407+
);
408+
name = SwiftFormat;
409+
outputFileListPaths = (
410+
);
411+
outputPaths = (
412+
);
413+
runOnlyForDeploymentPostprocessing = 0;
414+
shellPath = /bin/sh;
415+
shellScript = "if [[ \"$(uname -m)\" == arm64 ]]; then\n export PATH=\"/opt/homebrew/bin:$PATH\"\nfi\n\nif which swiftformat >/dev/null; then\n swiftformat .\nelse\n echo \"warning: SwiftFormat not installed, download from https://github.com/nicklockwood/SwiftFormat\"\nfi\n";
416+
};
417+
/* End PBXShellScriptBuildPhase section */
418+
396419
/* Begin PBXSourcesBuildPhase section */
397420
A9728DCD2918F79000342A77 /* Sources */ = {
398421
isa = PBXSourcesBuildPhase;

Cilicon/ANSIParser.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct ANSIParser {
44
typealias Color = NSColor
55
typealias Font = NSFont
66
static let fontName = "Andale Mono"
7-
7+
88
static let defaultFont = Font.monospacedSystemFont(ofSize: Font.systemFontSize, weight: .regular)
99
static let defaultAttributes: [NSAttributedString.Key: Any] = [
1010
.font: defaultFont as Any
@@ -37,9 +37,7 @@ struct ANSIParser {
3737
return result
3838
}
3939

40-
4140
private static func attributesFor(ansiCode: String) -> [NSAttributedString.Key: Any] {
42-
4341
var attributes: [NSAttributedString.Key: Any] = Self.defaultAttributes
4442
attributes[.font] = defaultFont
4543
let codes = ansiCode

Cilicon/AppleEvents/AppleEvents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import CoreServices
12
// From https://github.com/soffes/spotlight-tools/tree/master/UtilityKit
23
import Foundation
3-
import CoreServices
44

55
public enum AppleEvent {
66
case restart

Cilicon/CiliconApp.swift

+19-16
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ import Yams
33
@main
44
struct CiliconApp: App {
55
@State private var vmSource: String = ""
6-
6+
77
var body: some Scene {
88
Window("Cilicon", id: "cihost") {
9-
109
if ConfigManager.fileExists {
1110
switch Result(catching: { try ConfigManager().config }) {
12-
case .success(let config):
11+
case let .success(config):
1312
let contentView = ContentView(config: config)
1413
AnyView(contentView)
15-
case .failure(let error):
14+
case let .failure(error):
1615
Text(String(describing: error))
1716
}
18-
17+
1918
} else {
2019
Text("No Config found.\n\nTo create one, enter the path or an OCI image starting with oci:// below and press return")
2120
.multilineTextAlignment(.center)
@@ -29,17 +28,21 @@ struct CiliconApp: App {
2928
return
3029
}
3130
let scriptConfig = ScriptProvisionerConfig(run: "echo Hello World && sleep 10 && echo Shutting down")
32-
let config = Config(provisioner: .script(scriptConfig),
33-
hardware: .init(ramGigabytes: 8,
34-
display: .default,
35-
connectsToAudioDevice: false),
36-
directoryMounts: [],
37-
source: source,
38-
vmClonePath: URL(filePath: NSHomeDirectory()).appending(component: "vmclone").path,
39-
editorMode: false,
40-
retryDelay: 3,
41-
sshCredentials: .init(username: "admin", password: "admin"))
42-
31+
let config = Config(
32+
provisioner: .script(scriptConfig),
33+
hardware: .init(
34+
ramGigabytes: 8,
35+
display: .default,
36+
connectsToAudioDevice: false
37+
),
38+
directoryMounts: [],
39+
source: source,
40+
vmClonePath: URL(filePath: NSHomeDirectory()).appending(component: "vmclone").path,
41+
editorMode: false,
42+
retryDelay: 3,
43+
sshCredentials: .init(username: "admin", password: "admin")
44+
)
45+
4346
try? YAMLEncoder().encode(config).write(toFile: ConfigManager.path, atomically: true, encoding: .utf8)
4447
restart()
4548
}

Cilicon/Config/BuildkiteAgentProvisionerConfig.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import Foundation
22
struct BuildkiteAgentProvisionerConfig: Decodable {
33
let agentToken: String
44
let tags: [String]
5-
5+
66
enum CodingKeys: CodingKey {
77
case agentToken
88
case tags
99
}
10-
10+
1111
init(from decoder: Decoder) throws {
1212
let container = try decoder.container(keyedBy: CodingKeys.self)
1313
self.agentToken = try container.decode(String.self, forKey: .agentToken)

Cilicon/Config/Config.swift

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import Foundation
22

33
struct Config: Codable {
4-
internal init(provisioner: ProvisionerConfig, hardware: HardwareConfig, directoryMounts: [DirectoryMountConfig], source: VMSource, vmClonePath: String, numberOfRunsUntilHostReboot: Int? = nil, runnerName: String? = nil, editorMode: Bool, autoTransferImageVolume: String? = nil, retryDelay: Int, sshCredentials: SSHCredentials, preRun: String? = nil, postRun: String? = nil) {
4+
internal init(
5+
provisioner: ProvisionerConfig,
6+
hardware: HardwareConfig,
7+
directoryMounts: [DirectoryMountConfig],
8+
source: VMSource,
9+
vmClonePath: String,
10+
numberOfRunsUntilHostReboot: Int? = nil,
11+
runnerName: String? = nil,
12+
editorMode: Bool,
13+
autoTransferImageVolume: String? = nil,
14+
retryDelay: Int,
15+
sshCredentials: SSHCredentials,
16+
preRun: String? = nil,
17+
postRun: String? = nil
18+
) {
519
self.provisioner = provisioner
620
self.hardware = hardware
721
self.directoryMounts = directoryMounts
@@ -15,7 +29,7 @@ struct Config: Codable {
1529
self.preRun = preRun
1630
self.postRun = postRun
1731
}
18-
32+
1933
/// Provisioner Configuration.
2034
let provisioner: ProvisionerConfig
2135
/// Hardware Configuration.
@@ -42,7 +56,7 @@ struct Config: Codable {
4256
let preRun: String?
4357
/// A command to run after the provisioning commands are run.
4458
let postRun: String?
45-
59+
4660
enum CodingKeys: CodingKey {
4761
case provisioner
4862
case hardware
@@ -57,14 +71,16 @@ struct Config: Codable {
5771
case preRun
5872
case postRun
5973
}
60-
74+
6175
init(from decoder: Decoder) throws {
6276
let container = try decoder.container(keyedBy: CodingKeys.self)
6377
self.provisioner = try container.decode(ProvisionerConfig.self, forKey: .provisioner)
6478
self.hardware = try container.decodeIfPresent(HardwareConfig.self, forKey: .hardware) ?? .default
6579
self.directoryMounts = try container.decodeIfPresent([DirectoryMountConfig].self, forKey: .directoryMounts) ?? []
6680
self.source = try container.decode(VMSource.self, forKey: .source)
67-
self.vmClonePath = (try container.decodeIfPresent(String.self, forKey: .vmClonePath).map { ($0 as NSString).standardizingPath }) ?? URL(filePath: NSHomeDirectory()).appending(component: "vmclone").path
81+
self.vmClonePath = (
82+
try container.decodeIfPresent(String.self, forKey: .vmClonePath).map { ($0 as NSString).standardizingPath }
83+
) ?? URL(filePath: NSHomeDirectory()).appending(component: "vmclone").path
6884
self.numberOfRunsUntilHostReboot = try container.decodeIfPresent(Int.self, forKey: .numberOfRunsUntilHostReboot)
6985
self.runnerName = try container.decodeIfPresent(String.self, forKey: .runnerName)
7086
self.editorMode = try container.decodeIfPresent(Bool.self, forKey: .editorMode) ?? false
@@ -76,8 +92,7 @@ struct Config: Codable {
7692
}
7793

7894
struct SSHCredentials: Codable {
79-
static var `default` = Self.init(username: "admin", password: "admin")
95+
static var `default` = Self(username: "admin", password: "admin")
8096
let username: String
8197
let password: String
8298
}
83-

Cilicon/Config/ConfigManager.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ class ConfigManager {
66
static var fileExists: Bool {
77
FileManager.default.fileExists(atPath: path)
88
}
9+
910
let config: Config
10-
11+
1112
init() throws {
1213
let decoder = YAMLDecoder()
1314
guard let data = FileManager.default.contents(atPath: Self.path) else {
1415
throw ConfigManagerError.fileCouldNotBeRead
1516
}
1617
self.config = try decoder.decode(Config.self, from: data)
1718
}
18-
1919
}
2020

2121
enum ConfigManagerError: Error {

Cilicon/Config/DirectoryMountConfig.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ struct DirectoryMountConfig: Codable {
77
let guestFolder: String
88
/// Mount the folder as a read only volume.
99
let readOnly: Bool
10-
10+
1111
init(from decoder: Decoder) throws {
1212
let container = try decoder.container(keyedBy: CodingKeys.self)
1313
self.hostPath = (try container.decode(String.self, forKey: .hostPath) as NSString).standardizingPath
1414
self.guestFolder = try container.decode(String.self, forKey: .guestFolder)
1515
self.readOnly = try container.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
1616
}
17-
17+
1818
enum CodingKeys: CodingKey {
1919
case hostPath
2020
case guestFolder

Cilicon/Config/GitHubProvisionerConfig.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ struct GitHubProvisionerConfig: Decodable {
1313
let extraLabels: [String]?
1414
/// Default: `true`
1515
let downloadLatest: Bool
16-
16+
1717
let runnerGroup: String?
18-
18+
1919
let organizationURL: URL
20-
20+
2121
enum CodingKeys: CodingKey {
2222
case apiURL
2323
case appId
@@ -28,7 +28,7 @@ struct GitHubProvisionerConfig: Decodable {
2828
case organizationURL
2929
case downloadLatest
3030
}
31-
31+
3232
init(from decoder: Decoder) throws {
3333
let container = try decoder.container(keyedBy: CodingKeys.self)
3434
self.apiURL = try container.decodeIfPresent(URL.self, forKey: .apiURL)
@@ -40,5 +40,4 @@ struct GitHubProvisionerConfig: Decodable {
4040
self.organizationURL = try container.decodeIfPresent(URL.self, forKey: .organizationURL) ?? URL(string: "https://github.com/\(organization)")!
4141
self.downloadLatest = try container.decodeIfPresent(Bool.self, forKey: .downloadLatest) ?? true
4242
}
43-
4443
}

Cilicon/Config/HardwareConfig.swift

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ import Foundation
33
struct HardwareConfig: Codable {
44
static var `default`: HardwareConfig {
55
let ramAvailable = ProcessInfo.processInfo.physicalMemory / UInt64(1024 * 1024 * 1024)
6-
return Self.init(ramGigabytes: ramAvailable,
7-
display: .default,
8-
connectsToAudioDevice: true)
9-
6+
return Self(
7+
ramGigabytes: ramAvailable,
8+
display: .default,
9+
connectsToAudioDevice: true
10+
)
1011
}
11-
12+
1213
internal init(ramGigabytes: UInt64, cpuCores: Int? = nil, display: HardwareConfig.DisplayConfig, connectsToAudioDevice: Bool) {
1314
self.ramGigabytes = ramGigabytes
1415
self.cpuCores = cpuCores
1516
self.display = display
1617
self.connectsToAudioDevice = connectsToAudioDevice
1718
}
18-
19+
1920
/// Gigabytes of RAM for the Guest System.
2021
let ramGigabytes: UInt64
2122
/// Number of virtual CPU Cores. Defaults to the number of physical CPU cores.
@@ -24,25 +25,25 @@ struct HardwareConfig: Codable {
2425
let display: DisplayConfig
2526
/// Whether or not to forward audio from the guest system to the host system audio device.
2627
let connectsToAudioDevice: Bool
27-
28+
2829
enum CodingKeys: CodingKey {
2930
case ramGigabytes
3031
case cpuCores
3132
case display
3233
case connectsToAudioDevice
3334
}
34-
35+
3536
init(from decoder: Decoder) throws {
3637
let container = try decoder.container(keyedBy: CodingKeys.self)
3738
self.ramGigabytes = try container.decode(UInt64.self, forKey: .ramGigabytes)
3839
self.cpuCores = try container.decodeIfPresent(Int.self, forKey: .cpuCores)
3940
self.display = try container.decodeIfPresent(DisplayConfig.self, forKey: .display) ?? .default
4041
self.connectsToAudioDevice = try container.decodeIfPresent(Bool.self, forKey: .connectsToAudioDevice) ?? false
4142
}
42-
43+
4344
struct DisplayConfig: Codable {
4445
static let `default`: DisplayConfig = .init(width: 1920, height: 1200, pixelsPerInch: 80)
45-
46+
4647
let width: Int
4748
let height: Int
4849
let pixelsPerInch: Int

Cilicon/Config/ProvisionerConfig.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ enum ProvisionerConfig: Codable {
55
case gitlab(GitLabProvisionerConfig)
66
case buildkite(BuildkiteAgentProvisionerConfig)
77
case script(ScriptProvisionerConfig)
8-
8+
99
enum CodingKeys: CodingKey {
1010
case type
1111
case config
1212
}
13-
13+
1414
init(from decoder: Decoder) throws {
1515
let container = try decoder.container(keyedBy: CodingKeys.self)
1616
let type = try container.decode(ProvisionerType.self, forKey: .type)
@@ -29,18 +29,19 @@ enum ProvisionerConfig: Codable {
2929
self = .script(config)
3030
}
3131
}
32+
3233
func encode(to encoder: Encoder) throws {
3334
var container = encoder.container(keyedBy: CodingKeys.self)
3435
switch self {
35-
case .script(let config):
36+
case let .script(config):
3637
try container.encode(ProvisionerType.script, forKey: .type)
3738
try container.encode(config, forKey: .config)
3839
default:
39-
//don't need to encode anything else for now
40+
// don't need to encode anything else for now
4041
break
4142
}
4243
}
43-
44+
4445
enum ProvisionerType: String, Codable {
4546
case github
4647
case gitlab

Cilicon/Config/ScriptProvisionerConfig.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
struct ScriptProvisionerConfig: Codable {
44
/// The block to run
55
let run: String
6-
6+
77
init(run: String) {
88
self.run = run
99
}

0 commit comments

Comments
 (0)