Skip to content

Commit

Permalink
Merge pull request #175 from xonixx/selfupdate-is-broken-#174
Browse files Browse the repository at this point in the history
Selfupdate is broken #174
  • Loading branch information
xonixx authored Dec 9, 2024
2 parents f7a42e1 + 1256e64 commit 70f0f51
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
5 changes: 4 additions & 1 deletion Makesurefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@

@goal README.md @private
@doc 'compiles release version of README.md'
$AWK '
$AWK -v NEXT_VERSION="$NEXT_VERSION" '
{
gsub(/raw\.githubusercontent\.com\/xonixx\/makesure\/[^\/]+\//, "raw.githubusercontent.com/xonixx/makesure/v" NEXT_VERSION "/")
}
/^\$ \.\/makesure -h$/ { print; stop=1; system("./makesure -h") }
/^```$/ { stop=0 }
!stop' README.md > README.md.1
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ Usage: makesure [options...] [-f buildfile] [goals...]
Since `makesure` is a tiny utility represented by a single file, the recommended installation strategy is to keep it local to a project where it's used (this means in code repository). Not only this eliminates the need for repetitive installation for every dev on a project, but also allows using separate `makesure` version per project and update only as needed.

```sh
wget "https://raw.githubusercontent.com/xonixx/makesure/main/makesure?token=$(date +%s)" -Omakesure && \
wget "https://raw.githubusercontent.com/xonixx/makesure/v0.9.23/makesure" -Omakesure && \
chmod +x makesure && echo "makesure $(./makesure -v) installed"
```
or
```sh
curl "https://raw.githubusercontent.com/xonixx/makesure/main/makesure?token=$(date +%s)" -o makesure && \
curl "https://raw.githubusercontent.com/xonixx/makesure/v0.9.23/makesure" -o makesure && \
chmod +x makesure && echo "makesure $(./makesure -v) installed"
```

Expand Down Expand Up @@ -659,7 +659,7 @@ Install Bash completion for `./makesure` locally
```sh
[[ ! -f ~/.bash_completion ]] && touch ~/.bash_completion
grep makesure ~/.bash_completion >/dev/null || echo '. ~/.makesure_completion.bash' >> ~/.bash_completion
curl "https://raw.githubusercontent.com/xonixx/makesure/main/completion.bash?token=$(date +%s)" -o ~/.makesure_completion.bash
curl "https://raw.githubusercontent.com/xonixx/makesure/v0.9.23/completion.bash" -o ~/.makesure_completion.bash
echo 'Please reopen the shell to activate completion.'
```

Expand Down
36 changes: 25 additions & 11 deletions makesure.awk
Original file line number Diff line number Diff line change
Expand Up @@ -720,21 +720,35 @@ function currentTimeMillis( res) {
return +res
}

function selfUpdate( url, tmp, err, newVer) {
url = "https://raw.githubusercontent.com/xonixx/makesure/main/makesure?token=" rand()
function selfUpdate( tmp, err, newVer,line,sha) {
tmp = executeGetLine("mktemp /tmp/makesure_new.XXXXXXXXXX")
err = dl(url, tmp)
if (!err && !ok("chmod +x " tmp)) err = "can't chmod +x " tmp
# first get the last commit hash
err = dl("https://api.github.com/repos/xonixx/makesure/commits?per_page=1", tmp)
if (!err) {
newVer = executeGetLine(tmp " -v")
if (Version != newVer) {
if (!ok("cp " tmp " " quoteArg(Prog)))
err = "can't overwrite " Prog
else print "updated " Version " -> " newVer
} else print "you have latest version " Version " installed"
while (getline line < tmp) {
if (line ~ /"sha":/) {
if (match(line = substr(line, index(line, "\"sha\":") + 6), /"[a-z0-9]+"/))
sha = substr(line, RSTART + 1, RLENGTH - 2)
break
}
}
if (!sha) err = "unable to get the latest commit"
if (!err) {
# now download the latest executable
err = dl("https://raw.githubusercontent.com/xonixx/makesure/" sha "/makesure", tmp)
if (!err && !ok("chmod +x " tmp)) err = "can't chmod +x " tmp
if (!err) {
newVer = executeGetLine(tmp " -v")
if (Version != newVer) {
if (!ok("cp " tmp " " quoteArg(Prog)))
err = "can't overwrite " Prog
else print "updated " Version " -> " newVer
} else print "you have latest version " Version " installed"
}
}
}
rm(tmp)
if (err) die(err)
if (err) die(err "\nPlease use manual update: https://makesure.dev/Installation.html")
}

function renderDuration(deltaMillis,\
Expand Down
4 changes: 4 additions & 0 deletions tests/200_update.tush
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ $ ./$MAKESURE -f tests/200_update.sh test_wget | awk '{ sub(/[0-9][0-9.]+/,"YYY"
| goal 'test_wget' ...
| XXX
| running wget
| running wget
| updated XXX -> YYY
| running wget
| running wget
| you have latest version YYY installed
| YYY

Expand All @@ -24,7 +26,9 @@ $ ./$MAKESURE -f tests/200_update.sh test_curl | awk '{ sub(/[0-9][0-9.]+/,"YYY"
| goal 'test_curl' ...
| XXX
| running curl
| running curl
| updated XXX -> YYY
| running curl
| running curl
| you have latest version YYY installed
| YYY

0 comments on commit 70f0f51

Please sign in to comment.