Skip to content

Commit

Permalink
Reduce TSCBasic usages
Browse files Browse the repository at this point in the history
- Use `TSCAbsolutePath` typealias
- Use `Basics.localFileSystem` which is an alias to `TSCBasic.localFileSystem`
- Make `DescriptionPackage.init` async and remove `tsc_await` usage
  • Loading branch information
ikesyo committed Dec 13, 2024
1 parent 45231ba commit 2500ca6
Show file tree
Hide file tree
Showing 24 changed files with 107 additions and 103 deletions.
27 changes: 14 additions & 13 deletions Sources/ScipioKit/DescriptionPackage.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Foundation
import Workspace
import TSCBasic
import Basics
import enum TSCBasic.GraphError
import func TSCBasic.topologicalSort
import Collections
import PackageModel
import PackageLoading
// We may drop this annotation in SwiftPM's future release
@preconcurrency import PackageGraph
import Basics

struct DescriptionPackage: PackageLocator {
let mode: Runner.Mode
Expand Down Expand Up @@ -46,7 +48,7 @@ struct DescriptionPackage: PackageLocator {
// override default configuration to treat XIB files
workspaceConfiguration.additionalFileRules = FileRuleDescription.xcbuildFileTypes

let fileSystem = TSCBasic.localFileSystem
let fileSystem = Basics.localFileSystem
let authorizationProvider = try Workspace.Configuration.Authorization.default
.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: makeObservabilitySystem().topScope)
let workspace = try Workspace(
Expand All @@ -71,7 +73,7 @@ struct DescriptionPackage: PackageLocator {
mode: Runner.Mode,
onlyUseVersionsFromResolvedFile: Bool,
toolchainEnvironment: ToolchainEnvironment? = nil
) throws {
) async throws {
self.packageDirectory = packageDirectory
self.mode = mode
self.toolchain = try UserToolchain(
Expand All @@ -91,13 +93,10 @@ struct DescriptionPackage: PackageLocator {
forceResolvedVersions: onlyUseVersionsFromResolvedFile,
observabilityScope: scope
)
self.manifest = try tsc_await {
workspace.loadRootManifest(
at: packageDirectory.spmAbsolutePath,
observabilityScope: scope,
completion: $0
)
}
self.manifest = try await workspace.loadRootManifest(
at: packageDirectory.spmAbsolutePath,
observabilityScope: scope
)
self.workspace = workspace
}
}
Expand Down Expand Up @@ -178,8 +177,10 @@ private final class BuildProductsResolver {
}
} catch {
switch error {
case GraphError.unexpectedCycle: throw DescriptionPackage.Error.cycleDetected
default: throw error
case GraphError.unexpectedCycle:
throw DescriptionPackage.Error.cycleDetected
default:
throw error
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ScipioKit/Producer/BinaryExtractor.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import PackageGraph
import PackageModel
import TSCBasic
import Basics

struct BinaryExtractor {
var package: DescriptionPackage
Expand Down
2 changes: 1 addition & 1 deletion Sources/ScipioKit/Producer/Cache/CacheSystem.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import ScipioStorage
import TSCBasic
import Basics
import struct TSCUtility.Version
import Algorithms
// We may drop this annotation in SwiftPM's future release
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import ScipioStorage
import PackageGraph
import TSCBasic
import Basics

struct LocalDiskCacheStorage: CacheStorage {
private let fileSystem: any FileSystem
Expand Down
20 changes: 11 additions & 9 deletions Sources/ScipioKit/Producer/Compiler.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import PackageGraph
import TSCBasic
import Basics

protocol Compiler {
var descriptionPackage: DescriptionPackage { get }
Expand All @@ -16,20 +16,22 @@ extension Compiler {
buildConfiguration: BuildConfiguration,
sdks: Set<SDK>,
fileSystem: FileSystem = localFileSystem
) async throws -> [SDK: [AbsolutePath]] {
) async throws -> [SDK: [TSCAbsolutePath]] {
let extractor = DwarfExtractor()

var result = [SDK: [AbsolutePath]]()
var result = [SDK: [TSCAbsolutePath]]()

for sdk in sdks {
let dsymPath = descriptionPackage.buildDebugSymbolPath(buildConfiguration: buildConfiguration, sdk: sdk, target: target)
guard fileSystem.exists(dsymPath) else { continue }
let debugSymbol = DebugSymbol(dSYMPath: dsymPath,
target: target,
sdk: sdk,
buildConfiguration: buildConfiguration)
let debugSymbol = DebugSymbol(
dSYMPath: dsymPath,
target: target,
sdk: sdk,
buildConfiguration: buildConfiguration
)
let dumpedDSYMsMaps = try await extractor.dump(dwarfPath: debugSymbol.dwarfPath)
let bcSymbolMapPaths: [AbsolutePath] = dumpedDSYMsMaps.values.compactMap { uuid in
let bcSymbolMapPaths: [TSCAbsolutePath] = dumpedDSYMsMaps.values.compactMap { uuid in
let path = descriptionPackage.productsDirectory(
buildConfiguration: debugSymbol.buildConfiguration,
sdk: debugSymbol.sdk
Expand All @@ -45,7 +47,7 @@ extension Compiler {
}

extension DescriptionPackage {
fileprivate func buildDebugSymbolPath(buildConfiguration: BuildConfiguration, sdk: SDK, target: ScipioResolvedModule) -> AbsolutePath {
fileprivate func buildDebugSymbolPath(buildConfiguration: BuildConfiguration, sdk: SDK, target: ScipioResolvedModule) -> TSCAbsolutePath {
productsDirectory(buildConfiguration: buildConfiguration, sdk: sdk)
.appending(component: "\(target.name).framework.dSYM")
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/ScipioKit/Producer/DebugSymbol.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Foundation
import PackageGraph
import TSCBasic
import Basics

struct DebugSymbol {
var dSYMPath: AbsolutePath
var dSYMPath: TSCAbsolutePath
var target: ScipioResolvedModule
var sdk: SDK
var buildConfiguration: BuildConfiguration

var dwarfPath: AbsolutePath {
var dwarfPath: TSCAbsolutePath {
dSYMPath.appending(components: "Contents", "Resources", "DWARF", target.name)
}
}
Expand All @@ -21,7 +21,8 @@ struct DwarfExtractor<E: Executor> {
}

typealias Arch = String
func dump(dwarfPath: AbsolutePath) async throws -> [Arch: UUID] {

func dump(dwarfPath: TSCAbsolutePath) async throws -> [Arch: UUID] {
let result = try await executor.execute("/usr/bin/xcrun", "dwarfdump", "--uuid", dwarfPath.pathString)

let output = try result.unwrapOutput()
Expand Down
2 changes: 1 addition & 1 deletion Sources/ScipioKit/Producer/FrameworkProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageGraph
import PackageModel
import Collections
import protocol TSCBasic.FileSystem
import var TSCBasic.localFileSystem
import Basics

struct FrameworkProducer {
private let descriptionPackage: DescriptionPackage
Expand Down
4 changes: 2 additions & 2 deletions Sources/ScipioKit/Producer/InfoPlistGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import TSCBasic
import Basics

struct InfoPlistGenerator {
private let fileSystem: any FileSystem
Expand All @@ -8,7 +8,7 @@ struct InfoPlistGenerator {
self.fileSystem = fileSystem
}

func generateForResourceBundle(at path: AbsolutePath) throws {
func generateForResourceBundle(at path: TSCAbsolutePath) throws {
let body = resourceBundleBody
try fileSystem.writeFileContents(path.spmAbsolutePath, string: body)
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/ScipioKit/Producer/PIF/BuildParametersGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import TSCBasic
import Basics
import struct TSCBasic.ByteString
import XCBuildSupport
import SPMBuildCore

Expand Down Expand Up @@ -30,12 +31,12 @@ struct BuildParametersGenerator {
private let buildOptions: BuildOptions
private let fileSystem: any FileSystem

init(buildOptions: BuildOptions, fileSystem: any FileSystem = TSCBasic.localFileSystem) {
init(buildOptions: BuildOptions, fileSystem: any FileSystem = localFileSystem) {
self.buildOptions = buildOptions
self.fileSystem = fileSystem
}

func generate(for sdk: SDK, buildParameters: BuildParameters, destinationDir: AbsolutePath) throws -> AbsolutePath {
func generate(for sdk: SDK, buildParameters: BuildParameters, destinationDir: TSCAbsolutePath) throws -> TSCAbsolutePath {
let targetArchitecture = buildParameters.triple.arch?.rawValue ?? "arm64"

// Generate the run destination parameters.
Expand Down
16 changes: 8 additions & 8 deletions Sources/ScipioKit/Producer/PIF/FrameworkBundleAssembler.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import Foundation
import TSCBasic
import Basics

/// A assembler to generate framework bundle
/// This assembler just relocates framework components into the framework structure
struct FrameworkBundleAssembler {
private let frameworkComponents: FrameworkComponents
private let keepPublicHeadersStructure: Bool
private let outputDirectory: AbsolutePath
private let outputDirectory: TSCAbsolutePath
private let fileSystem: any FileSystem

private var frameworkBundlePath: AbsolutePath {
private var frameworkBundlePath: TSCAbsolutePath {
outputDirectory.appending(component: "\(frameworkComponents.frameworkName).framework")
}

init(
frameworkComponents: FrameworkComponents,
keepPublicHeadersStructure: Bool,
outputDirectory: AbsolutePath,
outputDirectory: TSCAbsolutePath,
fileSystem: some FileSystem
) {
self.frameworkComponents = frameworkComponents
Expand All @@ -26,7 +26,7 @@ struct FrameworkBundleAssembler {
}

@discardableResult
func assemble() throws -> AbsolutePath {
func assemble() throws -> TSCAbsolutePath {
try fileSystem.createDirectory(frameworkBundlePath, recursive: true)

try copyInfoPlist()
Expand Down Expand Up @@ -96,9 +96,9 @@ struct FrameworkBundleAssembler {
}

private func copyHeaderKeepingStructure(
header: AbsolutePath,
includeDir: AbsolutePath,
into headerDir: AbsolutePath
header: TSCAbsolutePath,
includeDir: TSCAbsolutePath,
into headerDir: TSCAbsolutePath
) throws {
let subdirectoryComponents: [String] = if header.dirname.hasPrefix(includeDir.pathString) {
header.dirname.dropFirst(includeDir.pathString.count)
Expand Down
18 changes: 9 additions & 9 deletions Sources/ScipioKit/Producer/PIF/FrameworkModuleMapGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import TSCBasic
import Basics
import PackageGraph
import PackageModel

Expand All @@ -15,7 +15,7 @@ struct FrameworkModuleMapGenerator {
private var fileSystem: any FileSystem

enum Error: LocalizedError {
case unableToLoadCustomModuleMap(AbsolutePath)
case unableToLoadCustomModuleMap(TSCAbsolutePath)

var errorDescription: String? {
switch self {
Expand All @@ -34,7 +34,7 @@ struct FrameworkModuleMapGenerator {
resolvedTarget: ScipioResolvedModule,
sdk: SDK,
keepPublicHeadersStructure: Bool
) throws -> AbsolutePath? {
) throws -> TSCAbsolutePath? {
let context = Context(
resolvedTarget: resolvedTarget,
sdk: sdk,
Expand Down Expand Up @@ -106,7 +106,7 @@ struct FrameworkModuleMapGenerator {
}
}

private func walkDirectoryContents(of directoryPath: AbsolutePath) throws -> Set<AbsolutePath> {
private func walkDirectoryContents(of directoryPath: TSCAbsolutePath) throws -> Set<TSCAbsolutePath> {
try fileSystem.getDirectoryContents(directoryPath).reduce(into: Set()) { headers, file in
let path = directoryPath.appending(component: file)
if fileSystem.isDirectory(path) {
Expand All @@ -118,8 +118,8 @@ struct FrameworkModuleMapGenerator {
}

private func generateHeaderEntry(
for header: AbsolutePath,
of directoryPath: AbsolutePath,
for header: TSCAbsolutePath,
of directoryPath: TSCAbsolutePath,
keepPublicHeadersStructure: Bool
) -> String {
if keepPublicHeadersStructure {
Expand All @@ -144,20 +144,20 @@ struct FrameworkModuleMapGenerator {
.map { " link framework \"\($0)\"" }
}

private func generateModuleMapFile(context: Context, outputPath: AbsolutePath) throws {
private func generateModuleMapFile(context: Context, outputPath: TSCAbsolutePath) throws {
let dirPath = outputPath.parentDirectory
try fileSystem.createDirectory(dirPath, recursive: true)

let contents = try generateModuleMapContents(context: context)
try fileSystem.writeFileContents(outputPath.spmAbsolutePath, string: contents)
}

private func constructGeneratedModuleMapPath(context: Context) throws -> AbsolutePath {
private func constructGeneratedModuleMapPath(context: Context) throws -> TSCAbsolutePath {
let generatedModuleMapPath = try packageLocator.generatedModuleMapPath(of: context.resolvedTarget, sdk: context.sdk)
return generatedModuleMapPath
}

private func convertCustomModuleMapForFramework(_ customModuleMap: AbsolutePath) throws -> String {
private func convertCustomModuleMapForFramework(_ customModuleMap: TSCAbsolutePath) throws -> String {
// Sometimes, targets have their custom modulemaps.
// However, these are not for frameworks
// This process converts them to modulemaps for frameworks
Expand Down
3 changes: 1 addition & 2 deletions Sources/ScipioKit/Producer/PIF/PIFCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PackageModel
import SPMBuildCore
import PackageGraph
import Basics
import var TSCBasic.localFileSystem

struct PIFCompiler: Compiler {
let descriptionPackage: DescriptionPackage
Expand All @@ -20,7 +19,7 @@ struct PIFCompiler: Compiler {
buildOptions: BuildOptions,
buildOptionsMatrix: [String: BuildOptions],
toolchainEnvironment: ToolchainEnvironment? = nil,
fileSystem: any FileSystem = TSCBasic.localFileSystem,
fileSystem: any FileSystem = localFileSystem,
executor: any Executor = ProcessExecutor()
) {
self.descriptionPackage = descriptionPackage
Expand Down
6 changes: 3 additions & 3 deletions Sources/ScipioKit/Producer/PIF/PIFGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import TSCBasic
import Basics
import SPMBuildCore
import PackageModel
import PackageGraph
Expand Down Expand Up @@ -35,7 +35,7 @@ struct PIFGenerator {
buildParameters: BuildParameters,
buildOptions: BuildOptions,
buildOptionsMatrix: [String: BuildOptions],
fileSystem: any FileSystem = TSCBasic.localFileSystem
fileSystem: any FileSystem = localFileSystem
) throws {
self.descriptionPackage = package
self.buildParameters = buildParameters
Expand All @@ -58,7 +58,7 @@ struct PIFGenerator {
return try jsonDecoder.decode(PIF.TopLevelObject.self, from: data)
}

func generateJSON(for sdk: SDK) throws -> AbsolutePath {
func generateJSON(for sdk: SDK) throws -> TSCAbsolutePath {
let topLevelObject = modify(try generatePIF(), for: sdk)

try PIF.sign(topLevelObject.workspace)
Expand Down
Loading

0 comments on commit 2500ca6

Please sign in to comment.