Skip to content
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
5 changes: 4 additions & 1 deletion Sources/Basics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ add_library(Basics
FileSystem+Extensions.swift
HTPClient+URLSession.swift
HTTPClient.swift
JSON+Extensions.swift)
JSON+Extensions.swift
SwiftVersion.swift)
target_link_libraries(Basics PUBLIC
TSCBasic
TSCUtility)
target_link_libraries(Basics PRIVATE
TSCclibc)
Copy link
Contributor

Choose a reason for hiding this comment

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

Because this is private (and imported using @_implementationOnly), it doesn't introduce a requirement on having the headers for TSCclibc around for any clients of libSwiftPM, right? Getting rid of that was a good cleanup and we'd hate to give it up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, that was my plan - its the same setup as TSC has

# NOTE(compnerd) workaround for CMake not setting up include flags yet
set_target_properties(Basics PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public struct HTTPClient: HTTPClientProtocol {
}
}
if request.options.addUserAgent, !request.headers.contains("User-Agent") {
request.headers.add(name: "User-Agent", value: "SwiftPackageManager/\(Versioning.currentVersion.displayString)")
request.headers.add(name: "User-Agent", value: "SwiftPackageManager/\(SwiftVersion.currentVersion.displayString)")
}
// execute
self._execute(request: request, requestNumber: 0) { result in
Expand Down
75 changes: 75 additions & 0 deletions Sources/Basics/SwiftVersion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
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
*/

@_implementationOnly import TSCclibc

public struct SwiftVersion {
/// The version number.
public var version: (major: Int, minor: Int, patch: Int)

/// Whether or not this is a development version.
public var isDevelopment: Bool

/// Build information, as an unstructured string.
public var buildIdentifier: String?

/// The major component of the version number.
public var major: Int { return version.major }
/// The minor component of the version number.
public var minor: Int { return version.minor }
/// The patch component of the version number.
public var patch: Int { return version.patch }

/// The version as a readable string.
public var displayString: String {
var result = "\(major).\(minor).\(patch)"
if isDevelopment {
result += "-dev"
}
if let buildIdentifier = self.buildIdentifier {
result += " (" + buildIdentifier + ")"
}
return result
}

/// The complete product version display string (including the name).
public var completeDisplayString: String {
var vendorPrefix = String(cString: SPM_VendorNameString())
if !vendorPrefix.isEmpty {
vendorPrefix += " "
}
return vendorPrefix + "Swift Package Manager - Swift " + displayString
}

/// The list of version specific identifiers to search when attempting to
/// load version specific package or version information, in order of
/// preference.
public var versionSpecificKeys: [String] {
return [
"@swift-\(major).\(minor).\(patch)",
"@swift-\(major).\(minor)",
"@swift-\(major)",
]
}

}

extension SwiftVersion {
/// The current version of the package manager.
public static let currentVersion = SwiftVersion(
version: (5, 4, 0),
isDevelopment: true,
buildIdentifier: getBuildIdentifier())
}

private func getBuildIdentifier() -> String? {
let buildIdentifier = String(cString: SPM_BuildIdentifierString())
return buildIdentifier.isEmpty ? nil : buildIdentifier
}
9 changes: 5 additions & 4 deletions Sources/Commands/SwiftBuildTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
*/

import ArgumentParser
import TSCUtility
import TSCBasic
import Basics
import Build
import PackageGraph
import SPMBuildCore
import Build
import TSCBasic
import TSCUtility

extension BuildSubset {
var argumentName: String {
Expand Down Expand Up @@ -79,7 +80,7 @@ public struct SwiftBuildTool: SwiftCommand {
_superCommandName: "swift",
abstract: "Build sources into binary products",
discussion: "SEE ALSO: swift run, swift package, swift test",
version: Versioning.currentVersion.completeDisplayString,
version: SwiftVersion.currentVersion.completeDisplayString,
helpNames: [.short, .long, .customLong("help", withSingleDash: true)])

@OptionGroup()
Expand Down
3 changes: 2 additions & 1 deletion Sources/Commands/SwiftPackageCollectionsTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import ArgumentParser
import Basics
import Foundation
import PackageCollections
import PackageModel
Expand Down Expand Up @@ -42,7 +43,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
_superCommandName: "swift",
abstract: "Interact with package collections",
discussion: "SEE ALSO: swift build, swift package, swift run, swift test",
version: Versioning.currentVersion.completeDisplayString,
version: SwiftVersion.currentVersion.completeDisplayString,
subcommands: [
Add.self,
Describe.self,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/SwiftPackageTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct SwiftPackageTool: ParsableCommand {
_superCommandName: "swift",
abstract: "Perform operations on Swift packages",
discussion: "SEE ALSO: swift build, swift run, swift test",
version: Versioning.currentVersion.completeDisplayString,
version: SwiftVersion.currentVersion.completeDisplayString,
subcommands: [
Clean.self,
PurgeCache.self,
Expand Down
7 changes: 4 additions & 3 deletions Sources/Commands/SwiftRunTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import ArgumentParser
import TSCBasic
import Basics
import Build
import TSCUtility
import PackageGraph
import PackageModel
import TSCBasic
import TSCUtility

/// An enumeration of the errors that can be generated by the run tool.
private enum RunError: Swift.Error {
Expand Down Expand Up @@ -86,7 +87,7 @@ public struct SwiftRunTool: SwiftCommand {
_superCommandName: "swift",
abstract: "Build and run an executable product",
discussion: "SEE ALSO: swift build, swift package, swift test",
version: Versioning.currentVersion.completeDisplayString,
version: SwiftVersion.currentVersion.completeDisplayString,
helpNames: [.short, .long, .customLong("help", withSingleDash: true)])

@OptionGroup()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/SwiftTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public struct SwiftTestTool: SwiftCommand {
_superCommandName: "swift",
abstract: "Build and run tests",
discussion: "SEE ALSO: swift build, swift run, swift package",
version: Versioning.currentVersion.completeDisplayString,
version: SwiftVersion.currentVersion.completeDisplayString,
helpNames: [.short, .long, .customLong("help", withSingleDash: true)])

@OptionGroup()
Expand Down
45 changes: 45 additions & 0 deletions Sources/PackageGraph/RepositoryPackageContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,48 @@ public class RepositoryPackageContainerProvider: PackageContainerProvider {
}
}
}

extension Git {
static func convertTagsToVersionMap(_ tags: [String]) -> [Version: [String]] {
// First, check if we need to restrict the tag set to version-specific tags.
var knownVersions: [Version: [String]] = [:]
var versionSpecificKnownVersions: [Version: [String]] = [:]

for tag in tags {
for versionSpecificKey in SwiftVersion.currentVersion.versionSpecificKeys {
if tag.hasSuffix(versionSpecificKey) {
let trimmedTag = String(tag.dropLast(versionSpecificKey.count))
if let version = Version(tag: trimmedTag) {
versionSpecificKnownVersions[version, default: []].append(tag)
}
break
}
}

if let version = Version(tag: tag) {
knownVersions[version, default: []].append(tag)
}
}
// Check if any version specific tags were found.
// If true, then return the version specific tags,
// or else return the version independent tags.
if !versionSpecificKnownVersions.isEmpty {
return versionSpecificKnownVersions
} else {
return knownVersions
}
}
}

extension Version {
/// Try a version from a git tag.
///
/// - Parameter tag: A version string possibly prepended with "v".
init?(tag: String) {
if tag.first == "v" {
self.init(string: String(tag.dropFirst()))
} else {
self.init(string: tag)
}
}
}
2 changes: 1 addition & 1 deletion Sources/PackageLoading/ManifestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
manifestPath: path,
toolsVersion: toolsVersion,
env: ProcessEnv.vars,
swiftpmVersion: Versioning.currentVersion.displayString,
swiftpmVersion: SwiftVersion.currentVersion.displayString,
fileSystem: fileSystem
)

Expand Down
7 changes: 4 additions & 3 deletions Sources/PackageLoading/ToolsVersionLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import TSCBasic
import Basics
import Foundation
import PackageModel
import TSCBasic
import TSCUtility
import Foundation

/// Protocol for the manifest loader interface.
public protocol ToolsVersionLoaderProtocol {
Expand All @@ -37,7 +38,7 @@ extension Manifest {
fileSystem: FileSystem
) throws -> AbsolutePath {
// Look for a version-specific manifest.
for versionSpecificKey in Versioning.currentVersionSpecificKeys {
for versionSpecificKey in SwiftVersion.currentVersion.versionSpecificKeys {
let versionSpecificPath = packagePath.appending(component: Manifest.basename + versionSpecificKey + ".swift")
if fileSystem.isFile(versionSpecificPath) {
return versionSpecificPath
Expand Down
12 changes: 6 additions & 6 deletions Sources/PackageModel/ToolsVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import TSCBasic

import Basics
import Foundation
import TSCBasic
import TSCUtility

/// Tools version represents version of the Swift toolchain.
Expand All @@ -27,9 +27,9 @@ public struct ToolsVersion: Equatable, Hashable, Codable {

/// The current tools version in use.
public static let currentToolsVersion = ToolsVersion(string:
"\(Versioning.currentVersion.major)." +
"\(Versioning.currentVersion.minor)." +
"\(Versioning.currentVersion.patch)")!
"\(SwiftVersion.currentVersion.major)." +
"\(SwiftVersion.currentVersion.minor)." +
"\(SwiftVersion.currentVersion.patch)")!

/// The minimum tools version that is required by the package manager.
public static let minimumRequired: ToolsVersion = .v4
Expand Down Expand Up @@ -106,7 +106,7 @@ public struct ToolsVersion: Equatable, Hashable, Codable {
packagePath: String
) throws {
// We don't want to throw any error when using the special vNext version.
if Versioning.currentVersion.isDevelopment && self == .vNext {
if SwiftVersion.currentVersion.isDevelopment && self == .vNext {
return
}

Expand Down
11 changes: 5 additions & 6 deletions Tests/FunctionalTests/VersionSpecificTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest

import TSCBasic
import Basics
import SourceControl
import TSCUtility

import SPMTestSupport
import TSCBasic
import TSCUtility
import XCTest

class VersionSpecificTests: XCTestCase {
/// Functional tests of end-to-end support for version specific dependency resolution.
Expand Down Expand Up @@ -112,7 +111,7 @@ class VersionSpecificTests: XCTestCase {
}
try repo.stage(file: "Package.swift")
try repo.commit(message: "OK v1.1.0")
try repo.tag(name: "1.1.0@swift-\(Versioning.currentVersion.major)")
try repo.tag(name: "1.1.0@swift-\(SwiftVersion.currentVersion.major)")

// The build should work now.
_ = try SwiftPMProduct.SwiftPackage.execute(["reset"], packagePath: primaryPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Basics
@testable import PackageLoading
import PackageModel
import TSCBasic
Expand Down Expand Up @@ -186,7 +187,7 @@ fileprivate func makeMockManifests(fileSystem: FileSystem, rootPath: AbsolutePat
manifestPath: manifestPath,
toolsVersion: ToolsVersion.currentToolsVersion,
env: [:],
swiftpmVersion: Versioning.currentVersion.displayString,
swiftpmVersion: SwiftVersion.currentVersion.displayString,
fileSystem: fileSystem)
manifests[key] = ManifestLoader.ManifestParseResult(compilerOutput: "mock-output-\(index)",
parsedManifest: "{ 'name': 'mock-manifest-\(index)' }")
Expand Down
12 changes: 6 additions & 6 deletions Tests/PackageLoadingTests/PD4_2LoadingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest

import TSCBasic
import TSCUtility
import SPMTestSupport
import Basics
import PackageModel
import PackageLoading
import SPMTestSupport
import TSCBasic
import TSCUtility
import XCTest

class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
override var toolsVersion: ToolsVersion {
Expand Down Expand Up @@ -327,7 +327,7 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
"let package = Package(name: \"Trivial\")"))

// Check at each possible spelling.
let currentVersion = Versioning.currentVersion
let currentVersion = SwiftVersion.currentVersion
let possibleSuffixes = [
"\(currentVersion.major).\(currentVersion.minor).\(currentVersion.patch)",
"\(currentVersion.major).\(currentVersion.minor)",
Expand Down
10 changes: 5 additions & 5 deletions Tests/PackageLoadingTests/ToolsVersionLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest

import TSCBasic
import SPMTestSupport

import Basics
import PackageModel
import PackageLoading
import SPMTestSupport
import TSCBasic
import TSCUtility
import XCTest

class ToolsVersionLoaderTests: XCTestCase {

Expand Down Expand Up @@ -674,7 +674,7 @@ class ToolsVersionLoaderTests: XCTestCase {
}

// Test version specific manifests.
let keys = Versioning.currentVersionSpecificKeys
let keys = SwiftVersion.currentVersion.versionSpecificKeys

// In case the count ever changes, we will need to modify this test.
XCTAssertEqual(keys.count, 3)
Expand Down