Merge pull request #7657 from dotnet-bot/from-tfs
[platform/upstream/coreclr.git] / init-tools.sh
1 #!/usr/bin/env bash
2
3 __scriptpath=$(cd "$(dirname "$0")"; pwd -P)
4
5 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to set.
6 # This is needed by CLI to function.
7 if [ -z "$HOME" ]; then
8     if [ ! -d "$__scriptpath/temp_home" ]; then
9         mkdir temp_home
10     fi
11     export HOME=$__scriptpath/temp_home
12     echo "HOME not defined; setting it to $HOME"
13 fi
14
15 __PACKAGES_DIR=$__scriptpath/packages
16 __TOOLRUNTIME_DIR=$__scriptpath/Tools
17 __DOTNET_PATH=$__TOOLRUNTIME_DIR/dotnetcli
18 __DOTNET_CMD=$__DOTNET_PATH/dotnet
19 if [ -z "$__BUILDTOOLS_SOURCE" ]; then __BUILDTOOLS_SOURCE=https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json; fi
20 __BUILD_TOOLS_PACKAGE_VERSION=$(cat $__scriptpath/BuildToolsVersion.txt)
21 __DOTNET_TOOLS_VERSION=$(cat $__scriptpath/DotnetCLIVersion.txt)
22 __BUILD_TOOLS_PATH=$__PACKAGES_DIR/Microsoft.DotNet.BuildTools/$__BUILD_TOOLS_PACKAGE_VERSION/lib
23 __PROJECT_JSON_PATH=$__TOOLRUNTIME_DIR/$__BUILD_TOOLS_PACKAGE_VERSION
24 __PROJECT_JSON_FILE=$__PROJECT_JSON_PATH/project.json
25 __PROJECT_JSON_CONTENTS="{ \"dependencies\": { \"Microsoft.DotNet.BuildTools\": \"$__BUILD_TOOLS_PACKAGE_VERSION\" }, \"frameworks\": { \"dnxcore50\": { } } }"
26
27 # Extended version of platform detection logic from dotnet/cli/scripts/obtain/dotnet-install.sh 16692fc
28 get_current_linux_name() {
29     # Detect Distro
30     if [ "$(cat /etc/*-release | grep -cim1 ubuntu)" -eq 1 ]; then
31         if [ "$(cat /etc/*-release | grep -cim1 16.04)" -eq 1 ]; then
32             echo "ubuntu.16.04"
33             return 0
34         fi
35         if [ "$(cat /etc/*-release | grep -cim1 16.10)" -eq 1 ]; then
36             echo "ubuntu.16.10"
37             return 0
38         fi
39
40         echo "ubuntu"
41         return 0
42     elif [ "$(cat /etc/*-release | grep -cim1 centos)" -eq 1 ]; then
43         echo "centos"
44         return 0
45     elif [ "$(cat /etc/*-release | grep -cim1 rhel)" -eq 1 ]; then
46         echo "rhel"
47         return 0
48     elif [ "$(cat /etc/*-release | grep -cim1 debian)" -eq 1 ]; then
49         echo "debian"
50         return 0
51     elif [ "$(cat /etc/*-release | grep -cim1 alpine)" -eq 1 ]; then
52         echo "alpine"
53         return 0
54     elif [ "$(cat /etc/*-release | grep -cim1 fedora)" -eq 1 ]; then
55         if [ "$(cat /etc/*-release | grep -cim1 23)" -eq 1 ]; then
56             echo "fedora.23"
57             return 0
58         fi
59         if [ "$(cat /etc/*-release | grep -cim1 24)" -eq 1 ]; then
60             echo "fedora.24"
61             return 0
62         fi
63     elif [ "$(cat /etc/*-release | grep -cim1 opensuse)" -eq 1 ]; then
64         if [ "$(cat /etc/*-release | grep -cim1 13.2)" -eq 1 ]; then
65             echo "opensuse.13.2"
66             return 0
67         fi
68         if [ "$(cat /etc/*-release | grep -cim1 42.1)" -eq 1 ]; then
69             echo "opensuse.42.1"
70             return 0
71         fi
72     fi
73
74     # Cannot determine Linux distribution, assuming Ubuntu 14.04.
75     echo "ubuntu"
76     return 0
77 }
78
79 if [ -z "$__DOTNET_PKG" ]; then
80 OSName=$(uname -s)
81     case $OSName in
82         Darwin)
83             OS=OSX
84             __DOTNET_PKG=dotnet-dev-osx-x64
85             ulimit -n 2048
86             ;;
87
88         Linux)
89             __DOTNET_PKG="dotnet-dev-$(get_current_linux_name)-x64"
90             OS=Linux
91             ;;
92
93         *)
94             echo "Unsupported OS '$OSName' detected. Downloading ubuntu-x64 tools."
95             OS=Linux
96             __DOTNET_PKG=dotnet-dev-ubuntu-x64
97             ;;
98   esac
99 fi
100
101 __CLIDownloadURL=https://dotnetcli.blob.core.windows.net/dotnet/Sdk/${__DOTNET_TOOLS_VERSION}/${__DOTNET_PKG}.${__DOTNET_TOOLS_VERSION}.tar.gz
102 echo ".NET CLI will be downloaded from $__CLIDownloadURL"
103 echo "Locating $__PROJECT_JSON_FILE to see if we already downloaded .NET CLI tools..." 
104
105 if [ ! -e $__PROJECT_JSON_FILE ]; then
106     echo "$__PROJECT_JSON_FILE not found. Proceeding to download .NET CLI tools. " 
107     if [ -e $__TOOLRUNTIME_DIR ]; then rm -rf -- $__TOOLRUNTIME_DIR; fi
108
109     if [ ! -e $__DOTNET_PATH ]; then
110         # curl has HTTPS CA trust-issues less often than wget, so lets try that first.
111         which curl > /dev/null 2> /dev/null
112         if [ $? -ne 0 ]; then
113           mkdir -p "$__DOTNET_PATH"
114           wget -q -O $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL
115           echo "wget -q -O $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL"
116         else
117           curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL
118           echo "curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL"
119         fi
120         cd $__DOTNET_PATH
121         tar -xf $__DOTNET_PATH/dotnet.tar
122         if [ -n "$BUILDTOOLS_OVERRIDE_RUNTIME" ]; then
123             find $__DOTNET_PATH -name *.ni.* | xargs rm 2>/dev/null
124             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/bin
125             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/bin/dnx
126             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/runtime/coreclr
127         fi
128
129         cd $__scriptpath
130     fi
131
132     mkdir "$__PROJECT_JSON_PATH"
133     echo $__PROJECT_JSON_CONTENTS > "$__PROJECT_JSON_FILE"
134
135     if [ ! -e $__BUILD_TOOLS_PATH ]; then
136         $__DOTNET_CMD restore "$__PROJECT_JSON_FILE" --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE
137     fi
138
139     # On ubuntu 14.04, /bin/sh (symbolic link) calls /bin/dash by default.
140     $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR
141
142     cp $__TOOLRUNTIME_DIR/NuGet.Frameworks.dll $__TOOLRUNTIME_DIR/dotnetcli/sdk/$__DOTNET_TOOLS_VERSION
143
144 else
145     echo "$__PROJECT_JSON_FILE found. Skipping .NET CLI installation."   
146 fi