Skip to content

Commit 71e65f3

Browse files
committed
Merge pull request #1616 from NativeScript/kerezov/setup-scripts
Improve Setup Scripts
2 parents f8069a1 + ab2e132 commit 71e65f3

File tree

4 files changed

+172
-41
lines changed

4 files changed

+172
-41
lines changed

lib/services/doctor-service.ts

+38
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
import {EOL} from "os";
44
import * as semver from "semver";
55
import * as path from "path";
6+
import * as helpers from "../common/helpers";
67
let clui = require("clui");
78

89
class DoctorService implements IDoctorService {
910
private static MIN_SUPPORTED_POD_VERSION = "0.38.2";
11+
private static DarwinSetupScriptLocation = path.join(__dirname, "..", "..", "setup", "mac-startup-shell-script.sh");
12+
private static DarwinSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-os-x";
13+
private static WindowsSetupScriptExecutable = "powershell.exe";
14+
private static WindowsSetupScriptArguments = ["start-process", "-FilePath", "PowerShell.exe", "-NoNewWindow", "-Wait", "-ArgumentList", '"-NoProfile -ExecutionPolicy Bypass -Command iex ((new-object net.webclient).DownloadString(\'https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1\'))"'];
15+
private static WindowsSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-win";
16+
private static LinuxSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-linux";
1017

1118
constructor(private $analyticsService: IAnalyticsService,
1219
private $androidToolsInfo: IAndroidToolsInfo,
@@ -17,6 +24,8 @@ class DoctorService implements IDoctorService {
1724
private $childProcess: IChildProcess,
1825
private $config: IConfiguration,
1926
private $npm: INodePackageManager,
27+
private $opener: IOpener,
28+
private $prompter: IPrompter,
2029
private $fs: IFileSystem) { }
2130

2231
public printWarnings(configOptions?: { trackResult: boolean }): boolean {
@@ -95,9 +104,38 @@ class DoctorService implements IDoctorService {
95104
this.$analyticsService.track("DoctorEnvironmentSetup", doctorResult ? "incorrect" : "correct").wait();
96105
}
97106

107+
if(doctorResult) {
108+
this.$logger.info("There seem to be issues with your configuration.");
109+
if (this.$hostInfo.isDarwin) {
110+
this.promptForHelp(DoctorService.DarwinSetupDocsLink, DoctorService.DarwinSetupScriptLocation, []).wait();
111+
} else if (this.$hostInfo.isWindows) {
112+
this.promptForHelp(DoctorService.WindowsSetupDocsLink, DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments).wait();
113+
} else {
114+
this.promptForDocs(DoctorService.LinuxSetupDocsLink).wait();
115+
}
116+
}
117+
98118
return doctorResult;
99119
}
100120

121+
private promptForDocs(link: string): IFuture<void> {
122+
return (() => {
123+
if (this.$prompter.confirm("Do you want to visit the official documentation?", () => helpers.isInteractive()).wait()) {
124+
this.$opener.open(link);
125+
}
126+
}).future<void>()();
127+
}
128+
129+
private promptForHelp(link: string, commandName: string, commandArguments: string[]): IFuture<void> {
130+
return (() => {
131+
this.promptForDocs(link).wait();
132+
133+
if (this.$prompter.confirm("Do you want to run the setup script?", () => helpers.isInteractive()).wait()) {
134+
this.$childProcess.spawnFromEvent(commandName, commandArguments, "close", { stdio: "inherit" }).wait();
135+
}
136+
}).future<void>()();
137+
}
138+
101139
private printPackageManagerTip() {
102140
if (this.$hostInfo.isWindows) {
103141
this.$logger.out("TIP: To avoid setting up the necessary environment variables, you can use the chocolatey package manager to install the Android SDK and its dependencies." + EOL);

setup/mac-startup-shell-script.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.rb)"

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 "To run this script, you need .NET 4.0 or later installed"
15+
if ((Read-Host "Do you want to open Microsoft Download Center (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

setup/native-script.rb

+68-22
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# coding: utf-8
22

33
# A script to setup developer's workstation for developing with NativeScript
4-
# To run it against PRODUCTION branch (recommended) use
5-
# ruby -e "$(curl -fsSL https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.rb)"
6-
# To run it against MASTER branch (usually only developers of NativeScript need to) use
7-
# ruby -e "$(curl -fsSL https://raw.githubusercontent.com/NativeScript/nativescript-cli/master/setup/native-script.rb)"
4+
# To run it against PRODUCTION branch (only one supported with self-elevation) use
5+
# sudo ruby -e "$(curl -fsSL https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.rb)"
86

97
# Only the user can manually download and install Xcode from App Store
8+
unless Process.uid == 0
9+
# Execute as root
10+
puts "This scripts needs sudo permissions"
11+
exec('sudo ruby -e "$(curl -fsSL https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.rb)"')
12+
end
13+
1014
puts "NativeScript requires Xcode."
1115
puts "If you do not have Xcode installed, download and install it from App Store and run it once to complete its setup."
1216
puts "Do you have Xcode installed? (y/n)"
1317

1418
xcode = gets.chomp
1519

16-
if xcode == "n" || xcode == "N"
20+
if xcode.downcase == "n"
1721
exit
1822
end
1923

@@ -22,33 +26,75 @@
2226
exit
2327
end
2428

25-
puts "You need to accept the Xcode license agreement to be able to use the Xcode command-line tools. (You might need to provide your password.)"
26-
system('sudo xcodebuild -license')
29+
puts "You need to accept the Xcode license agreement to be able to use the Xcode command-line tools."
30+
system('xcodebuild -license')
31+
32+
# Help with installing other dependencies
33+
$answer = ""
34+
35+
def execute(script, warning_message, run_as_root = false)
36+
if run_as_root
37+
result = system(script)
38+
else
39+
result = system("sudo su " + ENV['SUDO_USER'] + " -c '" + script + "'")
40+
end
41+
42+
if result.nil?
43+
STDERR.puts "ERROR: " + script + " execution FAILED"
44+
exit 1
45+
end
46+
47+
unless result
48+
STDERR.puts "WARNING: " + warning_message
49+
end
50+
end
2751

28-
# Install all other dependencies
29-
puts "Installing Homebrew... (You might need to provide your password.)"
30-
system('ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"')
52+
def install(program_name, message, script, run_as_root = false, show_all_option = true)
53+
if $answer != "a"
54+
puts "Allow the script to install " + program_name + "?"
55+
if show_all_option
56+
puts "Note that if you type all you won't be prompted for subsequent installations"
57+
end
58+
59+
loop do
60+
puts show_all_option ? "(Y)es/(N)o/(A)ll" : "(Y)es/(N)o"
61+
$answer = gets.chomp.downcase
62+
is_answer_yn = $answer == "y" || $answer == "n"
63+
break if show_all_option ? is_answer_yn || $answer == "a" : is_answer_yn
64+
end
65+
66+
if $answer == "n"
67+
puts "You have chosen not to install " + program_name + ". Some features of NativeScript may not work correctly if you haven't already installed it"
68+
return
69+
end
70+
end
71+
72+
puts message
73+
execute(script, program_name + " not installed", run_as_root)
74+
end
75+
76+
# Actually installing all other dependencies
77+
install("Homebrew", "Installing Homebrew...", 'ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</dev/null', false, false)
3178

3279
if !(`brew --version`.include? "git revision")
3380
puts "Homebrew is not installed or not configured properly. Download it from http://brew.sh/, install, set it up and run this script again."
3481
exit
3582
end
3683

37-
puts "Installing CocoaPods... This might take some time, please, be patient. (You might need to provide your password.)"
38-
system('sudo gem install cocoapods')
39-
40-
puts "Installing Homebrew Cask... (You might need to provide your password.)"
41-
system('brew install caskroom/cask/brew-cask')
84+
install("Java SE Development Kit", "Installing the Java SE Development Kit... This might take some time, please, be patient. (You will be prompted for your password)", 'brew cask install java', false, false)
85+
execute('echo "export JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.profile', "Unable to set JAVA_HOME")
4286

43-
puts "Installing the Java SE Development Kit... This might take some time, please, be patient. (You might need to provide your password.)"
44-
system('brew cask install java')
45-
system('echo "export JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.profile')
87+
install("Android SDK", "Installing Android SDK", 'brew install android-sdk')
88+
execute('echo "export ANDROID_HOME=/usr/local/opt/android-sdk" >> ~/.profile', "Unable to set ANDROID_HOME")
4689

47-
puts "Installing Android SDK"
48-
system('brew install android-sdk')
49-
system('echo "export ANDROID_HOME=/usr/local/opt/android-sdk" >> ~/.profile')
90+
# the -p flag is set in order to ensure zero status code even if the directory exists
91+
execute("mkdir -p ~/.cocoapods", "There was a problem in creating ~/.cocoapods directory")
92+
install("CocoaPods", "Installing CocoaPods... This might take some time, please, be patient.", 'gem install cocoapods -V', true)
5093

5194
puts "Configuring your system for Android development... This might take some time, please, be patient."
52-
system "echo yes | /usr/local/opt/android-sdk/tools/android update sdk --filter tools,platform-tools,android-23,build-tools-23.0.2,extra-android-m2repository --all --no-ui"
95+
# Note that multiple license acceptances may be required, hence the multiple y answers
96+
# the android tool will introduce a --accept-license option in subsequent releases
97+
execute("(for i in {1..5}; do echo y; sleep 4; done) | /usr/local/opt/android-sdk/tools/android update sdk --filter tools,platform-tools,android-23,build-tools-23.0.2,extra-android-m2repository --all --no-ui",
98+
"There seem to be some problems with the Android configuration")
5399

54100
puts "The ANDROID_HOME and JAVA_HOME environment variables have been added to your .profile. Restart the terminal to use them."

0 commit comments

Comments
 (0)