Skip to content
Open
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
29 changes: 2 additions & 27 deletions Sources/Commands/PackageCommands/AddProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Basics
import CoreCommands
import Foundation
import PackageGraph
import SwiftParser
@_spi(PackageRefactor) import SwiftRefactor
import SwiftSyntax
import TSCBasic
Expand Down Expand Up @@ -55,30 +54,7 @@ extension SwiftPackageCommand {
var targets: [String] = []

func run(_ swiftCommandState: SwiftCommandState) throws {
let workspace = try swiftCommandState.getActiveWorkspace()

guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
throw StringError("unknown package")
}

// Load the manifest file
let fileSystem = workspace.fileSystem
let manifestPath = packagePath.appending("Package.swift")
let manifestContents: ByteString
do {
manifestContents = try fileSystem.readFileContents(manifestPath)
} catch {
throw StringError("cannot find package manifest in \(manifestPath)")
}

// Parse the manifest.
let manifestSyntax = manifestContents.withData { data in
data.withUnsafeBytes { buffer in
buffer.withMemoryRebound(to: UInt8.self) { buffer in
Parser.parse(source: buffer)
}
}
}
let (manifestSyntax, manifestPath) = try swiftCommandState.readPackageManifestAsSyntaxTree()

// Map the product type.
let type: ProductDescription.ProductType = switch self.type {
Expand All @@ -101,12 +77,11 @@ extension SwiftPackageCommand {
)

try editResult.applyEdits(
to: fileSystem,
to: swiftCommandState.getActiveWorkspace().fileSystem,
manifest: manifestSyntax,
manifestPath: manifestPath,
verbose: !globalOptions.logging.quiet
)
}
}
}

