Skip to content

[GSoC] SwiftPM PR #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions Fixtures/Scripts/EchoArguments/EchoArguments.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let arguments = CommandLine.arguments.dropFirst()
print(arguments.map{"\"\($0)\""}.joined(separator: " "))
6 changes: 6 additions & 0 deletions Fixtures/Scripts/EchoArguments/PackageSyntax.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies" : [

],
"sourceFile" : "SCRIPT_DIR\/EchoArguments.swift"
}
4 changes: 4 additions & 0 deletions Fixtures/Scripts/EchoCWD/EchoCWD.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@package(name: "CwdDump", path: "cwd-dump")
import CwdDump

CwdDump.main()
16 changes: 16 additions & 0 deletions Fixtures/Scripts/EchoCWD/PackageSyntax.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"dependencies" : [
{
"package" : {
"raw" : "name:\"CwdDump\",path:\"SCRIPT_DIR\/cwd-dump\"",
"path" : "SCRIPT_DIR\/cwd-dump",
"name" : "CwdDump"
},
"modules" : [
"CwdDump"
]
}
],
"sourceFile" : "SCRIPT_DIR\/EchoCWD.swift"
}

19 changes: 19 additions & 0 deletions Fixtures/Scripts/EchoCWD/cwd-dump/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "CwdDump",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "CwdDump",
targets: ["CwdDump"])
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(name: "CwdDump")
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

public struct CwdDump {
public static func main() {
print(FileManager().currentDirectoryPath)
}
}
10 changes: 9 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ let package = Package(
/** Package model conventions and loading support */
name: "PackageLoading",
dependencies: ["SwiftToolsSupport-auto", "Basics", "PackageModel", "SourceControl"]),
.target(
name: "ScriptingCore",
/** Package models for scripting support */
dependencies: ["SwiftToolsSupport-auto", "Basics"]),

// MARK: Package Dependency Resolution

