[comp.lang.smalltalk] Smalltalk source example 1

new@udel.edu (Darren New) (10/06/89)

For your amusement/education:  in the spirit of Smalltalk sharing,
I present a few methods which I have needed during the development
of my Smalltalk project known as "GROPE".  Since changelists were
not persistant enough for my purposes, I had to make up my own 
routines to find the definitions I wanted to file out.

The first routine files out all classes of the category 
"GropeDiaplay" and also any method that references the
symbol #GropeDisplay.  These are suprisingly fast to find.
Note that a reference (like assignment) must actually
occur, or the compiler will compile it out. Just
saying  ! #GropeDisplay. ! at the beginning of a method
won't do it.

The second routine files out any routine that references
#GropeWiseDriver and also outputs any definitions that
have an instance variable who's name begins with the
string "grope". (I'm making changes to wise to make
it work with grope, and sometimes need to add an instance
variable.)  

The third routine finds all methods which have the given
string anywhere in the source for that method, including within
a comment.  Useful for finding (say) your initials in the classes
or methods you have changed in order to change them to use the
#GropeDisplay symbol instead.  This takes a while.  You can 
guess why I wrote it.
			    Happy Hacking
			    -- Darren

P.S., these should be read into a changelist and put somewhere.
They won't read into an image without the rest of Grope around.
-----------------------------------------------------------

'From Smalltalk-80, Version 2.2 of July 4, 1987 on 5 October 1989 at 3:25:44 pm'!



!GropeSpec class methodsFor: 'fileIn/Out'!

fileOutGrope
	"This will file out everything that is needed to put Grope (but not its  
	 drivers) into an image"
	"GropeSpec fileOutGrope"

	| gropeClasses file str sels |
	Smalltalk forgetDoIts.
	Smalltalk garbageCollect.
	GropeView storeQueueForms.
	gropeClasses _ SystemOrganization superclassOrder: 'Grope-Display'.
	file _ FileStream newFileNamed: 'Grope-Display.st'.
	file timeStamp.
	file nextChunkPut: '"File this in to load the basic GROPE display routines. Use other files for drivers."'.
	gropeClasses do: 
		[:cls | 
		file nextPut: Character cr.
		file nextPut: Character cr.
		cls
			fileOutOn: file
			moveSource: false
			toFile: 0].
	str _ 'GropeDisplay' asSymbol.
	Smalltalk allBehaviorsDo: [:cls | (cls whichSelectorsReferTo: str)
			do: 
				[:sel | 
				Transcript cr; show: cls name , '>>' , sel.
				cls
					fileOutMessage: sel
					on: file
					moveSource: false
					toFile: 0]].
	file nextChunkPut: '"End of Grope-Display.st"'.
	file shorten; close; release.
	Transcript cr; show: 'GROPE-DISPLAY saved!!'!

fileOutGropeWiseDriver
	"This will file out everything that is needed to put Grope's Wise driver 
	into an image"
	"GropeSpec fileOutGropeWiseDriver"

	| file str bool |
	str _ #GropeWiseDriver.
	Transcript clear; show: 'Filing out Grope/Wise driver...'; cr.
	Smalltalk forgetDoIts.
	Smalltalk garbageCollect.
	file _ FileStream newFileNamed: 'Grope-Wise-Driver.st'.
	file timeStamp.
	file nextChunkPut: '"File this in to load the WISE routines for displaying GROPE. FileIn Grope-Display.st first."'; cr.
	str _ 'GropeWiseDriver' asSymbol.
	file cr; nextChunkPut: '"-+-+-+-+-+-+-+-+-+-+- Here, the class definitions with new instance vars -+-+-+-+-+-+-+-+-+-+-"'; cr.
	Smalltalk allBehaviorsDo: [:cls | (cls whichSelectorsReferTo: str) isEmpty ifFalse: [('* grope*' match: cls instanceVariablesString)
				ifTrue: [file nextChunkPut: cls definition; cr]]].
	file cr; nextChunkPut: '"-+-+-+-+-+-+-+-+-+-+- Here, the method definitions for new or modified methods -+-+-+-+-+-+-+-+-+-+-"'; cr.
	Smalltalk allBehaviorsDo: [:cls | (cls whichSelectorsReferTo: str)
			do: 
				[:sel | 
				Transcript cr; show: cls name , '>>' , sel.
				cls
					fileOutMessage: sel
					on: file
					moveSource: false
					toFile: 0]].
	file nextChunkPut: '"End of GropeWiseDriver.st"'.
	file shorten; close; release.
	Transcript cr; show: 'GROPE-WISE DRIVER saved!!'!

findGropeParts: commentString 
	"Find all methods with the given string anywhere in their source and 
	browse the result."

	| i s |
	Transcript clear.
	Transcript show: CompiledMethod allInstances size printString.
	Transcript cr.
	i _ 0.
	Smalltalk
		browseAllSelect: 
			[:method | 
			i _ i + 1.
			i \\ 100 == 0 ifTrue: [Transcript show: i printString; cr].
			s _ method getSource.
			s notNil and: [(s findString: commentString startingAt: 1)
					> 0]]! !