Browse Source

Implement purpose-based templates

Alois Mahdal 10 years ago
parent
commit
f8cb2b28af
1 changed files with 39 additions and 7 deletions
  1. 39
    7
      bin/mkexec

+ 39
- 7
bin/mkexec View File

4
 use strict;
4
 use strict;
5
 use warnings;
5
 use warnings;
6
 
6
 
7
-sub usage; sub mkexec; sub guesstype; sub getcmd; sub launch_editor;
7
+sub usage; sub mkexec; sub guesstype; sub guesspurpose;
8
+sub getcmd; sub launch_editor;
8
 
9
 
9
 my $DEFAULT_TYPE = 'sh';
10
 my $DEFAULT_TYPE = 'sh';
10
 my $DEFAULT_MODE = 0755;
11
 my $DEFAULT_MODE = 0755;
17
 ## '''' ##   ' ' '  '   '    '     '      '       '        '         '
18
 ## '''' ##   ' ' '  '   '    '     '      '       '        '         '
18
 
19
 
19
 my $force = $DEFAULT_FORCE;
20
 my $force = $DEFAULT_FORCE;
20
-my $name; my $type; my $edit;
21
+my $name; my $type; my $purpose; my $edit;
21
 
22
 
22
 foreach (@ARGV) {
23
 foreach (@ARGV) {
23
     if (m/-f|--force/) {
24
     if (m/-f|--force/) {
26
         $edit++;
27
         $edit++;
27
     } elsif (m/-E|--no-edit/) {
28
     } elsif (m/-E|--no-edit/) {
28
         $edit = 0;
29
         $edit = 0;
30
+    } elsif (defined $type) {
31
+        $purpose = $_;
29
     } elsif (defined $name) {
32
     } elsif (defined $name) {
30
         $type = $_;
33
         $type = $_;
31
     } else {
34
     } else {
35
 
38
 
36
 usage unless defined $name;
39
 usage unless defined $name;
37
 $type = guesstype $name unless defined $type;
40
 $type = guesstype $name unless defined $type;
41
+$purpose = guesspurpose $name unless defined $purpose;
38
 
42
 
39
 my $bangs = {
43
 my $bangs = {
40
     pl   => `which perl`,
44
     pl   => `which perl`,
46
 };
50
 };
47
 
51
 
48
 my $templates = {
52
 my $templates = {
49
-    pl   => "use strict;\nuse warnings;\n",
50
-    py   => "if __name__ == '__main__':\n"
53
+    py => {
54
+        ''      => "if __name__ == '__main__':\n",
55
+        test    => "import unittest\n\n\n"
56
+                   . "class TestCase(unittest.TestCase):\n"
57
+                   . "    pass\n\n\n"
58
+                   . "if __name__=='__main__':\n"
59
+                   ."    unittest.main()\n",
60
+    },
61
+    pl => {
62
+        ''      => "use strict;\nuse warnings;\n",
63
+        test    => "use strict;\nuse warnings;\nuse Test::More;\n\n",
64
+    }
51
 };
65
 };
52
 
66
 
53
 my $editors = [ qw/ vim editor / ];
67
 my $editors = [ qw/ vim editor / ];
57
 $cmds->{editor}->{test} = "editor --version 2>/dev/null";
71
 $cmds->{editor}->{test} = "editor --version 2>/dev/null";
58
 $cmds->{editor}->{run}  = "editor '%s'";
72
 $cmds->{editor}->{run}  = "editor '%s'";
59
 
73
 
74
+unless (exists $templates->{$type}->{$purpose}) {
75
+    warn "undefined purpose '$purpose' for type '$type'\n";
76
+    $purpose = "";
77
+}
78
+
60
 
79
 
61
 ## ... ##   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
80
 ## ... ##   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
62
 ## RUN ## ----------------------------------------------------------------- #
81
 ## RUN ## ----------------------------------------------------------------- #
66
     $force and -e $name
85
     $force and -e $name
67
         and (defined `cp "$name" "$name~"` or die $!);
86
         and (defined `cp "$name" "$name~"` or die $!);
68
     (not -e $name or $force)
87
     (not -e $name or $force)
69
-        and mkexec $name, mkbody($type);
88
+        and mkexec $name, mkbody($type, $purpose);
70
     chmod $DEFAULT_MODE, $name;
89
     chmod $DEFAULT_MODE, $name;
71
     launch_editor $name if $edit;
90
     launch_editor $name if $edit;
72
 } else {
91
 } else {
79
 ## '''' ##   ..............................................................-'
98
 ## '''' ##   ..............................................................-'
80
 
99
 
81
 sub usage {
100
 sub usage {
82
-    print STDERR "usage: $0 [-f|-E] filename [type]\n";
101
+    print STDERR "usage: $0 [-f|-E] filename [type] [purpose]\n";
83
     exit 0;
102
     exit 0;
84
 }
103
 }
85
 
104
 
89
     return ( $ext ? $ext : $DEFAULT_TYPE);
108
     return ( $ext ? $ext : $DEFAULT_TYPE);
90
 }
109
 }
91
 
110
 
111
+sub guesspurpose {
112
+    my $name = shift;
113
+    my $testword = '[tT][eE][sS][tT]';
114
+    return 'test' if $name =~ m|^test_|i;
115
+    return 'test' if $name =~ m|_test.py$|i;
116
+    return 'test' if $name =~ m|_test.pl$|i;
117
+    return 'test' if $name =~ m|\btest\w*/|i;
118
+    return 'test' if $name =~ m|.*Test/|i;
119
+    return 'test' if $name =~ m|\bt/|;
120
+    return ''
121
+}
122
+
92
 sub mkbody {
123
 sub mkbody {
93
     my $type = shift;
124
     my $type = shift;
125
+    my $purpose = shift;
94
     my $tmpl = "";
126
     my $tmpl = "";
95
-    $tmpl .= $templates->{$type} if exists $templates->{$type};
127
+    $tmpl .= $templates->{$type}->{$purpose} if exists $templates->{$type};
96
     return sprintf "#!%s\n%s\n", $bangs->{$type}, $tmpl;
128
     return sprintf "#!%s\n%s\n", $bangs->{$type}, $tmpl;
97
 }
129
 }
98
 
130