mkexec 4.1KB

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