AmberCli.st 35 KB

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