forked from nitrogen/nitrogen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathembed
executable file
·288 lines (252 loc) · 10.5 KB
/
embed
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/perl
# vim:ts=4 sw=4 et
use strict;
use File::Basename;
use Cwd 'abs_path';
&main();
sub main {
&intro();
&runner();
}
sub intro {
print "**************************************************************\n";
print "**** NITROGEN WEB FRAMEWORK EMBEDDER ****\n";
print "**** ****\n";
print "**** Adding Nitrogen to an existing Erlang Application ****\n";
print "**** ****\n";
print "**************************************************************\n";
print "\n";
print "NOTE: BEFORE PROCEEDING IT'S HIGHLY RECOMMENDED THAT YOU MAKE\n";
print " NEW BRANCH IN YOUR SOURCE CODE MANAGEMENT SYSTEM, OR AT\n";
print " LEAST MAKE A BACKUP OF YOUR DIRECTORY, AS THIS WILL MAKE\n";
print " CHANGES TO THE CURRENT DIRECTORY TREE BY ADDING AND\n";
print " MODIFYING FILES.\n";
print "\n";
print "\n";
}
sub outro {
my ($config) = @_;
print "\n\n";
print "*******************************************************************************\n";
print "**** Installation Complete ****\n";
print "*******************************************************************************\n";
print "There are a few manual steps you must take before you're completely set up:\n";
print "\n";
print "1) You must make sure to start Nitrogen. The easiest way is by\n";
print " adding `nitrogen_sup:start_link()` to your application.\n";
print "\n";
print "2) You must make sure that the config files in $config are properly\n";
print " loaded. This can be done with a vm.args file if you're using a reltool\n";
print " generated release, or it can be done by adding additional -config calls to\n";
print " the commandline call that launches your app.\n";
print "\n";
print "3) You may need to add the new packages (nitrogen_core, simple_bridge,\n";
print " nprocreg, etc) to your app's code path (the easy way is with the -pa flag\n";
print " in the `erl` call.)\n";
print "\n\n";
print "Congratulations on adding Nitrogen to your Application\n\n";
}
sub runner {
my %yesno = (
1,"Yes",
0,"No",
"y", "Yes",
"n", "No"
);
my %servers = (
"c","cowboy",
"i","inets",
"m","mochiweb",
"w","webmachine",
"y","yaws"
);
my $to_dir = `pwd`;
chomp($to_dir);
my $from_file = abs_path($0);
my $from_dir = dirname($from_file);
print "Will be installing from '$from_dir' into '$to_dir'\n\n";
my $serverchar = &get_until_valid("Web server to use?\nOptions are (c)owboy, (i)nets, (m)ochiweb, (w)ebmachine, (y)aws.\nPlease choose",reverse(keys(%servers)));
my $server = $servers{$serverchar};
my $rebar_exists = -e "rebar.config";
#my $make_exists = -e "Makefile";
my $src = &get("src/nitrogen/", "Where to put the Erlang code for Nitrogen pages and initialization");
my $include = &get("include/", "Where to put the Erlang headers");
my $static = &get("priv/static/", "Where to put the static directory (for js, css, etc)");
my $templates = &get("priv/templates/","Where to put the templates directory");
my $config = &get("etc/", "Where to put the config files");
my $do_plugin = &get_until_valid("Install the plugin scripts and configs?", ("y","n"));
my $add_rebar = &get_until_valid("Add Nitrogen dependencies to rebar.config automatically? (nitrogen_core, simple_bridge, nprocreg, sync, $server, and any of ${server}'s dependencies.", ("y","n"));
my $add_make_plugins = $do_plugin=="y" && &get_until_valid("Add a 'make plugins' rule to the Makefile?", ("y", "n"));
my $add_copy_static = &get_until_valid("Add a 'make copy-static' rule to the Makefile?", ("y","n"));
my $run_make = &get_until_valid("Run 'make' after installation?", ("y","n"));
print "************************************************\n";
print "*** We're almost ready to go. Please review: \n";
print "*** Erlang Source: $src\n";
print "*** Erlang Headers: $include\n";
print "*** Static Resources: $static\n";
print "*** Nitrogen Templates: $templates\n";
print "*** Config Files: $config\n";
print "*** Install Plugin Script: $yesno{$do_plugin}\n";
print "*** Add 'make plugins' rule to Makefile: $yesno{$add_make_plugins}\n";
print "*** Add 'make copy-static' rule to Makefile: $yesno{$add_copy_static}\n";
print "*** Add dependencies to rebar.config: $yesno{$add_rebar}\n";
print "*** Run 'make' after all is complete: $yesno{$run_make}\n";
print "************************************************\n";
print "\n";
my $proceed = &get_until_valid("Are you sure you want to proceed?", ("y","n"));
if($proceed eq "y") {
system("mkdir -p $to_dir/$config");
system("cp $from_dir/rel/overlay/common/etc/*.config $to_dir/$config");
#system("cp $from_dir/rel/overlay/$server/etc/*.conf* $to_dir/$config");
system("mkdir -p $to_dir/$include");
system("cp $from_dir/rel/overlay/common/site/include/* $to_dir/$include");
system("mkdir -p $to_dir/$templates");
system("cp $from_dir/rel/overlay/common/site/templates/* $to_dir/$templates");
system("mkdir -p $to_dir/$static/nitrogen");
system("cp -R $from_dir/rel/overlay/common/site/static/* $to_dir/$static");
system("mkdir -p $to_dir/$src");
system("cp -R $from_dir/rel/overlay/common/site/src/* $to_dir/$src");
#system("cp -R $from_dir/rel/overlay/$server/site/src/* $to_dir/$src");
system("rm $to_dir/$src/nitrogen_app.erl $to_dir/$src/nitrogen.app.src");
if($do_plugin eq "y") {
system("cp $from_dir/rel/overlay/common/do-plugins.escript $to_dir/");
system("cp $from_dir/rel/overlay/common/plugins.config $to_dir/");
}
&fix_config_backend("$to_dir/$config/simple_bridge.config", $server);
&fix_template_path("$to_dir/$src", $templates);
&fix_config_static_paths("$to_dir/$config", $static);
#&fix_config_config_path("$to_dir/$config/yaws.config", $config) if($server eq "yaws");
&fix_plugin_config_paths("$to_dir/plugins.config", $include, $static) if($do_plugin eq "y");
if($add_rebar eq "y") {
print "Merging Nitrogen and $server dependencies\n";
system("$from_dir/rel/merge_platform_dependencies.escript $from_dir/rel/overlay/rebar.config.src $from_dir/rel/overlay/$server.deps $from_dir/rel/overlay/rebar.config");
&add_rebar_dependencies($from_dir,"$from_dir/rel/overlay/rebar.config","$to_dir/rebar.config");
unlink("$from_dir/rel/overlay/rebar.config");
}
&add_make_plugins("$to_dir/Makefile") if($add_make_plugins eq "y");
&add_copy_static($from_dir, "$to_dir/Makefile", "$to_dir/rebar.config", $static) if($add_copy_static eq "y");
system("make") if($run_make eq "y");
system("make copy-static") if($add_copy_static eq "y");
&outro($config);
}else{
print "Aborted!\n";
}
}
sub fix_plugin_config_paths {
my ($config, $include, $static) = @_;
print "Fixing static and include paths in $config\n";
&fix_one_file($config, "site\/static", &strip_trailing_slash($static));
&fix_one_file($config, "site\/include", &strip_trailing_slash($include));
}
sub fix_config_config_path {
my ($file, $config) = @_;
print "Fixing config paths in $file\n";
&fix_one_file($file, "etc", &strip_trailing_slash($config));
}
sub fix_config_backend {
my ($file, $backend) = @_;
print "Setting 'backend' in simple_bridge.config to '$backend'\n";
&fix_one_file($file, "BACKEND", $backend);
}
sub add_make_plugins {
print "Adding 'make plugins' to Makefile\n";
my ($makefile) = @_;
my $rule = "\n\nplugins:\n\t".'@(export PATH=`pwd`/`echo erts-*/bin`:$$PATH;escript do-plugins.escript)'."\n\n";
open FILE, ">>$makefile";
print FILE $rule;
close FILE;
}
sub add_copy_static {
print "Adding 'make copy-static' rule to Makefile\n";
my ($from_dir, $makefile, $rebar, $static) = @_;
my $deps_dir = `escript $from_dir/support/embed/get_deps_dir.escript $rebar`;
chomp($deps_dir);
my $rule = "\n\ncopy-static:\n\t@(cp -r $deps_dir/nitrogen_core/www/* $static/nitrogen/)\n\n";
open FILE, ">>$makefile";
print FILE $rule;
close FILE;
}
sub fix_template_path {
my ($src_path, $templatepath) = @_;
$src_path =~ s/\/$//;
$templatepath =~ s/\/$//;
my @files = &get_files($src_path);
foreach my $file (@files) {
&fix_template_path_in_file("$src_path/$file", $templatepath);
}
}
sub fix_config_static_paths {
my ($configs, $static) = @_;
$static =~ s/\/$//;
my @files = &get_files($configs);
foreach my $file (@files) {
&fix_config_static_path_one_file("$configs/$file", $static);
}
}
sub fix_one_file {
my ($file, $match, $replace) = @_;
my $newfile = "";
open FILE, '<', $file;
while(my $line = <FILE>) {
$line =~ s/$match/$replace/;
$newfile .= $line;
}
close FILE;
open FILE, '>', $file;
print FILE $newfile;
close FILE;
}
sub strip_trailing_slash {
my ($s) = @_;
$s =~ s/\/$//;
return $s;
}
sub fix_config_static_path_one_file {
my ($file, $static) = @_;
print "Fixing Static paths in $file\n";
&fix_one_file($file, "site\/static", &strip_trailing_slash($static));
}
sub fix_template_path_in_file {
my ($file, $templatepath) = @_;
print "Fixing Template paths in $file\n";
&fix_one_file($file, "site\/templates", &strip_trailing_slash($templatepath));
}
sub add_rebar_dependencies {
my ($from_dir, $from_file, $to_file) = @_;
system("escript $from_dir/support/embed/add_rebar_deps.escript $from_file $to_file");
}
sub get_files {
my ($path) = @_;
opendir(DIR, $path);
my @files = readdir(DIR);
closedir DIR;
return grep {!/^\.{1,2}$/} @files;
}
sub get {
my ($default, $prompt) = @_;
print "$prompt [Default: $default]: ";
my $val = <>;
chomp($val);
if($val eq "") {
return $default;
}else{
return $val;
}
}
sub get_until_valid {
my ($prompt, @list) = @_;
my $val;
do {
print "$prompt (".join("/",@list)."): ";
$val = <STDIN>;
chomp($val);
} while(not(in_list($val,@list)));
return $val;
}
sub in_list {
my ($val, @list) = @_;
for (@list) {
return 1 if($val eq $_);
}
return 0;
}