SPMVersions.versionOf("THE_PACKAGE_ID")
cat ./YourAdHere.xcworkspace/xcshareddata/swiftpm/Package.resolved | \
jq '.pins[] | select(.identity == "THE_PACKAGE_ID").state.version'
/* Begin PBXShellScriptBuildPhase section */ | |
07E370312A14208C0035FB2F /* Generate spm_versions.json */ = { | |
isa = PBXShellScriptBuildPhase; | |
buildActionMask = 2147483647; | |
files = ( | |
); | |
inputFileListPaths = ( | |
); | |
inputPaths = ( | |
"$(PROJECT_FILE_PATH)/project.xcworkspace/xcshareddata/swiftpm/Package.resolved", | |
); | |
name = "Generate spm_versions.json"; | |
outputFileListPaths = ( | |
); | |
outputPaths = ( | |
$TARGET_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/spm_versions.json, | |
); | |
runOnlyForDeploymentPostprocessing = 0; | |
shellPath = /bin/sh; | |
shellScript = "OUTPUT=\"$TARGET_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/spm_versions.json\"\ncat \"$PROJECT_FILE_PATH\"/project.xcworkspace/xcshareddata/swiftpm/Package.resolved | jq '[ .pins[] | { id: .identity, vers: .state.version }]' > \"$OUTPUT\"\n\necho built $OUTPUT\n"; | |
showEnvVarsInLog = 0; | |
}; | |
/* End PBXShellScriptBuildPhase section */ |
class SPMVersions { | |
private struct SPMPackage: Decodable { | |
let id: String | |
let vers: String | |
} | |
private static let packages: [SPMPackage] = { | |
let logger = print // or whatever | |
guard let filePath = Bundle.main.path(forResource: "spm_versions", ofType: "json") else { | |
logger("spm_versions.json not found, should have been created in a build phase") | |
return [] | |
} | |
var result = [SPMPackage]() | |
do { | |
if let jsonData = try String(contentsOfFile: filePath).data(using: .utf8) { | |
result = try JSONDecoder().decode([SPMPackage].self, from: jsonData) | |
} | |
} catch { | |
logger("failed to decode spm_versions.json, error: \(error)") | |
} | |
return result | |
}() | |
static func versionOf(_ packageName: String) -> String? { | |
packages.first { $0.id == packageName }?.vers | |
} | |
} |