1
0

AmberCli.st 29 KB

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