-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add swift package-registry
command
#3647
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9e7c32d
Add PackageRegistry and PackageRegistryTests targets
mattt a59f70e
Add swift-package-registry command
mattt 14243fe
Temporarily disable login and password options for set command
mattt d4c835d
Update comment in package manifest
mattt 5be6170
Update copyright year
mattt 2a01a5a
Update copyright year
mattt ba06e23
Update command abstract
mattt 45a32f9
Fix error: variable declared in 'guard' condition is not usable in it…
mattt cd0997f
Use JSONEncoder/JSONDecoder makeWithDefaults()
mattt c5e703e
Remove default fileSystem argument in getRegistryConfigurationPath
mattt b60f47f
Create extension on JSONDecoder to decode directly from JSON string
mattt 61ee7ad
Refactor registry configuration to new pattern
mattt d76923f
Fix test failure by using atomic write instead of file locks
mattt c0120a2
Refactor logic for getRegistriesConfig helper
mattt 050909a
Move JSONDecoder+Extensions to Basics module
mattt 6aa9be2
Look for registries configuration at .swiftpm/configuration/registrie…
mattt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Foundation | ||
|
||
extension JSONDecoder { | ||
public func decode<T>(_ type: T.Type, from string: String) throws -> T where T : Decodable { | ||
guard let data = string.data(using: .utf8) else { | ||
let context = DecodingError.Context(codingPath: [], debugDescription: "invalid UTF-8 string") | ||
throw DecodingError.dataCorrupted(context) | ||
} | ||
|
||
return try decode(type, from: data) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import ArgumentParser | ||
import Basics | ||
import TSCBasic | ||
import SPMBuildCore | ||
import Build | ||
import PackageModel | ||
import PackageLoading | ||
import PackageGraph | ||
import SourceControl | ||
import TSCUtility | ||
import XCBuildSupport | ||
import Workspace | ||
import Foundation | ||
import PackageRegistry | ||
|
||
private enum RegistryConfigurationError: Swift.Error { | ||
case missingScope(String? = nil) | ||
case invalidURL(String) | ||
} | ||
|
||
extension RegistryConfigurationError: CustomStringConvertible { | ||
var description: String { | ||
switch self { | ||
case .missingScope(let scope?): | ||
return "no existing entry for scope: \(scope)" | ||
case .missingScope: | ||
return "no existing entry for default scope" | ||
case .invalidURL(let url): | ||
return "invalid URL: \(url)" | ||
} | ||
} | ||
} | ||
|
||
public struct SwiftPackageRegistryTool: ParsableCommand { | ||
public static var configuration = CommandConfiguration( | ||
commandName: "package-registry", | ||
_superCommandName: "swift", | ||
abstract: "Interact with package registry and manage related configuration", | ||
discussion: "SEE ALSO: swift package", | ||
version: SwiftVersion.currentVersion.completeDisplayString, | ||
subcommands: [ | ||
Set.self, | ||
Unset.self | ||
], | ||
helpNames: [.short, .long, .customLong("help", withSingleDash: true)]) | ||
|
||
@OptionGroup() | ||
var swiftOptions: SwiftToolOptions | ||
|
||
public init() {} | ||
|
||
struct Set: SwiftCommand { | ||
static let configuration = CommandConfiguration( | ||
abstract: "Set a custom registry") | ||
|
||
@OptionGroup(_hiddenFromHelp: true) | ||
var swiftOptions: SwiftToolOptions | ||
|
||
@Flag(help: "Apply settings to all projects for this user") | ||
var global: Bool = false | ||
|
||
@Option(help: "Associate the registry with a given scope") | ||
var scope: String? | ||
|
||
// TODO: Uncomment once .netrc management is implemented | ||
|
||
// @Option(help: "Specify a user name for the remote machine") | ||
// var login: String? | ||
|
||
// @Option(help: "Supply a password for the remote machine") | ||
// var password: String? | ||
|
||
@Argument(help: "The registry URL") | ||
var url: String | ||
|
||
func run(_ swiftTool: SwiftTool) throws { | ||
guard let url = URL(string: self.url), | ||
url.scheme == "https" | ||
else { | ||
throw RegistryConfigurationError.invalidURL(self.url) | ||
} | ||
|
||
// TODO: Require login if password is specified | ||
|
||
let set: (inout RegistryConfiguration) throws -> Void = { configuration in | ||
if let scope = scope { | ||
configuration.scopedRegistries[scope] = .init(url: url) | ||
} else { | ||
configuration.defaultRegistry = .init(url: url) | ||
} | ||
} | ||
|
||
let configuration = try swiftTool.getRegistriesConfig() | ||
if global { | ||
try configuration.updateShared(with: set) | ||
} else { | ||
try configuration.updateLocal(with: set) | ||
} | ||
|
||
// TODO: Add login and password to .netrc | ||
} | ||
} | ||
|
||
struct Unset: SwiftCommand { | ||
static let configuration = CommandConfiguration( | ||
abstract: "Remove a configured registry") | ||
|
||
@OptionGroup(_hiddenFromHelp: true) | ||
var swiftOptions: SwiftToolOptions | ||
|
||
@Flag(help: "Apply settings to all projects for this user") | ||
var global: Bool = false | ||
|
||
@Option(help: "Associate the registry with a given scope") | ||
var scope: String? | ||
|
||
func run(_ swiftTool: SwiftTool) throws { | ||
let unset: (inout RegistryConfiguration) throws -> Void = { configuration in | ||
if let scope = scope { | ||
guard let _ = configuration.scopedRegistries[scope] else { | ||
throw RegistryConfigurationError.missingScope(scope) | ||
} | ||
configuration.scopedRegistries.removeValue(forKey: scope) | ||
} else { | ||
guard let _ = configuration.defaultRegistry else { | ||
throw RegistryConfigurationError.missingScope() | ||
} | ||
configuration.defaultRegistry = nil | ||
} | ||
} | ||
|
||
let configuration = try swiftTool.getRegistriesConfig() | ||
if global { | ||
try configuration.updateShared(with: unset) | ||
} else { | ||
try configuration.updateLocal(with: unset) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - | ||
|
||
|
||
private extension SwiftTool { | ||
func getRegistriesConfig() throws -> Workspace.Configuration.Registries { | ||
let localRegistriesFile = try Workspace.DefaultLocations.registriesConfigurationFile(forRootPackage: self.getPackageRoot()) | ||
|
||
let workspace = try getActiveWorkspace() | ||
let sharedRegistriesFile = workspace.location.sharedConfigurationDirectory.map { | ||
Workspace.DefaultLocations.registriesConfigurationFile(at: $0) | ||
} | ||
|
||
return try .init( | ||
localRegistriesFile: localRegistriesFile, | ||
sharedRegistriesFile: sharedRegistriesFile, | ||
fileSystem: localFileSystem | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
add_library(PackageRegistry | ||
RegistryConfiguration.swift) | ||
target_link_libraries(PackageRegistry PUBLIC | ||
TSCBasic | ||
PackageLoading | ||
PackageModel | ||
TSCUtility) | ||
# NOTE(compnerd) workaround for CMake not setting up include flags yet | ||
set_target_properties(PackageRegistry PROPERTIES | ||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) | ||
|
||
if(USE_CMAKE_INSTALL) | ||
install(TARGETS PackageRegistry | ||
ARCHIVE DESTINATION lib | ||
LIBRARY DESTINATION lib | ||
RUNTIME DESTINATION bin) | ||
endif() | ||
set_property(GLOBAL APPEND PROPERTY SwiftPM_EXPORTS PackageRegistry) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.