2
0

AmberCli.st 34 KB

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