Skip to content

Instantly share code, notes, and snippets.

@dive
Last active May 23, 2021 14:02
Show Gist options
  • Save dive/c4a51179aa96d229a32dd3492e5fdc2d to your computer and use it in GitHub Desktop.
Save dive/c4a51179aa96d229a32dd3492e5fdc2d to your computer and use it in GitHub Desktop.
MacPorts installation script
#!/bin/bash
set -euf -o pipefail
# FIXME: Check '/opt/local/bin/port' directly if MacPorts is not in the PATH?
if command -v port &> /dev/null; then
printf "%s\n%s\n%s\n" \
"MacPorts is already installed." \
"Consider to use 'sudo port selfupdate' command for upgrades." \
"Check the https://guide.macports.org/#installing.macports.uninstalling to uninstall MacPorts before reinstalling."
exit 0
fi
TEMP_PKG="$(mktemp -t macports).pkg"
_MACPORTS_LATEST_VERSION="$(curl --silent --url https://raw.githubusercontent.com/macports/macports-base/master/config/RELEASE_URL)"
MACPORTS_LATEST_VERSION="${_MACPORTS_LATEST_VERSION##*/v}"
MACOS_VERSION="$(sw_vers -productVersion)"
MACOS_PRODUCT_VERSION="$(cut -d '.' -f 1 <<< "${MACOS_VERSION}")"
# FIXME: There is no reliable way to detect the macOS codename. We can hardcode them or get rid of them
# https://support-sp.apple.com/sp/product?edid=${MACOS_VERSION} does not work as expected and not reliable too for detecting code names
MACPORTS_DOWNLOAD_URL="https://distfiles.macports.org/MacPorts/MacPorts-${MACPORTS_LATEST_VERSION}-${MACOS_PRODUCT_VERSION}-BigSur.pkg"
trap cleanup SIGINT SIGTERM ERR EXIT
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
rm -Rf "${TEMP_PKG}"
}
# Debug #
printf "Temp Path: %s\nMacPorts version: %s\nmacOS version: %s (%s)\nDownload URL: %s\n" \
"$TEMP_PKG" \
"$MACPORTS_LATEST_VERSION" \
"$MACOS_VERSION" \
"$MACOS_PRODUCT_VERSION" \
"$MACPORTS_DOWNLOAD_URL"
# End of Debug #
curl --output "${TEMP_PKG}" --remote-name "${MACPORTS_DOWNLOAD_URL}"
sudo installer -verbose -package "${TEMP_PKG}" -target /
@dive
Copy link
Author

dive commented May 23, 2021

From the discussion (macports-dev -- MacPorts developer discussion).

You can use the following command to check the script (runs the latest revision):

/bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/dive/c4a51179aa96d229a32dd3492e5fdc2d/raw/install_macports.sh)"

So far, I use the following piece of code to install MacPorts and the packages I need:

macports () {
    /bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/dive/c4a51179aa96d229a32dd3492e5fdc2d/raw/install_macports.sh)"
    PORTS=( git gh fzf ripgrep shfmt shellcheck speedtest-cli glances fswatch aria2 \
            coreutils cmake carthage xcodes rbenv )
    for PORT in "${PORTS[@]}"; do
        sudo port -N -q install "$PORT"
    done
}

Todo

  • Check that port is already installed and suggest using the sudo port -v selfupdate command or uninstall first
  • macOS code-names have to be normalised/removed from the download URLs
  • Run sudo port selfupdate after the installation?
  • Quick actions to update the $PATH for shells?

Problems

  • PATH was not modified as expected for zsh

Examples

Installation example with the -store option for the installer:

% sh install.sh
Temp Path: /var/folders/gn/8j0pyrwj5j3ggxprkczkpwtc0000gn/T/macports.nziqozsB.pkg
MacPorts version: 2.7.0
macOS version: 11.3.1 (11)

installer: Note: running installer as an admin user (instead of root) gives better Mac App Store fidelity
installer: Warning: macports.nziqozsB.pkg was signed with a certificate that is not valid for store submission.
installer: Installation Check: Passed
installer: Volume Check: Passed
installer: Starting install
installer: Install 0.0% complete
installer: Install 10.1% complete
installer: Install 62.4% complete
installer: Install 100.0% complete
installer: Finished install

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