Merge pull request #119126 from fabaff/pycomfoconnect
[NixPkgs.git] / pkgs / common-updater / generic-updater.nix
blob8483f9bbd1de1153c541ce71a09f933dc9a36fea
1 { stdenv, writeScript, coreutils, gnugrep, gnused, common-updater-scripts, nix }:
3 { pname
4 , version
5 , attrPath ? pname
6 , versionLister
7 , ignoredVersions ? ""
8 , rev-prefix ? ""
9 , odd-unstable ? false
10 , patchlevel-unstable ? false
13 let
14   # where to print git commands and debugging messages
15   fileForGitCommands = "update-git-commits.txt";
17   # shell script to update package
18   updateScript = writeScript "update-script.sh" ''
19     #! ${stdenv.shell}
20     set -o errexit
21     set -x
23     pname="$1"
24     version="$2"
25     attr_path="$3"
26     version_lister="$4"
27     ignored_versions="$5"
28     rev_prefix="$6"
29     odd_unstable="$7"
30     patchlevel_unstable="$8"
32     # print header
33     echo "# $pname-$version" >> ${fileForGitCommands}
35     function version_is_ignored() {
36       local tag="$1"
37       [ -n "$ignored_versions" ] && grep -E "$ignored_versions" <<< "$tag"
38     }
40     function version_is_unstable() {
41       local tag="$1"
42       local enforce="$2"
43       if [ -n "$odd_unstable" -o -n "$enforce" ]; then
44         local minor=$(echo "$tag" | ${gnused}/bin/sed -rne 's,^[0-9]+\.([0-9]+).*,\1,p')
45         if [ $((minor % 2)) -eq 1 ]; then
46           return 0
47         fi
48       fi
49       if [ -n "$patchlevel_unstable" -o -n "$enforce" ]; then
50         local patchlevel=$(echo "$tag" | ${gnused}/bin/sed -rne 's,^[0-9]+\.[0-9]+\.([0-9]+).*$,\1,p')
51         if ((patchlevel >= 90)); then
52           return 0
53         fi
54       fi
55       return 1
56     }
58     tags=$($version_lister $pname ${fileForGitCommands}) || exit 1
60     # print available tags
61     for tag in $tags; do
62         echo "# found $pname version: $tag" >> ${fileForGitCommands}
63     done
65     # cut any revision prefix not used in the NixOS package version
66     if [ -n "$rev_prefix" ]; then
67       tags=$(echo "$tags" | ${gnugrep}/bin/grep "^$rev_prefix")
68       tags=$(echo "$tags" | ${gnused}/bin/sed -e "s,^$rev_prefix,,")
69     fi
70     tags=$(echo "$tags" | ${gnugrep}/bin/grep "^[0-9]")
72     # sort the tags in decreasing order
73     tags=$(echo "$tags" | ${coreutils}/bin/sort --reverse --version-sort)
75     # find the newest tag
76     # do not consider development versions
77     for latest_tag in $tags; do
78       if version_is_ignored "$latest_tag"; then
79         echo "#   skip ignored version: $pname-$latest_tag" >> ${fileForGitCommands}
80         latest_tag=
81       elif version_is_unstable "$latest_tag"; then
82         echo "#   skip development version: $pname-$latest_tag" >> ${fileForGitCommands}
83         latest_tag=
84       else
85         if version_is_unstable "$latest_tag" "enforce"; then
86           echo "#   use potential development version: $pname-$latest_tag" >> ${fileForGitCommands}
87         fi
88         break
89       fi
90     done
92     if [ -n "$latest_tag" ]; then
93       # print commands to commit the changes
94       if [ "$version" != "$latest_tag" ]; then
95         pfile=$(EDITOR=echo ${nix}/bin/nix edit -f. "$attr_path")
96         echo "   git add $pfile " >> ${fileForGitCommands}
97         echo "   git commit -m '$attr_path: $version -> $latest_tag'" >> ${fileForGitCommands}
98       fi
100       # update the nix expression
101       ${common-updater-scripts}/bin/update-source-version "$attr_path" "$latest_tag"
102     fi
104     echo "" >> ${fileForGitCommands}
105   '';
108 [ updateScript pname version attrPath versionLister ignoredVersions rev-prefix odd-unstable patchlevel-unstable ]