AmberCli.st 34 KB

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