Skip to content

Commit e6a78f5

Browse files
authored
Replace hardcoded config fallback version with dynamic constant (#381)
* Replace hardcoded config fallback version with dynamic constant Previously, when loading the config file, the fallback version `”0.3.0"` was hardcoded to handle unversioned configs from earlier releases. This commit replaces the hardcoded value with the centralized `version` constant, ensuring consistency across the codebase and reducing the risk of version mismatches in the future. * Make `version` field in config mandatory and remove fallback logic This commit aligns with the decision to drop support for configurations without a version field (pre-0.3.0). The `version` property in `Config` is now non-optional and must be explicitly set at creation. - Removed the fallback assignment to "0.3.0" - Updated all code to require and use non-optional `version` - Adjusted the config initializer to take `version` as a parameter - Simplified conditional checks and eliminated `.version?` This ensures that swiftly no longer silently accepts or upgrades legacy configurations without an explicit version. * Fix test build by adding required `version` to Config initializers * Apply Swift formatting using `swiftformat` on Sources and Tests
1 parent ab2dc50 commit e6a78f5

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

Sources/Swiftly/Config.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ public struct Config: Codable, Equatable, Sendable {
99
public var inUse: ToolchainVersion?
1010
public var installedToolchains: Set<ToolchainVersion>
1111
public var platform: PlatformDefinition
12-
public var version: SwiftlyVersion?
12+
public var version: SwiftlyVersion
1313

14-
init(inUse: ToolchainVersion?, installedToolchains: Set<ToolchainVersion>, platform: PlatformDefinition) {
14+
init(inUse: ToolchainVersion?, installedToolchains: Set<ToolchainVersion>, platform: PlatformDefinition, version: SwiftlyVersion) {
1515
self.inUse = inUse
1616
self.installedToolchains = installedToolchains
1717
self.platform = platform
18+
self.version = version
1819
}
1920

2021
private static func makeEncoder() -> JSONEncoder {
@@ -28,12 +29,8 @@ public struct Config: Codable, Equatable, Sendable {
2829
do {
2930
let configFile = Swiftly.currentPlatform.swiftlyConfigFile(ctx)
3031
let data = try await fs.cat(atPath: configFile)
31-
var config = try JSONDecoder().decode(Config.self, from: data)
32-
if config.version == nil {
33-
// Assume that the version of swiftly is 0.3.0 because that is the last
34-
// unversioned release.
35-
config.version = try? SwiftlyVersion(parsing: "0.3.0")
36-
}
32+
let config = try JSONDecoder().decode(Config.self, from: data)
33+
3734
return config
3835
} catch {
3936
let msg = """

Sources/Swiftly/Init.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct Init: SwiftlyCommand {
4848
(
4949
config.version == SwiftlyVersion(major: 0, minor: 4, patch: 0, suffix: "dev") ||
5050
config.version == SwiftlyVersion(major: 0, minor: 4, patch: 0) ||
51-
(config.version?.major == 1 && config.version?.minor == 0)
51+
(config.version.major == 1 && config.version.minor == 0)
5252
)
5353
{
5454
// This is a simple upgrade from the 0.4.0 pre-releases, or 1.x
@@ -178,9 +178,8 @@ struct Init: SwiftlyCommand {
178178
// Force the configuration to be present. Generate it if it doesn't already exist or overwrite is set
179179
if overwrite || config == nil {
180180
let pd = try await Swiftly.currentPlatform.detectPlatform(ctx, disableConfirmation: assumeYes, platform: platform)
181-
var c = Config(inUse: nil, installedToolchains: [], platform: pd)
182-
// Stamp the current version of swiftly on this config
183-
c.version = SwiftlyCore.version
181+
let c = Config(inUse: nil, installedToolchains: [], platform: pd, version: SwiftlyCore.version)
182+
184183
try c.save(ctx)
185184
config = c
186185
}

Tests/SwiftlyTests/SwiftlyTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ public enum SwiftlyTests {
220220
return Config(
221221
inUse: nil,
222222
installedToolchains: [],
223-
platform: pd
223+
platform: pd,
224+
version: SwiftlyCore.version
224225
)
225226
}
226227

0 commit comments

Comments
 (0)