about summary refs log tree commit diff
path: root/pkgs/by-name/tr/tradingview/update.sh
blob: c3f38cbb4206cd75d1d144335a29bdc889cdf817 (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
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p curl jq git gnused gnugrep

#
# Get latest version of TradingView from Snapcraft.
#

snap_info=($(
  curl -s -H 'X-Ubuntu-Series: 16' \
    'https://api.snapcraft.io/api/v1/snaps/details/tradingview' \
  | jq --raw-output \
    '.revision,.download_sha512,.version,.last_updated'
))

# "revision" is the actual version identifier; "version" is for human consumption.
revision="${snap_info[0]}"
sha512="${snap_info[1]}"
sri=$(nix hash to-sri --type "sha512" $sha512)
upstream_version="${snap_info[2]}"
last_updated="${snap_info[3]}"

echo "Latest release is $upstream_version from $last_updated."

#
# Read the current TradingView version.
#

nixpkgs="$(git rev-parse --show-toplevel)"
tradingview_nix="$nixpkgs/pkgs/by-name/tr/tradingview/package.nix"
current_nix_version=$(
  grep 'version\s*=' "$tradingview_nix" \
  | sed -Ene 's/.*"(.*)".*/\1/p'
)

echo "Current nix version: $current_nix_version"

if [[ "$current_nix_version" = "$upstream_version" ]]; then
  echo "TradingView is already up-to-date"
  exit 0
fi

#
# Find and replace.
#

echo "Updating from ${current_nix_version} to ${upstream_version}, released ${last_updated}"
echo 's/hash\s*=\s*"[^"]*"\s*;/hash = "'"${sri}"'";/'
sed --regexp-extended \
  -e 's/revision\s*=\s*"[0-9]+"\s*;/revision = "'"${revision}"'";/' \
  -e 's#hash\s*=\s*"[^"]*"\s*;#hash = "'"${sri}"'";#' \
  -e 's/version\s*=\s*".*"\s*;/version = "'"${upstream_version}"'";/' \
  -i "$tradingview_nix"

#
# Attempt a build.
#

export NIXPKGS_ALLOW_UNFREE=1

if ! nix-build -A tradingview "$nixpkgs"; then
  echo "The updated TradingView failed to build."
  exit 1
fi

#
# Commit changes.
#
git add "$tradingview_nix"
git commit -m "tradingview: ${current_nix_version} -> ${upstream_version}"