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

View File

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