Automating dotnet core SDK updates on Mac
June 17, 2020 Leave a comment
I really enjoy working with dotnet core. It is fast, open source, and cross-platform. My preference these days for working with the .NET stack is to build dotnet core apps natively on Mac with SQL Server or PostgreSQL on Docker for Mac. We can then easily deploy Docker containers or in some cases dotnet core on an actual Debian or RHEL server with Nginx. ASP.NET 4.x still only runs on Windows Server and for that I use VMWare Fusion and deploy with Kudu.
On Linux, Microsoft provides package manager repos to maintain the dotnet core SDK, which is awesome. Microsoft also publishes a menu of Docker containers to build and run dotnet core apps. On Windows, the Visual Studio updater will install dotnet core updates for you. On Mac the same is true of Visual Studio for Mac.
But I have no particular use for Visual Studio for Mac. I use VS Code and vim. I don’t need to have Visual Studio for Mac just as a glorified package manager and I simply don’t like having things installed that I do not use. There may be a Homebrew way to manage the dotnet core SDKs but I’m not a Homebrew kind of guy and I put no effort into researching this.
Fortunately Microsoft has provided a couple of useful scripts dotnet-uninstall-pkgs.sh and dotnet-install.sh. It’s a pretty straightforward thing to script these together to maintain the LTS dotnet core SDKs.
sudo dotnet-upgrade-sdks
Problems solved. Now I need to figure out a solution to maintain mono since the Omnisharp intellisense engine in VS Code depends on mono.
Script to upgrade dotnet core LTS SDKs
#!/bin/sh | |
# Get the MSFT uninstall script from GitHub: | |
# | |
# curl -sSL https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/uninstall/dotnet-uninstall-pkgs.sh | sudo tee /usr/local/bin/dotnet-uninstall-pkgs > /dev/null | |
# sudo chmod +x /usr/local/bin/dotnet-uninstall-pkgs | |
# | |
uninstall_cmd=dotnet-uninstall-pkgs | |
# MSFT install script documented on docs.microsoft.com | |
# https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script | |
# | |
# curl -sSL https://dot.net/v1/dotnet-install.sh | sudo tee /usr/local/bin/dotnet-install > /dev/null | |
# chmod +x /usr/local/bin/dotnet-install | |
# | |
install_cmd=dotnet-install | |
# also set within dotnet-uninstall-pkgs.sh | |
dotnet_install_root="/usr/local/share/dotnet" | |
dotnet_path_file="/etc/paths.d/dotnet" | |
dotnet_tool_path_file="/etc/paths.d/dotnet-cli-tools" | |
current_userid=$(id -u) | |
if [ $current_userid -ne 0 ]; then | |
echo "$(basename "$0") requires superuser privileges to run" >&2 | |
exit 1 | |
fi | |
${uninstall_cmd} | |
${install_cmd} --install-dir ${dotnet_install_root} --no-path --channel 2.1 # old LTS Channel | |
${install_cmd} --install-dir ${dotnet_install_root} --no-path --channel LTS # current LTS Channel | |
echo adding dotnet and tools to path. | |
# created by pkg installers but not the install script | |
echo ${dotnet_install_root} > ${dotnet_path_file} | |
echo "~/.dotnet/tools" > ${dotnet_tool_path_file} | |
eval $(/usr/libexec/path_helper -s) |
Install the script with it’s dependencies
#!/bin/sh | |
curl -sSL https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/uninstall/dotnet-uninstall-pkgs.sh \ | |
| sudo tee /usr/local/bin/dotnet-uninstall-pkgs > /dev/null | |
sudo chmod +x /usr/local/bin/dotnet-uninstall-pkgs | |
curl -sSL https://dot.net/v1/dotnet-install.sh \ | |
| sudo tee /usr/local/bin/dotnet-install > /dev/null | |
chmod +x /usr/local/bin/dotnet-install | |
curl -sSL https://gist.github.com/breiter/aef0c0acbeb24cabe0fa16c7ecfdb88c/raw/b4e9de4b20141b0a05aadd03d5842752104b1475/dotnet-upgrade-sdks.sh \ | |
| sudo tee /usr/local/bin/dotnet-upgrade-sdks > /dev/null | |
chmod +x /usr/local/bin/dotnet-upgrade-sdks |