1#!/usr/bin/env bash
2
3prefetchExtensionZip() {
4 declare publisher="${1?}"
5 declare name="${2?}"
6 declare version="${3?}"
7
8 1>&2 echo
9 1>&2 echo "------------- Downloading extension ---------------"
10
11 declare extZipStoreName="${publisher}-${name}.zip"
12 declare extUrl="https://${publisher}.gallery.vsassets.io/_apis/public/gallery/publisher/${publisher}/extension/${name}/${version}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage";
13 1>&2 echo "extUrl='$extUrl'"
14 declare nixPrefetchArgs=( --name "$extZipStoreName" --print-path "$extUrl" )
15
16 1>&2 printf "$ nix-prefetch-url"
17 1>&2 printf " %q" "${nixPrefetchArgs[@]}"
18 1>&2 printf " 2> /dev/null\n"
19 declare zipShaWStorePath
20 zipShaWStorePath=$(nix-prefetch-url "${nixPrefetchArgs[@]}" 2> /dev/null)
21
22 1>&2 echo "zipShaWStorePath='$zipShaWStorePath'"
23 echo "$zipShaWStorePath"
24}
25
26
27prefetchExtensionUnpacked() {
28 declare publisher="${1?}"
29 declare name="${2?}"
30 declare version="${3?}"
31
32 declare zipShaWStorePath
33 zipShaWStorePath="$(prefetchExtensionZip "$publisher" "$name" "$version")"
34
35 declare zipStorePath
36 zipStorePath="$(echo "$zipShaWStorePath" | tail -n1)"
37 1>&2 echo "zipStorePath='$zipStorePath'"
38
39 function rm_tmpdir() {
40 1>&2 printf "rm -rf %q\n" "$tmpDir"
41 rm -rf "$tmpDir"
42 }
43 function make_trapped_tmpdir() {
44 tmpDir=$(mktemp -d)
45 trap rm_tmpdir EXIT
46 }
47
48 1>&2 echo
49 1>&2 echo "------------- Unpacking extension ---------------"
50
51 make_trapped_tmpdir
52 declare unzipArgs=( -q -d "$tmpDir" "$zipStorePath" )
53 1>&2 printf "$ unzip"
54 1>&2 printf " %q" "${unzipArgs[@]}"
55 1>&2 printf "\n"
56 unzip "${unzipArgs[@]}"
57
58 declare unpackedStoreName="${publisher}-${name}"
59
60 declare unpackedStorePath
61 unpackedStorePath="$(nix add-to-store -n "$unpackedStoreName" "$tmpDir")"
62 declare unpackedSha256
63 unpackedSha256="$(nix --extra-experimental-features nix-command hash path --base32 --type sha256 "$unpackedStorePath")"
64 1>&2 echo "unpackedStorePath='$unpackedStorePath'"
65 1>&2 echo "unpackedSha256='$unpackedSha256'"
66
67 rm_tmpdir
68
69 echo "$unpackedSha256"
70 echo "$unpackedStorePath"
71}
72
73
74prefetchExtensionJson() {
75 declare publisher="${1?}"
76 declare name="${2?}"
77 declare version="${3?}"
78
79 declare unpackedShaWStorePath
80 unpackedShaWStorePath="$(prefetchExtensionUnpacked "$publisher" "$name" "$version")"
81
82 declare unpackedStorePath
83 unpackedStorePath="$(echo "$unpackedShaWStorePath" | tail -n1)"
84 1>&2 echo "unpackedStorePath='$unpackedStorePath'"
85
86 declare jsonShaWStorePath
87 jsonShaWStorePath=$(nix-prefetch-url --print-path "file://${unpackedStorePath}/extension/package.json" 2> /dev/null)
88
89 1>&2 echo "jsonShaWStorePath='$jsonShaWStorePath'"
90 echo "$jsonShaWStorePath"
91}
92
93
94formatExtRuntimeDeps() {
95 declare publisher="${1?}"
96 declare name="${2?}"
97 declare version="${3?}"
98
99 declare jsonShaWStorePath
100 jsonShaWStorePath="$(prefetchExtensionJson "$publisher" "$name" "$version")"
101
102 declare jsonStorePath
103 jsonStorePath="$(echo "$jsonShaWStorePath" | tail -n1)"
104 1>&2 echo "jsonStorePath='$jsonStorePath'"
105
106 # Assume packages without an architectures are for x86_64 and remap arm64 to aarch64.
107 declare jqQuery
108 jqQuery=$(cat <<'EOF'
109.runtimeDependencies
110| map(select(.platforms[] | in({"linux": null, "darwin": null})))
111| map(select(.architectures == null).architectures |= ["x86_64"])
112| map(del(.architectures[] | select(. | in({"x86_64": null, "arm64": null}) | not)))
113| map((.architectures[] | select(. == "arm64")) |= "aarch64")
114| map(select(.architectures != []))
115| .[] | {
116 (.id + "__" + (.architectures[0]) + "-" + (.platforms[0])):
117 {installPath, binaries, urls: [.url, .fallbackUrl] | map(select(. != null))}
118}
119EOF
120)
121
122 1>&2 printf "$ cat %q | jq '%s'\n" "$jsonStorePath" "$jqQuery"
123 cat "$jsonStorePath" | jq "$jqQuery"
124}
125
126
127computeExtRtDepChecksum() {
128 declare rtDepJsonObject="${1?}"
129 declare url
130 url="$(echo "$rtDepJsonObject" | jq -j '.[].urls[0]')"
131 declare sha256
132 1>&2 printf "$ nix-prefetch-url '%s'\n" "$url"
133 sha256="$(nix-prefetch-url "$url")"
134 1>&2 echo "$sha256"
135 echo "$sha256"
136}
137
138
139computeAndAttachExtRtDepsChecksums() {
140 while read -r rtDepJsonObject; do
141 declare sha256
142 sha256="$(computeExtRtDepChecksum "$rtDepJsonObject")"
143 echo "$rtDepJsonObject" | jq --arg sha256 "$sha256" '.[].sha256 = $sha256'
144 done < <(cat - | jq -c '.')
145}
146
147
148jqStreamToJson() {
149 cat - | jq --slurp '. | add'
150}
151
152
153jsonToNix() {
154 # TODO: Replacing this non functional stuff with a proper json to nix
155 # implementation would allow us to produce a 'rt-deps-bin-srcs.nix' file instead.
156 false
157 cat - | sed -E -e 's/": /" = /g' -e 's/,$/;/g' -e 's/ }$/ };/g' -e 's/ ]$/ ];/g'
158}