If all is well, you now have a package calledMyFirstWebAppcontaining two classes,MyFirstWebAppandMyFirstWebAppTest. The first one should have 9 methods, the second 5. If you are unsure about your code, you can double
2.7 Saving Code to a Repository
Figure 2-12 Opening Monticello on your package
check with the full listing at the end of this document. Our Web app should now work as expected, and we have some tests to prove it.
But our code currently only lives in our development image. Let’s change that and move our code to a source code repository.
The Monticello Browser
For this we first have to use the Monticello Browser tool. In the first pane of the Nautilus Browser, click on the icon in front of your package named
MyFirstWebAppas shown in Figure 2-12.
Once opened, Monticello shows on it left pane the list of loaded packages. The currently selected one should be yours as depicted in Figure 2-13. The left pane of Monticello shows the list of repositories in which the cur- rently selected package can be saved. Indeed, Pharo uses distributed source code management. Your code can live on your local file system, or it can live on a server. As shown in Figure 2-13, by default, yourMyFirstWebApp
package can only be saved locally in a directory. We can easily add a re- mote repository. The main place for storing Pharo code is SmalltalkHub
Building and Deploying a Small Web application
Figure 2-13 The Monticello browser
http://www.smalltalkhub.com. Go over there and create yourself a new ac- count. Once you have an account, create a'MyFirstWebApp'project. You can leave the public option checked, it means that you and others can down- load the code without having to enter any credentials. Your project’s page should look like the one on Figure 2-14.
On this page, select and copy the Monticello registration template (make sure to copy the whole contents, including the username and password parts). Now, go back to Pharo and in Monticello, click on the+Repositorybutton (be sure that your package is selected in the left pane).
Select Smalltalkhub.com as repository type and overwrite the presented template with the one you just copied. It should look similar to Figure 2-15. Before accepting, fill in your user(name) and password (between the single quotes), the ones you gave during registration on SmalltalkHub.
Now, Monticello Browser shows you to select repositories to save your pack- age as shown in Figure 2-16.
You may have noticed that there is an asterisk (*) in front of your package name, indicating the package is dirty: i.e., it has uncommitted changes. By
2.7 Saving Code to a Repository
Figure 2-14 Smalltalkhub project page
clicking on the ’Changes’ button, Monticello will list everything that has changed or will tell you nothing has changed (this happens sometimes when Monticello gets out of sync). If Monticello finds actual changes, you will get a browser showing all the changes you made. Since this is the first version, all your changes are additions.
Committing to SmalltalkHub
Go back to the Monticello Browser and click the ’Save’ button (with your package and repository selected). Leave the version name, something like MyFirstWebApp-SvenVanCaekenberghe.1 alone, write a nice commit mes- sage in the second pane and press Accept to save your code to SmalltalkHub. When all goes well, you will see an upload progress bar and finally a version window that confirms the commit. You can close it later on.
If something goes wrong, you probably made a typo in your repository spec- ification. You can edit it by right-clicking on it in the Monticello Browser and selecting ‘Edit repository info’. If a save fails, you will get a Version Win- dow after some error message. Don’t close the Version Window. Your code now lives in your local package cache. Click the ‘Copy’ button and select your SmalltalkHub repository to try saving again.
Building and Deploying a Small Web application
Figure 2-15 Adding a Smalltalkhub repository in Monticello for your project.
You can now browse back to Smalltalkhub.com to confirm that your code arrived there.
After a successful commit, it is a good idea to save your image. In any case, your package should now no longer be dirty, and there should be no more differences between the local version and the one on SmalltalkHub. Defining a Project Configuration
Real software consists of several packages and will depend on extra external libraries and frameworks. In practice, software configuration management, including the management of dependencies and versions, is thus a neces- sity. To solve this problem, Pharo is using Metacello (the book Deep into Pharohttp://deepintopharo.comcontains a full chapter on it). And although we don’t really need it for our small example, we are going to use it anyway. Of course, we will not go into details as this is a complex subject.
To create a Metacello configuration, you define an object, what else did you expect? But we must respect some name conventions so Monticello can help us to generate part of this Metacello configuration. Open Monticello
2.7 Saving Code to a Repository
Figure 2-16 Multiple repositories for a project in Monticello
and click on the +Config button to add theConfigurationOfMyFirstWe- bAppconfiguration. With a right click on it, you can ”Browse configuration” which open a Nautilus browser on this newly created class. We are now going to define three methods: one defining a baseline for our configuration, one defining concrete package versions for that baseline, and one declaring that version as the stable released version. Here is the code:
ConfigurationOfMyFirstWebApp>>baseline1: spec <version: '1-baseline'>
spec for: #common do:[ spec blessing: #baseline; repository: 'http://smalltalkhub.com/mc/SvenVanCaekenberghe/MyFirstWebApp/main'; package: 'MyFirstWebApp' ] ConfigurationOfMyFirstWebApp>>version1: spec <version: '1' imports: #('1-baseline')> spec for: #common do: [
Building and Deploying a Small Web application blessing: #release; package: 'MyFirstWebApp' with: 'MyFirstWebApp-SvenVanCaekenberghe.1' ] ConfigurationOfMyFirstWebApp>>stable: spec <symbolicVersion: #'stable'> spec for: #common version: '1'
Once you committed the project (that consists in both the Metacello config- uration and the Monticello package'MyFirstWebApp'), you can test your configuration by trying to load it.
ConfigurationOfMyFirstWebApp load.
Of course, not much will happen since you already have the specified version loaded. For some feedback, make sure the Transcript is open and inspect the above expression.
Now add your SmalltalkHub repository to theConfigurationOfMyFirst- WebAppMonticello package. Double-check the changes in the Monticello Browser, remember we copied a whole class. Now commit by saving to your SmalltalkHub repository. Use the Web interface to verify that all went well.
2.8
Running a Real Cloud Server
So we created our first Web app and tested it locally. We stored our source code in the SmalltalkHub repository and created a Metacello configuration for it. Now we need a real cloud server to run our Web app.
It used to be hard and expensive to get access to a real server permanently connected to the internet. Not anymore: prices have comes down and op- erating cloud servers has become a much easier to use service. If you just want to test the deployment of this Pharo Web app, you can use cloud9 (http: //c9.io). It freely provides some testing environments after creating an ac- count. Note that cloud9 is for testing purpose only and that a real hosting solution such as Digital Ocean (http://www.digitalocean.com) is better. For this guide, we will be using Digital Ocean. The entry level server there, which is more than powerful enough for our experiment, costs just $5 a month. If you stop and remove the server after a couple of days, you will only pay cents. Go ahead and make yourself an account and register a credit card.
Create a Droplet
A server instance is called a Droplet. Click the ‘Create Droplet’ button and fill in the form. Pick a hostname, select the smallest size, pick a region close to you. As operating system image, we’ll be using a 32-bit Ubuntu Linux, ver- sion 13.04 x32. You can optionally use an SSH key pair to log in - it is a good