mircealungu
mircealungu

Reputation: 6981

Pharo: execute code at image startup when package is loaded

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?

Upvotes: 3

Views: 335

Answers (2)

EstebanLM
EstebanLM

Reputation: 4357

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.

Upvotes: 3

Uko
Uko

Reputation: 13386

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.

Upvotes: 2

Related Questions