init.sh: refactor

This commit is contained in:
Zihao Yu
2022-09-04 22:11:09 +08:00
parent cf4bd2fd79
commit e2c722ddb1

75
init.sh
View File

@ -1,66 +1,59 @@
#!/bin/bash #!/bin/bash
# usage: init repo branch [env] # usage: addenv env_name path
function init() { function addenv() {
if [ -d $1 ]; then sed -i -e "/^export $1=.*/d" ~/.bashrc
echo "$1 is already initialized, skipping..." echo "export $1=`readlink -e $2`" >> ~/.bashrc
return echo "By default this script will add environment variables into ~/.bashrc."
fi echo "After that, please run 'source ~/.bashrc' to let these variables take effect."
echo "If you use shell other than bash, please add these environment variables manually."
while [ ! -d $1 ]; do
git clone -b $2 https://github.com/NJU-ProjectN/$1.git
done
log="$1 `cd $1 && git log --oneline --no-abbrev-commit -n1`"$'\n'
rm -rf $1/.git
git add -A $1
git commit -am "$1 $2 initialized"$'\n\n'"$log"
if [ $3 ] ; then
sed -i -e "/^export $3=.*/d" ~/.bashrc
echo "export $3=`readlink -e $1`" >> ~/.bashrc
echo "By default this script will add environment variables into ~/.bashrc."
echo "After that, please run 'source ~/.bashrc' to let these variables take effect."
echo "If you use shell other than bash, please add these environment variables manually."
fi
} }
# usage: init_no_git repo branch # usage: init repo branch directory trace [env]
function init_no_git() { # trace = true|false
if [ -d $1 ]; then function init() {
echo "$1 is already initialized, skipping..." if [ -d $3 ]; then
echo "$3 is already initialized, skipping..."
return return
fi fi
while [ ! -d $1 ]; do while [ ! -d $3 ]; do
git clone -b $2 https://github.com/NJU-ProjectN/$1.git git clone -b $2 git@github.com:$1.git $3
done done
log="$1 `cd $1 && git log --oneline --no-abbrev-commit -n1`"$'\n' log="$1 `cd $3 && git log --oneline --no-abbrev-commit -n1`"$'\n'
sed -i -e "/^\/$1/d" .gitignore if [ $4 == "true" ] ; then
echo "/$1" >> .gitignore rm -rf $3/.git
git add -A $3
git commit -am "$1 $2 initialized"$'\n\n'"$log"
else
sed -i -e "/^\/$3/d" .gitignore
echo "/$3" >> .gitignore
git add -A .gitignore
git commit --no-verify --allow-empty -am "$1 $2 initialized without tracing"$'\n\n'"$log"
fi
git add -A .gitignore if [ $5 ] ; then
git commit --no-verify --allow-empty -am "$1 $2 initialized without tracing"$'\n\n'"$log" addenv $5 $3
fi
} }
case $1 in case $1 in
nemu) nemu)
init nemu ics2022 NEMU_HOME init NJU-ProjectN/nemu ics2022 nemu true NEMU_HOME
;; ;;
abstract-machine) abstract-machine)
init abstract-machine ics2022 AM_HOME init NJU-ProjectN/abstract-machine ics2022 abstract-machine true AM_HOME
init_no_git fceux-am ics2021 init NJU-ProjectN/fceux-am ics2021 fceux-am false
;; ;;
am-kernels) am-kernels)
init_no_git am-kernels ics2021 init NJU-ProjectN/am-kernels ics2021 am-kernels false
;; ;;
nanos-lite) nanos-lite)
init nanos-lite ics2021 init NJU-ProjectN/nanos-lite ics2021 nanos-lite true
;; ;;
navy-apps) navy-apps)
init navy-apps ics2021 NAVY_HOME init NJU-ProjectN/navy-apps ics2021 navy-apps true NAVY_HOME
;; ;;
*) *)
echo "Invalid input..." echo "Invalid input..."