summary refs log tree commit diff
path: root/pkgs/applications/editors/vscode/extensions/_maintainers/update-bin-srcs-lib.sh
blob: e3d1e5fb1397d74a6a3824ccb4cf58f964b5c005 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bash

prefetchExtensionZip() {
  declare publisher="${1?}"
  declare name="${2?}"
  declare version="${3?}"

  1>&2 echo
  1>&2 echo "------------- Downloading extension ---------------"

  declare extZipStoreName="${publisher}-${name}.zip"
  declare extUrl="https://${publisher}.gallery.vsassets.io/_apis/public/gallery/publisher/${publisher}/extension/${name}/${version}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage";
  1>&2 echo "extUrl='$extUrl'"
  declare nixPrefetchArgs=( --name "$extZipStoreName" --print-path "$extUrl" )

  1>&2 printf "$ nix-prefetch-url"
  1>&2 printf " %q" "${nixPrefetchArgs[@]}"
  1>&2 printf " 2> /dev/null\n"
  declare zipShaWStorePath
  zipShaWStorePath=$(nix-prefetch-url "${nixPrefetchArgs[@]}" 2> /dev/null)

  1>&2 echo "zipShaWStorePath='$zipShaWStorePath'"
  echo "$zipShaWStorePath"
}


prefetchExtensionUnpacked() {
  declare publisher="${1?}"
  declare name="${2?}"
  declare version="${3?}"

  declare zipShaWStorePath
  zipShaWStorePath="$(prefetchExtensionZip "$publisher" "$name" "$version")"

  declare zipStorePath
  zipStorePath="$(echo "$zipShaWStorePath" | tail -n1)"
  1>&2 echo "zipStorePath='$zipStorePath'"

  function rm_tmpdir() {
    1>&2 printf "rm -rf -- %q\n" "$tmpDir"
    rm -rf -- "$tmpDir"
    unset tmpDir
    trap - INT TERM HUP EXIT
  }
  function make_trapped_tmpdir() {
    tmpDir=$(mktemp -d)
    trap rm_tmpdir INT TERM HUP EXIT
  }

  1>&2 echo
  1>&2 echo "------------- Unpacking extension ---------------"

  make_trapped_tmpdir
  declare unzipArgs=( -q -d "$tmpDir" "$zipStorePath" )
  1>&2 printf "$ unzip"
  1>&2 printf " %q" "${unzipArgs[@]}"
  1>&2 printf "\n"
  unzip "${unzipArgs[@]}"

  declare unpackedStoreName="${publisher}-${name}"

  declare unpackedStorePath
  unpackedStorePath="$(nix add-to-store -n "$unpackedStoreName" "$tmpDir")"
  declare unpackedSha256
  unpackedSha256="$(nix hash-path --base32 --type sha256 "$unpackedStorePath")"
  1>&2 echo "unpackedStorePath='$unpackedStorePath'"
  1>&2 echo "unpackedSha256='$unpackedSha256'"

  rm_tmpdir

  echo "$unpackedSha256"
  echo "$unpackedStorePath"
}


prefetchExtensionJson() {
  declare publisher="${1?}"
  declare name="${2?}"
  declare version="${3?}"

  declare unpackedShaWStorePath
  unpackedShaWStorePath="$(prefetchExtensionUnpacked "$publisher" "$name" "$version")"

  declare unpackedStorePath
  unpackedStorePath="$(echo "$unpackedShaWStorePath" | tail -n1)"
  1>&2 echo "unpackedStorePath='$unpackedStorePath'"

  declare jsonShaWStorePath
  jsonShaWStorePath=$(nix-prefetch-url --print-path "file://${unpackedStorePath}/extension/package.json" 2> /dev/null)

  1>&2 echo "jsonShaWStorePath='$jsonShaWStorePath'"
  echo "$jsonShaWStorePath"
}


formatExtRuntimeDeps() {
  declare publisher="${1?}"
  declare name="${2?}"
  declare version="${3?}"

  declare jsonShaWStorePath
  jsonShaWStorePath="$(prefetchExtensionJson "$publisher" "$name" "$version")"

  declare jsonStorePath
  jsonStorePath="$(echo "$jsonShaWStorePath" | tail -n1)"
  1>&2 echo "jsonStorePath='$jsonStorePath'"

  # Assume packages without an architectures are for x86_64 and remap arm64 to aarch64.
  declare jqQuery
  jqQuery=$(cat <<'EOF'
.runtimeDependencies
| map(select(.platforms[] | in({"linux": null, "darwin": null})))
| map(select(.architectures == null).architectures |= ["x86_64"])
| map(del(.architectures[] | select(. | in({"x86_64": null, "arm64": null}) | not)))
| map((.architectures[] | select(. == "arm64")) |= "aarch64")
| map(select(.architectures != []))
| .[] | {
  (.id + "__" + (.architectures[0]) + "-" + (.platforms[0])):
    {installPath, binaries, urls: [.url, .fallbackUrl] | map(select(. != null))}
}
EOF
)

  1>&2 printf "$ cat %q | jq '%s'\n" "$jsonStorePath" "$jqQuery"
  cat "$jsonStorePath" | jq "$jqQuery"
}


computeExtRtDepChecksum() {
  declare rtDepJsonObject="${1?}"
  declare url
  url="$(echo "$rtDepJsonObject" | jq -j '.[].urls[0]')"
  declare sha256
  1>&2 printf "$ nix-prefetch-url '%s'\n" "$url"
  sha256="$(nix-prefetch-url "$url")"
  1>&2 echo "$sha256"
  echo "$sha256"
}


computeAndAttachExtRtDepsChecksums() {
  while read -r rtDepJsonObject; do
    declare sha256
    sha256="$(computeExtRtDepChecksum "$rtDepJsonObject")"
    echo "$rtDepJsonObject" | jq --arg sha256 "$sha256" '.[].sha256 = $sha256'
  done < <(cat - | jq  -c '.')
}


jqStreamToJson() {
  cat - | jq --slurp '. | add'
}


jsonToNix() {
  # TODO: Replacing this non functional stuff with a proper json to nix
  # implementation would allow us to produce a 'rt-deps-bin-srcs.nix' file instead.
  false
  cat - | sed -E -e 's/": /" = /g' -e 's/,$/;/g' -e 's/  }$/  };/g'  -e 's/  ]$/  ];/g'
}