fix(rust): fix workspace dependency patching

This commit is contained in:
Yusuf Bera Ertan 2023-02-25 04:53:05 +03:00
parent db72710500
commit 28b973a8d4
No known key found for this signature in database
GPG Key ID: 1D8F8FAF2294D6EA
2 changed files with 23 additions and 8 deletions

View File

@ -1,7 +1,7 @@
def normalizeWorkspaceDep:
if ($workspaceDependencies."\(.key)" | type) == "object"
then [., $workspaceDependencies."\(.key)"] | add
else [., {"version":$workspaceDependencies."\(.key)"}] | add
then [.value, $workspaceDependencies."\(.key)"] | add
else [.value, {"version":$workspaceDependencies."\(.key)"}] | add
end
# remove workspace option from the dependency
| del(.workspace)
@ -16,7 +16,7 @@ def mapWorkspaceDepsFor(name):
| to_entries
| map(
if (.value | type) == "object" and .value.workspace == true
then .value = (.value | normalizeWorkspaceDep)
then .value = (. | normalizeWorkspaceDep)
else .
end
)

View File

@ -51,8 +51,22 @@ in rec {
findCrateSource = source: let
cargo = "${pkgs.cargo}/bin/cargo";
jq = "${pkgs.jq}/bin/jq";
yj = "${pkgs.yj}/bin/yj";
sponge = "${pkgs.moreutils}/bin/sponge";
writeConvertScript = from: to:
pkgs.writers.writePython3
"${from}-to-${to}.py"
{libraries = [pkgs.python3Packages.toml];}
''
import toml
import json
import sys
t = ${from}.loads(sys.stdin.read())
sys.stdout.write(${to}.dumps(t))
'';
tomlToJson = writeConvertScript "toml" "json";
jsonToToml = writeConvertScript "json" "toml";
pkg = source.dep;
in ''
# If the target package is in a workspace, or if it's the top-level
@ -74,16 +88,16 @@ in rec {
fi
else
# we need to patch dependencies with `workspace = true` (workspace inheritance)
workspaceDependencies="$(cat "$tree/Cargo.toml" | ${yj} -tj | ${jq} -cr '.workspace.dependencies')"
workspaceDependencies="$(cat "$tree/Cargo.toml" | ${tomlToJson} | ${jq} -cr '.workspace.dependencies')"
if [[ "$workspaceDependencies" != "null" ]]; then
tree="$(pwd)/${pkg.name}-${pkg.version}"
cp -prd --no-preserve=mode,ownership "$(dirname $crateCargoTOML)" "$tree"
crateCargoTOML="$tree/Cargo.toml"
cat "$crateCargoTOML" \
| ${yj} -tj \
| ${tomlToJson} \
| ${jq} -cr --argjson workspaceDependencies "$workspaceDependencies" \
--from-file ${./patch-workspace-deps.jq} \
| ${yj} -jt \
| ${jsonToToml} \
| ${sponge} "$crateCargoTOML"
fi
fi
@ -97,7 +111,8 @@ in rec {
l.optionalString (!isPath) ''
tree="${source.path}"
${l.optionalString isGit (findCrateSource source)}
cp -prvd "$tree" $out/${source.name}
echo Vendoring crate ${source.name}
cp -prd "$tree" $out/${source.name}
chmod u+w $out/${source.name}
${l.optionalString isGit "printf '{\"files\":{},\"package\":null}' > \"$out/${source.name}/.cargo-checksum.json\""}
'';