AmberCli.st 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. Smalltalk current createPackage: 'AmberCli'!
  2. Object subclass: #AmberCli
  3. instanceVariableNames: ''
  4. package: 'AmberCli'!
  5. !AmberCli commentStamp!
  6. I am the Amber CLI (CommandLine Interface) tool which runs on Node.js.
  7. My responsibility is to start different Amber programs like the FileServer or the Repl.
  8. Which program to start is determined by the first commandline parameters passed to the AmberCli executable.
  9. Use `help` to get a list of all available options.
  10. Any further commandline parameters are passed to the specific program.
  11. ## Commands
  12. New commands can be added by creating a class side method in the `commands` protocol which takes one parameter.
  13. This parameter is an array of all commandline options + values passed on to the program.
  14. Any `camelCaseCommand` is transformed into a commandline parameter of the form `camel-case-command` and vice versa.!
  15. !AmberCli class methodsFor: 'commandline'!
  16. commandLineSwitches
  17. "Collect all methodnames from the 'commands' protocol of the class
  18. and select the ones with only one parameter.
  19. Then remove the ':' at the end of the name.
  20. Additionally all uppercase letters are made lowercase and preceded by a '-'.
  21. Example: fallbackPage: becomes --fallback-page.
  22. Return the Array containing the commandline switches."
  23. | switches |
  24. switches := ((self class methodsInProtocol: 'commands') collect: [ :each | each selector]).
  25. switches := switches select: [ :each | each match: '^[^:]*:$'].
  26. switches :=switches collect: [ :each |
  27. (each allButLast replace: '([A-Z])' with: '-$1') asLowercase].
  28. ^switches
  29. !
  30. handleArguments: args
  31. | selector |
  32. selector := self selectorForCommandLineSwitch: (args first).
  33. args remove: args first.
  34. self perform: selector withArguments: (Array with: args)
  35. !
  36. selectorForCommandLineSwitch: aSwitch
  37. "Add ':' at the end and replace all occurences of a lowercase letter preceded by a '-' with the Uppercase letter.
  38. Example: fallback-page becomes fallbackPage:.
  39. If no correct selector is found return 'help:'"
  40. | command selector |
  41. (self commandLineSwitches includes: aSwitch)
  42. ifTrue: [ selector := (aSwitch replace: '-[a-z]' with: [ :each | each second asUppercase ]), ':']
  43. ifFalse: [ selector := 'help:' ].
  44. ^selector
  45. ! !
  46. !AmberCli class methodsFor: 'commands'!
  47. help: args
  48. console log: 'Available Commands:'.
  49. self commandLineSwitches do: [ :each | console log: each ]
  50. !
  51. repl: args
  52. ^Repl new createInterface
  53. !
  54. serve: args
  55. ^(FileServer createServerWithArguments: args) start
  56. ! !
  57. !AmberCli class methodsFor: 'startup'!
  58. main
  59. "Main entry point for Amber applications.
  60. Parses commandline arguments and starts the according subprogram."
  61. | args nodeMinorVersion |
  62. nodeMinorVersion := ((process version) tokenize: '.') second asNumber.
  63. nodeMinorVersion < 8 ifTrue: [
  64. console log: 'You are currently using Node.js ', (process version).
  65. console log: 'Required is at least Node.js v0.8.x or greater.'.
  66. ^ -1.
  67. ].
  68. args := process argv.
  69. "Remove the first args which contain the path to the node executable and the script file."
  70. args removeFrom: 1 to: 2.
  71. (args isEmpty)
  72. ifTrue: [self help: nil]
  73. ifFalse: [^self handleArguments: args]
  74. ! !
  75. Object subclass: #FileServer
  76. instanceVariableNames: 'path http fs url host port basePath util username password fallbackPage'
  77. package: 'AmberCli'!
  78. !FileServer commentStamp!
  79. I am the Amber Smalltalk FileServer.
  80. My runtime requirement is a functional Node.js executable.
  81. To start a FileServer instance on port `4000` use the following code:
  82. FileServer new start
  83. A parameterized instance can be created with the following code:
  84. FileServer createServerWithArguments: options
  85. Here, `options` is an array of commandline style strings each followed by a value e.g. `#('--port', '6000', '--host', '0.0.0.0')`.
  86. A list of all available parameters can be printed to the commandline by passing `--help` as parameter.
  87. See the `Options` section for further details on how options are mapped to instance methods.
  88. After startup FileServer checks if the directory layout required by Amber is present and logs a warning on absence.
  89. ## Options
  90. Each option is of the form `--some-option-string` which is transformed into a selector of the format `someOptionString:`.
  91. The trailing `--` gets removed, each `-[a-z]` gets transformed into the according uppercase letter, and a `:` is appended to create a selector which takes a single argument.
  92. Afterwards, the selector gets executed on the `FileServer` instance with the value following in the options array as parameter.
  93. ## Adding new commandline parameters
  94. Adding new commandline parameters to `FileServer` is as easy as adding a new single parameter method to the `accessing` protocol.!
  95. !FileServer methodsFor: 'accessing'!
  96. basePath
  97. ^basePath ifNil: ['./']
  98. !
  99. basePath: aString
  100. basePath := aString
  101. !
  102. fallbackPage
  103. ^fallbackPage
  104. !
  105. fallbackPage: aString
  106. fallbackPage := aString
  107. !
  108. host
  109. ^host
  110. !
  111. host: hostname
  112. host := hostname
  113. !
  114. password: aPassword
  115. password := aPassword.
  116. !
  117. port
  118. ^port
  119. !
  120. port: aNumber
  121. port := aNumber
  122. !
  123. username: aUsername
  124. username := aUsername.
  125. ! !
  126. !FileServer methodsFor: 'initialization'!
  127. checkDirectoryLayout
  128. (fs existsSync: self basePath, 'index.html') ifFalse: [
  129. console warn: 'Warning: project directory does not contain index.html.'.
  130. console warn: ' You can specify the directory containing index.html with --base-path.'.].
  131. console warn: ' You can also specify a page to be served by default,'.
  132. console warn: ' for all paths the do not map to file, with --fallback-page.'.
  133. (fs existsSync: self basePath, 'st') ifFalse: [
  134. console warn: 'Warning: project directory is missing an "st" directory'].
  135. (fs existsSync: self basePath, 'js') ifFalse: [
  136. console warn: 'Warning: project directory is missing a "js" directory'].
  137. !
  138. initialize
  139. super initialize.
  140. path := self require: 'path'.
  141. http := self require: 'http'.
  142. fs := self require: 'fs'.
  143. util := self require: 'util'.
  144. url := self require: 'url'.
  145. host := self class defaultHost.
  146. port := self class defaultPort.
  147. username := nil.
  148. password := nil.
  149. fallbackPage := nil.
  150. ! !
  151. !FileServer methodsFor: 'private'!
  152. base64Decode: aString
  153. <return (new Buffer(aString, 'base64').toString())>
  154. !
  155. isAuthenticated: aRequest
  156. "Basic HTTP Auth: http://stackoverflow.com/a/5957629/293175
  157. and https://gist.github.com/1686663"
  158. | header token auth parts|
  159. (username isNil and: [password isNil]) ifTrue: [^true].
  160. "get authentication header"
  161. header := (aRequest headers at: 'authorization') ifNil:[''].
  162. (header isEmpty)
  163. ifTrue: [^false]
  164. ifFalse: [
  165. "get authentication token"
  166. token := (header tokenize: ' ') ifNil:[''].
  167. "convert back from base64"
  168. auth := self base64Decode: (token at: 2).
  169. "split token at colon"
  170. parts := auth tokenize: ':'.
  171. ((username = (parts at: 1)) and: [password = (parts at: 2)])
  172. ifTrue: [^true]
  173. ifFalse: [^false]
  174. ].
  175. !
  176. require: aModuleString
  177. "call to the require function"
  178. ^require value: aModuleString
  179. !
  180. writeData: data toFileNamed: aFilename
  181. console log: aFilename
  182. ! !
  183. !FileServer methodsFor: 'request handling'!
  184. handleGETRequest: aRequest respondTo: aResponse
  185. | uri filename |
  186. uri := (url parse: aRequest url) pathname.
  187. filename := path join: self basePath with: uri.
  188. fs exists: filename do: [:aBoolean |
  189. aBoolean
  190. ifFalse: [self respondNotFoundTo: aResponse]
  191. ifTrue: [self respondFileNamed: filename to: aResponse]]
  192. !
  193. handleOPTIONSRequest: aRequest respondTo: aResponse
  194. aResponse writeHead: 200 options: #{'Access-Control-Allow-Origin' -> '*'.
  195. 'Access-Control-Allow-Methods' -> 'GET, PUT, POST, DELETE, OPTIONS'.
  196. 'Access-Control-Allow-Headers' -> 'Content-Type, Accept'.
  197. 'Content-Length' -> 0.
  198. 'Access-Control-Max-Age' -> 10}.
  199. aResponse end
  200. !
  201. handlePUTRequest: aRequest respondTo: aResponse
  202. | file stream |
  203. (self isAuthenticated: aRequest)
  204. ifFalse: [self respondAuthenticationRequiredTo: aResponse. ^nil].
  205. file := '.', aRequest url.
  206. stream := fs createWriteStream: file.
  207. stream on: 'error' do: [:error |
  208. console warn: 'Error creating WriteStream for file ', file.
  209. console warn: ' Did you forget to create the necessary js/ or st/ directory in your project?'.
  210. console warn: ' The exact error is: ', error.
  211. self respondNotCreatedTo: aResponse].
  212. stream on: 'close' do: [
  213. self respondCreatedTo: aResponse].
  214. aRequest setEncoding: 'utf8'.
  215. aRequest on: 'data' do: [:data |
  216. stream write: data].
  217. aRequest on: 'end' do: [
  218. stream writable ifTrue: [stream end]]
  219. !
  220. handleRequest: aRequest respondTo: aResponse
  221. aRequest method = 'PUT'
  222. ifTrue: [self handlePUTRequest: aRequest respondTo: aResponse].
  223. aRequest method = 'GET'
  224. ifTrue:[self handleGETRequest: aRequest respondTo: aResponse].
  225. aRequest method = 'OPTIONS'
  226. ifTrue:[self handleOPTIONSRequest: aRequest respondTo: aResponse]
  227. !
  228. respondAuthenticationRequiredTo: aResponse
  229. aResponse
  230. writeHead: 401 options: #{'WWW-Authenticate' -> 'Basic realm="Secured Developer Area"'};
  231. write: '<html><body>Authentication needed</body></html>';
  232. end.
  233. !
  234. respondCreatedTo: aResponse
  235. aResponse
  236. writeHead: 201 options: #{'Content-Type' -> 'text/plain'. 'Access-Control-Allow-Origin' -> '*'};
  237. end.
  238. !
  239. respondFileNamed: aFilename to: aResponse
  240. | type filename |
  241. filename := aFilename.
  242. (fs statSync: aFilename) isDirectory ifTrue: [
  243. filename := filename, 'index.html'].
  244. fs readFile: filename do: [:ex :file |
  245. ex notNil
  246. ifTrue: [
  247. console log: filename, ' does not exist'.
  248. self respondNotFoundTo: aResponse]
  249. ifFalse: [
  250. type := self class mimeTypeFor: filename.
  251. type = 'application/javascript'
  252. ifTrue: [ type:=type,';charset=utf-8' ].
  253. aResponse
  254. writeHead: 200 options: #{'Content-Type' -> type};
  255. write: file encoding: 'binary';
  256. end]]
  257. !
  258. respondInternalErrorTo: aResponse
  259. aResponse
  260. writeHead: 500 options: #{'Content-Type' -> 'text/plain'};
  261. write: '500 Internal server error';
  262. end
  263. !
  264. respondNotCreatedTo: aResponse
  265. aResponse
  266. writeHead: 400 options: #{'Content-Type' -> 'text/plain'};
  267. write: 'File could not be created. Did you forget to create the st/js directories on the server?';
  268. end.
  269. !
  270. respondNotFoundTo: aResponse
  271. self fallbackPage isNil ifFalse: [^self respondFileNamed: self fallbackPage to: aResponse].
  272. aResponse
  273. writeHead: 404 options: #{'Content-Type' -> 'text/html'};
  274. write: '<html><body><p>404 Not found</p>';
  275. write: '<p>Did you forget to put an index.html file into the directory which is served by "bin/amber serve"? To solve this you can:<ul>';
  276. write: '<li>create an index.html in the served directory.</li>';
  277. write: '<li>can also specify the location of a page to be served whenever path does not resolve to a file with the "--fallback-page" option.</li>';
  278. write: '<li>change the directory to be served with the "--base-path" option.</li>';
  279. write: '</ul></p></body></html>';
  280. end
  281. !
  282. respondOKTo: aResponse
  283. aResponse
  284. writeHead: 200 options: #{'Content-Type' -> 'text/plain'. 'Access-Control-Allow-Origin' -> '*'};
  285. end.
  286. ! !
  287. !FileServer methodsFor: 'starting'!
  288. start
  289. "Checks if required directory layout is present (issue warning if not).
  290. Afterwards start the server."
  291. self checkDirectoryLayout.
  292. (http createServer: [:request :response |
  293. self handleRequest: request respondTo: response])
  294. on: 'error' do: [:error | console log: 'Error starting server: ', error];
  295. on: 'listening' do: [console log: 'Starting file server on http://', self host, ':', self port asString];
  296. listen: self port host: self host.
  297. !
  298. startOn: aPort
  299. self port: aPort.
  300. self start
  301. ! !
  302. FileServer class instanceVariableNames: 'mimeTypes'!
  303. !FileServer class methodsFor: 'accessing'!
  304. commandLineSwitches
  305. "Collect all methodnames from the 'accessing' protocol
  306. and select the ones with only one parameter.
  307. Then remove the ':' at the end of the name
  308. and add a '--' at the beginning.
  309. Additionally all uppercase letters are made lowercase and preceded by a '-'.
  310. Example: fallbackPage: becomes --fallback-page.
  311. Return the Array containing the commandline switches."
  312. | switches |
  313. switches := ((self methodsInProtocol: 'accessing') collect: [ :each | each selector]).
  314. switches := switches select: [ :each | each match: '^[^:]*:$'].
  315. switches :=switches collect: [ :each |
  316. (each allButLast replace: '([A-Z])' with: '-$1') asLowercase replace: '^([a-z])' with: '--$1' ].
  317. ^switches
  318. !
  319. defaultHost
  320. ^'127.0.0.1'
  321. !
  322. defaultMimeTypes
  323. ^ #{
  324. '%' -> 'application/x-trash'.
  325. '323' -> 'text/h323'.
  326. 'abw' -> 'application/x-abiword'.
  327. 'ai' -> 'application/postscript'.
  328. 'aif' -> 'audio/x-aiff'.
  329. 'aifc' -> 'audio/x-aiff'.
  330. 'aiff' -> 'audio/x-aiff'.
  331. 'alc' -> 'chemical/x-alchemy'.
  332. 'art' -> 'image/x-jg'.
  333. 'asc' -> 'text/plain'.
  334. 'asf' -> 'video/x-ms-asf'.
  335. 'asn' -> 'chemical/x-ncbi-asn1-spec'.
  336. 'aso' -> 'chemical/x-ncbi-asn1-binary'.
  337. 'asx' -> 'video/x-ms-asf'.
  338. 'au' -> 'audio/basic'.
  339. 'avi' -> 'video/x-msvideo'.
  340. 'b' -> 'chemical/x-molconn-Z'.
  341. 'bak' -> 'application/x-trash'.
  342. 'bat' -> 'application/x-msdos-program'.
  343. 'bcpio' -> 'application/x-bcpio'.
  344. 'bib' -> 'text/x-bibtex'.
  345. 'bin' -> 'application/octet-stream'.
  346. 'bmp' -> 'image/x-ms-bmp'.
  347. 'book' -> 'application/x-maker'.
  348. 'bsd' -> 'chemical/x-crossfire'.
  349. 'c' -> 'text/x-csrc'.
  350. 'c++' -> 'text/x-c++src'.
  351. 'c3d' -> 'chemical/x-chem3d'.
  352. 'cac' -> 'chemical/x-cache'.
  353. 'cache' -> 'chemical/x-cache'.
  354. 'cascii' -> 'chemical/x-cactvs-binary'.
  355. 'cat' -> 'application/vnd.ms-pki.seccat'.
  356. 'cbin' -> 'chemical/x-cactvs-binary'.
  357. 'cc' -> 'text/x-c++src'.
  358. 'cdf' -> 'application/x-cdf'.
  359. 'cdr' -> 'image/x-coreldraw'.
  360. 'cdt' -> 'image/x-coreldrawtemplate'.
  361. 'cdx' -> 'chemical/x-cdx'.
  362. 'cdy' -> 'application/vnd.cinderella'.
  363. 'cef' -> 'chemical/x-cxf'.
  364. 'cer' -> 'chemical/x-cerius'.
  365. 'chm' -> 'chemical/x-chemdraw'.
  366. 'chrt' -> 'application/x-kchart'.
  367. 'cif' -> 'chemical/x-cif'.
  368. 'class' -> 'application/java-vm'.
  369. 'cls' -> 'text/x-tex'.
  370. 'cmdf' -> 'chemical/x-cmdf'.
  371. 'cml' -> 'chemical/x-cml'.
  372. 'cod' -> 'application/vnd.rim.cod'.
  373. 'com' -> 'application/x-msdos-program'.
  374. 'cpa' -> 'chemical/x-compass'.
  375. 'cpio' -> 'application/x-cpio'.
  376. 'cpp' -> 'text/x-c++src'.
  377. 'cpt' -> 'image/x-corelphotopaint'.
  378. 'crl' -> 'application/x-pkcs7-crl'.
  379. 'crt' -> 'application/x-x509-ca-cert'.
  380. 'csf' -> 'chemical/x-cache-csf'.
  381. 'csh' -> 'text/x-csh'.
  382. 'csm' -> 'chemical/x-csml'.
  383. 'csml' -> 'chemical/x-csml'.
  384. 'css' -> 'text/css'.
  385. 'csv' -> 'text/comma-separated-values'.
  386. 'ctab' -> 'chemical/x-cactvs-binary'.
  387. 'ctx' -> 'chemical/x-ctx'.
  388. 'cu' -> 'application/cu-seeme'.
  389. 'cub' -> 'chemical/x-gaussian-cube'.
  390. 'cxf' -> 'chemical/x-cxf'.
  391. 'cxx' -> 'text/x-c++src'.
  392. 'dat' -> 'chemical/x-mopac-input'.
  393. 'dcr' -> 'application/x-director'.
  394. 'deb' -> 'application/x-debian-package'.
  395. 'dif' -> 'video/dv'.
  396. 'diff' -> 'text/plain'.
  397. 'dir' -> 'application/x-director'.
  398. 'djv' -> 'image/vnd.djvu'.
  399. 'djvu' -> 'image/vnd.djvu'.
  400. 'dl' -> 'video/dl'.
  401. 'dll' -> 'application/x-msdos-program'.
  402. 'dmg' -> 'application/x-apple-diskimage'.
  403. 'dms' -> 'application/x-dms'.
  404. 'doc' -> 'application/msword'.
  405. 'dot' -> 'application/msword'.
  406. 'dv' -> 'video/dv'.
  407. 'dvi' -> 'application/x-dvi'.
  408. 'dx' -> 'chemical/x-jcamp-dx'.
  409. 'dxr' -> 'application/x-director'.
  410. 'emb' -> 'chemical/x-embl-dl-nucleotide'.
  411. 'embl' -> 'chemical/x-embl-dl-nucleotide'.
  412. 'ent' -> 'chemical/x-pdb'.
  413. 'eps' -> 'application/postscript'.
  414. 'etx' -> 'text/x-setext'.
  415. 'exe' -> 'application/x-msdos-program'.
  416. 'ez' -> 'application/andrew-inset'.
  417. 'fb' -> 'application/x-maker'.
  418. 'fbdoc' -> 'application/x-maker'.
  419. 'fch' -> 'chemical/x-gaussian-checkpoint'.
  420. 'fchk' -> 'chemical/x-gaussian-checkpoint'.
  421. 'fig' -> 'application/x-xfig'.
  422. 'flac' -> 'application/x-flac'.
  423. 'fli' -> 'video/fli'.
  424. 'fm' -> 'application/x-maker'.
  425. 'frame' -> 'application/x-maker'.
  426. 'frm' -> 'application/x-maker'.
  427. 'gal' -> 'chemical/x-gaussian-log'.
  428. 'gam' -> 'chemical/x-gamess-input'.
  429. 'gamin' -> 'chemical/x-gamess-input'.
  430. 'gau' -> 'chemical/x-gaussian-input'.
  431. 'gcd' -> 'text/x-pcs-gcd'.
  432. 'gcf' -> 'application/x-graphing-calculator'.
  433. 'gcg' -> 'chemical/x-gcg8-sequence'.
  434. 'gen' -> 'chemical/x-genbank'.
  435. 'gf' -> 'application/x-tex-gf'.
  436. 'gif' -> 'image/gif'.
  437. 'gjc' -> 'chemical/x-gaussian-input'.
  438. 'gjf' -> 'chemical/x-gaussian-input'.
  439. 'gl' -> 'video/gl'.
  440. 'gnumeric' -> 'application/x-gnumeric'.
  441. 'gpt' -> 'chemical/x-mopac-graph'.
  442. 'gsf' -> 'application/x-font'.
  443. 'gsm' -> 'audio/x-gsm'.
  444. 'gtar' -> 'application/x-gtar'.
  445. 'h' -> 'text/x-chdr'.
  446. 'h++' -> 'text/x-c++hdr'.
  447. 'hdf' -> 'application/x-hdf'.
  448. 'hh' -> 'text/x-c++hdr'.
  449. 'hin' -> 'chemical/x-hin'.
  450. 'hpp' -> 'text/x-c++hdr'.
  451. 'hqx' -> 'application/mac-binhex40'.
  452. 'hs' -> 'text/x-haskell'.
  453. 'hta' -> 'application/hta'.
  454. 'htc' -> 'text/x-component'.
  455. 'htm' -> 'text/html'.
  456. 'html' -> 'text/html'.
  457. 'hxx' -> 'text/x-c++hdr'.
  458. 'ica' -> 'application/x-ica'.
  459. 'ice' -> 'x-conference/x-cooltalk'.
  460. 'ico' -> 'image/x-icon'.
  461. 'ics' -> 'text/calendar'.
  462. 'icz' -> 'text/calendar'.
  463. 'ief' -> 'image/ief'.
  464. 'iges' -> 'model/iges'.
  465. 'igs' -> 'model/iges'.
  466. 'iii' -> 'application/x-iphone'.
  467. 'inp' -> 'chemical/x-gamess-input'.
  468. 'ins' -> 'application/x-internet-signup'.
  469. 'iso' -> 'application/x-iso9660-image'.
  470. 'isp' -> 'application/x-internet-signup'.
  471. 'ist' -> 'chemical/x-isostar'.
  472. 'istr' -> 'chemical/x-isostar'.
  473. 'jad' -> 'text/vnd.sun.j2me.app-descriptor'.
  474. 'jar' -> 'application/java-archive'.
  475. 'java' -> 'text/x-java'.
  476. 'jdx' -> 'chemical/x-jcamp-dx'.
  477. 'jmz' -> 'application/x-jmol'.
  478. 'jng' -> 'image/x-jng'.
  479. 'jnlp' -> 'application/x-java-jnlp-file'.
  480. 'jpe' -> 'image/jpeg'.
  481. 'jpeg' -> 'image/jpeg'.
  482. 'jpg' -> 'image/jpeg'.
  483. 'js' -> 'application/javascript'.
  484. 'kar' -> 'audio/midi'.
  485. 'key' -> 'application/pgp-keys'.
  486. 'kil' -> 'application/x-killustrator'.
  487. 'kin' -> 'chemical/x-kinemage'.
  488. 'kpr' -> 'application/x-kpresenter'.
  489. 'kpt' -> 'application/x-kpresenter'.
  490. 'ksp' -> 'application/x-kspread'.
  491. 'kwd' -> 'application/x-kword'.
  492. 'kwt' -> 'application/x-kword'.
  493. 'latex' -> 'application/x-latex'.
  494. 'lha' -> 'application/x-lha'.
  495. 'lhs' -> 'text/x-literate-haskell'.
  496. 'lsf' -> 'video/x-la-asf'.
  497. 'lsx' -> 'video/x-la-asf'.
  498. 'ltx' -> 'text/x-tex'.
  499. 'lzh' -> 'application/x-lzh'.
  500. 'lzx' -> 'application/x-lzx'.
  501. 'm3u' -> 'audio/x-mpegurl'.
  502. 'm4a' -> 'audio/mpeg'.
  503. 'maker' -> 'application/x-maker'.
  504. 'man' -> 'application/x-troff-man'.
  505. 'mcif' -> 'chemical/x-mmcif'.
  506. 'mcm' -> 'chemical/x-macmolecule'.
  507. 'mdb' -> 'application/msaccess'.
  508. 'me' -> 'application/x-troff-me'.
  509. 'mesh' -> 'model/mesh'.
  510. 'mid' -> 'audio/midi'.
  511. 'midi' -> 'audio/midi'.
  512. 'mif' -> 'application/x-mif'.
  513. 'mm' -> 'application/x-freemind'.
  514. 'mmd' -> 'chemical/x-macromodel-input'.
  515. 'mmf' -> 'application/vnd.smaf'.
  516. 'mml' -> 'text/mathml'.
  517. 'mmod' -> 'chemical/x-macromodel-input'.
  518. 'mng' -> 'video/x-mng'.
  519. 'moc' -> 'text/x-moc'.
  520. 'mol' -> 'chemical/x-mdl-molfile'.
  521. 'mol2' -> 'chemical/x-mol2'.
  522. 'moo' -> 'chemical/x-mopac-out'.
  523. 'mop' -> 'chemical/x-mopac-input'.
  524. 'mopcrt' -> 'chemical/x-mopac-input'.
  525. 'mov' -> 'video/quicktime'.
  526. 'movie' -> 'video/x-sgi-movie'.
  527. 'mp2' -> 'audio/mpeg'.
  528. 'mp3' -> 'audio/mpeg'.
  529. 'mp4' -> 'video/mp4'.
  530. 'mpc' -> 'chemical/x-mopac-input'.
  531. 'mpe' -> 'video/mpeg'.
  532. 'mpeg' -> 'video/mpeg'.
  533. 'mpega' -> 'audio/mpeg'.
  534. 'mpg' -> 'video/mpeg'.
  535. 'mpga' -> 'audio/mpeg'.
  536. 'ms' -> 'application/x-troff-ms'.
  537. 'msh' -> 'model/mesh'.
  538. 'msi' -> 'application/x-msi'.
  539. 'mvb' -> 'chemical/x-mopac-vib'.
  540. 'mxu' -> 'video/vnd.mpegurl'.
  541. 'nb' -> 'application/mathematica'.
  542. 'nc' -> 'application/x-netcdf'.
  543. 'nwc' -> 'application/x-nwc'.
  544. 'o' -> 'application/x-object'.
  545. 'oda' -> 'application/oda'.
  546. 'odb' -> 'application/vnd.oasis.opendocument.database'.
  547. 'odc' -> 'application/vnd.oasis.opendocument.chart'.
  548. 'odf' -> 'application/vnd.oasis.opendocument.formula'.
  549. 'odg' -> 'application/vnd.oasis.opendocument.graphics'.
  550. 'odi' -> 'application/vnd.oasis.opendocument.image'.
  551. 'odm' -> 'application/vnd.oasis.opendocument.text-master'.
  552. 'odp' -> 'application/vnd.oasis.opendocument.presentation'.
  553. 'ods' -> 'application/vnd.oasis.opendocument.spreadsheet'.
  554. 'odt' -> 'application/vnd.oasis.opendocument.text'.
  555. 'ogg' -> 'application/ogg'.
  556. 'old' -> 'application/x-trash'.
  557. 'oth' -> 'application/vnd.oasis.opendocument.text-web'.
  558. 'oza' -> 'application/x-oz-application'.
  559. 'p' -> 'text/x-pascal'.
  560. 'p7r' -> 'application/x-pkcs7-certreqresp'.
  561. 'pac' -> 'application/x-ns-proxy-autoconfig'.
  562. 'pas' -> 'text/x-pascal'.
  563. 'pat' -> 'image/x-coreldrawpattern'.
  564. 'pbm' -> 'image/x-portable-bitmap'.
  565. 'pcf' -> 'application/x-font'.
  566. 'pcf.Z' -> 'application/x-font'.
  567. 'pcx' -> 'image/pcx'.
  568. 'pdb' -> 'chemical/x-pdb'.
  569. 'pdf' -> 'application/pdf'.
  570. 'pfa' -> 'application/x-font'.
  571. 'pfb' -> 'application/x-font'.
  572. 'pgm' -> 'image/x-portable-graymap'.
  573. 'pgn' -> 'application/x-chess-pgn'.
  574. 'pgp' -> 'application/pgp-signature'.
  575. 'pk' -> 'application/x-tex-pk'.
  576. 'pl' -> 'text/x-perl'.
  577. 'pls' -> 'audio/x-scpls'.
  578. 'pm' -> 'text/x-perl'.
  579. 'png' -> 'image/png'.
  580. 'pnm' -> 'image/x-portable-anymap'.
  581. 'pot' -> 'text/plain'.
  582. 'ppm' -> 'image/x-portable-pixmap'.
  583. 'pps' -> 'application/vnd.ms-powerpoint'.
  584. 'ppt' -> 'application/vnd.ms-powerpoint'.
  585. 'prf' -> 'application/pics-rules'.
  586. 'prt' -> 'chemical/x-ncbi-asn1-ascii'.
  587. 'ps' -> 'application/postscript'.
  588. 'psd' -> 'image/x-photoshop'.
  589. 'psp' -> 'text/x-psp'.
  590. 'py' -> 'text/x-python'.
  591. 'pyc' -> 'application/x-python-code'.
  592. 'pyo' -> 'application/x-python-code'.
  593. 'qt' -> 'video/quicktime'.
  594. 'qtl' -> 'application/x-quicktimeplayer'.
  595. 'ra' -> 'audio/x-realaudio'.
  596. 'ram' -> 'audio/x-pn-realaudio'.
  597. 'rar' -> 'application/rar'.
  598. 'ras' -> 'image/x-cmu-raster'.
  599. 'rd' -> 'chemical/x-mdl-rdfile'.
  600. 'rdf' -> 'application/rdf+xml'.
  601. 'rgb' -> 'image/x-rgb'.
  602. 'rm' -> 'audio/x-pn-realaudio'.
  603. 'roff' -> 'application/x-troff'.
  604. 'ros' -> 'chemical/x-rosdal'.
  605. 'rpm' -> 'application/x-redhat-package-manager'.
  606. 'rss' -> 'application/rss+xml'.
  607. 'rtf' -> 'text/rtf'.
  608. 'rtx' -> 'text/richtext'.
  609. 'rxn' -> 'chemical/x-mdl-rxnfile'.
  610. 'sct' -> 'text/scriptlet'.
  611. 'sd' -> 'chemical/x-mdl-sdfile'.
  612. 'sd2' -> 'audio/x-sd2'.
  613. 'sda' -> 'application/vnd.stardivision.draw'.
  614. 'sdc' -> 'application/vnd.stardivision.calc'.
  615. 'sdd' -> 'application/vnd.stardivision.impress'.
  616. 'sdf' -> 'chemical/x-mdl-sdfile'.
  617. 'sdp' -> 'application/vnd.stardivision.impress'.
  618. 'sdw' -> 'application/vnd.stardivision.writer'.
  619. 'ser' -> 'application/java-serialized-object'.
  620. 'sgf' -> 'application/x-go-sgf'.
  621. 'sgl' -> 'application/vnd.stardivision.writer-global'.
  622. 'sh' -> 'text/x-sh'.
  623. 'shar' -> 'application/x-shar'.
  624. 'shtml' -> 'text/html'.
  625. 'sid' -> 'audio/prs.sid'.
  626. 'sik' -> 'application/x-trash'.
  627. 'silo' -> 'model/mesh'.
  628. 'sis' -> 'application/vnd.symbian.install'.
  629. 'sit' -> 'application/x-stuffit'.
  630. 'skd' -> 'application/x-koan'.
  631. 'skm' -> 'application/x-koan'.
  632. 'skp' -> 'application/x-koan'.
  633. 'skt' -> 'application/x-koan'.
  634. 'smf' -> 'application/vnd.stardivision.math'.
  635. 'smi' -> 'application/smil'.
  636. 'smil' -> 'application/smil'.
  637. 'snd' -> 'audio/basic'.
  638. 'spc' -> 'chemical/x-galactic-spc'.
  639. 'spl' -> 'application/x-futuresplash'.
  640. 'src' -> 'application/x-wais-source'.
  641. 'stc' -> 'application/vnd.sun.xml.calc.template'.
  642. 'std' -> 'application/vnd.sun.xml.draw.template'.
  643. 'sti' -> 'application/vnd.sun.xml.impress.template'.
  644. 'stl' -> 'application/vnd.ms-pki.stl'.
  645. 'stw' -> 'application/vnd.sun.xml.writer.template'.
  646. 'sty' -> 'text/x-tex'.
  647. 'sv4cpio' -> 'application/x-sv4cpio'.
  648. 'sv4crc' -> 'application/x-sv4crc'.
  649. 'svg' -> 'image/svg+xml'.
  650. 'svgz' -> 'image/svg+xml'.
  651. 'sw' -> 'chemical/x-swissprot'.
  652. 'swf' -> 'application/x-shockwave-flash'.
  653. 'swfl' -> 'application/x-shockwave-flash'.
  654. 'sxc' -> 'application/vnd.sun.xml.calc'.
  655. 'sxd' -> 'application/vnd.sun.xml.draw'.
  656. 'sxg' -> 'application/vnd.sun.xml.writer.global'.
  657. 'sxi' -> 'application/vnd.sun.xml.impress'.
  658. 'sxm' -> 'application/vnd.sun.xml.math'.
  659. 'sxw' -> 'application/vnd.sun.xml.writer'.
  660. 't' -> 'application/x-troff'.
  661. 'tar' -> 'application/x-tar'.
  662. 'taz' -> 'application/x-gtar'.
  663. 'tcl' -> 'text/x-tcl'.
  664. 'tex' -> 'text/x-tex'.
  665. 'texi' -> 'application/x-texinfo'.
  666. 'texinfo' -> 'application/x-texinfo'.
  667. 'text' -> 'text/plain'.
  668. 'tgf' -> 'chemical/x-mdl-tgf'.
  669. 'tgz' -> 'application/x-gtar'.
  670. 'tif' -> 'image/tiff'.
  671. 'tiff' -> 'image/tiff'.
  672. 'tk' -> 'text/x-tcl'.
  673. 'tm' -> 'text/texmacs'.
  674. 'torrent' -> 'application/x-bittorrent'.
  675. 'tr' -> 'application/x-troff'.
  676. 'ts' -> 'text/texmacs'.
  677. 'tsp' -> 'application/dsptype'.
  678. 'tsv' -> 'text/tab-separated-values'.
  679. 'txt' -> 'text/plain'.
  680. 'udeb' -> 'application/x-debian-package'.
  681. 'uls' -> 'text/iuls'.
  682. 'ustar' -> 'application/x-ustar'.
  683. 'val' -> 'chemical/x-ncbi-asn1-binary'.
  684. 'vcd' -> 'application/x-cdlink'.
  685. 'vcf' -> 'text/x-vcard'.
  686. 'vcs' -> 'text/x-vcalendar'.
  687. 'vmd' -> 'chemical/x-vmd'.
  688. 'vms' -> 'chemical/x-vamas-iso14976'.
  689. 'vor' -> 'application/vnd.stardivision.writer'.
  690. 'vrm' -> 'x-world/x-vrml'.
  691. 'vrml' -> 'x-world/x-vrml'.
  692. 'vsd' -> 'application/vnd.visio'.
  693. 'wad' -> 'application/x-doom'.
  694. 'wav' -> 'audio/x-wav'.
  695. 'wax' -> 'audio/x-ms-wax'.
  696. 'wbmp' -> 'image/vnd.wap.wbmp'.
  697. 'wbxml' -> 'application/vnd.wap.wbxml'.
  698. 'wk' -> 'application/x-123'.
  699. 'wm' -> 'video/x-ms-wm'.
  700. 'wma' -> 'audio/x-ms-wma'.
  701. 'wmd' -> 'application/x-ms-wmd'.
  702. 'wml' -> 'text/vnd.wap.wml'.
  703. 'wmlc' -> 'application/vnd.wap.wmlc'.
  704. 'wmls' -> 'text/vnd.wap.wmlscript'.
  705. 'wmlsc' -> 'application/vnd.wap.wmlscriptc'.
  706. 'wmv' -> 'video/x-ms-wmv'.
  707. 'wmx' -> 'video/x-ms-wmx'.
  708. 'wmz' -> 'application/x-ms-wmz'.
  709. 'wp5' -> 'application/wordperfect5.1'.
  710. 'wpd' -> 'application/wordperfect'.
  711. 'wrl' -> 'x-world/x-vrml'.
  712. 'wsc' -> 'text/scriptlet'.
  713. 'wvx' -> 'video/x-ms-wvx'.
  714. 'wz' -> 'application/x-wingz'.
  715. 'xbm' -> 'image/x-xbitmap'.
  716. 'xcf' -> 'application/x-xcf'.
  717. 'xht' -> 'application/xhtml+xml'.
  718. 'xhtml' -> 'application/xhtml+xml'.
  719. 'xlb' -> 'application/vnd.ms-excel'.
  720. 'xls' -> 'application/vnd.ms-excel'.
  721. 'xlt' -> 'application/vnd.ms-excel'.
  722. 'xml' -> 'application/xml'.
  723. 'xpi' -> 'application/x-xpinstall'.
  724. 'xpm' -> 'image/x-xpixmap'.
  725. 'xsl' -> 'application/xml'.
  726. 'xtel' -> 'chemical/x-xtel'.
  727. 'xul' -> 'application/vnd.mozilla.xul+xml'.
  728. 'xwd' -> 'image/x-xwindowdump'.
  729. 'xyz' -> 'chemical/x-xyz'.
  730. 'zip' -> 'application/zip'.
  731. 'zmt' -> 'chemical/x-mopac-input'.
  732. '~' -> 'application/x-trash'
  733. }
  734. !
  735. defaultPort
  736. ^4000
  737. !
  738. mimeTypeFor: aString
  739. ^self mimeTypes at: (aString replace: '.*[\.]' with: '') ifAbsent: ['text/plain']
  740. !
  741. mimeTypes
  742. ^mimeTypes ifNil: [mimeTypes := self defaultMimeTypes]
  743. !
  744. printHelp
  745. console log: 'Available commandline options are:'.
  746. console log: '--help'.
  747. self commandLineSwitches do: [ :each |
  748. console log: each, ' <parameter>']
  749. !
  750. selectorForCommandLineSwitch: aSwitch
  751. "Remove the trailing '--', add ':' at the end
  752. and replace all occurences of a lowercase letter preceded by a '-' with
  753. the Uppercase letter.
  754. Example: --fallback-page becomes fallbackPage:"
  755. ^((aSwitch replace: '^--' with: '')
  756. replace: '-[a-z]' with: [ :each | each second asUppercase ]), ':'
  757. ! !
  758. !FileServer class methodsFor: 'initialization'!
  759. createServerWithArguments: options
  760. "If options are empty return a default FileServer instance.
  761. If options are given loop through them and set the passed in values
  762. on the FileServer instance.
  763. Commanline options map directly to methods in the 'accessing' protocol
  764. taking one parameter.
  765. Adding a method to this protocol makes it directly settable through
  766. command line options.
  767. "
  768. | server popFront front optionName optionValue switches |
  769. switches := self commandLineSwitches.
  770. server := self new.
  771. options ifEmpty: [^server].
  772. (options size even) ifFalse: [
  773. console log: 'Using default parameters.'.
  774. console log: 'Wrong commandline options or not enough arguments for: ' , options.
  775. console log: 'Use any of the following ones: ', switches.
  776. ^server].
  777. popFront := [:args |
  778. front := args first.
  779. args remove: front.
  780. front].
  781. [options notEmpty] whileTrue: [
  782. optionName := popFront value: options.
  783. optionValue := popFront value: options.
  784. (switches includes: optionName) ifTrue: [
  785. optionName := self selectorForCommandLineSwitch: optionName.
  786. server perform: optionName withArguments: (Array with: optionValue)]
  787. ifFalse: [
  788. console log: optionName, ' is not a valid commandline option'.
  789. console log: 'Use any of the following ones: ', switches ]].
  790. ^server.
  791. !
  792. main
  793. "Main entry point for Amber applications.
  794. Creates and starts a FileServer instance."
  795. | fileServer args |
  796. args := process argv.
  797. "Remove the first args which contain the path to the node executable and the script file."
  798. args removeFrom: 1 to: 3.
  799. args detect: [ :each |
  800. (each = '--help') ifTrue: [FileServer printHelp]]
  801. ifNone: [
  802. fileServer := FileServer createServerWithArguments: args.
  803. ^fileServer start]
  804. ! !
  805. Object subclass: #Repl
  806. instanceVariableNames: 'readline interface util session resultCount commands'
  807. package: 'AmberCli'!
  808. !Repl commentStamp!
  809. I am a class representing a REPL (Read Evaluate Print Loop) and provide a command line interface to Amber Smalltalk.
  810. On the prompt you can type Amber statements which will be evaluated after pressing <Enter>.
  811. The evaluation is comparable with executing a 'DoIt' in a workspace.
  812. My runtime requirement is a functional Node.js executable with working Readline support.!
  813. !Repl methodsFor: 'accessing'!
  814. commands
  815. ^ commands
  816. !
  817. prompt
  818. ^'amber >> '
  819. ! !
  820. !Repl methodsFor: 'actions'!
  821. clearScreen
  822. | esc cls |
  823. esc := String fromCharCode: 27.
  824. cls := esc, '[2J', esc, '[0;0f'.
  825. process stdout write: cls.
  826. interface prompt
  827. !
  828. close
  829. process stdin destroy
  830. !
  831. createInterface
  832. interface := readline createInterface: process stdin stdout: process stdout.
  833. interface on: 'line' do: [:buffer | self processLine: buffer].
  834. interface on: 'close' do: [self close].
  835. self printWelcome; setupHotkeys; setPrompt.
  836. interface prompt
  837. !
  838. eval: buffer
  839. ^ self eval: buffer on: DoIt new.
  840. !
  841. eval: buffer on: anObject
  842. | result |
  843. buffer isEmpty ifFalse: [
  844. self try: [
  845. result := Compiler new evaluateExpression: buffer on: anObject]
  846. catch: [:e |
  847. e isSmalltalkError
  848. ifTrue: [ e resignal ]
  849. ifFalse: [ process stdout write: e jsStack ]]].
  850. ^ result
  851. !
  852. printWelcome
  853. Transcript show: 'Welcome to Amber version ', Smalltalk current version, ' (NodeJS ', process versions node, ').'.
  854. Transcript show: 'Type :q to exit.'; cr.
  855. !
  856. setPrompt
  857. interface setPrompt: self prompt
  858. ! !
  859. !Repl methodsFor: 'initialization'!
  860. initialize
  861. super initialize.
  862. session := DoIt new.
  863. readline := require value: 'readline'.
  864. util := require value: 'util'.
  865. self setupCommands
  866. !
  867. setupCommands
  868. commands := Dictionary from: {
  869. {':q'} -> [process exit].
  870. {''} -> [interface prompt]}
  871. !
  872. setupHotkeys
  873. process stdin on: 'keypress' do: [:s :key | key ifNotNil: [self onKeyPress: key]].
  874. ! !
  875. !Repl methodsFor: 'private'!
  876. addVariableNamed: aString to: anObject
  877. | newClass newObject |
  878. newClass := self subclass: anObject class withVariable: aString.
  879. self encapsulateVariable: aString withValue: anObject in: newClass.
  880. newObject := newClass new.
  881. self setPreviousVariablesFor: newObject from: anObject.
  882. ^ newObject
  883. !
  884. assignNewVariable: buffer do: aBlock
  885. "Assigns a new variable and calls the given block with the variable's name and value
  886. if buffer contains an assignment expression. If it doesn't the block is called with nil for
  887. both arguments."
  888. ^ self parseAssignment: buffer do: [ :name :expr || varName value |
  889. varName := name ifNil: [self nextResultName].
  890. session := self addVariableNamed: varName to: session.
  891. [ value := self eval: varName, ' := ', (expr ifNil: [buffer]) on: session ]
  892. on: Error
  893. do: [ :e | ErrorHandler new logError: e. value := nil].
  894. aBlock value: varName value: value]
  895. !
  896. encapsulateVariable: aString withValue: anObject in: aClass
  897. "Add getter and setter for given variable to session."
  898. | compiler |
  899. compiler := Compiler new.
  900. compiler install: aString, ': anObject ^ ', aString, ' := anObject' forClass: aClass category: 'session'.
  901. compiler install: aString, ' ^ ', aString forClass: aClass category: 'session'.
  902. !
  903. executeCommand: aString
  904. "Tries to process the given string as a command. Returns true if it was a command, false if not."
  905. self commands keysAndValuesDo: [:names :cmd |
  906. (names includes: aString) ifTrue: [
  907. cmd value.
  908. ^ true]].
  909. ^ false
  910. !
  911. instanceVariableNamesFor: aClass
  912. "Yields all instance variable names for the given class, including inherited ones."
  913. ^ aClass superclass
  914. ifNotNil: [
  915. aClass instanceVariableNames copyWithAll: (self instanceVariableNamesFor: aClass superclass)]
  916. ifNil: [
  917. aClass instanceVariableNames]
  918. !
  919. isIdentifier: aString
  920. ^ aString match: '^[a-z_]\w*$' asRegexp
  921. !
  922. isVariableDefined: aString
  923. ^ (self instanceVariableNamesFor: session class) includes: aString
  924. !
  925. nextResultName
  926. resultCount := resultCount
  927. ifNotNil: [resultCount + 1]
  928. ifNil: [1].
  929. ^ 'res', resultCount asString
  930. !
  931. onKeyPress: key
  932. (key ctrl and: [key name = 'l'])
  933. ifTrue: [self clearScreen]
  934. !
  935. parseAssignment: aString do: aBlock
  936. "Assigns a new variable if the given string is an assignment expression. Calls the given block with name and value.
  937. If the string is not one no variable will be assigned and the block will be called with nil for both arguments."
  938. | assignment |
  939. assignment := (aString tokenize: ':=') collect: [:s | s trimBoth].
  940. ^ (assignment size = 2 and: [self isIdentifier: assignment first])
  941. ifTrue: [ aBlock value: assignment first value: assignment last ]
  942. ifFalse: [ aBlock value: nil value: nil ]
  943. !
  944. presentResultNamed: varName withValue: value
  945. Transcript show: varName, ': ', value class name, ' = ', value asString; cr.
  946. interface prompt
  947. !
  948. processLine: buffer
  949. "Processes lines entered through the readline interface."
  950. | show |
  951. show := [:varName :value | self presentResultNamed: varName withValue: value].
  952. (self executeCommand: buffer) ifFalse: [
  953. (self isVariableDefined: buffer)
  954. ifTrue: [show value: buffer value: (session perform: buffer)]
  955. ifFalse: [self assignNewVariable: buffer do: show]]
  956. !
  957. setPreviousVariablesFor: newObject from: oldObject
  958. (self instanceVariableNamesFor: oldObject class) do: [:each |
  959. newObject perform: each, ':' withArguments: {oldObject perform: each}].
  960. !
  961. subclass: aClass withVariable: varName
  962. "Create subclass with new variable."
  963. ^ ClassBuilder new
  964. addSubclassOf: aClass
  965. named: (self subclassNameFor: aClass) asSymbol
  966. instanceVariableNames: {varName}
  967. package: 'Compiler-Core'
  968. !
  969. subclassNameFor: aClass
  970. ^ (aClass name matchesOf: '\d+$')
  971. ifNotNil: [ | counter |
  972. counter := (aClass name matchesOf: '\d+$') first asNumber + 1.
  973. aClass name replaceRegexp: '\d+$' asRegexp with: counter asString]
  974. ifNil: [
  975. aClass name, '2'].
  976. ! !
  977. !Repl class methodsFor: 'initialization'!
  978. main
  979. self new createInterface
  980. ! !