Skip to content

Commit 1ce0dad

Browse files
author
Dimitar Kerezov
committed
Improve Mac 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, error if the command execution fails * Adding `-V` option for `sudo gem install cocoapods`. This triggers a verbose output * Outputting multiple license acceptances for android tools * Creating ~/.cocoapods directory in case it does not exist
1 parent f8069a1 commit 1ce0dad

File tree

1 file changed

+68
-22
lines changed

1 file changed

+68
-22
lines changed

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)