AmberCli.st 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. [result := Compiler new evaluateExpression: buffer on: anObject]
  868. tryCatch: [:e |
  869. e isSmalltalkError
  870. ifTrue: [ e resignal ]
  871. ifFalse: [ process stdout write: e jsStack ]]].
  872. ^ result
  873. !
  874. printWelcome
  875. Transcript show: 'Type :q to exit.'; cr.
  876. !
  877. setPrompt
  878. interface setPrompt: self prompt
  879. ! !
  880. !Repl methodsFor: 'initialization'!
  881. initialize
  882. super initialize.
  883. session := DoIt new.
  884. readline := require value: 'readline'.
  885. util := require value: 'util'.
  886. self setupCommands
  887. !
  888. setupCommands
  889. commands := Dictionary from: {
  890. {':q'} -> [process exit].
  891. {''} -> [interface prompt]}
  892. !
  893. setupHotkeys
  894. process stdin on: 'keypress' do: [:s :key | key ifNotNil: [self onKeyPress: key]].
  895. ! !
  896. !Repl methodsFor: 'private'!
  897. addVariableNamed: aString to: anObject
  898. | newClass newObject |
  899. newClass := self subclass: anObject class withVariable: aString.
  900. self encapsulateVariable: aString withValue: anObject in: newClass.
  901. newObject := newClass new.
  902. self setPreviousVariablesFor: newObject from: anObject.
  903. ^ newObject
  904. !
  905. assignNewVariable: buffer do: aBlock
  906. "Assigns a new variable and calls the given block with the variable's name and value
  907. if buffer contains an assignment expression. If it doesn't the block is called with nil for
  908. both arguments."
  909. ^ self parseAssignment: buffer do: [ :name :expr || varName value |
  910. varName := name ifNil: [self nextResultName].
  911. session := self addVariableNamed: varName to: session.
  912. [ value := self eval: varName, ' := ', (expr ifNil: [buffer]) on: session ]
  913. on: Error
  914. do: [ :e | ConsoleErrorHandler new logError: e. value := nil].
  915. aBlock value: varName value: value]
  916. !
  917. encapsulateVariable: aString withValue: anObject in: aClass
  918. "Add getter and setter for given variable to session."
  919. | compiler |
  920. compiler := Compiler new.
  921. compiler install: aString, ': anObject ^ ', aString, ' := anObject' forClass: aClass protocol: 'session'.
  922. compiler install: aString, ' ^ ', aString forClass: aClass protocol: 'session'.
  923. !
  924. executeCommand: aString
  925. "Tries to process the given string as a command. Returns true if it was a command, false if not."
  926. self commands keysAndValuesDo: [:names :cmd |
  927. (names includes: aString) ifTrue: [
  928. cmd value.
  929. ^ true]].
  930. ^ false
  931. !
  932. instanceVariableNamesFor: aClass
  933. "Yields all instance variable names for the given class, including inherited ones."
  934. ^ aClass superclass
  935. ifNotNil: [
  936. aClass instanceVariableNames copyWithAll: (self instanceVariableNamesFor: aClass superclass)]
  937. ifNil: [
  938. aClass instanceVariableNames]
  939. !
  940. isIdentifier: aString
  941. ^ aString match: '^[a-z_]\w*$' asRegexp
  942. !
  943. isVariableDefined: aString
  944. ^ (self instanceVariableNamesFor: session class) includes: aString
  945. !
  946. nextResultName
  947. resultCount := resultCount
  948. ifNotNil: [resultCount + 1]
  949. ifNil: [1].
  950. ^ 'res', resultCount asString
  951. !
  952. onKeyPress: key
  953. (key ctrl and: [key name = 'l'])
  954. ifTrue: [self clearScreen]
  955. !
  956. parseAssignment: aString do: aBlock
  957. "Assigns a new variable if the given string is an assignment expression. Calls the given block with name and value.
  958. If the string is not one no variable will be assigned and the block will be called with nil for both arguments."
  959. | assignment |
  960. assignment := (aString tokenize: ':=') collect: [:s | s trimBoth].
  961. ^ (assignment size = 2 and: [self isIdentifier: assignment first])
  962. ifTrue: [ aBlock value: assignment first value: assignment last ]
  963. ifFalse: [ aBlock value: nil value: nil ]
  964. !
  965. presentResultNamed: varName withValue: value
  966. Transcript show: varName, ': ', value class name, ' = ', value asString; cr.
  967. interface prompt
  968. !
  969. processLine: buffer
  970. "Processes lines entered through the readline interface."
  971. | show |
  972. show := [:varName :value | self presentResultNamed: varName withValue: value].
  973. (self executeCommand: buffer) ifFalse: [
  974. (self isVariableDefined: buffer)
  975. ifTrue: [show value: buffer value: (session perform: buffer)]
  976. ifFalse: [self assignNewVariable: buffer do: show]]
  977. !
  978. setPreviousVariablesFor: newObject from: oldObject
  979. (self instanceVariableNamesFor: oldObject class) do: [:each |
  980. newObject perform: each, ':' withArguments: {oldObject perform: each}].
  981. !
  982. subclass: aClass withVariable: varName
  983. "Create subclass with new variable."
  984. ^ ClassBuilder new
  985. addSubclassOf: aClass
  986. named: (self subclassNameFor: aClass) asSymbol
  987. instanceVariableNames: {varName}
  988. package: 'Compiler-Core'
  989. !
  990. subclassNameFor: aClass
  991. ^ (aClass name matchesOf: '\d+$')
  992. ifNotNil: [ | counter |
  993. counter := (aClass name matchesOf: '\d+$') first asNumber + 1.
  994. aClass name replaceRegexp: '\d+$' asRegexp with: counter asString]
  995. ifNil: [
  996. aClass name, '2'].
  997. ! !
  998. !Repl class methodsFor: 'initialization'!
  999. main
  1000. self new createInterface
  1001. ! !