Skip to content

Instantly share code, notes, and snippets.

@mr-fixit
Last active May 25, 2023 15:19
Show Gist options
  • Save mr-fixit/be5c9563e258802664f401822dd7f488 to your computer and use it in GitHub Desktop.
Save mr-fixit/be5c9563e258802664f401822dd7f488 to your computer and use it in GitHub Desktop.
To get the versions of your SPM packages

Example

At runtime

   SPMVersions.versionOf("THE_PACKAGE_ID")

In Terminal

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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment