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