Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace SwiftPM dependency with local Version type #1684

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions AWSSDKSwiftCLI/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
"version" : "1.3.1"
}
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bye bye, useless dependencies

{
"identity" : "swift-llbuild",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-llbuild.git",
"state" : {
"revision" : "fb7ebf0b06c0d7c45ca8e18b3371424503a38b5c",
"version" : "0.3.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
Expand All @@ -44,24 +35,6 @@
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "swift-package-manager",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-package-manager",
"state" : {
"revision" : "f5ea3972d7d6c574e8bb16a19b2a7bca98ea131b",
"version" : "0.6.0"
}
},
{
"identity" : "swift-tools-support-core",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-tools-support-core.git",
"state" : {
"revision" : "98a5916a811fcaaed770f1ed812e9405be762945",
"version" : "0.1.0"
}
}
],
"version" : 2
Expand Down
2 changes: 0 additions & 2 deletions AWSSDKSwiftCLI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-package-manager", from: "0.6.0"),
.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
],
Expand All @@ -34,7 +33,6 @@ let package = Package(
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Logging", package: "swift-log"),
.product(name: "PackageDescription", package: "swift-package-manager"),
]
),
.testTarget(
Expand Down
1 change: 0 additions & 1 deletion AWSSDKSwiftCLI/Sources/AWSCLIUtils/Process+Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import PackageDescription
import struct ArgumentParser.ExitCode

public extension Process {
Expand Down
1 change: 0 additions & 1 deletion AWSSDKSwiftCLI/Sources/AWSCLIUtils/Process+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import PackageDescription

public extension Process {
struct Swift {
Expand Down
8 changes: 8 additions & 0 deletions AWSSDKSwiftCLI/Sources/AWSCLIUtils/Process+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public func _run(_ process: Process) throws {
}

public func _runReturningStdOut(_ process: Process) throws -> String? {
// If debug and we have a non-nil test runner, then use that
// This allows developers to intercept processes when they run to assert that it is the expected process
#if DEBUG
if let testRunner = ProcessRunner.testRunner {
try testRunner.run(process)
return nil
}
#endif
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this code fixes this process runner so that it can be mocked with a test runner. Note that when the behavior is mocked, the returned output is nil; output may be mocked as well in the future if desired.

let stdOut = Pipe()
process.standardOutput = stdOut

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,46 @@

import Foundation
import ArgumentParser
import PackageDescription

// MARK: - Decodable
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The struct below replaces the one that was previously imported from the SwiftPM package.

public struct Version: Equatable {
public let major: Int
public let minor: Int
public let patch: Int

private var versionString: String { "\(major).\(minor).\(patch)" }

init(_ major: Int, _ minor: Int, _ patch: Int) {
self.major = major
self.minor = minor
self.patch = patch
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that creating a Version from a string is now a non-optional initializer that throws if the string is not a valid semantic version identifier.

public init(_ value: String) throws {
let components = value.split(separator: ".")
guard components.count == 3 else {
throw Error("Version does not have three components")
}
guard let major = Int(components[0]), let minor = Int(components[1]), let patch = Int(components[2]) else {
throw Error("Version components are not all Int")
}
self.init(major, minor, patch)
}
}

extension Version: CustomStringConvertible {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the previous Version, this struct provides the version string when printed.

public var description: String { versionString }
}

// MARK: - Codable

extension Version: Codable {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version encodes & decodes itself by encoding/decoding its version string.


extension Version: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let value = try container.decode(String.self)
self.init(stringLiteral: value)
try self.init(try String(from: decoder))
}

public func encode(to encoder: any Encoder) throws {
try versionString.encode(to: encoder)
}
}

Expand All @@ -37,11 +68,7 @@ public extension Version {

let normalizedVersionString = versionString.trimmingCharacters(in: .whitespacesAndNewlines)

guard let version = Version.init(normalizedVersionString) else {
throw Error("Failed to parse version from string \(normalizedVersionString)")
}

return version
return try Version(normalizedVersionString)
}
}

Expand All @@ -55,9 +82,7 @@ public extension Version {
Version(
self.major + 1,
0,
0,
prereleaseIdentifiers: prereleaseIdentifiers,
buildMetadataIdentifiers: buildMetadataIdentifiers
0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prerelease & build metadata identifiers are removed, since our project does not use them.

)
}

Expand All @@ -68,9 +93,7 @@ public extension Version {
Version(
self.major,
self.minor + 1,
0,
prereleaseIdentifiers: prereleaseIdentifiers,
buildMetadataIdentifiers: buildMetadataIdentifiers
0
)
}

Expand All @@ -81,17 +104,16 @@ public extension Version {
Version(
self.major,
self.minor,
self.patch + 1,
prereleaseIdentifiers: prereleaseIdentifiers,
buildMetadataIdentifiers: buildMetadataIdentifiers
self.patch + 1
)
}
}

// MARK: - ExpressibleByArgument

extension Version: ExpressibleByArgument {

public init?(argument: String) {
self.init(argument)
try? self.init(argument)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import ArgumentParser
import Foundation
import PackageDescription
import AWSCLIUtils

// MARK: - Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import ArgumentParser
import Foundation
import PackageDescription
import AWSCLIUtils

// MARK: - Command
Expand Down Expand Up @@ -147,7 +146,7 @@ struct PrepareRelease {
func createNewVersion(_ previousVersion: Version) throws -> Version {
let newVersion = previousVersion.incrementingMinor()
do {
try "\(newVersion)".write(toFile: "Package.version" , atomically: true, encoding: .utf8)
try "\(newVersion)".write(toFile: "Package.version" , atomically: true, encoding: .utf8)
} catch {
throw Error("Failed to write version \(newVersion) to Package.version")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import ArgumentParser
import Foundation
import PackageDescription
import AWSCLIUtils

// MARK: - Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Algorithms
import ArgumentParser
import Foundation
import PackageDescription
import AWSCLIUtils

// MARK: - Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import PackageDescription
import AWSCLIUtils

/// Builds the contents of the package manifest file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
import PackageDescription
import AWSCLIUtils

/// `PackageDependencies` is a representation of contents of the .plist stored at packageDependencies.plist
struct PackageDependencies: Codable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import PackageDescription
import AWSCLIUtils

/// Builds the contents of the package manifest file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import PackageDescription

struct ReleaseManifest: Codable {
let name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import PackageDescription
import AWSCLIUtils

// Builds the release notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

@testable import AWSSDKSwiftCLI
import PackageDescription
import AWSCLIUtils
import XCTest

class GeneratePackageManifestTests: CLITestCase {
Expand All @@ -16,10 +16,10 @@ class GeneratePackageManifestTests: CLITestCase {
func createPackageDependencies(
crtVersion: String,
clientRuntimeVersion: String
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below, many tests have been marked with throws since creating a Version from a string is now a throwing operation.

) {
let packageDependencies = PackageDependencies(
awsCRTSwiftVersion: .init(crtVersion)!,
clientRuntimeVersion: .init(clientRuntimeVersion)!
) throws {
let packageDependencies = try PackageDependencies(
awsCRTSwiftVersion: .init(crtVersion),
clientRuntimeVersion: .init(clientRuntimeVersion)
)
try! packageDependencies.save()
}
Expand All @@ -41,11 +41,11 @@ class GeneratePackageManifestTests: CLITestCase {

// MARK: Golden Path

func testGoldenPath() {
func testGoldenPath() throws {
let clientRuntimeVersion = "1.2.3"
let crtVersion = "3.2.1"
let services = ["EC2", "S3"]
createPackageDependencies(
try createPackageDependencies(
crtVersion: crtVersion,
clientRuntimeVersion: clientRuntimeVersion
)
Expand All @@ -61,10 +61,10 @@ class GeneratePackageManifestTests: CLITestCase {

// MARK: resolveVersions()

func testResolveVersionsRetrievesVersionsFromPackageDependencies() {
func testResolveVersionsRetrievesVersionsFromPackageDependencies() throws {
let clientRuntimeVersion = "1.2.3"
let crtVersion = "3.2.1"
createPackageDependencies(
try createPackageDependencies(
crtVersion: crtVersion,
clientRuntimeVersion: clientRuntimeVersion
)
Expand All @@ -74,10 +74,10 @@ class GeneratePackageManifestTests: CLITestCase {
XCTAssertEqual(versions.crt.description, crtVersion)
}

func testResolveVersionsWithExplicitVersions() {
func testResolveVersionsWithExplicitVersions() throws {
let clientRuntimeVersion = "1.2.3"
let crtVersion = "3.2.1"
let subject = GeneratePackageManifest.mock(
let subject = try GeneratePackageManifest.mock(
clientRuntimeVersion: .init(clientRuntimeVersion),
crtVersion: .init(crtVersion)
)
Expand Down
Loading
Loading