From 7a7f36ad9b4ec46667e1b338ebc0a40215e4d8d5 Mon Sep 17 00:00:00 2001 From: Chris Araman Date: Mon, 26 Apr 2021 16:44:17 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20Declare=20mas=20as=20a=20Swift?= =?UTF-8?q?=20Package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + Package.resolved | 61 ++++++++++++++++ Package.swift | 73 +++++++++++++++++++ Sources/MasKit/Commands/Open.swift | 1 + Sources/MasKit/Commands/Outdated.swift | 1 + Sources/MasKit/Commands/Upgrade.swift | 1 + Sources/MasKit/Commands/Version.swift | 1 + .../Commands/OpenCommandSpec.swift | 1 + .../Commands/UninstallCommandSpec.swift | 1 + .../MasKitTests/Extensions/Bundle+JSON.swift | 16 ++-- .../Models/SearchResultListSpec.swift | 1 + .../MasKitTests/Models/SearchResultSpec.swift | 1 + .../Network/NetworkSessionMock.swift | 1 + mas-cli.xcodeproj/project.pbxproj | 2 + script/lint | 2 +- 15 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 Package.resolved create mode 100644 Package.swift diff --git a/.gitignore b/.gitignore index 5621b3e..3b099ac 100644 --- a/.gitignore +++ b/.gitignore @@ -22,9 +22,11 @@ .VolumeIcon.icns ._* .apdisk +.build/ .envrc .fseventsd .rubygems/ +.swiftpm/ Carthage/ DerivedData Pods/ diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..e66dde2 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,61 @@ +{ + "object": { + "pins": [ + { + "package": "Commandant", + "repositoryURL": "https://github.com/Carthage/Commandant.git", + "state": { + "branch": null, + "revision": "a1671cf728db837cf5ec1980a80d276bbba748f6", + "version": "0.18.0" + } + }, + { + "package": "CwlCatchException", + "repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git", + "state": { + "branch": null, + "revision": "f809deb30dc5c9d9b78c872e553261a61177721a", + "version": "2.0.0" + } + }, + { + "package": "CwlPreconditionTesting", + "repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git", + "state": { + "branch": null, + "revision": "02b7a39a99c4da27abe03cab2053a9034379639f", + "version": "2.0.0" + } + }, + { + "package": "Nimble", + "repositoryURL": "https://github.com/Quick/Nimble.git", + "state": { + "branch": null, + "revision": "7a54aaf19a8ef16f67787c260fda81ead7ba4d67", + "version": "9.0.1" + } + }, + { + "package": "Quick", + "repositoryURL": "https://github.com/Quick/Quick.git", + "state": { + "branch": null, + "revision": "8cce6acd38f965f5baa3167b939f86500314022b", + "version": "3.1.2" + } + }, + { + "package": "Version", + "repositoryURL": "https://github.com/mxcl/Version.git", + "state": { + "branch": null, + "revision": "a94b48f36763c05629fc102837398505032dead9", + "version": "2.0.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..7b7c8e9 --- /dev/null +++ b/Package.swift @@ -0,0 +1,73 @@ +// swift-tools-version:5.3 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "mas", + platforms: [ + .macOS(.v10_11), + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .executable( + name: "mas", + targets: ["mas"] + ), + .library( + name: "MasKit", + targets: ["MasKit"] + ), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + .package(url: "https://github.com/Carthage/Commandant.git", from: "0.18.0"), + .package(url: "https://github.com/Quick/Nimble.git", from: "9.0.1"), + .package(url: "https://github.com/Quick/Quick.git", from: "3.1.2"), + .package(url: "https://github.com/mxcl/Version.git", from: "2.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "mas", + dependencies: ["MasKit"], + swiftSettings: [ + .unsafeFlags([ + "-F", "/System/Library/PrivateFrameworks", + "-I", "Sources/PrivateFrameworks/CommerceKit", + "-I", "Sources/PrivateFrameworks/StoreFoundation", + ]), + ] + ), + .target( + name: "MasKit", + dependencies: ["Commandant", "Version"], + swiftSettings: [ + .unsafeFlags([ + "-F", "/System/Library/PrivateFrameworks", + "-I", "Sources/PrivateFrameworks/CommerceKit", + "-I", "Sources/PrivateFrameworks/StoreFoundation", + ]), + ], + linkerSettings: [ + .linkedFramework("CommerceKit"), + .linkedFramework("StoreFoundation"), + .unsafeFlags(["-F", "/System/Library/PrivateFrameworks"]), + ] + ), + .testTarget( + name: "MasKitTests", + dependencies: ["MasKit", "Nimble", "Quick"], + resources: [.copy("JSON")], + swiftSettings: [ + .unsafeFlags([ + "-F", "/System/Library/PrivateFrameworks", + "-I", "Sources/PrivateFrameworks/CommerceKit", + "-I", "Sources/PrivateFrameworks/StoreFoundation", + ]), + ] + ), + ], + swiftLanguageVersions: [.v5] +) diff --git a/Sources/MasKit/Commands/Open.swift b/Sources/MasKit/Commands/Open.swift index 732c144..8394ac3 100644 --- a/Sources/MasKit/Commands/Open.swift +++ b/Sources/MasKit/Commands/Open.swift @@ -7,6 +7,7 @@ // import Commandant +import Foundation private let markerValue = "appstore" private let masScheme = "macappstore" diff --git a/Sources/MasKit/Commands/Outdated.swift b/Sources/MasKit/Commands/Outdated.swift index dbba1e7..e8f5f00 100644 --- a/Sources/MasKit/Commands/Outdated.swift +++ b/Sources/MasKit/Commands/Outdated.swift @@ -7,6 +7,7 @@ // import Commandant +import Foundation /// Command which displays a list of installed apps which have available updates /// ready to be installed from the Mac App Store. diff --git a/Sources/MasKit/Commands/Upgrade.swift b/Sources/MasKit/Commands/Upgrade.swift index baf09c2..b243d8e 100644 --- a/Sources/MasKit/Commands/Upgrade.swift +++ b/Sources/MasKit/Commands/Upgrade.swift @@ -7,6 +7,7 @@ // import Commandant +import Foundation /// Command which upgrades apps with new versions available in the Mac App Store. public struct UpgradeCommand: CommandProtocol { diff --git a/Sources/MasKit/Commands/Version.swift b/Sources/MasKit/Commands/Version.swift index bdf3c60..d74b35b 100644 --- a/Sources/MasKit/Commands/Version.swift +++ b/Sources/MasKit/Commands/Version.swift @@ -7,6 +7,7 @@ // import Commandant +import Foundation /// Command which displays the version of the mas tool. public struct VersionCommand: CommandProtocol { diff --git a/Tests/MasKitTests/Commands/OpenCommandSpec.swift b/Tests/MasKitTests/Commands/OpenCommandSpec.swift index 97c2ec5..97383e7 100644 --- a/Tests/MasKitTests/Commands/OpenCommandSpec.swift +++ b/Tests/MasKitTests/Commands/OpenCommandSpec.swift @@ -6,6 +6,7 @@ // Copyright © 2019 mas-cli. All rights reserved. // +import Foundation import Nimble import Quick diff --git a/Tests/MasKitTests/Commands/UninstallCommandSpec.swift b/Tests/MasKitTests/Commands/UninstallCommandSpec.swift index f988eba..eeef61a 100644 --- a/Tests/MasKitTests/Commands/UninstallCommandSpec.swift +++ b/Tests/MasKitTests/Commands/UninstallCommandSpec.swift @@ -6,6 +6,7 @@ // Copyright © 2018 mas-cli. All rights reserved. // +import Foundation import Nimble import Quick diff --git a/Tests/MasKitTests/Extensions/Bundle+JSON.swift b/Tests/MasKitTests/Extensions/Bundle+JSON.swift index b05bb55..ef0129f 100644 --- a/Tests/MasKitTests/Extensions/Bundle+JSON.swift +++ b/Tests/MasKitTests/Extensions/Bundle+JSON.swift @@ -24,7 +24,13 @@ extension Bundle { /// - Parameter fileName: Name of file to locate. /// - Returns: URL to file. static func url(for fileName: String) -> URL? { - Bundle(for: NetworkSessionMock.self).url(for: fileName) + var bundle = Bundle(for: NetworkSessionMock.self) + #if SWIFT_PACKAGE + // The Swift Package Manager places resources in a separate bundle from the executable. + bundle = + Bundle(url: bundle.bundleURL.deletingLastPathComponent().appendingPathComponent("mas_MasKitTests.bundle"))! + #endif + return bundle.url(for: fileName) } /// Builds a URL for a file in the JSON directory of the current bundle. @@ -33,13 +39,13 @@ extension Bundle { /// - Returns: URL to file. func url(for fileName: String) -> URL? { guard - let path = self.path( + let url = self.url( forResource: fileName.fileNameWithoutExtension, - ofType: fileName.fileExtension, - inDirectory: "JSON" + withExtension: fileName.fileExtension, + subdirectory: "JSON" ) else { fatalError("Unable to load file \(fileName)") } - return URL(fileURLWithPath: path) + return url } } diff --git a/Tests/MasKitTests/Models/SearchResultListSpec.swift b/Tests/MasKitTests/Models/SearchResultListSpec.swift index 8df863e..14e90f2 100644 --- a/Tests/MasKitTests/Models/SearchResultListSpec.swift +++ b/Tests/MasKitTests/Models/SearchResultListSpec.swift @@ -6,6 +6,7 @@ // Copyright © 2020 mas-cli. All rights reserved. // +import Foundation import Nimble import Quick diff --git a/Tests/MasKitTests/Models/SearchResultSpec.swift b/Tests/MasKitTests/Models/SearchResultSpec.swift index c9933d9..16d69d2 100644 --- a/Tests/MasKitTests/Models/SearchResultSpec.swift +++ b/Tests/MasKitTests/Models/SearchResultSpec.swift @@ -6,6 +6,7 @@ // Copyright © 2020 mas-cli. All rights reserved. // +import Foundation import Nimble import Quick diff --git a/Tests/MasKitTests/Network/NetworkSessionMock.swift b/Tests/MasKitTests/Network/NetworkSessionMock.swift index a3e2f52..f168cfd 100644 --- a/Tests/MasKitTests/Network/NetworkSessionMock.swift +++ b/Tests/MasKitTests/Network/NetworkSessionMock.swift @@ -7,6 +7,7 @@ // import Foundation + @testable import MasKit /// Mock NetworkSession for testing. diff --git a/mas-cli.xcodeproj/project.pbxproj b/mas-cli.xcodeproj/project.pbxproj index ccb3010..0d3fac8 100644 --- a/mas-cli.xcodeproj/project.pbxproj +++ b/mas-cli.xcodeproj/project.pbxproj @@ -234,6 +234,7 @@ B5DBF81221DEEC7C00F3B151 /* OpenCommandSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenCommandSpec.swift; sourceTree = ""; }; B5DBF81421E02BA900F3B151 /* StoreSearchMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreSearchMock.swift; sourceTree = ""; }; B5DBF81621E02E3400F3B151 /* OpenSystemCommandMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenSystemCommandMock.swift; sourceTree = ""; }; + C56F16DF2637825300EAC548 /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = ""; }; ED031A781B5127C00097692E /* mas */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = mas; sourceTree = BUILT_PRODUCTS_DIR; }; ED031A7B1B5127C00097692E /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; ED0F237E1B87522400AE40CD /* Install.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Install.swift; sourceTree = ""; }; @@ -543,6 +544,7 @@ C56F16DB2637711100EAC548 /* Sources */, C56F16DC2637711900EAC548 /* Tests */, ED031A791B5127C00097692E /* Products */, + C56F16DF2637825300EAC548 /* Package.swift */, ); sourceTree = ""; }; diff --git a/script/lint b/script/lint index 469a620..93503ab 100755 --- a/script/lint +++ b/script/lint @@ -24,7 +24,7 @@ git diff --check echo echo "--> 🕊️ Swift" -for SOURCE in Sources Tests; do +for SOURCE in Package.swift Sources Tests; do swiftformat --lint ${SOURCE} swift-format lint --configuration .swift-format --recursive ${SOURCE} swiftlint lint --strict ${SOURCE}