Skip to main content
Engineering LibreTexts

8.11: Executing Code Before and After Installation

  • Page ID
    45282
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Occasionally, you may find that you need to execute some code either before or after a package or project is loaded. For example, if you are installing a System Browser it would be a good idea to register it as default after it is loaded. Or maybe you want to open some workspaces after the installation.

    Metacello provides this feature by means of the messages preLoadDoIt: and postLoadDoIt:. The arguments to these messages are selectors of methods defined on the configuration class as shown below. For the moment, these pre- and post-scripts can be defined for a single package or for an entire project.

    Continuing with our example:

    ConfigurationOfCoolBrowser>>version08: spec
        <version: '0.8' imports: #('0.7-baseline')>
    
        spec for: #common do: [
            spec
                package: 'CoolBrowser-Core' with: [
                    spec
                        file: 'CoolBrowser-Core-BobJones.20';
                        preLoadDoIt: #preloadForCore;
                        postLoadDoIt: #postloadForCore:package: ];
                ....
                package: 'CoolBrowser-AddonsTests' with: 'CoolBrowser-AddonsTests-
         JohnLewis.1' ].
    
    ConfigurationOfCoolBrowser>>preloadForCore
        Transcript show: 'This is the preload script. Sorry I had no better idea'. 
    
    ConfigurationOfCoolBrowser>>postloadForCore: loader package: packageSpec
        Transcript cr;
            show: '#postloadForCore executed, Loader: ', loader printString,
                ' spec: ', packageSpec printString.
    
        Smalltalk at: #SystemBrowser ifPresent: [:cl | cl default: (Smalltalk classNamed:
         #CoolBrowser)].
    

    As you can notice, both methods, preLoadDoIt: and postLoadDoIt: receive a selector that will be performed before or after the load. You can also note that the method postloadForCore:package: takes two parameters. The pre/post load methods may take 0, 1 or 2 arguments. The loader is the first optional argument and the loaded packageSpec is the second optional argument. Depending on your needs you can choose which of those arguments you want.

    These pre and post load scripts can be used not only in version methods but also in baselines. If a script depends on a version, then you can put it there. If it is not likely to change among different versions, you can put it in the baseline method exactly in the same way.

    As mentioned, these pre and post load scripts can be at package level, but also at project level. For example, we can have the following configuration:

    ConfigurationOfCoolBrowser>>version08: spec
        <version: '0.8' imports: #('0.7-baseline')>
    
        spec for: #common do: [
            spec blessing: #release.
            
            spec preLoadDoIt: #preLoadForCoolBrowser.
            spec postLoadDoIt: #postLoadForCoolBrowser.
    
            spec
                package: 'CoolBrowser-Core' with: [
                    spec
                        file: 'CoolBrowser-Core-BobJones.20';
                        preLoadDoIt: #preloadForCore;
                        postLoadDoIt: #postloadForCore:package: ];
                package: 'CoolBrowser-Tests' with: 'CoolBrowser-Tests-JohnLewis.8';
                package: 'CoolBrowser-Addons' with: 'CoolBrowser-Addons-JohnLewis.6
        ';
                package: 'CoolBrowser-AddonsTests' with: 'CoolBrowser-AddonsTests-
        JohnLewis.1' ].
    

    In this example, we added pre and post load scripts at project level. Again, the selectors can receive 0, 1 or 2 arguments.


    This page titled 8.11: Executing Code Before and After Installation is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.