Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pharo: execute code at image startup when package is loaded

Tags:

pharo

I want to execute some code at image start-up time only if a package is loaded. I see that there exists

Smalltalk>>#addToStartUpList:

a method, which allows me to register a class which would provide startUp and shutDown hooks.

This is cool, but I don't want to execute this code manually. What's the recommended approach in Pharo?

like image 662
mircealungu Avatar asked Feb 06 '26 16:02

mircealungu


2 Answers

You need to put the code somewhere. The usual way to add this kind of registering is in the #initialize method of a class in your package.

For example, something like this:

MyClassWithStartUp class>>#initialize
    Smalltalk addToStartUpList: self

If your class MyClassWithStartUp is in the package you want to control... then it will be automatically registered when loaded.

like image 74
EstebanLM Avatar answered Feb 09 '26 12:02

EstebanLM


Here http://pharobyexample.org/drafts/Metacello.pdf in section 1.10 you can se how pre and post load scripts can be specified. You can use them to setup startup list.

The approach that I use is to check for the package if a script itself (and I put script in ~/Library/Preferences/pharo/.

For example consider this:

MCWorkingCopy allManagers 
  select: [ :e | e package name beginsWith: 'Renraku' ] 
  thenDo: [ :e |
    | repository |
    ...

If you want to do the same for a bunch of repositories, or you can use #detect for one and so on.

like image 22
Uko Avatar answered Feb 09 '26 12:02

Uko