123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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;
  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. bash => {
  55. 'ffoo' => "\n. \$(ffoom path) || exit 3\n\n"
  56. . "ffoo import pretty\n\n"
  57. . "FFOO_DEBUG=true",
  58. }
  59. };
  60. my $editors = [ qw/ vim editor / ];
  61. my $cmds;
  62. $cmds->{vim}->{test} = "vim --version 2>/dev/null";
  63. $cmds->{vim}->{run} = "vim +\"normal G\$\" '%s'";
  64. $cmds->{editor}->{test} = "editor --version 2>/dev/null";
  65. $cmds->{editor}->{run} = "editor '%s'";
  66. unless ($purpose eq ""
  67. or exists $templates->{$type}->{$purpose}) {
  68. warn "undefined purpose '$purpose' for type '$type'\n";
  69. $purpose = "";
  70. }
  71. ## ... ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  72. ## RUN ## ----------------------------------------------------------------- #
  73. ## ''' ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  74. if (exists $bangs->{$type}) {
  75. $force and -e $name
  76. and (defined `cp "$name" "$name~"` or die $!);
  77. (not -e $name or $force)
  78. and mkexec $name, mkbody($type, $purpose);
  79. chmod $DEFAULT_MODE, $name;
  80. launch_editor $name if $edit;
  81. } else {
  82. die "unknown type: $type\n";
  83. }
  84. ## .... ## ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''-.
  85. ## SUBZ ## ----------------------------------------------------------------- :
  86. ## '''' ## ..............................................................-'
  87. sub usage {
  88. print STDERR "usage: $0 [-f|-e] filename [type] [purpose]\n";
  89. exit 0;
  90. }
  91. sub guesstype {
  92. my $name = shift;
  93. my ($ext) = $name =~ m|\.(\w+)$|;
  94. return ( $ext ? $ext : $DEFAULT_TYPE);
  95. }
  96. sub guesspurpose {
  97. my $name = shift;
  98. my $testword = '[tT][eE][sS][tT]';
  99. return 'test' if $name =~ m|^test_|i;
  100. return 'test' if $name =~ m|_test.py$|i;
  101. return 'test' if $name =~ m|_test.pl$|i;
  102. return 'test' if $name =~ m|\btest\w*/|i;
  103. return 'test' if $name =~ m|.*Test/|i;
  104. return 'test' if $name =~ m|\bt/|;
  105. return ''
  106. }
  107. sub mkbody {
  108. my $type = shift;
  109. my $purpose = shift;
  110. my $tmpl = "";
  111. $tmpl .= $templates->{$type}->{$purpose}
  112. if exists $templates->{$type}
  113. and exists $templates->{$type}->{$purpose};
  114. return sprintf "#!%s\n%s\n", $bangs->{$type}, $tmpl;
  115. }
  116. sub mkexec {
  117. my ($name, $body) = @_;
  118. open EXE, ">", $name or die "cannot open $name for writing: $!\n";
  119. -W EXE or die "file $name is not writable\n";
  120. print EXE $body;
  121. close EXE or die "cannot close $name: $!\n";
  122. }
  123. sub get_cmd {
  124. foreach (@$editors) {
  125. return $cmds->{$_}->{run} if `$cmds->{$_}->{test}`
  126. }
  127. warn "no supported editor available\n";
  128. return;
  129. }
  130. sub launch_editor {
  131. my $name = shift;
  132. my $form = get_cmd;
  133. if ($form) {
  134. my $command = sprintf get_cmd, $name;
  135. exec "$command";
  136. } else { return }
  137. warn "failed to launch editor: $form\n";
  138. }