Skip to content

Commit

Permalink
Restore all commands as deprecated, add test coverage, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDesiatov committed May 8, 2024
1 parent 1ef0209 commit 46aec78
Show file tree
Hide file tree
Showing 15 changed files with 742 additions and 141 deletions.
Binary file modified Fixtures/SwiftSDKs/test-sdk.artifactbundle.tar.gz
Binary file not shown.
Binary file modified Fixtures/SwiftSDKs/test-sdk.artifactbundle.zip
Binary file not shown.
8 changes: 5 additions & 3 deletions Sources/PackageModel/SwiftSDKs/SwiftSDKBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ extension [SwiftSDKBundle] {
/// - hostTriple: triple of the machine on which the Swift SDK is building.
/// - targetTriple: triple of the machine for which the Swift SDK is building.
/// - Returns: ``SwiftSDK`` value with a given artifact ID, `nil` if none found.
public func selectSwiftSDK(id: String, hostTriple: Triple, targetTriple: Triple) -> SwiftSDK? {
public func selectSwiftSDK(id: String, hostTriple: Triple?, targetTriple: Triple) -> SwiftSDK? {
for bundle in self {
for (artifactID, variants) in bundle.artifacts {
guard artifactID == id else {
continue
}

for variant in variants {
guard variant.isSupporting(hostTriple: hostTriple) else {
continue
if let hostTriple {
guard variant.isSupporting(hostTriple: hostTriple) else {
continue
}
}

return variant.swiftSDKs.first { $0.targetTriple == targetTriple }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public final class SwiftSDKConfigurationStore {

guard var swiftSDK = swiftSDKs.selectSwiftSDK(
id: sdkID,
hostTriple: hostTriple,
hostTriple: nil,
targetTriple: targetTriple
) else {
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import Basics
import Foundation
import PackageModel

protocol ConfigurationSubcommand: SwiftSDKSubcommand {
/// An identifier of an already installed Swift SDK.
var sdkID: String { get }

/// A target triple of the Swift SDK.
var targetTriple: String { get }

/// Run a command related to configuration of Swift SDKs, passing it required configuration
/// values.
/// - Parameters:
/// - hostTriple: triple of the machine this command is running on.
/// - targetTriple: triple of the machine on which cross-compiled code will run on.
/// - swiftSDK: Swift SDK configuration fetched that matches currently set `sdkID` and
/// `targetTriple`.
/// - configurationStore: storage for configuration properties that this command operates on.
/// - swiftSDKsDirectory: directory containing Swift SDK artifact bundles and their configuration.
/// - observabilityScope: observability scope used for logging.
func run(
hostTriple: Triple,
targetTriple: Triple,
_ swiftSDK: SwiftSDK,
_ configurationStore: SwiftSDKConfigurationStore,
_ swiftSDKsDirectory: AbsolutePath,
_ observabilityScope: ObservabilityScope
) throws
}

extension ConfigurationSubcommand {
func run(
hostTriple: Triple,
_ swiftSDKsDirectory: AbsolutePath,
_ observabilityScope: ObservabilityScope
) throws {
fputs("warning: `swift sdk configuration` command is deprecated and will be removed in a future version of SwiftPM. Use `swift sdk configure` instead.\n", stderr)

let bundleStore = SwiftSDKBundleStore(
swiftSDKsDirectory: swiftSDKsDirectory,
fileSystem: self.fileSystem,
observabilityScope: observabilityScope,
outputHandler: { print($0) }
)
let configurationStore = try SwiftSDKConfigurationStore(
hostTimeTriple: hostTriple,
swiftSDKBundleStore: bundleStore
)
let targetTriple = try Triple(self.targetTriple)

guard let swiftSDK = try configurationStore.readConfiguration(
sdkID: sdkID,
targetTriple: targetTriple
) else {
throw SwiftSDKError.swiftSDKNotFound(
artifactID: sdkID,
hostTriple: hostTriple,
targetTriple: targetTriple
)
}

try run(
hostTriple: hostTriple,
targetTriple: targetTriple,
swiftSDK,
configurationStore,
swiftSDKsDirectory,
observabilityScope
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import Basics
import PackageModel

package struct DeprecatedSwiftSDKConfigurationCommand: ParsableCommand {
package static let configuration = CommandConfiguration(
commandName: "configuration",
abstract: """
Deprecated: use `swift sdk configure` instead.
Manages configuration options for installed Swift SDKs.
""",
subcommands: [
ResetConfiguration.self,
SetConfiguration.self,
ShowConfiguration.self,
]
)

package init() {}
}
133 changes: 133 additions & 0 deletions Sources/SwiftSDKCommand/Configuration/ResetConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import Basics
import CoreCommands
import PackageModel

struct ResetConfiguration: ConfigurationSubcommand {
static let configuration = CommandConfiguration(
commandName: "reset",
abstract: """
Resets configuration properties currently applied to a given Swift SDK and target triple. If no specific \
property is specified, all of them are reset for the Swift SDK.
"""
)

@OptionGroup(visibility: .hidden)
var locations: LocationOptions

@Flag(help: "Reset custom configuration for a path to a directory containing the SDK root.")
var sdkRootPath = false

@Flag(help: "Reset custom configuration for a path to a directory containing Swift resources for dynamic linking.")
var swiftResourcesPath = false

@Flag(help: "Reset custom configuration for a path to a directory containing Swift resources for static linking.")
var swiftStaticResourcesPath = false

@Flag(help: "Reset custom configuration for a path to a directory containing headers.")
var includeSearchPath = false

@Flag(help: "Reset custom configuration for a path to a directory containing libraries.")
var librarySearchPath = false

@Flag(help: "Reset custom configuration for a path to a toolset file.")
var toolsetPath = false

@Argument(
help: """
An identifier of an already installed Swift SDK. Use the `list` subcommand to see all available \
identifiers.
"""
)
var sdkID: String

@Argument(help: "A target triple of the Swift SDK specified by `sdk-id` identifier string.")
var targetTriple: String

func run(
hostTriple: Triple,
targetTriple: Triple,
_ swiftSDK: SwiftSDK,
_ configurationStore: SwiftSDKConfigurationStore,
_ swiftSDKsDirectory: AbsolutePath,
_ observabilityScope: ObservabilityScope
) throws {
var configuration = swiftSDK.pathsConfiguration
var shouldResetAll = true
var resetProperties = [String]()

if sdkRootPath {
configuration.sdkRootPath = nil
shouldResetAll = false
resetProperties.append(CodingKeys.sdkRootPath.stringValue)
}

if swiftResourcesPath {
configuration.swiftResourcesPath = nil
shouldResetAll = false
resetProperties.append(CodingKeys.swiftResourcesPath.stringValue)
}

if swiftStaticResourcesPath {
configuration.swiftResourcesPath = nil
shouldResetAll = false
resetProperties.append(CodingKeys.swiftStaticResourcesPath.stringValue)
}

if includeSearchPath {
configuration.includeSearchPaths = nil
shouldResetAll = false
resetProperties.append(CodingKeys.includeSearchPath.stringValue)
}

if librarySearchPath {
configuration.librarySearchPaths = nil
shouldResetAll = false
resetProperties.append(CodingKeys.librarySearchPath.stringValue)
}

if toolsetPath {
configuration.toolsetPaths = nil
shouldResetAll = false
resetProperties.append(CodingKeys.toolsetPath.stringValue)
}

if shouldResetAll {
if try !configurationStore.resetConfiguration(sdkID: sdkID, targetTriple: targetTriple) {
observabilityScope.emit(
warning: "No configuration for Swift SDK `\(sdkID)`"
)
} else {
observabilityScope.emit(
info: """
All configuration properties of Swift SDK `\(sdkID)` for target triple \
`\(targetTriple)` were successfully reset.
"""
)
}
} else {
var swiftSDK = swiftSDK
swiftSDK.pathsConfiguration = configuration
try configurationStore.updateConfiguration(sdkID: sdkID, swiftSDK: swiftSDK)

observabilityScope.emit(
info: """
These properties of Swift SDK `\(sdkID)` for target triple \
`\(targetTriple)` were successfully reset: \(resetProperties.joined(separator: ", ")).
"""
)
}
}
}
Loading

0 comments on commit 46aec78

Please sign in to comment.