|
| 1 | + |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use 5.010; |
| 5 | + |
| 6 | +# Core modules: |
| 7 | +use Config; |
| 8 | +use Cwd qw (getcwd); |
| 9 | +use File::Basename qw (basename); |
| 10 | +use File::Copy; |
| 11 | +use File::Spec; |
| 12 | +use FindBin qw($Bin); |
| 13 | +use File::Spec::Functions qw(catdir); |
| 14 | +use lib catdir($Bin, "lib"); |
| 15 | + |
| 16 | +# Non-core CPAN modules: |
| 17 | +# File::Copy::Recursive and Module::ScanDeps are loaded from |
| 18 | +# the 'lib' subdirectory of the directory of this script: |
| 19 | +use File::Copy::Recursive qw (fcopy); |
| 20 | +use Module::ScanDeps; |
| 21 | + |
| 22 | +print "\nPerl Distribution Compactor for Perl Executing Browser version 0.1.\n\n"; |
| 23 | + |
| 24 | +# Directory paths: |
| 25 | +my $root = getcwd; |
| 26 | +my $app_directory = catdir($root, "resources", "app"); |
| 27 | +my $perl_directory = catdir($root, "perl"); |
| 28 | +my $bin_original = catdir($perl_directory, "bin"); |
| 29 | +my $bin_compacted = catdir($perl_directory, "bin-compacted"); |
| 30 | +my $lib_original = catdir($perl_directory, "lib"); |
| 31 | +my $lib_compacted = catdir($perl_directory, "lib-compacted"); |
| 32 | + |
| 33 | +# Copying the Perl interpreter: |
| 34 | +if ($Config{osname} !~ "MSWin32") { |
| 35 | + fcopy (catdir($bin_original, "perl"), catdir($bin_compacted, "perl")); |
| 36 | +} else { |
| 37 | + fcopy (catdir($bin_original, "perl.exe"), catdir($bin_compacted, "perl.exe")); |
| 38 | + |
| 39 | + my @libraries = traverse_directory($bin_original, ".dll"); |
| 40 | + foreach my $library (@libraries) { |
| 41 | + my $filename = basename($library, ".dll"); |
| 42 | + fcopy ($library, catdir($bin_compacted, $filename)); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +print "Perl interpreter copied.\n\n"; |
| 47 | + |
| 48 | +# Subroutine invocation to |
| 49 | +# get recursively all Perl scripts in the 'resources/app' subdirectory: |
| 50 | +my @scripts = traverse_directory($app_directory, ".pl"); |
| 51 | + |
| 52 | +# Get all dependencies from all Perl scripts: |
| 53 | +my $script_counter; |
| 54 | +foreach my $script (@scripts) { |
| 55 | + $script_counter++; |
| 56 | + print "Script Nr. $script_counter: $script\n"; |
| 57 | + |
| 58 | + my $dependencies_hashref = |
| 59 | + scan_deps(files => [$script], recurse => 3, compile => 'true'); |
| 60 | + |
| 61 | + my $module_counter; |
| 62 | + while (my($partial_path, $module_name) = each(%{$dependencies_hashref})) { |
| 63 | + foreach my $include_path (@INC) { |
| 64 | + my $module_full_path = catdir($include_path, $partial_path); |
| 65 | + if (-e $module_full_path) { |
| 66 | + $module_counter++; |
| 67 | + print "Dependency Nr. $module_counter: $module_full_path"; |
| 68 | + |
| 69 | + fcopy ($module_full_path, catdir($lib_compacted, $partial_path)); |
| 70 | + print " ... copied.\n"; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + print "\n"; |
| 76 | +} |
| 77 | + |
| 78 | +# Rename Perl directories: |
| 79 | +rename $bin_original, catdir($perl_directory, "bin-original"); |
| 80 | +rename $bin_compacted, catdir($perl_directory, "bin"); |
| 81 | + |
| 82 | +rename $lib_original, catdir($perl_directory, "lib-original"); |
| 83 | +rename $lib_compacted, catdir($perl_directory, "lib"); |
| 84 | + |
| 85 | +# Perl scripts recursive lister subroutine: |
| 86 | +sub traverse_directory { |
| 87 | + my ($entry, $file_extension) = @_; |
| 88 | + my @files; |
| 89 | + |
| 90 | + return if not -d $entry; |
| 91 | + opendir (my $directory_handle, $entry) or die $!; |
| 92 | + while (my $subentry = readdir $directory_handle) { |
| 93 | + next if $subentry eq '.' or $subentry eq '..'; |
| 94 | + my $full_path = catdir($entry, $subentry); |
| 95 | + if (-f $full_path and $full_path =~ $file_extension) { |
| 96 | + push @files, $full_path; |
| 97 | + } else { |
| 98 | + my @subdirectory_files = |
| 99 | + traverse_directory ($full_path, $file_extension); |
| 100 | + push @files, @subdirectory_files; |
| 101 | + } |
| 102 | + } |
| 103 | + close $directory_handle; |
| 104 | + |
| 105 | + return @files; |
| 106 | +} |
0 commit comments