Skip to content

Commit

Permalink
Merge pull request #151 from ikesyo/introduce-packagelocator-protocol
Browse files Browse the repository at this point in the history
Factor out DescriptionPackage's path-related functions to PackageLocator protocol
  • Loading branch information
ikesyo authored Oct 22, 2024
2 parents 3d8f3bc + 3873363 commit 4c2cc39
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 74 deletions.
55 changes: 2 additions & 53 deletions Sources/ScipioKit/DescriptionPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Workspace
import TSCBasic
import PackageModel
import PackageLoading
// We can drop this annotation with SwiftPM release/6.0
// We may drop this annotation in SwiftPM's future release
@preconcurrency import PackageGraph
import Basics

struct DescriptionPackage {
struct DescriptionPackage: PackageLocator {
let mode: Runner.Mode
let packageDirectory: ScipioAbsolutePath
private let toolchain: UserToolchain
Expand Down Expand Up @@ -35,61 +35,10 @@ struct DescriptionPackage {
manifest.displayName
}

var buildDirectory: ScipioAbsolutePath {
packageDirectory.appending(component: ".build")
}

var workspaceDirectory: ScipioAbsolutePath {
buildDirectory.appending(component: "scipio")
}

var supportedSDKs: Set<SDK> {
Set(manifest.platforms.map(\.platformName).compactMap(SDK.init(platformName:)))
}

var derivedDataPath: ScipioAbsolutePath {
workspaceDirectory.appending(component: "DerivedData")
}

func generatedModuleMapPath(of target: ScipioResolvedModule, sdk: SDK) throws -> ScipioAbsolutePath {
let relativePath = try TSCBasic.RelativePath(validating: "ModuleMapsForFramework/\(sdk.settingValue)")
return workspaceDirectory
.appending(relativePath)
.appending(component: target.modulemapName)
}

/// Returns an Products directory path
/// It should be the default setting of `TARGET_BUILD_DIR`
func productsDirectory(buildConfiguration: BuildConfiguration, sdk: SDK) -> ScipioAbsolutePath {
let intermediateDirectoryName = productDirectoryName(
buildConfiguration: buildConfiguration,
sdk: sdk
)
return derivedDataPath.appending(components: ["Products", intermediateDirectoryName])
}

/// Returns a directory path which contains assembled frameworks
var assembledFrameworksRootDirectory: ScipioAbsolutePath {
workspaceDirectory.appending(component: "AssembledFrameworks")
}

/// Returns a directory path of the assembled frameworks path for the specific Configuration/Platform
func assembledFrameworksDirectory(buildConfiguration: BuildConfiguration, sdk: SDK) -> ScipioAbsolutePath {
let intermediateDirName = productDirectoryName(buildConfiguration: buildConfiguration, sdk: sdk)
return assembledFrameworksRootDirectory
.appending(component: intermediateDirName)
}

/// Returns an intermediate directory name in the Products dir.
/// e.g. `Debug` / `Debug-iphoneos`
private func productDirectoryName(buildConfiguration: BuildConfiguration, sdk: SDK) -> String {
if sdk == .macOS {
return buildConfiguration.settingsValue
} else {
return "\(buildConfiguration.settingsValue)-\(sdk.settingValue)"
}
}

// MARK: Initializer

private static func makeWorkspace(toolchain: UserToolchain, packagePath: ScipioAbsolutePath) throws -> Workspace {
Expand Down
60 changes: 60 additions & 0 deletions Sources/ScipioKit/PackageLocator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import TSCBasic
import PackageModel

/// Holds the packageDirectory Scipio works on, and defines some path-related functionalities.
protocol PackageLocator {
var packageDirectory: ScipioAbsolutePath { get }
}

extension PackageLocator {
var buildDirectory: ScipioAbsolutePath {
packageDirectory.appending(component: ".build")
}

var workspaceDirectory: ScipioAbsolutePath {
buildDirectory.appending(component: "scipio")
}

var derivedDataPath: ScipioAbsolutePath {
workspaceDirectory.appending(component: "DerivedData")
}

func generatedModuleMapPath(of target: ScipioResolvedModule, sdk: SDK) throws -> ScipioAbsolutePath {
let relativePath = try TSCBasic.RelativePath(validating: "ModuleMapsForFramework/\(sdk.settingValue)")
return workspaceDirectory
.appending(relativePath)
.appending(component: target.modulemapName)
}

/// Returns an Products directory path
/// It should be the default setting of `TARGET_BUILD_DIR`
func productsDirectory(buildConfiguration: BuildConfiguration, sdk: SDK) -> ScipioAbsolutePath {
let intermediateDirectoryName = productDirectoryName(
buildConfiguration: buildConfiguration,
sdk: sdk
)
return derivedDataPath.appending(components: ["Products", intermediateDirectoryName])
}

/// Returns a directory path which contains assembled frameworks
var assembledFrameworksRootDirectory: ScipioAbsolutePath {
workspaceDirectory.appending(component: "AssembledFrameworks")
}

/// Returns a directory path of the assembled frameworks path for the specific Configuration/Platform
func assembledFrameworksDirectory(buildConfiguration: BuildConfiguration, sdk: SDK) -> ScipioAbsolutePath {
let intermediateDirName = productDirectoryName(buildConfiguration: buildConfiguration, sdk: sdk)
return assembledFrameworksRootDirectory
.appending(component: intermediateDirName)
}

/// Returns an intermediate directory name in the Products dir.
/// e.g. `Debug` / `Debug-iphoneos`
private func productDirectoryName(buildConfiguration: BuildConfiguration, sdk: SDK) -> String {
if sdk == .macOS {
return buildConfiguration.settingsValue
} else {
return "\(buildConfiguration.settingsValue)-\(sdk.settingValue)"
}
}
}
1 change: 1 addition & 0 deletions Sources/ScipioKit/Producer/Cache/CacheSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ScipioStorage
import TSCBasic
import struct TSCUtility.Version
import Algorithms
// We may drop this annotation in SwiftPM's future release
@preconcurrency import PackageGraph
import PackageModel
import SourceControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ struct FrameworkComponentsCollector {
}
}

private let descriptionPackage: DescriptionPackage
private let buildProduct: BuildProduct
private let sdk: SDK
private let buildOptions: BuildOptions
private let packageLocator: any PackageLocator
private let fileSystem: any FileSystem

init(
descriptionPackage: DescriptionPackage,
buildProduct: BuildProduct,
sdk: SDK,
buildOptions: BuildOptions,
fileSystem: any FileSystem
packageLocator: some PackageLocator,
fileSystem: some FileSystem
) {
self.descriptionPackage = descriptionPackage
self.buildProduct = buildProduct
self.sdk = sdk
self.buildOptions = buildOptions
self.packageLocator = packageLocator
self.fileSystem = fileSystem
}

Expand Down Expand Up @@ -95,15 +95,15 @@ struct FrameworkComponentsCollector {

/// Copy content data to the build artifacts
private func copyModuleMapContentsToBuildArtifacts(_ data: Data) throws -> ScipioAbsolutePath {
let generatedModuleMapPath = try descriptionPackage.generatedModuleMapPath(of: buildProduct.target, sdk: sdk)
let generatedModuleMapPath = try packageLocator.generatedModuleMapPath(of: buildProduct.target, sdk: sdk)

try fileSystem.writeFileContents(generatedModuleMapPath.spmAbsolutePath, data: data)
return generatedModuleMapPath
}

private func generateFrameworkModuleMap() throws -> AbsolutePath? {
let modulemapGenerator = FrameworkModuleMapGenerator(
descriptionPackage: descriptionPackage,
packageLocator: packageLocator,
fileSystem: fileSystem
)

Expand All @@ -119,7 +119,7 @@ struct FrameworkComponentsCollector {
}

private func generatedFrameworkPath() -> AbsolutePath {
descriptionPackage.productsDirectory(
packageLocator.productsDirectory(
buildConfiguration: buildOptions.buildConfiguration,
sdk: sdk
)
Expand Down
8 changes: 4 additions & 4 deletions Sources/ScipioKit/Producer/PIF/ModuleMapGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct FrameworkModuleMapGenerator {
var configuration: BuildConfiguration
}

private var descriptionPackage: DescriptionPackage
private var packageLocator: any PackageLocator
private var fileSystem: any FileSystem

enum Error: LocalizedError {
Expand All @@ -25,8 +25,8 @@ struct FrameworkModuleMapGenerator {
}
}

init(descriptionPackage: DescriptionPackage, fileSystem: any FileSystem) {
self.descriptionPackage = descriptionPackage
init(packageLocator: some PackageLocator, fileSystem: some FileSystem) {
self.packageLocator = packageLocator
self.fileSystem = fileSystem
}

Expand Down Expand Up @@ -118,7 +118,7 @@ struct FrameworkModuleMapGenerator {
}

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

Expand Down
4 changes: 2 additions & 2 deletions Sources/ScipioKit/Producer/PIF/PIFCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ struct PIFCompiler: Compiler {
logger.info("📦 Building \(target.name) for \(sdkNames)")

let xcBuildClient: XCBuildClient = .init(
package: descriptionPackage,
buildProduct: buildProduct,
buildOptions: buildOptions,
configuration: buildOptions.buildConfiguration
configuration: buildOptions.buildConfiguration,
packageLocator: descriptionPackage
)

for sdk in sdks {
Expand Down
16 changes: 8 additions & 8 deletions Sources/ScipioKit/Producer/PIF/XCBuildClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import PackageGraph
import PackageModel

struct XCBuildClient {
private let descriptionPackage: DescriptionPackage
private let buildOptions: BuildOptions
private let buildProduct: BuildProduct
private let configuration: BuildConfiguration
private let packageLocator: any PackageLocator
private let fileSystem: any FileSystem
private let executor: any Executor

init(
package: DescriptionPackage,
buildProduct: BuildProduct,
buildOptions: BuildOptions,
configuration: BuildConfiguration,
packageLocator: some PackageLocator,
fileSystem: any FileSystem = localFileSystem,
executor: any Executor = ProcessExecutor(decoder: StandardOutputDecoder())
executor: some Executor = ProcessExecutor(decoder: StandardOutputDecoder())
) {
self.descriptionPackage = package
self.buildProduct = buildProduct
self.buildOptions = buildOptions
self.configuration = configuration
self.packageLocator = packageLocator
self.fileSystem = fileSystem
self.executor = executor
}
Expand Down Expand Up @@ -58,7 +58,7 @@ struct XCBuildClient {
try await executor.build(
pifPath: pifPath,
configuration: configuration,
derivedDataPath: descriptionPackage.derivedDataPath,
derivedDataPath: packageLocator.derivedDataPath,
buildParametersPath: buildParametersPath,
target: buildProduct.target
)
Expand All @@ -74,16 +74,16 @@ struct XCBuildClient {

private func assembleFramework(sdk: SDK) throws {
let frameworkComponentsCollector = FrameworkComponentsCollector(
descriptionPackage: descriptionPackage,
buildProduct: buildProduct,
sdk: sdk,
buildOptions: buildOptions,
packageLocator: packageLocator,
fileSystem: fileSystem
)

let components = try frameworkComponentsCollector.collectComponents(sdk: sdk)

let frameworkOutputDir = descriptionPackage.assembledFrameworksDirectory(
let frameworkOutputDir = packageLocator.assembledFrameworksDirectory(
buildConfiguration: buildOptions.buildConfiguration,
sdk: sdk
)
Expand All @@ -98,7 +98,7 @@ struct XCBuildClient {
}

private func assembledFrameworkPath(target: ScipioResolvedModule, of sdk: SDK) throws -> AbsolutePath {
let assembledFrameworkDir = descriptionPackage.assembledFrameworksDirectory(
let assembledFrameworkDir = packageLocator.assembledFrameworksDirectory(
buildConfiguration: buildOptions.buildConfiguration,
sdk: sdk
)
Expand Down

0 comments on commit 4c2cc39

Please sign in to comment.