2022-07-30 09:38:37 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Generates a Nix expression to fetch swiftpm dependencies, and a
|
|
|
|
# configurePhase snippet to prepare a working directory for swift-build.
|
|
|
|
|
|
|
|
set -eu -o pipefail
|
|
|
|
shopt -s lastpipe
|
|
|
|
|
|
|
|
stateFile=".build/workspace-state.json"
|
2022-11-15 10:46:22 +00:00
|
|
|
if [[ ! -f "$stateFile" ]]; then
|
|
|
|
echo >&2 "Missing $stateFile. Run 'swift package resolve' first."
|
2022-07-30 09:38:37 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-11-15 10:46:22 +00:00
|
|
|
|
2023-04-20 12:13:22 +00:00
|
|
|
stateVersion="$(jq .version $stateFile)"
|
|
|
|
if [[ $stateVersion -lt 5 || $stateVersion -gt 6 ]]; then
|
2022-07-30 09:38:37 +00:00
|
|
|
echo >&2 "Unsupported $stateFile version"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Iterate dependencies and prefetch.
|
2022-11-15 10:46:22 +00:00
|
|
|
hashes=""
|
2022-07-30 09:38:37 +00:00
|
|
|
jq -r '.object.dependencies[] | "\(.subpath) \(.packageRef.location) \(.state.checkoutState.revision)"' $stateFile \
|
|
|
|
| while read -r name url rev; do
|
|
|
|
echo >&2 "-- Fetching $name"
|
2024-11-23 19:17:30 +00:00
|
|
|
hash="$(nurl "$url" "$rev" --json --submodules=true --fetcher=fetchgit | jq -r .args.hash)"
|
|
|
|
hashes+="
|
|
|
|
\"$name\" = \"$hash\";"
|
2022-07-30 09:38:37 +00:00
|
|
|
echo >&2
|
|
|
|
done
|
2022-11-15 10:46:22 +00:00
|
|
|
hashes+=$'\n'" "
|
2022-07-30 09:38:37 +00:00
|
|
|
|
|
|
|
# Generate output.
|
|
|
|
mkdir -p nix
|
|
|
|
# Copy the workspace state, but clear 'artifacts'.
|
|
|
|
jq '.object.artifacts = []' < $stateFile > nix/workspace-state.json
|
|
|
|
# Build an expression for fetching sources, and preparing the working directory.
|
|
|
|
cat > nix/default.nix << EOF
|
|
|
|
# This file was generated by swiftpm2nix.
|
2022-11-15 10:46:22 +00:00
|
|
|
{
|
|
|
|
workspaceStateFile = ./workspace-state.json;
|
|
|
|
hashes = {$hashes};
|
2022-07-30 09:38:37 +00:00
|
|
|
}
|
|
|
|
EOF
|
|
|
|
echo >&2 "-- Generated ./nix"
|