home *** CD-ROM | disk | FTP | other *** search
/ PC World 1997 November / PCWorld_1997-11_cd.bin / software / programy / komix / DATA.Z / installabl.tcl < prev    next >
Text File  |  1997-03-18  |  8KB  |  331 lines

  1. #---------------------------------------------------------------------------
  2. #
  3. #      (c)     Cayenne Software Inc.    1997
  4. #
  5. #      File:           @(#)installabl.tcl    /main/hindenburg/2
  6. #      Author:         <generated>
  7. #      Description:    VCM integration file
  8. #---------------------------------------------------------------------------
  9. # SccsId = @(#)installabl.tcl    /main/hindenburg/2   18 Mar 1997 Copyright 1997 Cayenne Software Inc.
  10.  
  11. # Start user added include file section
  12. # End user added include file section
  13.  
  14.  
  15. # This class represents installable cutomization files.
  16. # Each instance is one file, the attibutes specify
  17. # how the file must be installed.
  18.  
  19. Class InstallableCustomFile : {GCObject} {
  20.     method destructor
  21.     constructor
  22.     method sourcePath
  23.     method customFileVersion
  24.     method createCustomFileVersion
  25.     method writeFrom
  26.     method readTo
  27.     method filterCopy
  28.     method appendSection
  29.     method removeSection
  30.     method delete
  31.     method install
  32.     method unInstall
  33.     method manager
  34.  
  35.     # Name of the customization file.
  36.     #
  37.     attribute name
  38.  
  39.     # Type of the customization file
  40.     #
  41.     attribute type
  42.  
  43.     # List of levels if file cannot be installed
  44.     # on all levels, empty otherwise.
  45.     #
  46.     attribute level
  47.  
  48.     # Where to install from.
  49.     #
  50.     attribute source
  51.  
  52.     # Prefix that is used to distinguish between
  53.     # source files with the same name.
  54.     #
  55.     attribute sourcePrefix
  56.  
  57.     # Method that yields a temporaray file
  58.     # with the source file.
  59.     #
  60.     attribute sourceMethod
  61.  
  62.     # If incremental is 0, installation overwrites any existing files and 
  63.     # uninstall deletes.
  64.     # Otherwise install appends to a file,
  65.     # and uninstall just removes this part.
  66.     # In case of a new file the behavior is similar.
  67.     #
  68.     attribute incremental
  69.     attribute _manager
  70. }
  71.  
  72. method InstallableCustomFile::destructor {this} {
  73.     # Start destructor user section
  74.     # End destructor user section
  75. }
  76.  
  77. constructor InstallableCustomFile {class this name type manager} {
  78.     set this [GCObject::constructor $class $this]
  79.     $this name $name
  80.     $this type $type
  81.     $this manager $manager
  82.     return $this
  83. }
  84.  
  85.  
  86. # Returns the source path for this file.
  87. #
  88. method InstallableCustomFile::sourcePath {this} {
  89.     # check source method first. This will yield a temp file.
  90.     if { [$this sourceMethod] != "" } {
  91.     set tmpFile [eval [$this sourceMethod]]
  92.     return $tmpFile
  93.     }
  94.  
  95.     set sourceFile [$this source]
  96.     # if source is a directory then we must still append the file name
  97.     if [file isdirectory $sourceFile] {
  98.     set prefixName "[$this sourcePrefix][$this name]"
  99.     set sourceFile [path_name concat $sourceFile $prefixName [$this type]]
  100.     }
  101.  
  102.     return $sourceFile
  103. }
  104.  
  105.  
  106. # Returns corresponding customFileVersion if it exists.
  107. #
  108. method InstallableCustomFile::customFileVersion {this} {
  109.     set customLevel [[ClientContext::global] currentCustomLevel]
  110.     set name [$this name]
  111.     set type [$this type]
  112.     return  [$customLevel findCustomFileVersion $name $type]
  113. }
  114.  
  115.  
  116. # Creates corresponding custom file version.
  117. #
  118. method InstallableCustomFile::createCustomFileVersion {this} {
  119.     set customLevel [[ClientContext::global] currentCustomLevel]
  120.     set name [$this name]
  121.     set type [$this type]
  122.     return [$customLevel createCustomFileVersion $name $type]
  123. }
  124.  
  125.  
  126. # Write the customization file using the
  127. # specified path as source.
  128. #
  129. method InstallableCustomFile::writeFrom {this path} {
  130.     set customFile [$this customFileVersion]
  131.  
  132.     # if customization file does not exist create it
  133.     if [$customFile isNil] {
  134.     set customFile [$this createCustomFileVersion]
  135.     }
  136.     
  137.     # edit and upload contents
  138.     $customFile edit
  139.     $customFile upLoad $path
  140.     $customFile quit
  141. }
  142.  
  143.  
  144. # Read contents of customization file to path.
  145. #
  146. method InstallableCustomFile::readTo {this path} {
  147.     set customFile [$this customFileVersion]
  148.     if [$customFile isNil] {
  149.     return
  150.     }
  151.  
  152.     $customFile downLoad $path
  153. }
  154.  
  155.  
  156. # Copy 'registerObject' definitions from 'from'
  157. # to 'to', filtering any elements that also occur in 'filter'.
  158. # Mode specifies the open mode for 'to'.
  159. #
  160. method InstallableCustomFile::filterCopy {this from to filter {mode w}} {
  161.     if { $filter != "" } {
  162.     set filterDesc [open $filter r]
  163.     set filterContents [read $filterDesc]
  164.     close $filterDesc
  165.     } else {
  166.     set filterContents "None"
  167.     }
  168.  
  169.     set fromDesc [open $from r]
  170.     set toDesc [open $to $mode]
  171.  
  172.     # parse variables: definition is current definition
  173.     # inDefinition states whether we are in a definition
  174.     set definition ""
  175.     set inDefinition 0
  176.     while { [gets $fromDesc line] >= 0 } {
  177.     # get start of a registerObject definition
  178.     if [regexp ^registerObject $line] {
  179.         set inDefinition 1
  180.     }
  181.  
  182.     if $inDefinition {
  183.         if { $definition == "" } {
  184.         set definition $line
  185.         } else {
  186.         append definition "\n$line"
  187.         }
  188.  
  189.         # end of definition: append if a new one
  190.         if [regexp ^\} $line] {
  191.         set inDefinition 0
  192.         if { [string first $definition $filterContents] == -1 } {
  193.             puts $toDesc $definition
  194.         }
  195.         set definition ""
  196.         }
  197.     }
  198.     }
  199.  
  200.     close $fromDesc
  201.     close $toDesc
  202. }
  203.  
  204.  
  205. # Read the contents of path and insert in the file
  206. # if they did not yet exist there.
  207. #
  208. method InstallableCustomFile::appendSection {this path} {
  209.     $this filterCopy [$this sourcePath] $path $path a
  210.  
  211.     # remove sourcePath for temp files
  212.     if { [$this sourceMethod] != "" } {
  213.     BasicFS::removeFile $sourcePath
  214.     }
  215.  
  216.     return
  217. }
  218.  
  219.  
  220. # Returns path of temporary file containing
  221. # the contents of path minus the contents of source.
  222. #
  223. method InstallableCustomFile::removeSection {this path} {
  224.     set destFile [BasicFS::tmpFile]
  225.     $this filterCopy $path $destFile [$this sourcePath]
  226.  
  227.     BasicFS::removeFile $path
  228.  
  229.     # remove sourcePath for temp files
  230.     if { [$this sourceMethod] != "" } {
  231.     BasicFS::removeFile $sourcePath
  232.     }
  233.  
  234.     return $destFile
  235. }
  236.  
  237.  
  238. # Deletes the customization file from the
  239. # current level.
  240. #
  241. method InstallableCustomFile::delete {this} {
  242.     set customLevel [[ClientContext::global] currentCustomLevel]
  243.     set name [$this name]
  244.     set type [$this type]
  245.     
  246.     set customFile [$customLevel findCustomFileVersion $name $type]
  247.     if [$customFile isNil] {
  248.     return
  249.     }
  250.  
  251.     $customLevel remove $customFile
  252. }
  253.  
  254.  
  255. # Install the file at the current custom level.
  256. #
  257. method InstallableCustomFile::install {this} {
  258.     if { ![$this incremental] } {
  259.     set sourcePath [$this sourcePath]
  260.     $this writeFrom $sourcePath
  261.     # if a source method was specified sourcePath is a temp file
  262.     if { [$this sourceMethod] != "" } {
  263.         BasicFS::removeFile $sourcePath
  264.     }
  265.     return
  266.     }
  267.  
  268.     # create custom file if it did not exists, otherwise fill temp file
  269.     # with old contents
  270.     set tmpFile [BasicFS::tmpFile]
  271.     if [[$this customFileVersion] isNil] {
  272.     $this createCustomFileVersion
  273.     } else {
  274.     $this readTo $tmpFile
  275.     }
  276.     
  277.     # append section to the file
  278.     $this appendSection $tmpFile
  279.  
  280.     # install
  281.     $this writeFrom $tmpFile
  282.     BasicFS::removeFile $tmpFile
  283. }
  284.  
  285.  
  286. # Uninstall file from current custom level.
  287. #
  288. method InstallableCustomFile::unInstall {this} {
  289.     if { ![$this incremental] } {
  290.     $this delete
  291.     return
  292.     }
  293.  
  294.     # get old contents
  295.     set tmpFile [BasicFS::tmpFile]
  296.     $this readTo $tmpFile
  297.     
  298.     # remove my contents
  299.     set tmpFile [$this removeSection $tmpFile]
  300.  
  301.     # if file is empty delete. otherwise write to file
  302.     set tmpDesc [open $tmpFile r]
  303.     set tmpContents [read $tmpDesc]
  304.     close $tmpDesc
  305.     if [regexp {[A-Za-z]} $tmpContents] {
  306.     $this writeFrom $tmpFile
  307.     } else {
  308.     $this delete
  309.     }
  310.  
  311.     BasicFS::removeFile $tmpFile
  312. }
  313.  
  314. # Do not delete this line -- regeneration end marker
  315.  
  316. method InstallableCustomFile::manager {this args} {
  317.     if {$args == ""} {
  318.         return [$this _manager]
  319.     }
  320.     set ref [$this _manager]
  321.     if {$ref != ""} {
  322.         [$ref _customFileSet] removeValue $this
  323.     }
  324.     set obj [lindex $args 0]
  325.     if {$obj != ""} {
  326.         [$obj _customFileSet] append $this
  327.     }
  328.     $this _manager $obj
  329. }
  330.  
  331.