home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activetcltk / ActiveTcl8.3.4.1-8.win32-ix86.exe / ActiveTcl8.3.4.1-win32-ix86 / demos / Snack / wrap.tcl < prev    next >
Encoding:
Text File  |  2001-10-22  |  4.7 KB  |  169 lines

  1. #!/bin/sh
  2. # the next line restarts using wish \
  3. exec wish8.3 "$0" "$@"
  4.  
  5. # This utility creates stand-alone executables from Snack Tcl scripts
  6. # It needs the freewrap tool which can be downloaded from
  7. # http://freewrap.sourceforge.net/
  8. # Edit this file to set the freewrap variable below according to
  9. # your freewrap install path.
  10.  
  11.  
  12. pack [label .l1 -text "This utility creates\nstand-alone executables"]
  13. update
  14.  
  15. # Get the Snack package's directory
  16.  
  17. package require -exact snack 2.1
  18. set tmp [package ifneeded snack [package provide snack]]
  19. set tmp [lindex [lindex [split $tmp ";"] 0] end]
  20. set snackdir [file dirname $tmp]
  21.  
  22. switch -glob [string tolower $tcl_platform(platform)] {
  23.  windows {
  24. # Customize freewrap install dir here
  25. # freewrap.exe is included in the Windows binary distribution
  26.   set freewrap [file join [pwd] freewrap.exe]
  27.   set tmpdir C:/temp
  28.   regsub -all {\\} $tmpdir / tmpdir
  29.   set wrapdir [file join $tmpdir wrap]
  30.   set appextension .exe
  31.  }
  32.  unix {
  33. # Customize freewrap install dir here
  34. # freewrap executable for Linux-i386 is included in the source distribution
  35.   set freewrap [file join [pwd] freewrap]
  36.   set tmpdir /tmp
  37.   set wrapdir [file join $tmpdir wrap]
  38.   set appextension ""
  39.  }
  40.  default {
  41.   error "unknown os $tcl_platform(os)"
  42.  }
  43. }
  44. if {[info exists argv] == 0} { set argv "" }
  45. set mainprog [file rootname [lindex $argv 0]]
  46. if {$mainprog == ""} {
  47.  set mainprog [file rootname [lindex [file split \
  48.      [tk_getOpenFile -filetypes {{{Tcl scripts} {.tcl}}}]] end]]
  49.   if {$mainprog == ""} return
  50. }
  51.  
  52. if {[file executable $freewrap] == 0} {
  53.     tk_messageBox -message "This utility requires the freewrap program which is available at http://freewrap.sourceforge.net/. Edit wrap.tcl in order to specify its install path."
  54.  exit
  55. }
  56.  
  57. # Clean-up previous work directory
  58.  
  59. file delete -force $wrapdir
  60.  
  61. # Copy Snack's files
  62.  
  63. foreach {dir list} [list $wrapdir/snack \
  64.     [list $snackdir/libsnack[info sharedlibextension] \
  65.     $snackdir/snack.tcl $snackdir/pkgIndex.tcl ]] {
  66.  file mkdir $dir
  67.  foreach file $list {
  68.   file copy $file $dir
  69.  }
  70. }
  71.  
  72. # Copy standard extension packages if they are needed
  73.  
  74. set f0 [open ${mainprog}.tcl r]
  75. while {[eof $f0] == 0} {
  76.   set line [gets $f0]
  77.   if {[string match {*package require*snackogg*} $line]} {
  78.     file copy $snackdir/libsnackogg[info sharedlibextension] $dir
  79.   }
  80.   if {[string match {*package require*snacksphere*} $line]} {
  81.     file copy $snackdir/libsnacksphere[info sharedlibextension] $dir
  82.   }
  83. }
  84. close $f0
  85.  
  86. # copy script files to the wrap directory, and insert magic code
  87.  
  88. set files ${mainprog}.tcl
  89. set files [concat $files [lrange $argv 1 end]]
  90.  
  91. foreach fn $files {
  92.   if {[string match *.tcl $fn]} {
  93.     set f1 [open $fn r]
  94.     set f2 [open $wrapdir/$fn w]
  95.  
  96.     set search 1
  97.     while {$search == 1 && [eof $f1] == 0} {
  98.       set line [gets $f1]
  99.       if [string match {package require*snack*} $line] {
  100.     puts $f2 "set auto_path \"\[file join $tmpdir wrap snack\] \$auto_path\""
  101.     puts $f2 "rename load _load"
  102.     puts $f2 "proc load {fname args} {"
  103.     puts $f2 { set f [open $fname]}
  104.     puts $f2 { fconfigure $f -encoding binary -translation binary}
  105.     puts $f2 { set data [read $f]}
  106.     puts $f2 { close $f}
  107.  
  108.     puts $f2 " switch -glob \[string tolower \$::tcl_platform(platform)\] {"
  109.     puts $f2 "  unix {set tmpdir \"/tmp\"}"
  110.     puts $f2 "  win* {"
  111.     puts $f2 "    if {\[info exists ::env(TEMP)] && \$::env(TEMP) != \"\"} {"
  112.     puts $f2 {      set tmpdir $::env(TEMP)}
  113.     puts $f2 "    } elseif {\[info exists ::env(TMP)\] && \$::env(TMP) != \"\"} {"
  114.     puts $f2 {      set tmpdir $::env(TMP)}
  115.     puts $f2 "    } else {"
  116.     puts $f2 {      set tmpdir ""}
  117.     puts $f2 "    }"
  118.     puts $f2 "  }"
  119.     puts $f2 "  macintosh {"
  120.     puts $f2 "    set tmpdir ."
  121.     puts $f2 "  }"
  122.     puts $f2 " }"
  123.  
  124.  
  125.     puts $f2 " set fname2 \[file join \$tmpdir \[file rootname \[file tail \$fname\]\].\[pid\]\]"
  126.     puts $f2 { set f [open $fname2 w]}
  127.     puts $f2 { fconfigure $f -encoding binary -translation binary}
  128.     puts $f2 { puts -nonewline $f $data}
  129.     puts $f2 { close $f}
  130.     puts $f2 { eval _load $fname2 $args}
  131.     puts $f2 " foreach f \[glob -nocomplain \[file join \$tmpdir libsnack*\]\] \{"
  132.     puts $f2 "  catch \{file delete -force \$f\}"
  133.     puts $f2 " \}"
  134.     puts $f2 "}"
  135.     set search 0
  136.       }
  137.       puts $f2 $line
  138.     }
  139.     
  140.     # copy the rest
  141.     puts $f2 [read $f1]
  142.     close $f1
  143.     close $f2
  144.   } else {
  145.     if {$fn != ""} {
  146.       file copy $fn [file join $wrapdir $fn]
  147.     }
  148.   }
  149. }
  150.  
  151. set appdir [pwd]
  152. cd $wrapdir
  153.  
  154. set wrapfiles ""
  155. foreach item [glob -nocomplain * */* */*/*] {
  156.  if [file isdirectory $item] continue
  157.  lappend wrapfiles [file join $wrapdir $item]
  158. }
  159.  
  160. # Do the actual wrapping
  161.  
  162. eval exec $freewrap ${mainprog}.tcl $wrapfiles
  163. file copy -force $wrapdir/${mainprog}$appextension $appdir
  164.  
  165. catch {exec chmod -R 777 $wrapdir}
  166. catch {tk_messageBox -message "Created: ${mainprog}$appextension"}
  167. cd $appdir
  168. exit
  169.