1+ #! /usr/bin/env bash
2+
3+ # Let's quit on interrupt of subcommands
4+ trap '
5+ trap - INT # restore default INT handler
6+ echo "Interrupted"
7+ kill -s INT "$$"
8+ ' INT
9+
10+ get_url () {
11+ fork=$2
12+ release=v6.0.0-beta.1
13+ echo " https://github.com/$fork /PowerShell/releases/download/$release /$1 "
14+ }
15+
16+ fork=" PowerShell"
17+ # Get OS specific asset ID and package name
18+ case " $OSTYPE " in
19+ linux* )
20+ source /etc/os-release
21+ # Install curl and wget to download package
22+ case " $ID " in
23+ centos* )
24+ if ! hash curl 2> /dev/null; then
25+ echo " curl not found, installing..."
26+ sudo yum install -y curl
27+ fi
28+
29+ package=powershell-6.0.0_beta.1-1.el7.centos.x86_64.rpm
30+ ;;
31+ ubuntu)
32+ if ! hash curl 2> /dev/null; then
33+ echo " curl not found, installing..."
34+ sudo apt-get install -y curl
35+ fi
36+
37+ case " $VERSION_ID " in
38+ 14.04)
39+ package=powershell_6.0.0-beta.1-1ubuntu1.14.04.1_amd64.deb
40+ ;;
41+ 16.04)
42+ package=powershell_6.0.0-beta.1-1ubuntu1.16.04.1_amd64.deb
43+ ;;
44+ * )
45+ echo " Ubuntu $VERSION_ID is not supported!" >&2
46+ exit 2
47+ esac
48+ ;;
49+ opensuse)
50+ if ! hash curl 2> /dev/null; then
51+ echo " curl not found, installing..."
52+ sudo zypper install -y curl
53+ fi
54+
55+
56+ case " $VERSION_ID " in
57+ 42.1)
58+ # TODO during next release remove fork and fix package name
59+ fork=TravisEz13
60+ package=powershell-6.0.0_beta.1-1.suse.42.1.x86_64.rpm
61+ ;;
62+ * )
63+ echo " OpenSUSE $VERSION_ID is not supported!" >&2
64+ exit 2
65+ esac
66+ ;;
67+ * )
68+ echo " $NAME is not supported!" >&2
69+ exit 2
70+ esac
71+ ;;
72+ darwin* )
73+ # We don't check for curl as macOS should have a system version
74+ package=powershell-6.0.0-beta.1-osx.10.12-x64.pkg
75+ ;;
76+ * )
77+ echo " $OSTYPE is not supported!" >&2
78+ exit 2
79+ ;;
80+ esac
81+
82+ curl -L -o " $package " $( get_url " $package " " $fork " )
83+
84+ if [[ ! -r " $package " ]]; then
85+ echo " ERROR: $package failed to download! Aborting..." >&2
86+ exit 1
87+ fi
88+
89+ # Installs PowerShell package
90+ case " $OSTYPE " in
91+ linux* )
92+ source /etc/os-release
93+ # Install dependencies
94+ echo " Installing PowerShell with sudo..."
95+ case " $ID " in
96+ centos)
97+ # yum automatically resolves dependencies for local packages
98+ sudo yum install " ./$package "
99+ ;;
100+ ubuntu)
101+ # dpkg does not automatically resolve dependencies, but spouts ugly errors
102+ sudo dpkg -i " ./$package " & > /dev/null
103+ # Resolve dependencies
104+ sudo apt-get install -f
105+ ;;
106+ opensuse)
107+ # Install the Microsoft public key so that zypper trusts the package
108+ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
109+ # zypper automatically resolves dependencies for local packages
110+ sudo zypper --non-interactive install " ./$package " & > /dev/null
111+ ;;
112+ * )
113+ esac
114+ ;;
115+ darwin* )
116+ patched=0
117+ if hash brew 2> /dev/null; then
118+ brew update
119+ if [[ ! -d $( brew --prefix openssl) ]]; then
120+ echo " Installing OpenSSL with brew..."
121+ if ! brew install openssl; then
122+ echo " ERROR: OpenSSL failed to install! Crypto functions will not work..." >&2
123+ # Don't abort because it is not fatal
124+ elif ! brew install curl --with-openssl; then
125+ echo " ERROR: curl failed to build against OpenSSL; SSL functions will not work..." >&2
126+ # Still not fatal
127+ else
128+ # OpenSSL installation succeeded; reme mber to patch System.Net.Http after PowerShell installation
129+ patched=1
130+ fi
131+ fi
132+
133+ else
134+ echo " ERROR: brew not found! OpenSSL may not be available..." >&2
135+ # Don't abort because it is not fatal
136+ fi
137+
138+ echo " Installing $package with sudo ..."
139+ sudo installer -pkg " ./$package " -target /
140+ if [[ $patched -eq 1 ]]; then
141+ echo " Patching System.Net.Http for libcurl and OpenSSL..."
142+ find /usr/local/microsoft/powershell -name System.Net.Http.Native.dylib | xargs sudo install_name_tool -change /usr/lib/libcurl.4.dylib /usr/local/opt/curl/lib/libcurl.4.dylib
143+ fi
144+ ;;
145+ esac
146+
147+ powershell -noprofile -c ' "Congratulations! PowerShell is installed at $PSHOME"'
148+ success=$?
149+
150+ if [[ " $success " != 0 ]]; then
151+ echo " ERROR: PowerShell failed to install!" >&2
152+ exit " $success "
153+ fi
0 commit comments