summarylogtreecommitdiffstats
path: root/update-version
blob: fbeb628498de9e759f5598b87c401689e188c6c4 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash

## UPDATE VERSION SCRIPT
## ---------------------

# Update the version and hash in the PKGBUILD and upload it

## CREDITS
# ASCII art using the figlet CLI from AUR (https://aur.archlinux.org/packages/figlet) and the pagga font from the figlet-fonts-extra package from AUR (https://aur.archlinux.org/packages/figlet-fonts-extra_)

## DEPENDENCIES
# git
# pacman
# namcap
# base-devel (group)

## SETUP
# Unalias everything to prevent weirdness
unalias -a
# Fail and exit when the first error occurs
set -e
# Enable globs
shopt -s nullglob
# For debbuging
#set -xv

## UTILITY
# Print messages in colour
# Green text
function printg {
	echo -e "\e[1;32m$*\e[0m"
}
# Blue text
function printb {
	echo -e "\e[1;34m$*\e[0m"
}
# Magenta text
function printm {
	echo -e "\e[1;35m$*\e[0m"
}
# Yellow text
function printw {
	echo -e "\e[1;33m$*\e[0m"
}
# Red text
function printr {
	echo -e "\e[1;31m$*\e[0m"
}
# Join elements of an array (taken from SO - https://stackoverflow.com/a/17841619; thank you to the answerer)
function join_by {
	local d=${1-} f=${2-}
	if shift 2; then
		printf %s "$f" "${@/#/$d}"
	fi
}
# Print the logo
function print_logo {
	printw ""
	printw "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"
	printw "░█▀▄░█░█░█▄█░█▀█░░░█░█░█▀▀░█▀▄░█▀▀░▀█▀░█▀█░█▀█░"
	printw "░█▀▄░█░█░█░█░█▀▀░░░▀▄▀░█▀▀░█▀▄░▀▀█░░█░░█░█░█░█░"
	printw "░▀▀░░▀▀▀░▀░▀░▀░░░░░░▀░░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░"
	printw "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"
	printw ""
}

## CONFIG
# Set variables
[[ -z "$PKGBUILD_PATH" ]] && export PKGBUILD_PATH=./PKGBUILD
[[ -z "$SRCINFO_PATH" ]] && export SRCINFO_PATH=./.SRCINFO
[[ -z "$INSTALL_HOOKS_PATH" ]] && export INSTALL_HOOKS_PATH=./yeet.install

[[ -z "$TEMP_DIR" ]] && mkdir -p /tmp/pkgupdate/ && export TEMP_DIR=/tmp/pkgupdate/

[[ -z "$PACMAN_BIN" ]] && export PACMAN_BIN=/usr/bin/pacman
[[ -z "$SUDO_BIN" ]] && export SUDO_BIN=/usr/bin/sudo

[[ -z "$GIT_MESSAGE" ]] && export GIT_MESSAGE="$1"
[[ -z "$GIT_FILES" ]] && export GIT_FILES="$PKGBUILD_PATH $SRCINFO_PATH $INSTALL_HOOKS_PATH ./update-version"

# Source the PKGBUILD so we can access its fields
source "$PKGBUILD_PATH"

## UI
# Print the logo
print_logo

## MAIN
# Update the version first
printm "==> Bump version"
if [[ -z "$1" ]]; then
	printr "ERR No new version specified; aborting"
	exit 1
else
	printb "==> Updating version to $1"
	new_version="$1"
	# Replace it with the new version
	sed -i "s/pkgver=.*/pkgver=$new_version/g" "$PKGBUILD_PATH"
fi

# Update the pkgrel next (if needed)
printm "==> Bump PKGREL"
if [[ -z "$2" ]]; then
	printw "==> No PKGREL specified, skipping"
else
	printb "==> Updating PKGREL to $2"
	new_rel="$2"
	# Replace the old pkgrel with the new pkgrel
	sed -i "s/pkgrel=.*/pkgrel=$new_rel/g" "$PKGBUILD_PATH"
fi

printm "==> Update checksums"
# Now download the sources and get their SHA256SUMS
printb "==> Retrieving sources and generating SHA256 checksums..."
# First make a tmp directory for the files
mkdir -p ""$TEMP_DIR""
# Array of generated checksums
generated_checksums=()
# Then retrieve them
for source_file_or_url in "${source[@]}"; do
	# Ensure they are files to be downloaded, not local ones
	if echo $source_file_or_url | grep -q '::' ; then
		# Parse the URL and file
		source_url=$(echo $source_file_or_url | awk -F '::' '{ print $2 }')
		source_file=$(echo $source_file_or_url | awk -F '::' '{ print $1 }')
		# Get the file
		printb "==> Found source $source_file; retrieving from $source_url using cURL"
		curl -L -o "$TEMP_DIR/$source_file" "$source_url"
		printg "==> Succesfully retrieved $source_file!"
		# Generate the checksum
		printb "==> Generating SHA256 checksum for $source_file..."
		sha256_checksum=$(sha256sum ""$TEMP_DIR"$source_file" | awk '{ print $1 }')
		generated_checksums+=($sha256_checksum)
		printg "==> Checksum generated: $sha256_checksum"
	else
		# It is a local file, generate a checksum
		source_file=$source_file_or_url
		printg "==> Found local source file $source_file"
		# Generate the checksum
		printb "==> Generating SHA256 checksum for $source_file..."
		sha256_checksum=$(sha256sum "$TEMP_DIR/$source_file" | awk '{ print $1 }')
		generated_checksums+=($sha256_checksum)
		printg "==> Checksum generated: $sha256_checksum"
	fi
done
# Replace the checksums in the file
sed -i "s/sha256sums=.*/sha256sums=('$(join_by ' ' $generated_checksums)')/g" "$PKGBUILD_PATH"
printg "==> Updated checksums succesfully!"

# Print out .SRCINFO
printm "==> Update .SRCINFO"
printb "==> Updating .SRCINFO..."
cp "$SRCINFO_PATH" "$TEMP_DIR/.SRCINFO.bak"
makepkg --printsrcinfo > "$SRCINFO_PATH"
printg "==> Updated .SRCINFO succesfully!"

# Run checks
# - `namcap` should pass
# - `makepkg` should exit succesfully
printm "==> Run checks (namcap, makepkg)"
# Run namcap
printb "==> Running namcap..."
namcap_output=$(namcap "$PKGBUILD_PATH")
if [[ -z $namcap_output ]]; then
	printg "==> namcap successful!"
else
	printr "ERR namcap check failed: $namcap_output"
fi
# Run makepkg
printb "==> Running makepkg..."
BUILDDIR="$TEMP_DIR" makepkg -fp "$PKGBUILD_PATH"
printg "==> makepkg successful!"

# Pushing to git
printm "==> Push to AUR"
# Run git add, commit, push
printb "==> Files to commit: $GIT_FILES"
printb "==> Commit message: $GIT_MESSAGE"
# Ask for confirmation
read -p $'\e[1;33m ==> Push to AUR?\e[0m [\e[1;32my\e[0m/\e[1;31mN\e[0m] ' -n 1 -r
printb ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
	printb "==> Pushing files..."
	git add $GIT_FILES
	git commit -m "$GIT_MESSAGE"
	git push
	# We're done!
	printg "==> Succesfully updated package!"
else
	printr "ERR Aborting push; cancelled by user"
fi