AmberCli.st 35 KB

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