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
kinetic|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
}
check_arch () {
arch=$(dpkg --print-architecture)
}
## 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],"
echo " or you can choose the Swift version most suitable for your project."
echo " The latest release/stable version of Swift in this repository is currently 5.7 "
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
}
user_input_armhf () {
# 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],"
echo " or you can choose the Swift version most suitable for your project."
echo " The latest release/stable version of Swift in this repository is currently 5.7 "
echo
echo "---------------------------------------------------------"
echo
echo " 1) latest/main - This will update to the latest release/stable version of Swift available"
echo " 2) Swift version 5.1.* - this will update to the latest point/patch release of Swift 5.1.5"
echo
echo "---------------------------------------------------------"
echo
read -r -p "Enter number [1/2] : " n < /dev/tty
echo
case $n in
1) main=" main"; echo "Installing the Swift main/latest repository"; echo;;
2) v5_1=" v5_1"; main=""; echo "Installing the Swift version 5.1.* 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_1=""
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=('kinetic' '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_arch
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 [ "${arch}" == "armhf" ];
then
case $vers in
bionic|focal|jammy|buster|bullseye) user_input_armhf;;
*) user_input_armhf;;
esac
elif [ "${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_1$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
echo
echo "+--------------------------------- ❤️ ---------------------------------+"
echo "│ Please help support this repository at 'https://ko-fi.com/futurejones' │"
echo "+------------------------------------------------------------------------+"
echo
}
main
@MaxDesiatov
Copy link

MaxDesiatov commented Aug 9, 2022

@futurejones I'm trying to use this script (or at least what's served at https://archive.swiftlang.xyz/install.sh) on CI, and the requirement of user input makes it fail, see this log for example https://github.com/swiftwasm/carton/runs/7747219715?check_suite_focus=true. Is there any way to make this non-interactive? Thanks!

@futurejones
Copy link
Author

futurejones commented Aug 9, 2022

@MaxDesiatov you will need to change from this script to a manual install of the repo.
In your workflow script change -

curl -s https://archive.swiftlang.xyz/install.sh | $sudoCommand bash

to this -

# install dependencies
export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q update && \
apt-get -q install -y \
ca-certificates \
curl \
gnupg

# add repo key
curl -fsSL https://archive.swiftlang.xyz/swiftlang_repo.gpg.key | gpg --dearmor -o /usr/share/keyrings/swiftlang_repo.gpg.key

# add repo to sources.list
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/swiftlang_repo.gpg.key] https://archive.swiftlang.xyz/ubuntu jammy main" | tee /etc/apt/sources.list.d/swiftlang.list > /dev/null

# install swift
export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q update && \
apt-get -q install -y \
swiftlang

you may need to add sudo to some of these commands.

@MaxDesiatov
Copy link

MaxDesiatov commented Aug 9, 2022

This makes sense, thanks!

@purpln
Copy link

purpln commented Sep 13, 2022

5.7 please

@diegotl
Copy link

diegotl commented Nov 14, 2022

Looks like archive.swiftlang.xyz is down since at least one day

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