Skip to main content
Engineering LibreTexts

8.15: Conditional Loading

  • Page ID
    45551
  • \( \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}}\)

    When loading a project, usually the user wants to decide whether to load or not certain packages depending on a specific condition, for example, the existence of certain other packages in the image. Suppose you want to load Seaside in your image. Seaside has a tool that depends on OmniBrowser and it is used for managing instances of web servers. What can be done with this little tool can also be done by code. If you want to load such tool you need OmniBrowser. However, other users may not need such package. An alternative could be to provide different groups, one that includes such package and one that does not. The problem is that the final user should be aware of this and load different groups in different situations. With conditional loading you can, for example, load that Seaside tool only if OmniBrowser is present in the image. This will be done automatically by Metacello and there is no need to explicitly load a particular group.

    Suppose that our CoolToolSet starts to provide many more features. We first split the core in two packages: ’CoolToolSet-Core’ and ’CoolToolSet-CB’. CoolBrowser can be present in one image, but not in another one. We want to load the package ’CoolToolSet-CB’ by default only and if CoolBrowser is present.

    The mentioned conditionals are achieved in Metacello by using the project attributes we saw in the previous section. They are defined in the project method. Example:

    ConfigurationOfCoolBrowser >>project
        | |
        ^ project ifNil: [ | constructor |
            "Bootstrap Metacello if it is not already loaded"
            self class ensureMetacello.
            "Construct Metacello project"
            constructor := (Smalltalk at: #MetacelloVersionConstructor) on: self.
            project := constructor project.
            projectAttributes := ((Smalltalk at: #CBNode ifAbsent: []) == nil
                ifTrue: [ #( #’CBNotPresent’ ) ]
                ifFalse: [ #( #’CBPresent’ ) ]).
            project projectAttributes: projectAttributes.
            project loadType: #linear.
            project ]
    

    As you can see in the code, we check if CBNode class (a class from CoolBrowser) is present and depending on that we set a specific project attribute. This is flexible enough to let you define your own conditions and set the amount of project attributes you wish (you can define an array of attributes). Now the question is how to use these project attributes. In the following baseline we see an example:

    ConfigurationOfCoolToolSet >>baseline02: spec
        <version: '0.2-baseline'>
    
        spec for: #common do: [
            spec blessing: #baseline.
            spec repository: 'http://www.example.com/CoolToolSet'.
            spec project: 'CoolBrowser default' with: [
                    spec
                        className: 'ConfigurationOfCoolBrowser';
                        versionString: '1.0';
                        loads: #('default' );
                        file: 'CoolBrowser-Metacello';
                        repository: 'http://www.example.com/CoolBrowser' ];
                project: 'CoolBrowser Tests'
                    copyFrom: 'CoolBrowser default'
                    with: [ spec loads: #('Tests').].
            spec
                package: 'CoolToolSet-Core';
                package: 'CoolToolSet-Tests' with: [
                    spec requires: #('CoolToolSet-Core') ];
                package: 'CoolToolSet-CB';
    
            spec for: #CBPresent do: [
                spec
                    group: 'default' with: #('CoolToolSet-CB' )
                    yourself ].
    
            spec for: #CBNotPresent do: [
                spec
                    package: 'CoolToolSet-CB' with: [ spec requires: 'CoolBrowser default'
                ];
                    yourself ].
            ].
    

    You can notice that the way to use project attributes is through the existing method for:do:. Inside that method you can do whatever you want: define groups, dependencies, etc. In our case, if CoolBrowser is present, then we just add ’CoolToolSet-CB’ to the default group. If it is not present, then ’CoolBrowser default’ is added to dependency to ’CoolToolSet-CB’. In this case, we do not add it to the default group because we do not want that. If desired, the user should explicitly load that package also.

    Again, notice that inside the for:do: you are free to do whatever you want.


    This page titled 8.15: Conditional Loading 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.