]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - fetch_version
multiple improvements
[perso/Immae/Config/Nix.git] / fetch_version
CommitLineData
177da38b
IB
1#!/bin/bash
2
3usage() {
4 echo "$0 file.json"
6b53d116 5 echo "$0 [-n|--name name] [-b|--branch branch] [-f|--file out_file] [-h|--help] (-u|--url) url"
177da38b
IB
6 exit
7}
8
9branch="master"
10while [[ $# -gt 0 ]]; do
11 a="$1"
12 shift
13 case "$a" in
14 *.json)
15 file=$a
16 content=$(cat $a)
17 name="$(echo "$content" | jq -r ".meta.name")"
18 url="$(echo "$content" | jq -r ".meta.url")"
19 branch="$(echo "$content" | jq -r ".meta.branch")"
6b53d116 20 break
177da38b
IB
21 ;;
22 -n|--name)
23 name=$1
24 shift
25 ;;
26 -u|--url)
27 url=$1
28 shift
29 ;;
30 -b|--branch)
31 branch=$1
32 shift
33 ;;
6b53d116
IB
34 -f|--file)
35 file=$1
36 shift
37 ;;
177da38b
IB
38 -h|--help)
39 usage
40 ;;
41 esac
42done
43if [ -z "$url" ]; then
44 usage
45fi
46if [ -z "$name" ]; then
47 name=$(echo "$url" | cut -d"/" -f5)
48fi
49if [ -z "$file" ]; then
50 file=$name.json
51fi
52
53# function fetch_ledger () {
54# pushd $HOME/projets/ledger >/dev/null 2>/dev/null
55# git fetch origin
56# tag="$(git describe origin/next | sed -e "s/^v//")"
57# rev="$(git show-ref -s refs/remotes/origin/next)"
58# sha="$(nix-prefetch-url --unpack file://<(git archive --format=tar.gz HEAD) 2>/dev/null)"
59# popd >/dev/null 2>/dev/null
60# }
61
62# awk_describe='BEGIN {
63# FS = "[ /^]+"
64# while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) {
65# if (!sha)
66# sha = substr($0, 1, 7)
67# tag = $3
68# }
69# while ("curl -s " ARGV[1] "/releases/tag/" tag | getline)
70# if ($3 ~ "commits")
71# com = $2
72# printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha
73# }'
74
75function fetch_github () {
76 rev="$(git ls-remote --refs $url refs/heads/$branch | head -n1 | cut -f1)"
77 sha="$(nix-prefetch-url --unpack $url/archive/$branch.tar.gz)"
78 # Différent du git-describe et github-spécifique
79 #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//")
80 tag=${rev:0:7}-$branch
81}
82
6b53d116
IB
83function fetch_other () {
84 rev="$(git ls-remote --refs $url refs/heads/$branch | head -n1 | cut -f1)"
85 sha="$(nix-prefetch-git --url $url --rev refs/heads/$branch | jq -r '.sha256')"
86 tag=${rev:0:7}-$branch
87}
177da38b 88
6b53d116
IB
89case "$url" in
90 https://*github.com/*)
91 fetch_github 2>/dev/null
92 owner=$(echo "$url" | cut -d"/" -f4)
93 repo=$(echo "$url" | cut -d"/" -f5)
177da38b 94
6b53d116
IB
95 F='{
96 "tag": $tag,
97 "meta": {
98 "name": $name,
99 "url": $url,
100 "branch": $branch
101 },
102 "github": {
103 "owner": $owner,
104 "repo": $repo,
105 "rev": $rev,
106 "sha256": $sha,
107 "fetchSubmodules": true
108 }
109 }'
110 ;;
111 *)
112 fetch_other 2>/dev/null
113 F='{
114 "tag": $tag,
115 "meta": {
116 "name": $name,
117 "url": $url,
118 "branch": $branch
119 },
120 "git": {
121 "url": $url,
122 "rev": $rev,
123 "sha256": $sha,
124 "fetchSubmodules": true
125 }
126 }'
127 ;;
128esac
177da38b
IB
129
130jq -n \
131 --arg name "$name" \
132 --arg owner "$owner" \
133 --arg repo "$repo" \
134 --arg tag "$tag" \
135 --arg rev "$rev" \
136 --arg url "$url" \
137 --arg branch "$branch" \
138 --arg sha "$sha" \
139 "$F" > $file