#!perl ################################################################### # # # FILE: "RuntimeBuilder.pl" # created: 4/23/98 # # Author: Michael Ziege # E-mail: # # Description: This is the successor of 'ExtractModules.pl'. # Save this script as Droplet; drag & drop a PerlScript.pl textfile # on it. # -> a folder called PerlScript Ä is installed and in there the runtime # "PerlScript.run" and a folder called "shared libs" which contains # the dynamically loaded shared libraries. # All imported modules are copied into the resource fork of # "PerlScript.run". # Installed on a target machine, the runtime together with the shared # library folder is a fully functional double clickable application and # it's not necessesary to have MacPerl installed. # For MacPerl PPC runtimes only. # have fun # ################################################################### $| = 1; my (%MyINC, @dynaLoaderModules); BEGIN { exit unless $ARGV[0]; my( $textTemplPath) = @_; #suppress warnings about redefining subroutines #by 'eval(use...)': local $^W = 0; print "Checking for imported modules...\n"; open FILE, $ARGV[0] or die "Couldn't open file $textTemplPath: $!"; while () { if(/^use.*;/ || /^require.*;/){ eval($_); #read the %INC } } close FILE; %MyINC = %INC; @dynaLoaderModules = @DynaLoader::dl_modules; } use strict; use Mac::AppleEvents; use Mac::Files; use Mac::Resources; use Mac::Memory; use File::Copy; use Mac::Dialogs; #globals: my ($prgPath, $textTemplPath, $textTemplName, $textTemplFolder, $runtimeFolderPath, $runtimePath, $libPath, $libFile, $libFileName, $sharedLibPath, $sharedLibFolder, $res, $changes, $sourceCodeRes, $sourceCodeText,$id, $textHand, @dir, $aText, $needResources, $resourceName, $orig, $dest); ($prgPath = $0) =~ s/(.+:).*?$/$1/g; ($textTemplPath) = $ARGV[0]; ($textTemplName = $textTemplPath) =~ s/.+:(.*?)(.pl$|$)/$1/; ($textTemplFolder = $textTemplPath) =~ s/(.+):.*?$/$1/g; $runtimeFolderPath = $textTemplFolder.':'.$textTemplName.' Ä'; $runtimePath = $runtimeFolderPath.':'.$textTemplName.'.run'; $libPath = $ENV{MACPERL}.'lib:'; #shared library path: $sharedLibPath = $libPath.'MacPPC:auto:'; $sharedLibFolder = $runtimeFolderPath.':'.'shared libs'; print "Saving \"$textTemplName.pl\" as Runtime in \"$runtimeFolderPath:$textTemplName.run\"...\n"; mkdir($runtimeFolderPath, 0755) unless -e $runtimeFolderPath; SaveAsRuntime($textTemplPath, $runtimePath); $res= OpenResFile($runtimePath) || die $^E; UseResFile $res; $changes = <<'END_TEXT'; BEGIN { unshift (@INC,":shared libs:"); } END_TEXT print "Saving imported modules in resource fork of $textTemplName.run...\n"; #first 'TEXT' resource is source code: $sourceCodeRes= Get1IndResource('TEXT', 1); $sourceCodeText= $sourceCodeRes-> get(); RemoveResource($sourceCodeRes); $sourceCodeRes->dispose; $sourceCodeText =~ s/(^use.*|^require.*)/$changes$1/; $textHand = new Handle($sourceCodeText); AddResource($textHand, 'TEXT', '128', '!') or die $!; WriteResource($textHand) or die $!;; ReleaseResource($textHand) or die $!;; $textHand->dispose; $id= 5000; #first copy the 4 Dynaloader libs: opendir DIR, $libPath.'auto:DynaLoader:' or die "$!"; @dir = readdir DIR; closedir DIR; foreach $libFile (@dir) { $aText = ""; $libFileName = $libPath.'auto:DynaLoader:'.$libFile; $resourceName = 'auto:DynaLoader:'.$libFile; open LIBFILE, $libFileName or die "$!"; while() { $aText .= $_; } close LIBFILE; $textHand = new Handle($aText); AddResource($textHand, 'TEXT', $id, $resourceName) or die $!; WriteResource($textHand) or die $!;; ReleaseResource($textHand) or die $!;; $textHand->dispose; $id++; #print "Copied auto lib ".$resourceName." into resource fork.\n"; } #copy Mac::Memory and Mac::Resources if needed: if ($needResources) { $aText = ""; $libFile = 'Mac:Resources.pm'; open LIBFILE, $libPath.$libFile or die "Couldn't open $libFile: $^E"; while() { $aText .= $_; } close LIBFILE; $textHand = new Handle($aText); AddResource($textHand, 'TEXT', $id, $libFile) or die $!; WriteResource($textHand) or die $!;; ReleaseResource($textHand) or die $!;; $textHand->dispose; $id++; print "Copied module ".$libFile." into resource fork.\n"; } foreach $libFileName(keys %MyINC) { $aText= ""; open(libFile, $MyINC{$libFileName}) or die $!; while(){ $aText.= $_; } close libFile; $textHand= new Handle($aText); #UNIX-> Mac Folder $libFileName=~ s/\//:/g; AddResource($textHand, 'TEXT', $id, $libFileName) or die $!; WriteResource($textHand) or die $!;; ReleaseResource($textHand) or die $!;; $textHand-> dispose; $id++; print "Copied module ".$libFileName." into resource fork.\n"; } mkdir($sharedLibFolder, 0755) unless -e $sharedLibFolder; foreach (@dynaLoaderModules) { $_ =~ s/::/:/; ($dest = $_) =~ s/.+:(.*?$)/$1/; $orig = $_.':'.$dest; copy($sharedLibPath.$orig, $sharedLibFolder.':'.$dest) or die "file copy:$^E"; } CloseResFile($res); print "Finished.\n"; print "\007"; sleep(3); MacPerl::Quit(3); sub SaveAsRuntime { my($textTemplPath, $runTimePath) = @_; MacPerl::DoAppleScript <<"END_SCRIPT"; tell application "MacPerl" Save file  "$textTemplPath" in file  "$runTimePath" as Runtime end tell END_SCRIPT }