src/bin/make-standalone-script - lemplate

Data types defined

Source code

  1. #!/usr/bin/env perl

  2. use strict;
  3. use warnings;
  4. use FindBin qw($Bin);
  5. use lib "$Bin/../../lib", "$Bin/../lib";
  6. use Template;
  7. use IO::All;

  8. {
  9.     my $script = io(shift)->all;
  10.     $script =~ s{^#!/usr/bin/perl$}{#!/usr/bin/env perl}m;

  11.     $script =~ /(.*\n#BOOTSTRAP-BEGIN\n).*\n(#BOOTSTRAP-END\n.*)/s
  12.         or die;

  13.     print $1 . guts() . $2;
  14. }

  15. sub guts {
  16.     my $output = '';
  17.     for (qw(
  18.         Number::Compare
  19.         Text::Glob
  20.         File::Find::Rule
  21.         Template::Constants
  22.         Template::Base
  23.         Template::Config
  24.         Template::Document
  25.         Template::Exception
  26.         Template::Service
  27.         Template::Provider
  28.         Template
  29.         Template::Grammar
  30.         Template::Directive
  31.         Template::Parser
  32.         Lemplate::Directive
  33.         Lemplate::Grammar
  34.         Lemplate::Parser
  35.         Lemplate::Runtime
  36.         Lemplate::Runtime::Compact
  37.         Lemplate
  38.     )) {
  39.         $output .= get_module($_);
  40.     }
  41.     return disable_libs() . $output;
  42. }

  43. sub disable_libs {
  44.     return <<'...';
  45. # This is the standalone Lemplate compiler.
  46. #
  47. # All you need is this program and the program called `perl`. You don't need
  48. # to install any Perl modules.
  49. #
  50. # If you downloaded this program from the internet, don't forget to put it in
  51. # your path and make sure it is executable. Like this:
  52. #
  53. #     mv lemplate /usr/local/bin/
  54. #     chmod +x /usr/local/bin/lemplate
  55. #
  56. # Try this command to make sure it works:
  57. #
  58. #     lemplate --help

  59. use Config;
  60. BEGIN {
  61.     @INC = (
  62.         $Config::Config{archlib},
  63.         $Config::Config{privlib},
  64.     );
  65. }
  66. use strict;
  67. use warnings;

  68. ...
  69. }

  70. sub get_module {
  71.     my $module = shift;
  72.     eval "require $module; 1" or die "$module not found";
  73.     $module =~ s{::}{/}g;
  74.     $module .= '.pm';
  75.     my $content = io($INC{$module})->all;
  76.     # Get rid of DATA section
  77.     $content =~ s/^__(END|DATA)__.*//sm;
  78.     # Remove POD
  79.     $content =~ s/^=\w+.*?(\n=cut\n|\z)//msg;
  80.     # Remove comments
  81.     $content =~ s/^#.*\n//gm;

  82.     # Return the concatenation of prerequisite modules
  83.     return
  84.         "#\n# Inline include of $module\n#\n" .
  85.         "BEGIN { \$INC{'$module'} = 'dummy/$module'; }\n" .
  86.         "BEGIN {\n" .
  87.         "#line 0 \"$module\"\n" .
  88.         $content .
  89.         "\n}\n" .
  90.         "";
  91. }