]> git.donarmstrong.com Git - perltidy.git/blob - pm2pl
all debian patches are now upstream
[perltidy.git] / pm2pl
1 #!/usr/bin/env perl
2 use strict;
3
4 # This script will recombine the perltidy binary script and all of its modules
5 # into a single, monolithic script.  I use it for making a temporary "sandbox"
6 # for debugging.
7
8 # This is also useful for making a copy of previous versions for parallel
9 # debugging sessions.
10
11 # usage:
12 #   perl pm2pl
13
14 # Run this from the perltidy main installation directory.  It reads
15 # bin/perltidy and lib/*.pm and writes a file 'perltidy-VERSION.pl' in the
16 # current directory.
17
18 # This should work for a system with File::Spec,
19 # and for older Windows/Unix systems without File::Spec.
20 my $script  = 'bin/perltidy';
21 my @modules = qw(
22   lib/Perl/Tidy.pm
23   lib/Perl/Tidy/Debugger.pm
24   lib/Perl/Tidy/DevNull.pm
25   lib/Perl/Tidy/Diagnostics.pm
26   lib/Perl/Tidy/FileWriter.pm
27   lib/Perl/Tidy/Formatter.pm
28   lib/Perl/Tidy/HtmlWriter.pm
29   lib/Perl/Tidy/IOScalar.pm
30   lib/Perl/Tidy/IOScalarArray.pm
31   lib/Perl/Tidy/IndentationItem.pm
32   lib/Perl/Tidy/LineBuffer.pm
33   lib/Perl/Tidy/LineSink.pm
34   lib/Perl/Tidy/LineSource.pm
35   lib/Perl/Tidy/Logger.pm
36   lib/Perl/Tidy/Tokenizer.pm
37   lib/Perl/Tidy/VerticalAligner.pm
38   lib/Perl/Tidy/VerticalAligner/Alignment.pm
39   lib/Perl/Tidy/VerticalAligner/Line.pm
40 );
41
42 # try to make the pathnames system independent
43 eval "use File::Spec;";
44 my $missing_file_spec = $@;
45 unless ($missing_file_spec) {
46     $script = File::Spec->catfile( split '/', $script );
47     foreach my $module (@modules) {
48         $module = File::Spec->catfile( split '/', $module );
49     }
50 }
51
52 my $VERSION = get_version("lib/Perl/Tidy.pm");
53 my $outfile = "perltidy-$VERSION.pl";
54 open OUTFILE, "> $outfile" or die "can't open file '$outfile' : $!\n";
55 print "Creating standalone formatter script '$outfile' ....\n ";
56
57 # first, open the script and copy the first (hash-bang) line
58 # (Note: forward slashes in file names here will work in Windows)
59 open SCRIPT, "< $script" or die "can't open script file '$script' : $!\n";
60 my $hash_bang = <SCRIPT>;
61 print OUTFILE $hash_bang;
62
63 # then copy all modules
64 foreach my $module (@modules) {
65     open PM, "< $module" or die "can't open my module file '$module' : $!\n";
66     while (<PM>) {
67         last if /^\s*__END__\s*$/;
68         print OUTFILE unless $_ =~ /^use Perl::Tidy/;
69     }
70     close PM;
71 }
72
73 # then, copy the rest of the script except for the 'use PerlTidy' statement
74 while (<SCRIPT>) {
75     last if /^\s*__END__\s*$/;
76     print OUTFILE unless $_ =~ /^use Perl::Tidy/;
77 }
78 close SCRIPT;
79 close OUTFILE;
80 chmod 0755, $outfile;
81
82 my $testfile = "somefile.pl";
83 print <<EOM;
84
85 You can now run $outfile to reformat perl scripts.  
86 For example, the following command:
87
88     perl $outfile $testfile
89
90 will produce the output file $testfile.tdy
91 EOM
92
93 sub get_version {
94     my ($file) = @_;
95     my $fh;
96     open( $fh, "<", $file ) || die "cannot open $fh: $!\n";
97     while ( my $line = <$fh> ) {
98
99         # Looking for something simple like this, with or without quotes,
100         # with semicolon and no sidecomments:
101         #                     $VERSION   =   "20180202.245"  ;
102         #                 our $VERSION   =    20104202       ;
103         if ( $line =~
104             /^((our)?\s*\$VERSION\s*=\s*\'?)  ([^'#]+)   (\'?) \s* ;/x )
105         {
106             $VERSION = $3;
107             last;
108         }
109     }
110     return $VERSION;
111 }