9.4 The Package

A package can hold a set of classes part of the same class category.

Let’s save our Morphic-Learning category as a package using our Installed Packages Browser.

...World menu → Open...Installed Packages...

ch10-Package-with-requirement

Figure 9.7: Installed Packages Browser

Note that we invoked Feature require: 'SVG' earlier. Looking at the package names, we can observe several things of note:

Now this is important. When a packaged Feature is required, it may specify that it also requires other packages to work properly, and in fact to specify that those packages be up to a specific version level.

This is the key to being able to safely compose packages which have requirements.

OK, let’s click on the New button and type Morphic-Learning into the prompt. This results in a new package with the same name as our Morphic-Learning category. Note that this is version 1, revision 0 (1.0 at right) and that the package has yet to be saved.

ch10-NewPackage-Morphic-Learning

Figure 9.8: New Package – Morphic-Learning

Let’s say that in our clock morph we want to use color named as in the Color-Extra package. So to be able to load our Morphic-Learning package which makes use of this we need to select our new package and click on the add Requirement button at center, right.

This brings up a list of loaded packages to choose from.

ch10-Add-Requirement

Figure 9.9: Select package (or Cuis base version) to require

Now when we save our package, we see the pathname where the package file was created. We can now safely email this file, check it into a version control system, make a copy to our backup thumb drive.

ch10-Saved-with-requirement

Figure 9.10: Saved package – Morphic-Learning

As mentioned above, package files are just text files with a special format which Cuis-Smalltalk knows how to load. If you open a File List browser and view the package file, you will see information on how the package was created, what it provides and requires, and, if you filled in the comment box in the Installed Packages browser, a description.

 CuisLogo Type “Morphic Toys” into the comment box, re-save your package, and (re)select the package in a File List to see your package description.

Exercise 9.1: Describe a package

 CuisLogo If you have not already done so, create and save a Spacewar! package. There are no additional requirements to specify.

Exercise 9.2: Save Spacewar! package

There are other interesting things we can do with packages. We can include several class categories in a single package. Consider we want to span our CuisBook classes in two categories TheCuisBook-Models and TheCuisBook-Views. A new package created with the name TheCuisBook includes these two class categories; this label is a prefix to search for the matching categories to include in the package.

Therefore, often, a package comes with several categories to organize the classes in matching domains. We encourage to do so. When an application or framework grows, to keep a sound organisation, you may fell the need to reshape the class categories: rename, split, merge, etc. As long as you keep the same prefix in the class categories and the package name, your classes will be safe in the same package. In the System Browser, you can drag and drop any class in any class category to reorganize.

 CuisLogo Create a TheCuisBook package from the two class categories TheCuisBook-Models and TheCuisBook-Views. The former contains a TheBook class and the later a TheBookMorph class. Save the package on disk.

Exercise 9.3: Two class categories, one package

Imagine we need to print the page number of the TheBook table of contents as lower cased roman number, as we do with the printed version of this book. The code is very simple:

4 printStringRoman asLowercase
⇒ 'iv'

Instead of invoking this sequence of messages each time we need it, we add a dedicated message to the Integer class:

Integer>>printStringToc
   ^ self printStringRoman asLowercase

Now within our TheBook’s methods we just do things like:

../..
 aPage := Page new.
 aPage number: 1 printStringToc.
 ../..

Now we are facing a problem. For the need of the TheBook package we extend the Integer class with a method printStringToc, however this method addition is part of the Cuis-Smalltalk core system and its associated default change set. See Figure 9.11, the Change Sorter tool exactly shows that.

ch10-ChangeSetToCore

Figure 9.11: Change Sorter, supplementary method to core

Therefore when saving our TheBook package this method is not included and it is lost when quitting Cuis-Smalltalk. To include it in our package we categorize it in a method category with the *TheCuisBook prefix. *TheCuisBook-printing is a good candidate. In the System Browser method pane, over printStringToc, do ...Contextual menu → more...change category... and key in *TheCuisBook-printing.

Now the Change Sorter writes about Integer>>printStringToc: Method was moved to some other package. The Installed Packages tools now tells us we have an extension, use its browse button to get an update on the package contents.

ch10-BrowsePackageChange

Figure 9.12: Package with extension to the Integer class of the Kernel-Numbers system class category

Observe how each category – class or method one – of an extension is prefixed with a *.

 note In addition to adding a package preload requirement, you can also select a requirement and delete or update it using the buttons at the lower right. Sometimes a package changes which your code depends on and you have to change your code to accord. When this happens, to want to be sure to require the newer, changed version. Selecting a requirement and pressing update will update the requirement to use the latest loaded package version.