2017-05-05 13:01:04 +02:00
#!/bin/bash
#Companion code for the blog https://cloudywindows.com
#call this code direction from the web with:
2017-11-15 18:51:52 +01:00
#bash <(wget -O - https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/installpsh-osx.sh) ARGUMENTS
#bash <(curl -s https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/installpsh-osx.sh) <ARGUMENTS>
2017-05-05 13:01:04 +02:00
2019-05-11 00:34:27 +02:00
#Usage - if you do not have the ability to run scripts directly from the web,
2017-05-05 13:01:04 +02:00
# pull all files in this repo folder and execute, this script
# automatically prefers local copies of sub-scripts
#Completely automated install requires a root account or sudo with a password requirement
#Switches
2019-03-08 22:00:04 +01:00
# -includeide - installs vscode and vscode PowerShell extension (only relevant to machines with desktop environment)
# -interactivetesting - do a quick launch test of vscode (only relevant when used with -includeide)
2019-05-11 00:34:27 +02:00
# -preview - installs the latest preview release of PowerShell side-by-side with any existing production releases
2017-05-05 13:01:04 +02:00
#gitrepo paths are overrideable to run from your own fork or branch for testing or private distribution
VERSION = "1.1.2"
gitreposubpath = "PowerShell/PowerShell/master"
gitreposcriptroot = " https://raw.githubusercontent.com/ $gitreposubpath /tools "
thisinstallerdistro = osx
repobased = true
2017-07-21 23:20:51 +02:00
gitscriptname = "installpsh-osx.sh"
2018-06-28 20:46:58 +02:00
powershellpackageid = powershell
2017-05-05 13:01:04 +02:00
2019-05-11 00:34:27 +02:00
echo " *** PowerShell Development Environment Installer $VERSION for $thisinstallerdistro "
2017-05-05 13:01:04 +02:00
echo " *** Original script is at: $gitreposcriptroot / $gitscriptname "
2017-10-09 22:23:29 +02:00
echo " *** Arguments used: $* "
2017-05-05 13:01:04 +02:00
# Let's quit on interrupt of subcommands
trap '
trap - INT # restore default INT handler
echo "Interrupted"
kill -s INT " $$ "
' INT
#Verify The Installer Choice (for direct runs of this script)
lowercase( ) {
2019-03-08 22:00:04 +01:00
echo " $1 " | tr "[:upper:]" "[:lower:]"
2017-05-05 13:01:04 +02:00
}
2017-07-21 23:20:51 +02:00
2019-03-08 22:00:04 +01:00
OS = $( lowercase " $( uname) " )
2017-05-05 13:01:04 +02:00
if [ " ${ OS } " = = "windowsnt" ] ; then
OS = windows
DistroBasedOn = windows
elif [ " ${ OS } " = = "darwin" ] ; then
OS = osx
DistroBasedOn = osx
else
2019-03-08 22:00:04 +01:00
OS = $( uname)
2017-05-05 13:01:04 +02:00
if [ " ${ OS } " = = "SunOS" ] ; then
OS = solaris
DistroBasedOn = sunos
elif [ " ${ OS } " = = "AIX" ] ; then
DistroBasedOn = aix
elif [ " ${ OS } " = = "Linux" ] ; then
if [ -f /etc/redhat-release ] ; then
DistroBasedOn = 'redhat'
2019-06-21 22:36:11 +02:00
elif [ -f /etc/system-release ] ; then
DIST = $( sed s/\ release.*// < /etc/system-release)
if [ [ $DIST = = *"Amazon Linux" * ] ] ; then
DistroBasedOn = 'amazonlinux'
else
DistroBasedOn = 'redhat'
fi
2017-05-05 13:01:04 +02:00
elif [ -f /etc/SuSE-release ] ; then
DistroBasedOn = 'suse'
elif [ -f /etc/mandrake-release ] ; then
DistroBasedOn = 'mandrake'
elif [ -f /etc/debian_version ] ; then
DistroBasedOn = 'debian'
fi
if [ -f /etc/UnitedLinux-release ] ; then
2019-03-08 22:00:04 +01:00
DIST = " ${ DIST } [ $( ( tr "\n" ' ' | sed s/VERSION.*//) < /etc/UnitedLinux-release )] "
2017-05-05 13:01:04 +02:00
DistroBasedOn = unitedlinux
fi
2019-03-08 22:00:04 +01:00
OS = $( lowercase " $OS " )
DistroBasedOn = $( lowercase " $DistroBasedOn " )
2017-05-05 13:01:04 +02:00
fi
fi
if [ " $DistroBasedOn " != " $thisinstallerdistro " ] ; then
2019-03-08 22:00:04 +01:00
echo " *** This installer is only for $thisinstallerdistro and you are running $DistroBasedOn , please run \" $gitreposcriptroot \install-powershell.sh\" to see if your distro is supported AND to auto-select the appropriate installer if it is. "
exit 1
2017-05-05 13:01:04 +02:00
fi
## Check requirements and prerequisites
2019-05-11 00:34:27 +02:00
echo " *** Installing PowerShell for $DistroBasedOn ... "
2017-05-05 13:01:04 +02:00
2018-06-28 20:46:58 +02:00
if [ [ " ' $* ' " = ~ preview ] ] ; then
2018-06-25 20:44:43 +02:00
echo
2018-06-28 20:46:58 +02:00
echo "-preview was used, the latest preview release will be installed (side-by-side with your production release)"
powershellpackageid = powershell-preview
2018-06-25 20:44:43 +02:00
fi
2017-05-05 13:01:04 +02:00
if ! hash brew 2>/dev/null; then
echo "Homebrew is not found, installing..."
2017-07-21 23:20:51 +02:00
ruby -e " $( curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install) " < /dev/null 2> /dev/null
else
echo "Howebrew is already installed, skipping..."
2017-05-05 13:01:04 +02:00
fi
2017-07-21 23:20:51 +02:00
if ! hash brew 2>/dev/null; then
2017-05-05 13:01:04 +02:00
echo "ERROR: brew did not install correctly, exiting..." >& 2
exit 3
fi
2019-06-20 20:08:32 +02:00
# Suppress output, it's very noisy on Azure DevOps
2017-10-09 22:23:29 +02:00
echo "Refreshing Homebrew cache..."
2017-11-13 21:21:53 +01:00
for count in { 1..2} ; do
# Try the update twice if the first time fails
brew update > /dev/null && break
# If the update fails again after increasing the Git buffer size, exit with error.
if [ [ $count = = 2 ] ] ; then
echo "ERROR: Refreshing Homebrew cache failed..." >& 2
exit 2
fi
# The update failed for the first try. An error we see a lot in our CI is "RPC failed; curl 56 SSLRead() return error -36".
# What 'brew update' does is to fetch the newest version of Homebrew from GitHub using git, and the error comes from git.
# A potential solution is to increase the Git buffer size to a larger number, say 150 mb. The default buffer size is 1 mb.
echo "First attempt of update failed. Increase Git buffer size and try again ..."
git config --global http.postBuffer 157286400
sleep 5
done
2017-10-09 22:23:29 +02:00
2017-10-25 17:25:35 +02:00
if ! hash pwsh 2>/dev/null; then
2017-07-21 23:20:51 +02:00
echo "Installing PowerShell..."
2018-06-28 20:46:58 +02:00
if ! brew cask install ${ powershellpackageid } ; then
2017-07-21 23:20:51 +02:00
echo "ERROR: PowerShell failed to install! Cannot install powershell..." >& 2
2017-05-05 13:01:04 +02:00
fi
else
2017-07-21 23:20:51 +02:00
echo "PowerShell is already installed, skipping..."
2017-05-05 13:01:04 +02:00
fi
if [ [ " ' $* ' " = ~ includeide ] ] ; then
2017-10-09 22:23:29 +02:00
echo "*** Installing VS Code PowerShell IDE..."
2017-05-05 13:01:04 +02:00
if [ [ ! -d $( brew --prefix visual-studio-code) ] ] ; then
2017-08-22 21:10:00 +02:00
if ! brew cask install visual-studio-code; then
2017-05-05 13:01:04 +02:00
echo "ERROR: Visual Studio Code failed to install..." >& 2
exit 1
fi
else
2017-07-21 23:20:51 +02:00
brew upgrade visual-studio-code
2017-05-05 13:01:04 +02:00
fi
2017-10-09 22:23:29 +02:00
echo "*** Installing VS Code PowerShell Extension"
2017-07-21 23:20:51 +02:00
code --install-extension ms-vscode.PowerShell
2018-06-25 20:44:43 +02:00
if [ [ " ' $* ' " = ~ -interactivetesting ] ] ; then
echo "*** Loading test code in VS Code"
curl -O ./testpowershell.ps1 https://raw.githubusercontent.com/DarwinJS/CloudyWindowsAutomationCode/master/pshcoredevenv/testpowershell.ps1
code ./testpowershell.ps1
fi
2017-05-05 13:01:04 +02:00
fi
2019-03-08 22:00:04 +01:00
# shellcheck disable=SC2016
2017-11-03 16:47:26 +01:00
pwsh -noprofile -c ' " Congratulations! PowerShell is installed at $PSHOME .
Run ` "pwsh`" to start a PowerShell session." '
2017-05-05 13:01:04 +02:00
success = $?
if [ [ " $success " != 0 ] ] ; then
echo "ERROR: PowerShell failed to install!" >& 2
exit " $success "
fi
if [ [ " $repobased " = = true ] ] ; then
2019-05-11 00:34:27 +02:00
echo "*** NOTE: Run your regular package manager update cycle to update PowerShell"
2017-05-05 13:01:04 +02:00
fi
2017-07-21 23:20:51 +02:00
echo "*** Install Complete"