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