]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - fetch_version
492aff11ddd1d912413743e8658cb19e8c24a6bf
[perso/Immae/Config/Nix.git] / fetch_version
1 #!/bin/bash
2
3 usage() {
4 echo "$0 file.json"
5 echo "$0 [-n|--name name] [-b|--branch branch_or_rev] [-f|--file out_file] [-h|--help] (-u|--url) url"
6 exit
7 }
8
9 branch="master"
10 while [[ $# -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")"
20 break
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 ;;
34 -f|--file)
35 file=$1
36 shift
37 ;;
38 -h|--help)
39 usage
40 ;;
41 esac
42 done
43 if [ -z "$url" ]; then
44 usage
45 fi
46 if [ -z "$name" ]; then
47 name=$(echo "$url" | cut -d"/" -f5)
48 fi
49 if [ -z "$file" ]; then
50 file=$name.json
51 fi
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
75 function get_ref () {
76 case "$1" in
77 refs/*)
78 echo "$1"
79 ;;
80 *)
81 echo "refs/heads/$1"
82 ;;
83 esac
84 }
85
86 function get_name () {
87 branch="$1"
88 rev="$2"
89 minirev=${rev:0:7}
90
91 case "$branch" in
92 refs/tags/*)
93 b="${branch#refs/tags/}"
94 echo "${b//\//-}"
95 ;;
96 refs/heads/*)
97 b=${branch#refs/heads/}
98 echo "$minirev-${b//\//-}"
99 ;;
100 refs/*)
101 b=${branch#refs/}
102 echo "$minirev-${b//\//-}"
103 ;;
104 *)
105 echo "$minirev-${branch//\//-}"
106 ;;
107 esac
108 }
109
110 function fetch_github () {
111 rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
112 sha="$(nix-prefetch-url --unpack $url/archive/$rev.tar.gz)"
113 # Différent du git-describe et github-spécifique
114 #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//")
115 tag=$(get_name $branch $rev)
116 }
117
118 function fetch_other () {
119 rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
120 sha="$(nix-prefetch-git --url $url --rev $(get_ref $branch) | jq -r '.sha256')"
121 tag=$(get_name $branch $rev)
122 }
123
124 case "$url" in
125 https://*github.com/*)
126 fetch_github 2>/dev/null
127 owner=$(echo "$url" | cut -d"/" -f4)
128 repo=$(echo "$url" | cut -d"/" -f5)
129
130 F='{
131 "tag": $tag,
132 "meta": {
133 "name": $name,
134 "url": $url,
135 "branch": $branch
136 },
137 "github": {
138 "owner": $owner,
139 "repo": $repo,
140 "rev": $rev,
141 "sha256": $sha,
142 "fetchSubmodules": true
143 }
144 }'
145 ;;
146 *)
147 fetch_other 2>/dev/null
148 F='{
149 "tag": $tag,
150 "meta": {
151 "name": $name,
152 "url": $url,
153 "branch": $branch
154 },
155 "git": {
156 "url": $url,
157 "rev": $rev,
158 "sha256": $sha,
159 "fetchSubmodules": true
160 }
161 }'
162 ;;
163 esac
164
165 jq -n \
166 --arg name "$name" \
167 --arg owner "$owner" \
168 --arg repo "$repo" \
169 --arg tag "$tag" \
170 --arg rev "$rev" \
171 --arg url "$url" \
172 --arg branch "$branch" \
173 --arg sha "$sha" \
174 "$F" > $file