Skip to content

Commit

Permalink
Added ability to read package manifest json from provided path (#732)
Browse files Browse the repository at this point in the history
* Added ability to read package manifest json from provided path

* Replace [FilePath] with single String?

* Add new jsonPackageManifestPath value to asYaml/load/reset
  • Loading branch information
rock88 authored May 19, 2024
1 parent 7a275da commit ef8d189
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Sources/Frontend/Commands/ScanCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ struct ScanCommand: FrontendCommand {
@Flag(help: "Only output results")
var quiet: Bool = defaultConfiguration.$quiet.defaultValue

@Option(help: "JSON package manifest path (obtained using `swift package describe --type json` or manually)")
var jsonPackageManifestPath: String?

private static let defaultConfiguration = Configuration()

func run() throws {
Expand Down Expand Up @@ -162,6 +165,7 @@ struct ScanCommand: FrontendCommand {
configuration.apply(\.$buildArguments, buildArguments)
configuration.apply(\.$relativeResults, relativeResults)
configuration.apply(\.$retainCodableProperties, retainCodableProperties)
configuration.apply(\.$jsonPackageManifestPath, jsonPackageManifestPath)

try scanBehavior.main { project in
try Scan().perform(project: project)
Expand Down
17 changes: 13 additions & 4 deletions Sources/PeripheryKit/SPM/SPM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ public struct SPM {
}

public struct Package: Decodable {
public static func load() throws -> Self {
public static func load(jsonPackageManifestPath: String? = nil) throws -> Self {
Logger().contextualized(with: "spm:package").debug("Loading \(FilePath.current)")
let jsonString = try Shell.shared.exec(["swift", "package", "describe", "--type", "json"], stderr: false)

guard let jsonData = jsonString.data(using: .utf8) else {
throw PeripheryError.packageError(message: "Failed to read swift package description.")
let jsonData: Data

if let jsonPackageManifestPath {
jsonData = try Data(contentsOf: URL(fileURLWithPath: jsonPackageManifestPath))
} else {
let jsonString = try Shell.shared.exec(["swift", "package", "describe", "--type", "json"], stderr: false)

guard let data = jsonString.data(using: .utf8) else {
throw PeripheryError.packageError(message: "Failed to read swift package description.")
}

jsonData = data
}

let decoder = JSONDecoder()
Expand Down
2 changes: 1 addition & 1 deletion Sources/PeripheryKit/SPM/SPMProjectDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Shared
public final class SPMProjectDriver {
public static func build() throws -> Self {
let configuration = Configuration.shared
let package = try SPM.Package.load()
let package = try SPM.Package.load(jsonPackageManifestPath: configuration.jsonPackageManifestPath)
let targets: [SPM.Target]

if !configuration.schemes.isEmpty {
Expand Down
10 changes: 10 additions & 0 deletions Sources/Shared/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public final class Configuration {
@Setting(key: "relative_results", defaultValue: false)
public var relativeResults: Bool

@Setting(key: "json_package_manifest_path", defaultValue: nil)
public var jsonPackageManifestPath: String?

// Non user facing.
public var guidedSetup: Bool = false
public var removalOutputBasePath: FilePath?
Expand Down Expand Up @@ -252,6 +255,10 @@ public final class Configuration {
config[$retainCodableProperties.key] = retainCodableProperties
}

if $jsonPackageManifestPath.hasNonDefaultValue {
config[$jsonPackageManifestPath.key] = jsonPackageManifestPath
}

return try Yams.dump(object: config)
}

Expand Down Expand Up @@ -337,6 +344,8 @@ public final class Configuration {
$relativeResults.assign(value)
case $retainCodableProperties.key:
$retainCodableProperties.assign(value)
case $jsonPackageManifestPath.key:
$jsonPackageManifestPath.assign(value)
default:
logger.warn("\(path.string): invalid key '\(key)'")
}
Expand Down Expand Up @@ -377,6 +386,7 @@ public final class Configuration {
$buildArguments.reset()
$relativeResults.reset()
$retainCodableProperties.reset()
$jsonPackageManifestPath.reset()
}

// MARK: - Helpers
Expand Down

0 comments on commit ef8d189

Please sign in to comment.