Expand Down Expand Up @@ -213,7 +217,7 @@ let package = Package(
.target(
/** High-level commands */
name: "Commands",
dependencies: ["SwiftToolsSupport-auto", "Basics", "Build", "PackageGraph", "SourceControl", "Xcodeproj", "Workspace", "XCBuildSupport", "ArgumentParser", "PackageCollections"]),
dependencies: ["SwiftToolsSupport-auto", "Basics", "Build", "PackageGraph", "SourceControl", "Xcodeproj", "Workspace", "XCBuildSupport", "ArgumentParser", "PackageCollections", "ScriptingCore"]),
.target(
/** The main executable provided by SwiftPM */
name: "swift-package",
Expand All @@ -230,6 +234,10 @@ let package = Package(
/** Runs an executable product */
name: "swift-run",
dependencies: ["Commands"]),
.target(
/** Manages and runs a script */
name: "swift-script",
dependencies: ["Commands"]),
.target(
/** Interacts with package collections */
name: "swift-package-collection",
Expand Down
26 changes: 25 additions & 1 deletion Sources/Basics/FileSystem+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2020 Apple Inc. and the Swift project authors
Copyright (c) 2020 - 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
Expand Down Expand Up @@ -110,3 +110,27 @@ extension FileSystem {
return idiomaticConfigDirectory
}
}

// MARK: - script cache

extension FileSystem {
/// SwiftPM cache directory under user's caches directory (if exists)
public var swiftScriptCacheDirectory: AbsolutePath {
return self.dotSwiftScriptCachesDirectory
}

fileprivate var dotSwiftScriptCachesDirectory: AbsolutePath {
return self.dotSwiftPM.appending(component: "scripts")
}
}

extension FileSystem {
public func getOrCreateSwiftScriptCacheDirectory() throws -> AbsolutePath {

Choose a reason for hiding this comment

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

Could this instead just call getOrCreateSwiftPMCacheDirectory() and then append the scripts subdirectory to it? Otherwise this function seems to duplicate some of the existing code.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ummm... In fact the scripts dir is supposed to be beside cache, which I'm afraid may "escape" if we simply append ../scripts to the cache dir (also, we need to create it if there's none, which needs a function to wrap up).
I thought it may be better to hardcode the path with ~/.swiftpm/scripts to make it more accessible.

Choose a reason for hiding this comment

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

That makes sense — I'm not 100% familiar with how the layout is supposed to look here, it just seemed as if there was some code overlap here.

let idiomaticCacheDirectory = self.swiftScriptCacheDirectory
// Create if necessary
if !self.exists(idiomaticCacheDirectory) {
try self.createDirectory(idiomaticCacheDirectory, recursive: true)
}
return idiomaticCacheDirectory
}
}
2 changes: 2 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ add_subdirectory(PackagePlugin)
add_subdirectory(SPMBuildCore)
add_subdirectory(SPMLLBuild)
add_subdirectory(SourceControl)
add_subdirectory(ScriptingCore)
add_subdirectory(swift-build)
add_subdirectory(swift-package)
add_subdirectory(swift-run)
add_subdirectory(swift-script)
add_subdirectory(swift-test)
add_subdirectory(Workspace)
add_subdirectory(XCBuildSupport)
Expand Down
4 changes: 3 additions & 1 deletion Sources/Commands/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
# Copyright (c) 2014 - 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
Expand All @@ -18,6 +18,7 @@ add_library(Commands
SwiftPackageCollectionsTool.swift
SwiftPackageTool.swift
SwiftRunTool.swift
SwiftScriptTool.swift
SwiftTestTool.swift
SwiftTool.swift
SymbolGraphExtract.swift
Expand All @@ -28,6 +29,7 @@ target_link_libraries(Commands PUBLIC
Build
PackageCollections
PackageGraph
ScriptingCore
SourceControl
TSCBasic
TSCUtility
Expand Down
9 changes: 7 additions & 2 deletions Sources/Commands/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ extension Error: CustomStringConvertible {

// The name has underscore because of SR-4015.
func handle(error: Swift.Error) {

switch error {
case Diagnostics.fatalError:
break
Expand All @@ -55,7 +54,7 @@ func print(error: Any) {

func print(diagnostic: Diagnostic, stdoutStream: OutputByteStream) {

let writer = InteractiveWriter.stderr
let writer = InteractiveWriter(stream: stdoutStream)

if !(diagnostic.location is UnknownLocation) {
writer.write(diagnostic.location.description)
Expand All @@ -79,6 +78,12 @@ func print(diagnostic: Diagnostic, stdoutStream: OutputByteStream) {
writer.write("\n")
}

extension Diagnostic: ByteStreamable {
public func write(to stream: WritableByteStream) {
print(diagnostic: self, stdoutStream: stream)
}
}

/// This class is used to write on the underlying stream.
///
/// If underlying stream is a not tty, the string will be written in without any
Expand Down
6 changes: 3 additions & 3 deletions Sources/Commands/SwiftRunTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public struct SwiftRunTool: SwiftCommand {

/// Executes the executable at the specified path.
private func run(
_ excutablePath: AbsolutePath,
_ executablePath: AbsolutePath,
originalWorkingDirectory: AbsolutePath,
arguments: [String]) throws
{
Expand All @@ -246,8 +246,8 @@ public struct SwiftRunTool: SwiftCommand {
try ProcessEnv.chdir(originalWorkingDirectory)
}

let pathRelativeToWorkingDirectory = excutablePath.relative(to: originalWorkingDirectory)
try exec(path: excutablePath.pathString, args: [pathRelativeToWorkingDirectory.pathString] + arguments)
let pathRelativeToWorkingDirectory = executablePath.relative(to: originalWorkingDirectory)
try exec(path: executablePath.pathString, args: [pathRelativeToWorkingDirectory.pathString] + arguments)
}

/// Determines if a path points to a valid swift file.
Expand Down
Loading