Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install script for the Swift Community Apt Repository - https://swiftlang.xyz
#! /bin/bash
#
# Install script for the Swift Community Apt Repository - swiftlang.xyz
#
# check distribution version and infer distro with switch/case
## as all distribution versions have different names we can use this information to infer the distribution name.
check_ver () {
for var in "${SUPPORTED_VER[@]}"
do
if grep -q $var tmp_sources.txt;
then
vers=$var
case $vers in
jammy|focal|bionic) dist="ubuntu";;
bullseye|buster) dist="debian";;
esac
break
fi
done
}
## raspbian is an edge case
## check for raspbian
check_raspbian () {
if grep -q "raspbian" tmp_sources.txt;
then
dist="raspbian"
fi
}
## deepin is an edge case
## check for deepin
check_deepin () {
if grep -q "deepin" tmp_sources.txt;
then
dist="debian"
vers="buster"
fi
}
## check if dist/version is supported
check_supported () {
if [ $dist == "unsupported" ] || [ $vers == "unsupported" ]
then
echo sorry your system is unsupported.
echo
exit 0
fi
}
## user_input allows the selection of the available repository sections
user_input_all () {
# User Input
echo
echo "--------------- Choose Swift Release Version ---------------"
echo
echo " If you always want to have the latest release/stable version of Swift available choose [1], if not, choose the version most suitable for your project. "
echo " The latest release/stable version of Swift in this repository is currently 5.6.3 "
echo
echo "---------------------------------------------------------"
echo
echo " 1) latest/main - This will update to the latest release/stable version of Swift available"
echo " 2) Swift version 5.4.* - this will update to the latest point/patch release of Swift 5.4"
echo " 3) Swift version 5.5.* - this will update to the latest point/patch release of Swift 5.5"
echo " 4) Swift version 5.6.* - this will update to the latest point/patch release of Swift 5.6"
echo
echo "---------------------------------------------------------"
echo
read -r -p "Enter number [1/2/3/4] : " n < /dev/tty
echo
case $n in
1) main=" main"; echo "Installing the Swift main/latest repository"; echo;;
2) v5_4=" v5_4"; main=""; echo "Installing the Swift version 5.4.* repository"; echo;;
3) v5_5=" v5_5"; main=""; echo "Installing the Swift version 5.5.* repository"; echo;;
4) v5_6=" v5_6"; main=""; echo "Installing the Swift version 5.6.* repository"; echo;;
*) echo "Invalid option"; echo; exit 1;;
esac
}
## check gpg is installed
gpg_check () {
echo "Checking for gpg..."
if command -v gpg > /dev/null; then
echo "Detected gpg..."
else
echo -n "Installing gnupg for GPG verification... "
apt-get install -y gnupg &> /dev/null
echo "done."
if [ "$?" -ne "0" ]; then
echo "Unable to install GPG! Your base system has a problem; please check your default OS's package repositories because GPG should work."
echo "Repository installation aborted."
exit 1
fi
fi
}
install_debian_keyring () {
if [ "${dist}" = "debian" ]; then
echo "Installing debian-archive-keyring which is needed for installing "
echo "apt-transport-https on many Debian systems."
apt-get install -y debian-archive-keyring &> /dev/null
fi
}
file_check () {
if [ -f "$apt_source_path" ]
then
rm $apt_source_path
# remove old format list file
rm -f /etc/apt/sources.list.d/swiftlang.list
fi
}
main() {
# default repo values
v5_4=""
v5_5=""
v5_6=""
main=" main"
# default values
gpg_key="swiftlang_repo.gpg.key"
gpg_key_url="https://archive.swiftlang.xyz/swiftlang_repo.gpg.key"
apt_source_path="/etc/apt/sources.list.d/swiftlang-release.list"
# SUPPORTED_DIST=('ubuntu' 'debian' 'raspbian')
SUPPORTED_VER=('jammy' 'focal' 'bionic' 'bullseye' 'buster')
# default to unsupported
dist="unsupported"
vers="unsupported"
# ----
# run apt update
echo -n "running update... "
apt-get update > tmp_sources.txt
echo "done."
# scan tmp_sources.txt
echo -n "scanning sources... "
#check_dist
check_ver
check_deepin
check_raspbian
echo "done."
# clean tmp file
rm tmp_sources.txt
# check if system is supported - if not - exit
check_supported
# if ok continue with install
# ----
echo system detected is compatible with $dist/$vers
echo
# check prerequisists
gpg_check
install_debian_keyring
echo -n "Installing apt-transport-https... "
apt-get install -y apt-transport-https &> /dev/null
echo "done."
# import the gpg key
echo -n "Importing swiftlang gpg key... "
sudo rm /usr/share/keyrings/${gpg_key} &> /dev/null
curl -fsSL "${gpg_key_url}" | sudo gpg --dearmor -o /usr/share/keyrings/${gpg_key}
echo "done."
# user input
if [ "${dist}" != "raspbian" ];
then
case $vers in
bionic|focal|jammy|buster|bullseye) user_input_all;;
*) user_input_all;;
esac
fi
# add source list file
echo -n "Installing $apt_source_path... "
file_check
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/$gpg_key] https://archive.swiftlang.xyz/$dist/ $vers$v5_4$v5_5$v5_6$main" | sudo tee $apt_source_path > /dev/null
echo "done."
#
echo -n "Running apt-get update... "
# update apt on this system
apt-get update &> /dev/null
echo "done."
echo
echo "The repository is setup!"
echo "You can now install swift using 'sudo apt install swiftlang' "
echo
}
main
@MaxDesiatov
Copy link

MaxDesiatov commented Aug 9, 2022

This makes sense, thanks!

@purpln
Copy link

purpln commented Sep 13, 2022

5.7 please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment