forked from toddlipcon/thrift_erl_skel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_new_thrift.pl
executable file
·57 lines (46 loc) · 1.45 KB
/
make_new_thrift.pl
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
#!/usr/bin/perl
use FindBin;
sub first_service {
my ($thrift_file) = @_;
my $service = undef;
open SERVICE, "< $thrift_file";
while (my $line = <SERVICE>)
{
if ($line =~ m#service\s+([\w\.]+)#)
{
$service_name = $1;
last;
}
}
close SERVICE;
return $service_name;
}
my $SKEL_DIR="$FindBin::Bin/thrift_skel";
my $SKEL_SUB_SHORTNAME = 'SKEL_SHORTNAME';
my $SKEL_SUB_ERLANGIFIED_LONGNAME = 'SKEL_ERLANGIFIED_LONGNAME';
die "usage: $0 short_name thrift_file port" unless @ARGV == 3;
my ($SHORT_NAME, $THRIFT_FILE, $SERVICE_PORT) = @ARGV;
my $ERLANG_SERVICE = lcfirst &first_service($THRIFT_FILE);
&do("cp -r $SKEL_DIR $SHORT_NAME");
&do("thrift -r --gen erl $THRIFT_FILE");
&do("mv gen-erl/*.erl $SHORT_NAME/src");
&do("mkdir -p $SHORT_NAME/include");
&do("mv gen-erl/*.hrl $SHORT_NAME/include");
&do("rmdir gen-erl");
&do("perl -p -i -e 's/$SKEL_SUB_SHORTNAME/$SHORT_NAME/g;' `find $SHORT_NAME -type f`");
&do("perl -p -i -e 's/$SKEL_SUB_ERLANGIFIED_LONGNAME/$ERLANG_SERVICE/g;' `find $SHORT_NAME -type f`");
&do("perl -p -i -e 's/SKEL_PORT/$SERVICE_PORT/g;' `find $SHORT_NAME -type f`");
# Rename
my $files_string = `find $SHORT_NAME -type f -print0`;
my @files = split /\0/, $files_string;
foreach my $file (@files) {
my $newfile = $file;
$newfile =~ s/thrift_skel/$SHORT_NAME/;
print "$file -> $newfile\n";
rename($file, $newfile);
}
sub do {
my $todo = shift;
print "$todo\n";
system($todo);
}