|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2020 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +pubspec2nix() { |
| 18 | + local pubspec_in="$1"; |
| 19 | + local nix_out="$2"; |
| 20 | + if [ -z "$pubspec_in" ] || [ -z "$nix_out" ]; then |
| 21 | + echo "usage: dart2nix <pubspec.lock> <packages.nix>" >&2 |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + local url_template='https://storage.googleapis.com/pub-packages/packages/{{description.name}}-{{version}}.tar.gz' |
| 25 | + local grep_template=' { name = .{{lock.description.name}}.; version = .{{lock.version}}.; sha256 = ' |
| 26 | + local pkg_template=' { name = "{{lock.description.name}}"; version = "{{lock.version}}"; sha256 = "{{sha256}}"; }' |
| 27 | + local PATH="$PATH:/nix/store/s8jfiwj2p6hn3ycj840naf2jkac4azx6-jq-1.6-bin/bin" # 'yq' doesn't work unless jq is in $PATH |
| 28 | + local yq="/nix/store/0l1wva10sr2yyqj78pf4nbqkq1al61qr-python3.8-yq-2.11.1/bin/yq" |
| 29 | + local mustache="/nix/store/q1lllkgf81rzpsf9lyhw1660q55zn3l7-mustache-go-1.2.0/bin/mustache" |
| 30 | + [ -f "$nix_out" ] || echo '[' > "$nix_out" |
| 31 | + $yq -c '.packages[]' "$pubspec_in" | while read dep ; do |
| 32 | + match="$( echo '{"lock":'$dep'}' | $mustache <(echo "$grep_template") )" |
| 33 | + if grep "$match" "$nix_out" ; then |
| 34 | + #echo "already got this one, moving on: $dep" |
| 35 | + #elif true; then |
| 36 | + #echo "would download this one: $match" |
| 37 | + true |
| 38 | + else |
| 39 | + url="$( |
| 40 | + echo "$dep" \ |
| 41 | + | $mustache <(echo "$url_template") |
| 42 | + )" |
| 43 | + sha256="$( |
| 44 | + /nix/store/ckl9rqyajn45crcyw3b743bcaqx1j2w1-nix-2.3.10/bin/nix-hash --flat --base32 --type sha256 <(curl -L "$url") |
| 45 | + )" |
| 46 | + echo '{"sha256":"'$sha256'","lock":'$dep'}' | $mustache <(echo "$pkg_template") >> "$nix_out" |
| 47 | + fi |
| 48 | + done |
| 49 | + echo ']' >> "$nix_out" |
| 50 | +} |
| 51 | +pubspec2nix "$@" |
0 commit comments