|
@@ -1,176 +0,0 @@
|
1
|
|
-#!/usr/bin/perl
|
2
|
|
-# make an executable file
|
3
|
|
-
|
4
|
|
-use strict;
|
5
|
|
-use warnings;
|
6
|
|
-
|
7
|
|
-sub usage; sub mkexec; sub guesstype; sub guesspurpose;
|
8
|
|
-sub getcmd; sub launch_editor;
|
9
|
|
-
|
10
|
|
-my $DEFAULT_TYPE = 'sh';
|
11
|
|
-my $DEFAULT_MODE = 0755;
|
12
|
|
-my $DEFAULT_FORCE = 0;
|
13
|
|
-my $DEFAULT_EDIT = 0;
|
14
|
|
-
|
15
|
|
-
|
16
|
|
-## .... ## . . . . . . . . . . .
|
17
|
|
-## INIT ## -------------------------------------------------------------------
|
18
|
|
-## '''' ## ' ' ' ' ' ' ' ' ' ' '
|
19
|
|
-
|
20
|
|
-my $force = $DEFAULT_FORCE;
|
21
|
|
-my $edit = $DEFAULT_EDIT;
|
22
|
|
-my $name; my $type; my $purpose;
|
23
|
|
-
|
24
|
|
-foreach (@ARGV) {
|
25
|
|
- if (m/\b-f\b|\b--force\b/) {
|
26
|
|
- $force++;
|
27
|
|
- } elsif (m/\b-e\b|\b--edit\b/) {
|
28
|
|
- $edit++;
|
29
|
|
- } elsif (defined $type) {
|
30
|
|
- $purpose = $_;
|
31
|
|
- } elsif (defined $name) {
|
32
|
|
- $type = $_;
|
33
|
|
- } else {
|
34
|
|
- $name = $_;
|
35
|
|
- }
|
36
|
|
-}
|
37
|
|
-
|
38
|
|
-usage unless defined $name;
|
39
|
|
-$type = guesstype $name unless defined $type;
|
40
|
|
-$purpose = guesspurpose $name unless defined $purpose;
|
41
|
|
-
|
42
|
|
-my $bangs = {
|
43
|
|
- pl => `which perl`,
|
44
|
|
- sh => '/bin/sh',
|
45
|
|
- py => `which python`,
|
46
|
|
- bash => '/bin/bash',
|
47
|
|
- sed => `which sed`,
|
48
|
|
- bc => `which bc`,
|
49
|
|
- exp => `which expect` . " -f",
|
50
|
|
-};
|
51
|
|
-
|
52
|
|
-my $templates = {
|
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
|
|
- },
|
65
|
|
- bash => {
|
66
|
|
- 'ffoo' => "\n. \"\$(ffoom path)\" || exit 3\n\n"
|
67
|
|
- . "ffoo import pretty\n\n"
|
68
|
|
- . "FFOO_DEBUG=true",
|
69
|
|
- 'shellfu' => "\n. \"\$(shellfu-get path)\" || exit 3\n\n"
|
70
|
|
- . "shellfu import pretty\n\n"
|
71
|
|
- . "PRETTY_DEBUG=\${PRETTY_DEBUG:-true}\n\n"
|
72
|
|
- . "usage() {\n"
|
73
|
|
- . " mkusage \"arg...\"\n"
|
74
|
|
- . "}\n\n"
|
75
|
|
- . "main() {\n"
|
76
|
|
- . " while true; do case \$1 in\n"
|
77
|
|
- . " -*) usage ;;\n"
|
78
|
|
- . " *) break ;;\n"
|
79
|
|
- . " esac done\n"
|
80
|
|
- . "}\n\n"
|
81
|
|
- . "main \"\$@\"",
|
82
|
|
- },
|
83
|
|
-};
|
84
|
|
-
|
85
|
|
-my $editors = [ qw/ vim editor / ];
|
86
|
|
-my $cmds;
|
87
|
|
-$cmds->{vim}->{test} = "vim --version 2>/dev/null";
|
88
|
|
-$cmds->{vim}->{run} = "vim +\"normal G\$\" '%s'";
|
89
|
|
-$cmds->{editor}->{test} = "editor --version 2>/dev/null";
|
90
|
|
-$cmds->{editor}->{run} = "editor '%s'";
|
91
|
|
-
|
92
|
|
-unless ($purpose eq ""
|
93
|
|
- or exists $templates->{$type}->{$purpose}) {
|
94
|
|
- warn "undefined purpose '$purpose' for type '$type'\n";
|
95
|
|
- $purpose = "";
|
96
|
|
-}
|
97
|
|
-
|
98
|
|
-
|
99
|
|
-## ... ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
100
|
|
-## RUN ## ----------------------------------------------------------------- #
|
101
|
|
-## ''' ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
102
|
|
-
|
103
|
|
-if (exists $bangs->{$type}) {
|
104
|
|
- $force and -e $name
|
105
|
|
- and (defined `cp "$name" "$name~"` or die $!);
|
106
|
|
- (not -e $name or $force)
|
107
|
|
- and mkexec $name, mkbody($type, $purpose);
|
108
|
|
- chmod $DEFAULT_MODE, $name;
|
109
|
|
- launch_editor $name if $edit;
|
110
|
|
-} else {
|
111
|
|
- die "unknown type: $type\n";
|
112
|
|
-}
|
113
|
|
-
|
114
|
|
-
|
115
|
|
-## .... ## ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''-.
|
116
|
|
-## SUBZ ## ----------------------------------------------------------------- :
|
117
|
|
-## '''' ## ..............................................................-'
|
118
|
|
-
|
119
|
|
-sub usage {
|
120
|
|
- print STDERR "usage: $0 [-f|-e] filename [type] [purpose]\n";
|
121
|
|
- exit 0;
|
122
|
|
-}
|
123
|
|
-
|
124
|
|
-sub guesstype {
|
125
|
|
- my $name = shift;
|
126
|
|
- my ($ext) = $name =~ m|\.(\w+)$|;
|
127
|
|
- return ( $ext ? $ext : $DEFAULT_TYPE);
|
128
|
|
-}
|
129
|
|
-
|
130
|
|
-sub guesspurpose {
|
131
|
|
- my $name = shift;
|
132
|
|
- my $testword = '[tT][eE][sS][tT]';
|
133
|
|
- return 'test' if $name =~ m|^test_|i;
|
134
|
|
- return 'test' if $name =~ m|_test.py$|i;
|
135
|
|
- return 'test' if $name =~ m|_test.pl$|i;
|
136
|
|
- return 'test' if $name =~ m|\btest\w*/|i;
|
137
|
|
- return 'test' if $name =~ m|.*Test/|i;
|
138
|
|
- return 'test' if $name =~ m|\bt/|;
|
139
|
|
- return ''
|
140
|
|
-}
|
141
|
|
-
|
142
|
|
-sub mkbody {
|
143
|
|
- my $type = shift;
|
144
|
|
- my $purpose = shift;
|
145
|
|
- my $tmpl = "";
|
146
|
|
- $tmpl .= $templates->{$type}->{$purpose}
|
147
|
|
- if exists $templates->{$type}
|
148
|
|
- and exists $templates->{$type}->{$purpose};
|
149
|
|
- return sprintf "#!%s\n%s\n", $bangs->{$type}, $tmpl;
|
150
|
|
-}
|
151
|
|
-
|
152
|
|
-sub mkexec {
|
153
|
|
- my ($name, $body) = @_;
|
154
|
|
- open EXE, ">", $name or die "cannot open $name for writing: $!\n";
|
155
|
|
- -W EXE or die "file $name is not writable\n";
|
156
|
|
- print EXE $body;
|
157
|
|
- close EXE or die "cannot close $name: $!\n";
|
158
|
|
-}
|
159
|
|
-
|
160
|
|
-sub get_cmd {
|
161
|
|
- foreach (@$editors) {
|
162
|
|
- return $cmds->{$_}->{run} if `$cmds->{$_}->{test}`
|
163
|
|
- }
|
164
|
|
- warn "no supported editor available\n";
|
165
|
|
- return;
|
166
|
|
-}
|
167
|
|
-
|
168
|
|
-sub launch_editor {
|
169
|
|
- my $name = shift;
|
170
|
|
- my $form = get_cmd;
|
171
|
|
- if ($form) {
|
172
|
|
- my $command = sprintf get_cmd, $name;
|
173
|
|
- exec "$command";
|
174
|
|
- } else { return }
|
175
|
|
- warn "failed to launch editor: $form\n";
|
176
|
|
-}
|