1
0

AmberCli.st 34 KB

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