Skip to content

Commit

Permalink
Added ability to read package manifest json from provided path
Browse files Browse the repository at this point in the history
  • Loading branch information
rock88 committed May 12, 2024
1 parent 39d98c5 commit 5869eca
Show file tree
Hide file tree
Showing 4 changed files with 21 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(parsing: .upToNextOption, help: "JSON package manifest path (obtained using `swift package describe --type json` or manually)")
var jsonPackageManifestPath: [FilePath] = defaultConfiguration.$jsonPackageManifestPath.defaultValue

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: [FilePath] = []) 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 path = jsonPackageManifestPath.first {
jsonData = try Data(contentsOf: path.url)
} 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
3 changes: 3 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: [])
public var jsonPackageManifestPath: [FilePath]

// Non user facing.
public var guidedSetup: Bool = false
public var removalOutputBasePath: FilePath?
Expand Down

0 comments on commit 5869eca

Please sign in to comment.