-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·216 lines (191 loc) · 5.84 KB
/
install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/hhvm
<?hh
// define some constants
define('MANGO_TEMP_PATH', '/var/tmp/mango');
define('MANGO_INSTALL_PATH', '/usr/lib/mango');
define('MANGO_BINARY_PATH', '/usr/bin/mango');
define('MANGO_LIBRARY_FOLDER', 'library');
define('MANGO_FRAMEWORK_FOLDER', 'framework');
function printLn(string $line) : void {
printf("%s\n", $line);
}
function usage(string $message) : void {
printLn(" install: $message");
printLn(" usage: install [-y|--yes] [-c|--channel=channel]");
printLn("");
printLn(" options:");
printLn(" -y|--yes: non-interactive mode");
printLn("-c|--channel: allows you to specify which channel you wish to install from. defaults to 'master'");
printLn("");
exit(1);
}
function error(string $message) : void {
printLn("fatal error: $message");
cleanup();
exit(1);
}
function cleanup() : void {
printLn("* Cleaning up...");
removeDirectory(MANGO_TEMP_PATH);
}
function removeDirectory(string $path) : void {
if (file_exists($path)) {
$dir = scandir($path);
foreach ($dir as $f) {
if ($f != '.' && $f != '..') {
$fullPath = "$path/$f";
if (is_dir($fullPath)) {
removeDirectory($fullPath);
} else {
unlink($fullPath);
}
}
}
rmdir($path);
}
}
function copyDirectory(string $source, string $destination) : void {
if (!file_exists($destination)) {
mkdir($destination, 0777, true);
}
$dir = scandir($source);
foreach ($dir as $f) {
if ($f != '.' && $f != '..') {
$sourcePath = "$source/$f";
$destinationPath = "$destination/$f";
if (is_dir($sourcePath)) {
copyDirectory($sourcePath, $destinationPath);
} else {
copy($sourcePath, $destinationPath);
}
}
}
}
function proceed(string $message, bool $skip = false) : bool {
if (!$skip) {
printf("%s [Y/n] ", $message);
$stdin = fopen('php://stdin', 'r');
$input = trim(fgets($stdin));
if ($input == 'n') {
return false;
} else {
return true;
}
} else {
return true;
}
}
function downloadFile(string $url, string $path) : bool {
$file = fopen($path, 'w');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$success = curl_exec($curl);
curl_close($curl);
fclose($file);
return $success;
}
function extractPackage(string $package, string $output) : bool {
$archive = new ZipArchive();
if ($archive->open($package)) {
$archive->extractTo($output);
$archive->close();
return true;
} else {
return false;
}
}
// parse command line arguments
$NON_INTERACTIVE_MODE = false;
$CHANNEL = 'master';
$opts = getopt('yc:', array('yes', 'channel:'));
foreach (array_keys($opts) as $opt) {
if ($opt == 'y' || $opt == 'yes') {
$NON_INTERACTIVE_MODE = true;
} else if ($opt == 'c' || $opt == 'channel') {
$CHANNEL = $opts[$opt];
} else {
usage("Unknown argument: $opt");
}
}
// define installation variables
$MANGO_SOURCE = "https://github.com/jaderfeijo/mango-framework/archive/$CHANNEL.zip";
$MANGO_SOURCE_RAW = "https://raw.githubusercontent.com/jaderfeijo/mango-framework/$CHANNEL";
// begin installation
if (!$NON_INTERACTIVE_MODE) {
printLn("====================================================================");
printLn("Mango Framework Installer");
printLn("====================================================================");
printLn("This script will install the '$CHANNEL' version of the");
printLn("Mango Framework in the following paths:");
printLn("");
printLn(" ".MANGO_INSTALL_PATH);
printLn(" ".MANGO_BINARY_PATH);
printLn("");
}
// proceed confirmation
if (proceed('Do you wish to proceed with the installation?', $NON_INTERACTIVE_MODE)) {
// check if we already have the files required to install
$LOCAL_MANGO_PATH = null;
if (file_exists(MANGO_FRAMEWORK_FOLDER)) {
$LOCAL_MANGO_PATH = MANGO_FRAMEWORK_FOLDER;
}
// download the installation files if necessary
if ($LOCAL_MANGO_PATH == null) {
printLn("* Downloading the latest version of the framework from '$CHANNEL'...");
if (!file_exists(MANGO_TEMP_PATH)) {
if (!mkdir(MANGO_TEMP_PATH, 0777, true)) {
error("failed to create mango temporary path at '".MANGO_TEMP_PATH."'");
}
}
$package = MANGO_TEMP_PATH."/mango-$CHANNEL.zip";
if (downloadFile($MANGO_SOURCE, $package)) {
printLn("* Extracting downloaded package...");
if (extractPackage($package, MANGO_TEMP_PATH)) {
$LOCAL_MANGO_PATH = MANGO_TEMP_PATH.'/mango-framework-'.$CHANNEL.'/'.MANGO_FRAMEWORK_FOLDER;
} else {
error("failed to extract package");
}
} else {
error("failed to download package");
}
}
// install the framework
if (file_exists($LOCAL_MANGO_PATH)) {
$frameworkPath = MANGO_INSTALL_PATH.'/'.MANGO_FRAMEWORK_FOLDER;
if (file_exists($frameworkPath)) {
printLn("* Removing previous version of the framework...");
removeDirectory($frameworkPath);
}
printLn("* Installing latest version of the framework...");
if (!file_exists($frameworkPath)) {
if (!mkdir($frameworkPath, 0777, true)) {
error("failed to create framework folder at '".$frameworkPath."'");
}
}
copyDirectory($LOCAL_MANGO_PATH, $frameworkPath);
printLn("* Setting permissions...");
chmod($frameworkPath.'/tools/mango', 0777);
$libraryPath = MANGO_INSTALL_PATH."/".MANGO_LIBRARY_FOLDER;
if (!file_exists($libraryPath)) {
printLn("* Creating library folder...");
if (!mkdir($libraryPath, 0777, true)) {
error("failed to create mango library folder at '".$libraryPath."'");
}
}
if (!file_exists(MANGO_BINARY_PATH)) {
$linkTarget = $frameworkPath.'/tools/mango';
printLn("* Creating mango symbolic link at '".MANGO_BINARY_PATH."'...");
if (!symlink($linkTarget, MANGO_BINARY_PATH)) {
error("failed to create symbolc link at '".MANGO_BINARY_PATH."'");
}
}
cleanup();
printLn("* All done!");
} else {
error("The installation files could not be found!");
}
} else {
printLn("Aborting...");
}