-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
library: add purge, label; staging: add list
- Loading branch information
Showing
16 changed files
with
264 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
Sources/StarvalveControl/Commands/LibraryLabelCommand.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-FileCopyrightText: 2024 Legiayayana <[email protected]> | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import Starvalve | ||
|
||
struct LibraryLabelCommand: ParsableCommand { | ||
static let configuration = CommandConfiguration( | ||
commandName: "label", | ||
abstract: "Updates the label for Steam Libraries." | ||
) | ||
|
||
@Argument(help: "The path of the library to update.", completion: .directory) | ||
var path: URL | ||
|
||
@Argument(help: "The label to apply to the library.") | ||
var label: String? = nil | ||
|
||
@OptionGroup var globals: GlobalOptions | ||
|
||
func run() { | ||
var steam = SteamHelper(steamPath: globals.steamPath) | ||
|
||
guard let libraries = steam.libraryFolders else { | ||
preconditionFailure("Steam libraries failed to parse.") | ||
} | ||
|
||
let target = path.canonicalPath.path | ||
|
||
for library in libraries.entries { | ||
if library.path.canonicalPath.path == target { | ||
library.label = label ?? "" | ||
try? TextVDF.write(url: library.path.appending(path: "libraryfolder.vdf", directoryHint: .notDirectory), vdf: library.singleVdf()) | ||
steam.libraryFolders = libraries | ||
print("set library the label of \(library.path.path, color: .green) to \"\(label ?? "", color: .green)\"") | ||
return | ||
} | ||
} | ||
|
||
print("could not find library library \(target, color: .red)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
Sources/StarvalveControl/Commands/ListStagingLibrariesCommand.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-FileCopyrightText: 2024 Legiayayana <[email protected]> | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import Starvalve | ||
|
||
struct ListStagingLibrariesCommand: ParsableCommand { | ||
static let configuration = CommandConfiguration( | ||
commandName: "list", | ||
abstract: "List games with their corresponding staging directories." | ||
) | ||
|
||
@OptionGroup var globals: GlobalOptions | ||
|
||
func run() { | ||
let steam = SteamHelper(steamPath: globals.steamPath) | ||
|
||
guard let libraries = steam.libraryFolders else { | ||
preconditionFailure("Steam libraries failed to parse.") | ||
} | ||
|
||
for library in libraries.entries { | ||
for (appId, _) in library.apps { | ||
guard let appInfo = AppInfo(libraryPath: library.path, appId: appId) else { | ||
continue | ||
} | ||
|
||
let acf = appInfo.acf | ||
guard let stagingIndex = acf.stagingFolder else { | ||
continue | ||
} | ||
|
||
guard let stagingFolder = libraries.entries[optionally: stagingIndex] else { | ||
print("⚠️ \(acf.name, color: .green) (\(acf.appId, color: .magenta)) has an invalid staging folder!") | ||
continue | ||
} | ||
print("\(acf.name, color: .green) (\(acf.appId, color: .magenta)) has staging library set to \(stagingFolder.path.path).") | ||
} | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
Sources/StarvalveControl/Commands/PurgeLibraryCommand.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// SPDX-FileCopyrightText: 2024 Legiayayana <[email protected]> | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import Starvalve | ||
|
||
struct PurgeLibraryCommand: ParsableCommand { | ||
static let configuration = CommandConfiguration( | ||
commandName: "purge", | ||
abstract: "Removes the Steam Library and updates staging directories." | ||
) | ||
|
||
@Argument(help: "The path of the library to remove.", completion: .directory) | ||
var path: URL | ||
|
||
@Option(help: "The staging library to use for any libraries that rely on the specified library.", completion: .directory) | ||
var stagingPath: URL? | ||
|
||
@OptionGroup var globals: GlobalOptions | ||
|
||
func run() { | ||
var steam = SteamHelper(steamPath: globals.steamPath) | ||
|
||
guard let libraries = steam.libraryFolders else { | ||
preconditionFailure("Steam libraries failed to parse.") | ||
} | ||
|
||
let target = path.canonicalPath.path | ||
let stagingTarget = stagingPath?.canonicalPath.path | ||
var index: Int? | ||
var selectedIndex: Int? | ||
|
||
for libraryIndex in 0...libraries.entries.count - 1 { | ||
let library = libraries.entries[libraryIndex] | ||
let libraryPath = library.path.canonicalPath.path | ||
if libraryPath == target && index == nil { | ||
index = libraryIndex | ||
} | ||
|
||
if libraryPath == stagingTarget { | ||
selectedIndex = libraryIndex | ||
} | ||
} | ||
|
||
guard let index = index else { | ||
print("could not find library path \(target, color: .red)") | ||
return | ||
} | ||
|
||
libraries.entries.remove(at: index) | ||
|
||
// for library in libraries.entries { | ||
// for (appId, _) in library.apps { | ||
// guard let appInfo = AppInfo(libraryPath: library.path, appId: appId) else { | ||
// continue | ||
// } | ||
|
||
// var acf = appInfo.acf | ||
// guard var stagingIndex = acf.stagingFolder else { | ||
// continue | ||
// } | ||
|
||
// if stagingIndex == index { | ||
// stagingIndex = selectedIndex ?? 0 | ||
// print("updated staging folder for app \(acf.name)") | ||
// } else if stagingIndex > index { | ||
// stagingIndex -= 1 | ||
// print("adjusted staging folder for app \(acf.name)") | ||
// } else { | ||
// continue | ||
// } | ||
|
||
// acf.stagingFolder = stagingIndex | ||
|
||
// guard let _ = try? TextVDF.write(url: appInfo.acfPath, vdf: acf.vdf()) else { | ||
// print("could not write file for appmanifest \(acf.name)") | ||
// continue | ||
// } | ||
// } | ||
// } | ||
|
||
// todo: delete folder if it exists? | ||
|
||
steam.libraryFolders = libraries | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-FileCopyrightText: 2024 Legiayayana <[email protected]> | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import ArgumentParser | ||
import Starvalve | ||
|
||
struct StagingCommand: ParsableCommand { | ||
static let configuration = CommandConfiguration( | ||
commandName: "staging", | ||
abstract: "Commands relating to Staging Directories", | ||
subcommands: [ListStagingLibrariesCommand.self], | ||
defaultSubcommand: ListStagingLibrariesCommand.self | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// SPDX-FileCopyrightText: 2024 Legiayayana <[email protected]> | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import ArgumentParser | ||
import Foundation | ||
|
||
enum ByteFormatting: UInt { | ||
|
@@ -20,6 +21,12 @@ enum ASCIIColor: String { | |
case `default` = "\u{001B}[0;0m" | ||
} | ||
|
||
extension Collection { | ||
subscript(optionally index: Index) -> Element? { | ||
indices.contains(index) ? self[index] : nil | ||
} | ||
} | ||
|
||
extension URL { | ||
@inlinable var canonicalPath: URL { | ||
guard let path = try? resourceValues(forKeys: [.canonicalPathKey]).canonicalPath else { | ||
|
@@ -50,6 +57,17 @@ extension URL { | |
} | ||
} | ||
|
||
extension URL: @retroactive ExpressibleByArgument { | ||
/// initializes a string via a string argument. | ||
public init(argument: String) { | ||
if let url = URL(string: argument) { | ||
self = url | ||
} else { | ||
self = URL(filePath: argument) | ||
} | ||
} | ||
} | ||
|
||
extension FileManager { | ||
func directorySize(atPath path: URL) throws -> UInt { | ||
var size: UInt = 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.