Moving Class to a Package programmaticaly

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Moving Class to a Package programmaticaly

HilaireFernandes
Hello,

I want to move class from package "MyPackage-subPackage" to
"MyPackage-Classes" with the code bellow:

org := (RPackageOrganizer default packageNamed: 'MyPackage').
org addClassTag: #Classes.
org definedClasses do: [ :aClass | |tag|
    tag := org classTagForClass: aClass.
    org addClassDefinition: aClass toClassTag: #Classes.
    org removeClassDefinition: aClass fromClassTag: tag name.
 ].


It produces this result when browsed with Nautilus, but the class
definition still show the old package:

Object subclass: #MyClass
    instanceVariableNames: ''
    classVariableNames: ''
    package: 'MyPackage-subPackage'

Therefore when saving the package the old package is still represented.

How to do it?


Thanks

Hilaire

--

Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Moving Class to a Package programmaticaly

Thierry Goubier
Hi Hilaire,

what you are describing is moving a class from a package tag to another
package tag in the same package.

And you are manipulating directly the RPackageOrganizer for that.

As RPackage was designed, to avoid infinite recursion, some operations
apply only onto the RPackageOrganizer itself and do not change system
categories, and, as such, are not be used directly. It is wiser to do
the change via the system categories, and RPackage will pick it up
correctly. You will also benefit from a proper system announcements as well.

This should work:

org := (RPackageOrganizer default packageNamed: 'MyPackage').
tag := org addClassTag: #Classes.
org definedClasses do: [ :aClass |
        aClass category: tag categoryName   ].

Regards,

Thierry

Le 26/05/2017 à 18:36, Hilaire a écrit :

> Hello,
>
> I want to move class from package "MyPackage-subPackage" to
> "MyPackage-Classes" with the code bellow:
>
> org := (RPackageOrganizer default packageNamed: 'MyPackage').
> org addClassTag: #Classes.
> org definedClasses do: [ :aClass | |tag|
>      tag := org classTagForClass: aClass.
>      org addClassDefinition: aClass toClassTag: #Classes.
>      org removeClassDefinition: aClass fromClassTag: tag name.
>   ].
>
>
> It produces this result when browsed with Nautilus, but the class
> definition still show the old package:
>
> Object subclass: #MyClass
>      instanceVariableNames: ''
>      classVariableNames: ''
>      package: 'MyPackage-subPackage'
>
> Therefore when saving the package the old package is still represented.
>
> How to do it?
>
>
> Thanks
>
> Hilaire
>


Reply | Threaded
Open this post in threaded view
|

Re: Moving Class to a Package programmaticaly

Denis Kudriashov

2017-05-26 19:22 GMT+02:00 Thierry Goubier <[hidden email]>:

This should work:

org := (RPackageOrganizer default packageNamed: 'MyPackage').
tag := org addClassTag: #Classes.
org definedClasses do: [ :aClass |
        aClass category: tag categoryName   ].

In Pharo 6 there is new tag API for classes:

#MyPackage asPackage definedClasses do: [:each | each tagWith: #Classes]
Reply | Threaded
Open this post in threaded view
|

Re: Moving Class to a Package programmaticaly

Thierry Goubier


2017-05-27 13:58 GMT+02:00 Denis Kudriashov <[hidden email]>:

2017-05-26 19:22 GMT+02:00 Thierry Goubier <[hidden email]>:

This should work:

org := (RPackageOrganizer default packageNamed: 'MyPackage').
tag := org addClassTag: #Classes.
org definedClasses do: [ :aClass |
        aClass category: tag categoryName   ].

In Pharo 6 there is new tag API for classes:

#MyPackage asPackage definedClasses do: [:each | each tagWith: #Classes]

Will:

#MyPackage asPackage definedClasses do: [:each | each tagWith: #MyPackage]

put the classes in the #MyPackage or in the #MyPackage-MyPackage system category?

Will that API stay the same when we will be able to put a class in multiple categories (or a method in multiple protocols)?

Regards,

Thierry
Reply | Threaded
Open this post in threaded view
|

Re: Moving Class to a Package programmaticaly

Denis Kudriashov

2017-05-27 14:15 GMT+02:00 Thierry Goubier <[hidden email]>:
Will:

#MyPackage asPackage definedClasses do: [:each | each tagWith: #MyPackage]

put the classes in the #MyPackage or in the #MyPackage-MyPackage system category?

#tagWith: not touches package of class. It only adds receiver into package tag.
I not understand the question about system category because I don't know what it is exactly in current system.

Now in class definition we have "package: " part. And it will be modified with new tag-suffix. It will become "package: 'ActualPackageName-TagName'"
   

Will that API stay the same when we will be able to put a class in multiple categories (or a method in multiple protocols)?

Yes but #tagWith: will add extra tag into class/method. It will not remove existing tags.
But in Pharo6 it is only new API. Multiple tags are not supported and #tagWith: removes existing tag.

Reply | Threaded
Open this post in threaded view
|

Re: Moving Class to a Package programmaticaly

Thierry Goubier


2017-05-27 14:45 GMT+02:00 Denis Kudriashov <[hidden email]>:

2017-05-27 14:15 GMT+02:00 Thierry Goubier <[hidden email]>:
Will:

#MyPackage asPackage definedClasses do: [:each | each tagWith: #MyPackage]

put the classes in the #MyPackage or in the #MyPackage-MyPackage system category?

#tagWith: not touches package of class. It only adds receiver into package tag.
I not understand the question about system category because I don't know what it is exactly in current system.

The package and tag names are translated into a 'PackageName-TagName' system category.

But: the tag #PackageName has a special meaning in RPackage (for now). It means : what is defined in the package, and not in any other tag. 


Now in class definition we have "package: " part. And it will be modified with new tag-suffix. It will become "package: 'ActualPackageName-TagName'"

Just like it is now in system category then.
 
   

Will that API stay the same when we will be able to put a class in multiple categories (or a method in multiple protocols)?

Yes but #tagWith: will add extra tag into class/method. It will not remove existing tags.
But in Pharo6 it is only new API. Multiple tags are not supported and #tagWith: removes existing tag.

The problem I foresee is that at a certain point in the future, not advertised, that semantic will change to not mean 'remove existing tag'.

If I start using that API, I'll need to add a test in my stuff that will flag that API semantic change without name change.

Thierry


Reply | Threaded
Open this post in threaded view
|

Re: Moving Class to a Package programmaticaly

Denis Kudriashov

2017-05-27 15:05 GMT+02:00 Thierry Goubier <[hidden email]>:
The problem I foresee is that at a certain point in the future, not advertised, that semantic will change to not mean 'remove existing tag'.

If I start using that API, I'll need to add a test in my stuff that will flag that API semantic change without name change.

That's true. 
Also you can think that multiple tags are working and implement your code with exta untag logic to ensure singe tag property:
aClass untagFrom: #existingTag.
aClass tagWith: #newTag   
Reply | Threaded
Open this post in threaded view
|

Re: Moving Class to a Package programmaticaly

HilaireFernandes
In reply to this post by Thierry Goubier
Thanks Thierry


Le 26/05/2017 à 19:22, Thierry Goubier a écrit :
>
> This should work:

--
Dr. Geo
http://drgeo.eu