Skip to content

Commit 2f6e3d9

Browse files
author
Dimitar Kerezov
committed
Improve Windows setup script
Changes include: * Asking permission from user for every installation - this includes an `all` option for eliminating subsequent questions * Improved error handling - warning upon non-zero exit code * Initial check for latest .NET Framework installed - should be at least 4 * Check for correct Chocolatey installation * Fixed the syntax in docs header on invoking the setup script from the command prompt * Added example syntax in docs header for invoking the setup script from powershell itself
1 parent 1ce0dad commit 2f6e3d9

File tree

1 file changed

+64
-19
lines changed

1 file changed

+64
-19
lines changed

setup/native-script.ps1

+64-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,76 @@
11

22
# A PowerShell script to set up Windows machine for NativeScript development
3-
# To run it against the PRODUCTION branch (only one supported with self-elevation) use
4-
# @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1'))"
3+
# NOTE: The scripts requires at least a version 4.0 .NET framework installed
4+
# To run it inside a COMMAND PROMPT against the production branch (only one supported with self-elevation) use
5+
# @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1'))"
6+
# To run it inside a WINDOWS POWERSHELL console against the production branch (only one supported with self-elevation) use
7+
# iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1'))
8+
9+
# Check if latest .NET framework installed is at least 4
10+
$dotNetVersions = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse | Get-ItemProperty -name Version,Release -EA 0 | Where { $_.PSChildName -match '^(?!S)\p{L}'} | Select Version
11+
$latestDotNetVersion = $dotNetVersions.GetEnumerator() | Sort-Object Version | Select-Object -Last 1
12+
$latestDotNetMajorNumber = $latestDotNetVersion.Version.Split(".")[0]
13+
if ($latestDotNetMajorNumber -lt 4) {
14+
Write-Host -ForegroundColor Red "Running this script requires at least a version 4.0 .NET framework installed"
15+
if ((Read-Host "Do you want to open Microsoft's Download Center in your default browser(y/n)") -eq 'y') {
16+
Start-Process -FilePath "https://www.microsoft.com/en-us/download/search.aspx?q=.net%20framework&p=0&r=10&t=&s=Relevancy~Descending"
17+
}
18+
19+
exit 1
20+
}
521

622
# Self-elevate
723
$isElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")
824
if (-not $isElevated) {
9-
start-process -FilePath PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Verb Runas -Wait -Command "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1'))"
10-
exit 0
25+
start-process -FilePath PowerShell.exe -Verb Runas -Wait -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1'))"
26+
exit 0
27+
}
28+
29+
# Help with installing other dependencies
30+
$script:answer = ""
31+
function Install($programName, $message, $script, $shouldExit) {
32+
if ($script:answer -ne "a") {
33+
Write-Host -ForegroundColor Green "Allow the script to install $($programName)?"
34+
Write-Host "Tip: Note that if you type a you won't be prompted for subsequent installations"
35+
do {
36+
$script:answer = (Read-Host "(Y)es/(N)o/(A)ll").ToLower()
37+
} until ($script:answer -eq "y" -or $script:answer -eq "n" -or $script:answer -eq "a")
38+
39+
if ($script:answer -eq "n") {
40+
Write-Host -ForegroundColor Yellow "You have chosen not to install $($programName). Some features of NativeScript may not work correctly if you haven't already installed it"
41+
return
42+
}
43+
}
44+
45+
Write-Host $message
46+
Invoke-Expression($script)
47+
if ($LASTEXITCODE -ne 0) {
48+
Write-Host -ForegroundColor Yellow "WARNING: $($programName) not installed"
49+
}
1150
}
1251

52+
function Pause {
53+
Write-Host "Press any key to continue..."
54+
[void][System.Console]::ReadKey($true)
55+
}
56+
57+
# Actually installing all other dependencies
1358
# Install Chocolately
14-
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
59+
Install "Chocolately(It's mandatory for the rest of the script)" "Installing Chocolately" "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))"
1560

16-
# install dependenciess with Chocolately
61+
if ((Get-Command "cinst" -ErrorAction SilentlyContinue) -eq $null) {
62+
Write-Host -ForegroundColor Red "Chocolatey is not installed or not configured properly. Download it from https://chocolatey.org/, install, set it up and run this script again."
63+
Pause
64+
exit 1
65+
}
1766

18-
write-host "To ensure consistent environment, this script will re-install all NativeScript dependencies."
67+
# Install dependenciess with Chocolately
1968

20-
write-host -BackgroundColor Black -ForegroundColor Yellow "Installing Google Chrome (required to debug NativeScript apps)"
21-
cinst googlechrome --force --yes
69+
Install "Google Chrome" "Installing Google Chrome (required to debug NativeScript apps)" "cinst googlechrome --force --yes"
2270

23-
write-host -BackgroundColor Black -ForegroundColor Yellow "Installing Java Development Kit"
24-
cinst jdk8 --force --yes
71+
Install "Java Development Kit" "Installing Java Development Kit" "cinst jdk8 --force --yes"
2572

26-
write-host -BackgroundColor Black -ForegroundColor Yellow "Installing Android SDK"
27-
cinst android-sdk --force --yes
73+
Install "Android SDK" "Installing Android SDK" "cinst android-sdk --force --yes"
2874

2975
# setup android sdk
3076
echo yes | cmd /c "$env:localappdata\Android\android-sdk\tools\android" update sdk --filter "tools,platform-tools,android-23" --all --no-ui
@@ -33,17 +79,16 @@ echo yes | cmd /c "$env:localappdata\Android\android-sdk\tools\android" update s
3379
# setup environment
3480

3581
if (!$env:ANDROID_HOME) {
36-
[Environment]::SetEnvironmentVariable("ANDROID_HOME", "$env:localappdata\Android\android-sdk", "User")
37-
$env:ANDROID_HOME = "$env:localappdata\Android\android-sdk";
82+
[Environment]::SetEnvironmentVariable("ANDROID_HOME", "$env:localappdata\Android\android-sdk", "User")
83+
$env:ANDROID_HOME = "$env:localappdata\Android\android-sdk";
3884
}
3985

4086
if (!$env:JAVA_HOME) {
4187
$curVer = (Get-ItemProperty "HKLM:\SOFTWARE\JavaSoft\Java Development Kit").CurrentVersion
4288
$javaHome = (Get-ItemProperty "HKLM:\Software\JavaSoft\Java Development Kit\$curVer").JavaHome
4389
[Environment]::SetEnvironmentVariable("JAVA_HOME", $javaHome, "User")
44-
$env:JAVA_HOME = $javaHome;
90+
$env:JAVA_HOME = $javaHome;
4591
}
4692

47-
write-host -BackgroundColor Black -ForegroundColor Yellow "This script has modified your environment. You need to log off and log back on for the changes to take effect."
48-
Write-Host "Press any key to continue..."
49-
[void][System.Console]::ReadKey($true)
93+
Write-Host -ForegroundColor Green "This script has modified your environment. You need to log off and log back on for the changes to take effect."
94+
Pause

0 commit comments

Comments
 (0)