1
0

AmberCli.st 33 KB

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