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
...
Note that we invoked Feature require: 'SVG'
earlier. Looking at the package names, we can observe several things
of note:
SVG
has version
1, revision 19.
YAXO
, Color-Extras
and
LinearAlgebra
which we never asked for.
SVG
.
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.
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.
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.
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.
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.
If you have not already done so, create and save a Spacewar! package. There are no additional requirements to specify.
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.
Create a
TheCuisBook
package from the two class categoriesTheCuisBook-Models
andTheCuisBook-Views
. The former contains aTheBook
class and the later aTheBookMorph
class. Save the package on disk.
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.
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.
Observe how each category – class or method one – of an extension is
prefixed with a *
.
In addition to adding a package preload requirement, you can also select a requirement and
delete
orupdate
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 pressingupdate
will update the requirement to use the latest loaded package version.