AmberCli.st 35 KB

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