Skip to content

Commit

Permalink
Created dependency updating script / workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoneDroid committed Mar 22, 2024
1 parent fd59b6c commit 53a6e00
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 1 deletion.
93 changes: 93 additions & 0 deletions .github/scripts/Update-Dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env bash

#
# Update a dependency from a remote repository.
#

echo '>>> Updating dependencies.'

set -o errexit # Exit for any error

dry=false


source "$( dirname "$0" )/Update-Dependency.sh"


update "dry=${dry}" \
'MySQLite' \
'https://github.com/FPtje/MySQLite' \
'gamemode/libraries/mysqlite/'

update "dry=${dry}" \
'PropProtection' \
'https://github.com/fptje/falcos-Prop-protection' \
'gamemode/modules/fpp/pp/' \
'lua/fpp'

update "dry=${dry}" \
'GModFunctional' \
'https://github.com/fptje/GModFunctional' \
'gamemode/libraries/fn.lua'

update "dry=${dry}" \
'Simplerr' \
'https://github.com/fptje/simplerr' \
'gamemode/libraries/simplerr.lua'

update "dry=${dry}" \
'CAMI' \
'https://github.com/glua/CAMI' \
'gamemode/libraries/sh_cami.lua' \
'lua/autorun'

update "dry=${dry}" \
'Spectate' \
'https://github.com/fptje/FSpectate' \
'gamemode/modules/fspectate/' \
'lua/fspectate'


function noCommits {

local current=$(
git rev-parse HEAD
)

local branch=$(
git rev-parse \
--abbrev-ref HEAD
)

local origin=$(
git rev-parse \
"origin/${branch}"
)

if [ "$current" == "$origin" ] ; then
return 0
else
return 1
fi
}


if noCommits ; then

echo ">>> All dependencies are already up-to-date."

echo ">>> Finished"

exit 0

fi


echo ">>> Pushing commits to repository."

if [ "$dry" == 'false' ] ; then
git push origin
fi

echo ">>> Finished"

195 changes: 195 additions & 0 deletions .github/scripts/Update-Dependency.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#!/usr/bin/env bash

#
# Exports an update function that copies
# a dependency from a remote repository.
#


function print {
local text="$1"
echo ">>> ${text}"
}

function alert {
local text="$1"
echo -e "\e[31m[!] ${text}\e[0m"
}

function cyan {
local text="$1"
echo -e "\e[36m${text}\e[0m"
}


function hasRemote {

local remote="$1"

git remote |
grep --silent "^$remote$"

return $?
}

function hasChanges {

git status --porcelain 'gamemode' |
grep --silent '.'

return $?
}

function toFolder (

local path="$1"

local last="$( basename "$path" )"

if [[ "$last" == *.* ]] ; then
path="$( dirname "$path" )"
fi

echo "$path"
)


function update {

local Usage="Use update 'Remote Name' 'Repository Url' 'Target Folder' ( 'Source Folder' )"


local dry="$1"

if [ "$dry" == 'dry=true' ] ; then
dry=true
else
dry=false
fi


local remote="$2"

if [ -z "$remote" ] ; then
alert "The <Remote Name> parameter is missing"
print "$Usage"
return 1
fi


local repository="$3"

if [ -z "$repository" ] ; then
alert "The <Repository Url> parameter is missing"
print "$Usage"
return 1
fi


local target="$4"

if [ -z "$target" ] ; then
alert "The <Target Folder> parameter is missing"
print "$Usage"
return 1
fi


local source="$5"


echo ""
print "[ Updating $( cyan "$remote" ) ]( Dry : ${dry} )"


if hasRemote "$remote" ; then

print "Updating remote repository url."

git remote set-url "$remote" "$repository"

if (( $? != 0 )) ; then
alert "Failed to update remote repository"
return 1
fi

else

print "Adding remote repository url."

git remote add "$remote" "$repository"

if (( $? != 0 )) ; then
alert "Failed to add remote repository"
return 1
fi
fi


print "Fetching remote repository"

git fetch "$remote"

if (( $? != 0 )) ; then
alert "Failed to fetch remote repository"
return 1
fi


print "Removing local target folder"

git rm -r --force "$target"

if (( $? != 0 )) ; then
alert "Failed to remove local target folder"
return 1
fi


print "Reading data from remote"

local location="${remote}/master"

if [ -n "$source" ] ; then
location="${location}:${source}"
fi


local prefix="$( toFolder "$target" )"

git read-tree \
--prefix="$prefix" \
-u \
"$location"

if (( $? != 0 )) ; then
alert "Failed to read data from remote"
return 1
fi


if hasChanges ; then

print "Committing dependency changes"

if [ "$dry" == 'false' ] ; then

git add 'gamemode'

git commit \
-m "Dependency Update - ${remote}"

fi

print "Committed changes"

return 0

else
print "The dependency is already up-to-date."
return 0
fi
}


export -f update
1 change: 0 additions & 1 deletion .github/scripts/check-modified-subtree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
echo 'Checking for any dependency changes.'

set -o errexit # Exit for any error
set -o nounset # Error when referencing unset vars


commit=$(
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/update-depencencies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

name : Update Dependencies

on :
workflow_dispatch :

permissions :
contents : write

jobs :
update :

runs-on : ubuntu-latest

steps :

- name : Checkout Repository
uses : actions/checkout@v4

- name : Setup Git
run : |
git config user.name Dependency-Updater
git config user.email [email protected]
- name : Update & Dependencies
run : ./.github/scripts/Update-Dependencies.sh

0 comments on commit 53a6e00

Please sign in to comment.