/
mk_module
executable file
·139 lines (94 loc) · 2.68 KB
/
mk_module
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
#!/usr/bin/perl -w
use strict;
use lib qw(/web/lib/perl);
use Getopt::Std;
use HTML::Template;
# global options
use vars qw/ %opt /;
my $opt_string = 'd:f:h?';
getopts( "$opt_string", \%opt ); # or &usage();
&usage() if $opt{'h'} || $opt{'?'};
my $dirname = 0;
$dirname = $opt{'d'};
my $filename = 0;
if($opt{'f'}) {
$filename = $opt{'f'};
} else {
die "You must supply a filename\n";
}
my $module_name = $filename . ".pm";
my $test_name = $filename . ".t";
# config
my $base_var = 'KOBJ_ROOT';
my $base = $ENV{$base_var} || die "$base_var is undefined in the environment";
my $module_dir = "Kynetx";
my $modules = $base . "/$module_dir" ;
my $test_dir = "t";
my $tests = $base . "/$test_dir";
my $tmpls = $base . "/etc/tmpl";
my $module_tmpl = $tmpls . "/module.tmpl";
my $test_tmpl = $tmpls . "/test.tmpl";
if ($dirname) {
$modules .= "/" . $dirname;
$tests .= "/" . $dirname;
# if the directory doesn't exist, create it
if (! -e $modules ) {
print "Creating directory $modules\n";
mkdir($modules);
# do_svn($modules);
}
if (! -e $tests ) {
print "Creating directory $tests\n";
mkdir($tests);
# do_svn($tests);
}
}
# don't overwrite
if(-e "$modules/$module_name" || -e "tests/$test_name") {
die "Error: You're attempting to overwrite $modules/$module_name or\n $tests/$test_name. If this is what you want, remove them first. \n";
}
# create module
print "Creating module $module_name in $modules\n";
do_template($module_tmpl,$modules,$module_name);
# create test
print "Creating test $test_name in $tests\n";
do_template($test_tmpl,$tests,$test_name);
# add both to svn
#print "Adding module $module_name to SVN\n";
#do_svn("$modules/$module_name");
#print "Adding test $test_name to SVN\n";
#do_svn("$tests/$test_name");
1;
sub usage {
print STDERR <<EOF;
usage:
mk_module [-d directory] -f module_name
Creates a module, an associated test file, and puts both in SVN.
Options are:
-d dir : put new module in directory dir
-f name : module is named name (do not include .pm extension in name)
EOF
exit;
}
sub do_template {
my($template,$dir,$file) = @_;
# open the html template
my $test_template = HTML::Template->new(filename => $template);
# fill in the parameters
$test_template->param(MODULE_DIR => $dirname) if $dirname;
$test_template->param(MODULE_NAME => $filename);
#if the dir doesn't exist, make it
if(! -e $dir) {
mkdir $dir;
}
# print the file
open(FH,">$dir/$file");
print FH $test_template->output;
close(FH);
}
sub do_svn {
my($svn_name) = @_;
my @args = ('svn', 'add', $svn_name);
system(@args) == 0
or die "Can't add $svn_name to SVN repository.";
}