aboutsummaryrefslogtreecommitdiff
path: root/fetch_version
blob: db0af1b813a3c6e17e320e29b5072d663f31478c (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
#!/bin/bash

usage() {
  echo "$0 file.json"
  echo "$0 [-n|--name name] [-b|--branch branch] [-h|--help] (-u|--url) url"
  exit
}

branch="master"
while [[ $# -gt 0 ]]; do
  a="$1"
  shift
  case "$a" in
    *.json)
      file=$a
      content=$(cat $a)
      name="$(echo "$content" | jq -r ".meta.name")"
      url="$(echo "$content" | jq -r ".meta.url")"
      branch="$(echo "$content" | jq -r ".meta.branch")"
      ;;
    -n|--name)
      name=$1
      shift
      ;;
    -u|--url)
      url=$1
      shift
      ;;
    -b|--branch)
      branch=$1
      shift
      ;;
    -h|--help)
      usage
      ;;
  esac
done
if [ -z "$url" ]; then
  usage
fi
if [ -z "$name" ]; then
  name=$(echo "$url" | cut -d"/" -f5)
fi
if [ -z "$file" ]; then
  file=$name.json
fi

# function fetch_ledger () {
#   pushd $HOME/projets/ledger >/dev/null 2>/dev/null
#   git fetch origin
#   tag="$(git describe origin/next | sed -e "s/^v//")"
#   rev="$(git show-ref -s refs/remotes/origin/next)"
#   sha="$(nix-prefetch-url --unpack file://<(git archive --format=tar.gz HEAD) 2>/dev/null)"
#   popd >/dev/null 2>/dev/null
# }

# awk_describe='BEGIN {
#   FS = "[ /^]+"
#   while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) {
#     if (!sha)
#       sha = substr($0, 1, 7)
#     tag = $3
#   }
#   while ("curl -s " ARGV[1] "/releases/tag/" tag | getline)
#     if ($3 ~ "commits")
#       com = $2
#   printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha
# }'

function fetch_github () {
  rev="$(git ls-remote --refs $url refs/heads/$branch | head -n1 | cut -f1)"
  sha="$(nix-prefetch-url --unpack $url/archive/$branch.tar.gz)"
  # Différent du git-describe et github-spécifique
  #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//")
  tag=${rev:0:7}-$branch
}

fetch_github 2>/dev/null

owner=$(echo "$url" | cut -d"/" -f4)
repo=$(echo "$url" | cut -d"/" -f5)

F='{
  "tag": $tag,
  "meta": {
    "name": $name,
    "url": $url,
    "branch": $branch
  },
  "github": {
    "owner": $owner,
    "repo": $repo,
    "rev": $rev,
    "sha256": $sha,
    "fetchSubmodules": true
  }
}'

jq -n \
  --arg name "$name" \
  --arg owner "$owner" \
  --arg repo "$repo" \
  --arg tag "$tag" \
  --arg rev "$rev" \
  --arg url "$url" \
  --arg branch "$branch" \
  --arg sha "$sha" \
  "$F" > $file