#! /usr/bin/env perl BEGIN{$^W++} # Turn on debugging info # Converts all of the scripts in the given directory. # # Use the -f option to cause it to constantly check for new changes. # Todo: # - remove files / directories from the OUTPUT directory that no longer exist in the SOURCE directory # - (possibly) simply copy over non-source-files in the SOURCE directory directly to the OUTPUT directory # These are all standard libraries, but maybe I went a bit overboard... use File::Find; #no warnings 'File::Find'; use File::Spec qw(splitdir); use File::Basename; use Cwd 'abs_path'; $SOURCE_DIRECTORY = "$ENV{HOME}/www/bookmarklets/src"; $SOURCE_EXTENSION = "js"; $OUTPUT_DIRECTORY = "$ENV{HOME}/www/bookmarklets/html"; $OUTPUT_EXTENSION = "html"; $CONV_SCRIPT = File::Spec->catfile( #(File::Spec->splitpath($0))[1], abs_path(dirname($0)), "conv_bkmklt.pl"); my $forever = 0; if (@ARGV && $ARGV[0] eq '-f') { $forever = 1; } for (;;) { find(\&foreach_file, $SOURCE_DIRECTORY); if ($forever) { sleep(1); } else { last; } } sub foreach_file { my $file = $_; my $dir = $File::Find::dir; my $fullname = $File::Find::name; return unless (-f $fullname); return unless ($file =~ /\.$SOURCE_EXTENSION$/i); my $out_dir = $dir; $out_dir =~ s/^$SOURCE_DIRECTORY//; my $out_file = File::Spec->catfile($OUTPUT_DIRECTORY, $out_dir, $file); $out_file =~ s/\.$SOURCE_EXTENSION$/.$OUTPUT_EXTENSION/; # Is this file new or been updated? #print "$out_file\n"; if ((! -e $out_file) || (-M $file < -M $out_file) || (-M $CONV_SCRIPT < -M $out_file)) { my $disp = File::Spec->catfile($out_dir, $file); $disp =~ s/^.//; # Remove the preceding slash print "$disp\n"; # Make sure the whole parent directory exists my @dirs = File::Spec->splitdir(dirname($out_file)); my $dir_acc = ''; foreach my $dir (@dirs) { $dir_acc = File::Spec->catdir($dir_acc, $dir); if (! -d $dir_acc) { mkdir($dir_acc, 0777); } } # Unix specific (the only line in the whole script :( ) system "mkdir --parents " . File::Spec->catdir($OUTPUT_DIRECTORY, $out_dir); open PIN, "$CONV_SCRIPT $fullname|" or die "$!"; open FOUT, ">$out_file" or die "$!"; print FOUT while (); close PIN; close FOUT; } }