Object subclass: #ScriptConsole instanceVariableNames: '' classVariableNames: 'IsRVM OutputToTranscript PrintTarget' poolDictionaries: '' category: 'Scripting'! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! ScriptConsole class instanceVariableNames: ''! !ScriptConsole class methodsFor: 'as yet unclassified'! << aString self print: aString! ! !ScriptConsole class methodsFor: 'as yet unclassified'! cr self println: ''.! ! !ScriptConsole class methodsFor: 'as yet unclassified' stamp: 'sm 6/1/2011 14:50'! detectAvailableMechanismForStdout "The RoarVM has its own primitive" | cls | cls := Smalltalk at: #RVMOperations ifAbsent: [nil]. IsRVM := (cls notNil and: [cls perform: #isRVM]). IsRVM ifTrue: [ PrintTarget := cls. ^ self. ] ifFalse: [ "Squeak 4.2 provides a stream at FileStream>>stdout" (FileStream respondsTo: #stdout) ifTrue: [ PrintTarget := FileStream stdout. ^ self. ]. ]. PrintTarget := nil. ^ nil! ! !ScriptConsole class methodsFor: 'as yet unclassified'! initialize OutputToTranscript := false. self detectAvailableMechanismForStdout.! ! !ScriptConsole class methodsFor: 'as yet unclassified'! outputToTranscript ^ OutputToTranscript! ! !ScriptConsole class methodsFor: 'as yet unclassified'! outputToTranscript: aBool OutputToTranscript := aBool! ! !ScriptConsole class methodsFor: 'as yet unclassified'! print: somethingAsString self print: somethingAsString withLineEnding: ''.! ! !ScriptConsole class methodsFor: 'as yet unclassified' stamp: 'sm 6/1/2011 14:47'! print: somethingAsString withLineEnding: lineEnd "Will output the somethingAsString to stdout using one of the available mechansims and to the transcript if requested" | output | output := somethingAsString asString. PrintTarget ifNotNil: [ IsRVM ifTrue: [ PrintTarget print: output. ] ifFalse: [ PrintTarget nextPutAll: (output, lineEnd) ]. ]. (OutputToTranscript or: [PrintTarget isNil]) ifTrue: [ Transcript show: output, lineEnd. ].! ! !ScriptConsole class methodsFor: 'as yet unclassified'! println: somethingAsString self print: somethingAsString withLineEnding: String crlf.! ! Object subclass: #ScriptStarter instanceVariableNames: '' classVariableNames: 'IsResuming QuitVM SafeBeforeQuit' poolDictionaries: '' category: 'Scripting'! !ScriptStarter commentStamp: 'StefanMarr 5/14/2011 16:29' prior: 0! ScriptStarter provides a startUp method and registers itself on the startUpList to enable scripting using command-line arguments. The first argument on the command-line is used to identify a class on which #run: is invoked with the arguments as an array. #run: can be used similar to classic main(String[] args) methods in other languages. ! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! ScriptStarter class instanceVariableNames: ''! !ScriptStarter class methodsFor: 'helper'! executeRunFor: scriptClassName with: arguments | scriptClass | scriptClass := Smalltalk classNamed: scriptClassName. scriptClass ifNotNil: [ scriptClass run: arguments. QuitVM ifTrue: [ Smalltalk snapshot: SafeBeforeQuit andQuit: true. ]. ].! ! !ScriptStarter class methodsFor: 'helper'! getSystemAttributes | arguments arg i | arguments := OrderedCollection new. i := 2. [[arg := SmalltalkImage current getSystemAttribute: i] value == nil] whileFalse: [arguments addLast: arg. i := i + 1]. ^ arguments! ! !ScriptStarter class methodsFor: 'initialization' stamp: 'sm 5/30/2011 22:33'! initialize "Automatically install ScriptStarter when the code is filed in." self install. QuitVM := true. SafeBeforeQuit := false. IsResuming := false.! ! !ScriptStarter class methodsFor: 'system startup entry' stamp: 'sm 5/25/2011 12:41'! install "Will register ScriptStarter on the StartUpList to activate it on VM startup." | pref | Smalltalk addToStartUpList: ScriptStarter. "Disable the loading of documents on startup" pref := Smalltalk at: #Preferences ifAbsent: [nil]. pref ifNotNil: [ (pref respondsTo: #disable:) ifTrue: [ pref disable: #readDocumentAtStartup. ] ] ifNil: [ (Smalltalk globals at: #AbstractLauncer ifAbsent: [nil]) ifNotNilDo: [:launcher | launcher readDocumentAtStartup: false ] ].! ! !ScriptStarter class methodsFor: 'system startup entry' stamp: 'sm 5/30/2011 22:35'! startUp "startUp is invoked during startup. ScriptStarter has to be registered in the SystemDirectory's StartUpList See ScriptStarter>>install" | arguments scriptClassName | "Ok, only, and really only during startup" IsResuming ifFalse: [ Transcript show: 'ScriptStarter did not execute a given script, since it assumes to be executed after a snapshot and not during startup.'. ^ self. ]. "First make sure that stdout output can be used if available." ScriptConsole detectAvailableMechanismForStdout. arguments := self getSystemAttributes. arguments size > 0 ifTrue: [scriptClassName := arguments at: 1. self executeRunFor: scriptClassName with: arguments.]! ! !ScriptStarter class methodsFor: 'system startup entry' stamp: 'sm 5/30/2011 22:30'! startUp: resuming IsResuming := resuming. ^ super startUp: resuming.! ! !ScriptStarter class methodsFor: 'system startup entry'! uninstall "Will remove ScriptStarter from the StartUpList" Smalltalk removeFromStartUpList: ScriptStarter. self flag: 'TODO: restore original setting for readDocumentAtStartup, see >>install.'. Transcript show: 'You might need to restore the original setting for readDocumentAtStartup'.! ! !ScriptStarter class methodsFor: 'configuration'! quitVMAfterScriptExecution ^ QuitVM! ! !ScriptStarter class methodsFor: 'configuration'! quitVMAfterScriptExecution: aBool QuitVM := aBool! ! !ScriptStarter class methodsFor: 'configuration'! saveImageBeforeQuit ^ SafeBeforeQuit! ! !ScriptStarter class methodsFor: 'configuration'! saveImageBeforeQuit: aBool SafeBeforeQuit := aBool! ! !ScriptStarter class methodsFor: 'transporter' stamp: 'sm 2/13/2011 19:09'! transportersForFileOutMenu ^ { Transporter forPackage: (PackageInfo named: 'Scripting') }! ! ScriptConsole initialize! ScriptStarter initialize! Object subclass: #HelloWorld instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Scripting-Examples'! !HelloWorld commentStamp: 'StefanMarr 5/14/2011 16:42' prior: 0! Simple example for how to use ScriptStarter. HelloWorld prints all arguments given to #run:.! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! HelloWorld class instanceVariableNames: ''! !HelloWorld class methodsFor: 'script entry'! run: arguments ScriptConsole print: 'Hello World!!'. arguments do: [:elem | ScriptConsole print: elem]. SmalltalkImage current snapshot: false andQuit: true! !