/
MCPackageManager.class.st
315 lines (268 loc) · 9.24 KB
/
MCPackageManager.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"
MCPackageManager is a kind of package wrapper knowing if the package is dirty or not.
The class side manages registered package managers.
Instance Variables:
package <MCPackage>
modified <Boolean>
Class Instance Variables:
registry <Dictionary>
"
Class {
#name : #MCPackageManager,
#superclass : #Model,
#instVars : [
'package',
'modified'
],
#classVars : [
'PrivateAnnouncer'
],
#classInstVars : [
'registry'
],
#category : #'Monticello-Versioning'
}
{ #category : #accessing }
MCPackageManager class >> allManagers [
^ self registry values
]
{ #category : #private }
MCPackageManager class >> announcer [
^PrivateAnnouncer ifNil: [ SystemAnnouncer uniqueInstance ]
]
{ #category : #private }
MCPackageManager class >> announcer: anAnnouncer [
PrivateAnnouncer := anAnnouncer
]
{ #category : #'system changes' }
MCPackageManager class >> bestMatchingManagerForCategory: aSystemCategory do: aBlock [
| bestMatches bestMatchingManagerAndPackage |
bestMatches := OrderedCollection new.
self registry do: [ :mgr |
| candidatePackages bestMatchingPackage |
candidatePackages := mgr packageSet packages select: [ :package |
package name asUppercase beginsWith: aSystemCategory asUppercase ].
bestMatchingPackage := candidatePackages detectMin: [ :package |
package name size ].
bestMatchingPackage ifNotNil: [
bestMatches add: mgr -> bestMatchingPackage ] ].
bestMatchingManagerAndPackage := bestMatches detectMin: [ :managerAndPackage |
managerAndPackage value name size ].
bestMatchingManagerAndPackage ifNotNil: [ :managerAndPackage |
aBlock value: managerAndPackage key ]
]
{ #category : #'system changes' }
MCPackageManager class >> classModified: anEvent [
self
managersForClass: anEvent classAffected
do:[ :mgr | mgr modified: true ].
]
{ #category : #'system changes' }
MCPackageManager class >> classMoved: anEvent [
self managersForPackage: anEvent oldPackage do: [ :mgr | mgr modified: true ].
self managersForPackage: anEvent newPackage do: [ :mgr | mgr modified: true ].
]
{ #category : #'system changes' }
MCPackageManager class >> classRemoved: anEvent [
"Informs the registry who use to keep this class that its changed. Unlike #classModified:, class is not anymore in RPackages so it will not be found, that's why we look for system category instead if class is included or not"
"Note. Class protocols returns symbols and not Protocol instances!"
(RPackageOrganizer default
packageMatchingExtensionName: anEvent categoryName) ifNotNil: [ :package |
package mcWorkingCopy ifNotNil: [ :workingCopy | workingCopy modified: true ] ].
anEvent classAffected protocols, anEvent classAffected classSide protocols
do: [ :aProtocolName |
(aProtocolName beginsWith: '*')
ifTrue:
[ (RPackageOrganizer default
packageMatchingExtensionName: aProtocolName allButFirst trimBoth)
ifNotNil: [ :package |
package mcWorkingCopy ifNotNil: [ :workingCopy |
workingCopy modified: true ] ] ] ]
]
{ #category : #'system changes' }
MCPackageManager class >> classRenamed: anEvent [
self classModified: anEvent.
anEvent classAffected extendingPackages
do: [ :pkg | self managersForPackage: pkg do: [ :mgr | mgr modified: true ] ]
]
{ #category : #accessing }
MCPackageManager class >> forPackage: aPackage [
^ self registry
at: aPackage
ifAbsent: [|mgr|
mgr := self new initializeWithPackage: aPackage.
self registry at: aPackage put: mgr.
self announcer announce: (MCWorkingCopyCreated workingCopy: mgr package: aPackage).
mgr ]
]
{ #category : #initialize }
MCPackageManager class >> initialize [
"Remove this later"
self unregisterForNotifications.
]
{ #category : #'system changes' }
MCPackageManager class >> managersForCategory: aSystemCategory do: aBlock [
"Got to be careful here - we might get method categories where capitalization is problematic."
| cat foundOne index |
foundOne := false.
cat := aSystemCategory ifNil: [ ^nil ]. "yes this happens; for example in eToy projects"
"first ask PackageInfos, their package name might not match the category"
self
bestMatchingManagerForCategory: aSystemCategory
do: [ :mgr |
aBlock value: mgr.
foundOne := true ].
foundOne ifTrue: [ ^self ].
[ "Loop over categories until we found a matching one"
self registry keys detect: [ :aPackage | aPackage name sameAs: cat ]
ifFound: [:aPackage | | mgr |
mgr := self registry at: aPackage.
aBlock value: mgr.
foundOne := true.
false]
ifNone: [
index := cat lastIndexOf: $-.
index > 0]
] whileTrue: [
"Step up to next level package"
cat := cat copyFrom: 1 to: index-1 ].
foundOne ifFalse: [
"Create a new (but only top-level)"
aBlock value: (MCWorkingCopy forPackage: (MCPackage named: aSystemCategory capitalized)).
].
]
{ #category : #'system changes' }
MCPackageManager class >> managersForClass: aClass category: methodCategory do: aBlock [
(methodCategory isEmptyOrNil or:[methodCategory first ~= $*]) ifTrue:[
"Not an extension method"
^self managersForClass: aClass do: aBlock.
].
self managersForCategory: methodCategory allButFirst do: aBlock.
]
{ #category : #'system changes' }
MCPackageManager class >> managersForClass: aClass do: aBlock [
| rPackage |
rPackage := aClass package.
self registry do: [:mgr |
(mgr packageSet packages includes: rPackage)
ifTrue: [aBlock value: mgr]]
]
{ #category : #'system changes' }
MCPackageManager class >> managersForClass: aClass selector: aSelector do: aBlock [
^self managersForClass: aClass category: (aClass organization categoryOfElement: aSelector) do: aBlock
]
{ #category : #'system changes' }
MCPackageManager class >> managersForPackage: aPackage do: aBlock [
self registry do: [:mgr |
(mgr packageSet includesSystemCategory: aPackage name) ifTrue: [
aBlock value: mgr.
]
].
]
{ #category : #'system changes' }
MCPackageManager class >> methodModified: anEvent [
"If the method has just been loaded by monticello itself, do not mark it as dirty"
anEvent propertyAt: #eventSource ifPresent: [ :source |
source == self class package name
ifTrue: [ ^ self ]].
"trait methods aren't handled here"
anEvent isProvidedByATrait
ifTrue: [ ^ self ].
^ self
managersForClass: anEvent methodClass
selector: anEvent selector
do: [ :mgr | mgr modified: true ]
]
{ #category : #'system changes' }
MCPackageManager class >> methodMoved: anEvent [
self managersForPackage: anEvent oldPackage do: [ :mgr | mgr modified: true ].
self managersForPackage: anEvent newPackage do: [ :mgr | mgr modified: true ].
]
{ #category : #'system changes' }
MCPackageManager class >> methodRemoved: anEvent [
self managersForClass: anEvent methodClass category: anEvent protocol do:[:mgr| mgr modified: true].
]
{ #category : #'system changes' }
MCPackageManager class >> packageRenamed: anAnnouncement [
self allManagers
detect: [ :each | each packageName = anAnnouncement newName ]
ifFound: [ :newPackage | newPackage modified: true ].
(self allManagers
detect: [ :each | each packageName = anAnnouncement oldName ])
unload.
]
{ #category : #'event registration' }
MCPackageManager class >> registerInterestOnSystemChangesOnAnnouncer: anAnnouncer [
anAnnouncer weak
when: RPackageRenamed send: #packageRenamed: to: self;
when: ClassAdded, ClassModifiedClassDefinition, ClassCommented , ClassParentRenamed send: #classModified: to: self;
when: ClassRenamed send:#classRenamed: to:self;
when: ClassRepackaged send: #classMoved: to: self;
when: ClassRemoved send: #classRemoved: to: self;
when: MethodAdded, MethodModified, MethodRecategorized send: #methodModified: to: self;
when: MethodRepackaged send: #methodMoved: to: self;
when: MethodRemoved send: #methodRemoved: to: self.
]
{ #category : #accessing }
MCPackageManager class >> registry [
^ registry ifNil: [registry := Dictionary new]
]
{ #category : #'event registration' }
MCPackageManager class >> unregisterForNotifications [
SystemAnnouncer uniqueInstance unsubscribe: self
]
{ #category : #private }
MCPackageManager >> announcer [
^self class announcer
]
{ #category : #initialization }
MCPackageManager >> initialize [
super initialize.
modified := false.
]
{ #category : #initialization }
MCPackageManager >> initializeWithPackage: aPackage [
package := aPackage.
self initialize.
]
{ #category : #accessing }
MCPackageManager >> modified [
^ modified
]
{ #category : #accessing }
MCPackageManager >> modified: aBoolean [
modified = aBoolean
ifTrue: [ ^ self ].
modified := aBoolean.
self changed: #modified.
modified
ifFalse: [
ChangesLog default logChange: '"' , self packageName , '"' ].
self announcer announce: (MCPackageModified package: self package)
]
{ #category : #accessing }
MCPackageManager >> package [
^ package
]
{ #category : #accessing }
MCPackageManager >> packageName [
^ package name
]
{ #category : #accessing }
MCPackageManager >> packageNameWithStar [
^ modified
ifTrue: ['* ', self packageName]
ifFalse: [self packageName]
]
{ #category : #accessing }
MCPackageManager >> packageSet [
^ package packageSet
]
{ #category : #operations }
MCPackageManager >> unregister [
self class registry
removeKey: package
ifAbsent: [ ^ self ].
self class changed: (Array with: #unregistered with: package).
self announcer announce: (MCWorkingCopyDeleted workingCopy: self package: package)
]