1 define: #Platform &slots: {
7 #endianness -> #LittleEndian.
11 p@(Platform traits) update
14 [name := lobby systemPlatform as: String.
15 nodeName := lobby systemName as: String.
16 systemRelease := lobby systemRelease as: String.
17 systemVersion := lobby systemVersion as: String.
18 machine := lobby systemMachine as: String.
19 endianness := lobby isLittleEndian ifTrue: [#LittleEndian] ifFalse: [#BigEndian].
20 bytesPerWord := lobby bytesPerWord. ]
23 p@(Platform traits) current
26 Platform traits Current ::= Platform current.
28 Platform traits PosixNames ::= #{#Linux. #Darwin. #Cygwin. #FreeBSD. #NetBSD. #OpenBSD. #SunOS. #BeOS. #Haiku}.
30 Platform traits WindowsNames ::= #{#Win32. #Win64}.
32 p@(Platform traits) isPosix
33 "Whether the underlying platform implements (enough of?) the POSIX API."
34 [p PosixNames anySatisfy: [| :name | (name as: p name) = p name]].
36 p@(Platform traits) isWindows
37 "Whether the underlying platform implements (enough of?) the POSIX API."
38 [p WindowsNames anySatisfy: [| :name | (name as: p name) = p name]].
40 p@(Platform traits) lineEnding
42 p isPosix ifTrue: ['\n'] ifFalse: [p isWindows ifTrue: ['\r\n'] ifFalse: ['\r']]
45 s@(String traits) escapeForShell
46 "Performs shell-escaping for POSIX-style command shells."
50 (';<>*|`&$#!()[]{}: \\\'"' includes: char) ifTrue: [result nextPut: $\\].
55 p@(Platform traits) run: command
56 [lobby systemExecute: command].
58 Image startupActions at: #Platform put:
60 Platform Current update
63 define: #Environment &parents: {Mapping. ExtensibleCollection} &slots: {
64 #variables -> Dictionary new
67 p@(Platform traits) environment [Environment].
69 Image startupActions at: #Environment put:
72 e@(Environment traits) update
74 variables ::= lobby environmentVariables.
75 e variables := e variables newSizeOf: variables.
78 eachString ::= each as: String.
79 equalsIndex ::= eachString indexOf: $= ifAbsent: [eachString length].
80 key ::= eachString first: equalsIndex.
81 value ::= eachString allButFirst: equalsIndex + 1.
82 e variables at: key := value].
85 e@(Environment traits) size
90 e@(Environment traits) capacity
95 _@(Environment traits) acceptsKey: _
98 _@(Environment traits) acceptsKey: _@(String traits)
101 _@(Environment traits) accepts: _
104 _@(Environment traits) accepts: _@(String traits)
107 _@(Environment traits) defaultElement
110 e@(Environment traits) at: name ifAbsent: block
112 e variables at: name ifAbsent: block
115 e@(Environment traits) at: name put: value
117 lobby environmentAt: name put: value.
118 e variables at: name put: value
121 e@(Environment traits) removeKey: name ifAbsent: block
123 (e variables includesKey: name) ifTrue: [lobby environmentRemoveKey: name].
124 e variables removeKey: name ifAbsent: block
127 e@(Environment traits) keyAtValue: obj ifAbsent: block
129 e variables keyAtValue: obj ifAbsent: block
132 e@(Environment traits) keysAndValuesDo: block
134 e variables keysAndValuesDo: block
137 e@(Environment traits) allSatisfy: block
139 e variables allSatisfy: block
142 e@(Environment traits) printOn: s [s ; e printName].
144 e@(Environment traits) homePath [e at: 'HOME'].
145 e@(Environment traits) currentPath [e at: 'PWD'].
146 e@(Environment traits) temporaryPath [e at: 'TMPDIR'].
147 e@(Environment traits) includedPaths
148 "Where the shell expects to find binary executables."
149 [(e at: 'PATH') ifNil: [#{}] ifNotNilDo: #(splitWith: $:) `er].
150 e@(Environment traits) userName [e at: 'LOGNAME'].
151 e@(Environment traits) terminalType [e at: 'TERM'].
153 e@(Environment traits) sourcePaths
154 "Where Slate expects to find code."
155 [(e at: 'SLATEPATH') ifNil: [#{}] ifNotNilDo: #(splitWith: $:) `er].
157 "Load defaultPaths addAll:
158 (Environment sourcePaths collect: [| :each | each as: File Locator])."
160 p@(Platform traits) edit: m@(Method traits) &editor &reload
163 (p environment at: 'EDITOR' ifAbsent:
164 [(query: 'Select an editor for files: ')
166 [| :editor | p environment at: 'EDITOR' put: editor. editor]]).
167 reload `defaultsTo: False.
170 [warn: 'No source information available.'. False]
173 src source resource locator
175 [warn: 'File-based source information unavailable.'. False]
178 result ::= p run: editor
179 ; (src lineNumber ifNil: [''] ifNotNilDo: [| :n | ' +' ; n printString])
180 ; ' ' ; (fileName as: String).
181 result /\ reload ifTrue: [m reload].
185 p@(Platform traits) view: f@(File traits) &pager
188 (p environment at: 'PAGER' ifAbsent:
189 [(query: 'Select a viewer for files: ')
191 [| :pager | p environment at: 'PAGER' put: pager. pager]]).
192 p run: pager ; ' ' ; (f locator as: String)
195 p@(Platform traits) browse: url &browser
198 (p environment at: 'BROWSER' ifAbsent:
199 [(query: 'Select a browser for URLs: ')
201 [| :browser | p environment at: 'BROWSER' put: browser. browser]]).
202 p run: browser ; ' ' ; (s as: String)
205 shell ::= lobby newSubSpace.
207 _@shell didNotUnderstand: message at: position
208 "Form a command string and execute it."
214 message selector isUnarySelector
216 [command ; message selector.
217 message optionals pairsDo:
219 command ; ' -' ; (key as: String) allButFirst allButLast ; ' '.
220 value = True ifFalse: [command ; (value as: String)]]].
221 message selector isKeywordSelector ifTrue:
222 [keywords ::= message selector keywords.
223 command ; keywords first.
224 keywords size = 1 ifTrue: "Read a string or array of arguments."
225 [args ::= message arguments second.
227 [args is: String] -> [command ; ' ' ; args].
228 [args is: Collection] -> [args do: [| :arg | command ; ' ' ; arg]]
230 message arguments allButFirst: message selector arity do:
231 [| :arg | command ; ' ' ; (arg as: String)]] writingAs: String)
232 ifNil: [resend] ifNotNilDo: [| :cmd | [Platform run: cmd]]]