34 changes: 5 additions & 29 deletions Sources/Commands/PackageCommands/AddTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import CoreCommands
import Foundation
import PackageGraph
import PackageModel
import SwiftParser
@_spi(PackageRefactor) import SwiftRefactor
import SwiftSyntax
import SwiftSyntaxBuilder
Expand Down Expand Up @@ -76,35 +75,12 @@ extension SwiftPackageCommand {
var testingLibrary: AddPackageTarget.TestHarness = .default

func run(_ swiftCommandState: SwiftCommandState) async throws {
let workspace = try swiftCommandState.getActiveWorkspace()

guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
throw StringError("unknown package")
}

// Load the manifest file
let fileSystem = workspace.fileSystem
let manifestPath = packagePath.appending("Package.swift")
let manifestContents: ByteString
do {
manifestContents = try fileSystem.readFileContents(manifestPath)
} catch {
throw StringError("cannot find package manifest in \(manifestPath)")
}

// Parse the manifest.
let manifestSyntax = manifestContents.withData { data in
data.withUnsafeBytes { buffer in
buffer.withMemoryRebound(to: UInt8.self) { buffer in
Parser.parse(source: buffer)
}
}
}
let (manifestSyntax, manifestPath) = try swiftCommandState.readPackageManifestAsSyntaxTree()

// Move sources into their own folder if they're directly in `./Sources`.
try await self.moveSingleTargetSources(
workspace: workspace,
packagePath: packagePath,
workspace: swiftCommandState.getActiveWorkspace(),
packagePath: manifestPath,
verbose: !self.globalOptions.logging.quiet,
observabilityScope: swiftCommandState.observabilityScope
)
Expand Down Expand Up @@ -140,7 +116,7 @@ extension SwiftPackageCommand {
)

try editResult.applyEdits(
to: fileSystem,
to: swiftCommandState.getActiveWorkspace().fileSystem,
manifest: manifestSyntax,
manifestPath: manifestPath,
verbose: !self.globalOptions.logging.quiet
Expand All @@ -150,7 +126,7 @@ extension SwiftPackageCommand {
try self.addAuxiliaryFiles(
target: target,
testHarness: self.testingLibrary,
fileSystem: fileSystem,
fileSystem: swiftCommandState.getActiveWorkspace().fileSystem,
rootPath: manifestPath.parentDirectory
)
}
Expand Down
28 changes: 2 additions & 26 deletions Sources/Commands/PackageCommands/AddTargetDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import CoreCommands
import Foundation
import PackageGraph
import PackageModel
import SwiftParser
@_spi(PackageRefactor) import SwiftRefactor
import SwiftSyntax
import TSCBasic
Expand All @@ -43,30 +42,7 @@ extension SwiftPackageCommand {
var package: String?

func run(_ swiftCommandState: SwiftCommandState) throws {
let workspace = try swiftCommandState.getActiveWorkspace()

guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
throw StringError("unknown package")
}

// Load the manifest file
let fileSystem = workspace.fileSystem
let manifestPath = packagePath.appending("Package.swift")
let manifestContents: ByteString
do {
manifestContents = try fileSystem.readFileContents(manifestPath)
} catch {
throw StringError("cannot find package manifest in \(manifestPath)")
}

// Parse the manifest.
let manifestSyntax = manifestContents.withData { data in
data.withUnsafeBytes { buffer in
buffer.withMemoryRebound(to: UInt8.self) { buffer in
Parser.parse(source: buffer)
}
}
}
let (manifestSyntax, manifestPath) = try swiftCommandState.readPackageManifestAsSyntaxTree()

let dependency: PackageTarget.Dependency
if let package {
Expand All @@ -84,7 +60,7 @@ extension SwiftPackageCommand {
)

try editResult.applyEdits(
to: fileSystem,
to: swiftCommandState.getActiveWorkspace().fileSystem,
manifest: manifestSyntax,
manifestPath: manifestPath,
verbose: !globalOptions.logging.quiet
Expand Down
29 changes: 2 additions & 27 deletions Sources/Commands/PackageCommands/AddTargetPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import CoreCommands
import Foundation
import PackageGraph
import PackageModel
import SwiftParser
@_spi(PackageRefactor) import SwiftRefactor
import SwiftSyntax
import TSCBasic
Expand All @@ -42,30 +41,7 @@ extension SwiftPackageCommand {
var package: String?

func run(_ swiftCommandState: SwiftCommandState) throws {
let workspace = try swiftCommandState.getActiveWorkspace()

guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
throw StringError("unknown package")
}

// Load the manifest file
let fileSystem = workspace.fileSystem
let manifestPath = packagePath.appending("Package.swift")
let manifestContents: ByteString
do {
manifestContents = try fileSystem.readFileContents(manifestPath)
} catch {
throw StringError("cannot find package manifest in \(manifestPath)")
}

// Parse the manifest.
let manifestSyntax = manifestContents.withData { data in
data.withUnsafeBytes { buffer in
buffer.withMemoryRebound(to: UInt8.self) { buffer in
Parser.parse(source: buffer)
}
}
}
let (manifestSyntax, manifestPath) = try swiftCommandState.readPackageManifestAsSyntaxTree()

let pluginUsage: PackageTarget.PluginUsage = .plugin(name: pluginName, package: package)

Expand All @@ -78,12 +54,11 @@ extension SwiftPackageCommand {
)

try editResult.applyEdits(
to: fileSystem,
to: swiftCommandState.getActiveWorkspace().fileSystem,
manifest: manifestSyntax,
manifestPath: manifestPath,
verbose: !globalOptions.logging.quiet
)
}
}
}

1 change: 1 addition & 0 deletions Sources/Commands/PackageCommands/Migrate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ extension SwiftPackageCommand {
switch error {
case .cannotFindPackage,
.cannotAddSettingsToPluginTarget,
.cannotFindDependencies,
.existingDependency,
.malformedManifest:
break
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2026 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 Basics
import CoreCommands
import Foundation
import PackageGraph
import PackageModel
import SwiftParser
import SwiftSyntax
import TSCBasic
import Workspace

extension SwiftCommandState {
/// Read the package manifest of the current package on disk and parse it as a syntax tree.
func readPackageManifestAsSyntaxTree() throws -> (syntax: SourceFileSyntax, path: Basics.AbsolutePath) {
guard let packagePath = try getWorkspaceRoot().packages.first else {
throw StringError("unknown package")
}

let manifestPath = packagePath.appending(component: Manifest.filename)
let manifestContents: ByteString
do {
manifestContents = try getActiveWorkspace().fileSystem.readFileContents(manifestPath)
} catch {
throw StringError("cannot find package manifest in \(manifestPath)")
}

let sourceFileSyntax = manifestContents.withData { data in
data.withUnsafeBytes { buffer in
buffer.withMemoryRebound(to: UInt8.self) { buffer in
Parser.parse(source: buffer)
}
}
}
return (sourceFileSyntax, manifestPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public struct SwiftPackageCommand: AsyncParsableCommand {
PurgeCache.self,
Reset.self,
Update.self,
UpgradeDependencies.self,
Describe.self,
Init.self,
Format.self,
Expand Down
Loading
Loading