Skip to content

Commit cfe04db

Browse files
John ButeJohn Bute
John Bute
authored and
John Bute
committed
formatting
1 parent df39470 commit cfe04db

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

Sources/Basics/Concurrency/PID.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
1+
//===----------------------------------------------------------------------===//
12
//
2-
// PID.swift
3-
// SwiftPM
3+
// This source file is part of the Swift open source project
44
//
5-
// Created by John Bute on 2025-04-24.
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
67
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
712

813
import Foundation
914

1015
public protocol PIDFileHandler {
11-
var scratchDirectory: AbsolutePath {get set}
16+
var scratchDirectory: AbsolutePath { get set }
1217

1318
init(scratchDirectory: AbsolutePath)
14-
19+
1520
func readPID() -> Int32?
1621
func deletePIDFile() throws
1722
func writePID(pid: pid_t) throws
1823
func getCurrentPID() -> Int32
1924
}
2025

21-
22-
2326
public struct PIDFile: PIDFileHandler {
24-
2527
public var scratchDirectory: AbsolutePath
26-
28+
2729
public init(scratchDirectory: AbsolutePath) {
2830
self.scratchDirectory = scratchDirectory
2931
}
30-
32+
3133
/// Return the path of the PackageManager.lock.pid file where the PID is located
3234
private var lockFilePath: AbsolutePath {
33-
return self.scratchDirectory.appending(component: "PackageManager.lock.pid")
35+
self.scratchDirectory.appending(component: "PackageManager.lock.pid")
3436
}
3537

3638
/// Read the pid file
3739
public func readPID() -> Int32? {
3840
// Check if the file exists
39-
let filePath = lockFilePath.pathString
40-
guard FileManager.default.fileExists(atPath: filePath) else {
41+
let filePath = self.lockFilePath.pathString
42+
guard FileManager.default.fileExists(atPath: filePath) else {
4143
print("File does not exist at path: \(filePath)")
4244
return nil
4345
}
4446

4547
do {
4648
// Read the contents of the file
47-
let pidString = try String(contentsOf: lockFilePath.asURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
49+
let pidString = try String(contentsOf: lockFilePath.asURL, encoding: .utf8)
50+
.trimmingCharacters(in: .whitespacesAndNewlines)
4851

4952
// Check if the PID string can be converted to an Int32
5053
if let pid = Int32(pidString) {
@@ -60,17 +63,16 @@ public struct PIDFile: PIDFileHandler {
6063

6164
/// Get the current PID of the process
6265
public func getCurrentPID() -> Int32 {
63-
return getpid()
66+
getpid()
6467
}
6568

6669
/// Write .pid file containing PID of process currently using .build directory
6770
public func writePID(pid: pid_t) throws {
68-
try "\(pid)".write(to: lockFilePath.asURL, atomically: true, encoding: .utf8)
71+
try "\(pid)".write(to: self.lockFilePath.asURL, atomically: true, encoding: .utf8)
6972
}
70-
73+
7174
/// Delete PID file at URL
7275
public func deletePIDFile() throws {
73-
try FileManager.default.removeItem(at: lockFilePath.asURL)
76+
try FileManager.default.removeItem(at: self.lockFilePath.asURL)
7477
}
75-
7678
}

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public final class SwiftCommandState {
289289
private let hostTriple: Basics.Triple?
290290

291291
private let pidManipulator: PIDFileHandler
292+
292293
package var preferredBuildConfiguration = BuildConfiguration.debug
293294

294295
/// Create an instance of this tool.
@@ -410,7 +411,7 @@ public final class SwiftCommandState {
410411
self.sharedSwiftSDKsDirectory = try fileSystem.getSharedSwiftSDKsDirectory(
411412
explicitDirectory: options.locations.swiftSDKsDirectory ?? options.locations.deprecatedSwiftSDKsDirectory
412413
)
413-
414+
414415
self.pidManipulator = pidManipulator ?? PIDFile(scratchDirectory: self.scratchDirectory)
415416

416417
// set global process logging handler
@@ -1098,7 +1099,7 @@ public final class SwiftCommandState {
10981099
}
10991100

11001101
self.workspaceLock = workspaceLock
1101-
1102+
11021103
if lockAcquired || self.options.locations.ignoreLock {
11031104
do {
11041105
try self.pidManipulator.writePID(pid: self.pidManipulator.getCurrentPID())
@@ -1117,7 +1118,7 @@ public final class SwiftCommandState {
11171118
self.workspaceLockState = .unlocked
11181119

11191120
self.workspaceLock?.unlock()
1120-
1121+
11211122
do {
11221123
try self.pidManipulator.deletePIDFile()
11231124
} catch {

Tests/CommandsTests/SwiftCommandStateTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,7 @@ final class SwiftCommandStateTests: CommandsTestCase {
539539
let scratchPath = tmpDir.appending(component: "scratch")
540540
try localFileSystem.createDirectory(scratchPath)
541541

542-
var pidHandler: PIDFile = PIDFile(scratchDirectory: scratchPath)
543-
544-
542+
var pidHandler = PIDFile(scratchDirectory: scratchPath)
545543

546544
// Ensure no PID file exists initially
547545
XCTAssertNil(pidHandler.readPID(), "No PID should exist initially")
@@ -550,7 +548,6 @@ final class SwiftCommandStateTests: CommandsTestCase {
550548
let currentPID = pidHandler.getCurrentPID()
551549
try pidHandler.writePID(pid: currentPID)
552550

553-
554551
// Read PID back
555552
let readPID = pidHandler.readPID()
556553
XCTAssertEqual(readPID, currentPID, "PID read should match written PID")

0 commit comments

Comments
 (0)