Importer-Exporter.st 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. Smalltalk current createPackage: 'Importer-Exporter'!
  2. Object subclass: #AbstractExporter
  3. instanceVariableNames: ''
  4. package: 'Importer-Exporter'!
  5. !AbstractExporter commentStamp!
  6. I am an abstract exporter for Amber source code.!
  7. !AbstractExporter methodsFor: 'convenience'!
  8. chunkEscape: aString
  9. "Replace all occurrences of !! with !!!! and trim at both ends."
  10. ^(aString replace: '!!' with: '!!!!') trimBoth
  11. !
  12. classNameFor: aClass
  13. ^aClass isMetaclass
  14. ifTrue: [ aClass instanceClass name, ' class' ]
  15. ifFalse: [
  16. aClass isNil
  17. ifTrue: [ 'nil' ]
  18. ifFalse: [ aClass name ] ]
  19. ! !
  20. !AbstractExporter methodsFor: 'fileOut'!
  21. recipe
  22. "Recipe to export a given package."
  23. self subclassResponsibility
  24. ! !
  25. AbstractExporter class instanceVariableNames: 'default'!
  26. !AbstractExporter class methodsFor: 'instance creation'!
  27. default
  28. ^ default ifNil: [ default := self new ]
  29. ! !
  30. AbstractExporter subclass: #ChunkExporter
  31. instanceVariableNames: ''
  32. package: 'Importer-Exporter'!
  33. !ChunkExporter commentStamp!
  34. I am an exporter dedicated to outputting Amber source code in the classic Smalltalk chunk format.
  35. I do not output any compiled code.!
  36. !ChunkExporter methodsFor: 'accessing'!
  37. extensionCategoriesOfPackage: aPackage
  38. "Issue #143: sort protocol alphabetically"
  39. | name map result |
  40. name := aPackage name.
  41. result := OrderedCollection new.
  42. (Package sortedClasses: Smalltalk current classes) do: [:each |
  43. {each. each class} do: [:aClass |
  44. map := Dictionary new.
  45. aClass protocolsDo: [:category :methods |
  46. (category match: '^\*', name) ifTrue: [ map at: category put: methods ]].
  47. result addAll: ((map keys sorted: [:a :b | a <= b ]) collect: [:category |
  48. MethodCategory name: category theClass: aClass methods: (map at: category)]) ]].
  49. ^result
  50. !
  51. methodsOfCategory: aCategory
  52. "Issue #143: sort methods alphabetically"
  53. ^(aCategory methods) sorted: [:a :b | a selector <= b selector]
  54. !
  55. ownCategoriesOfClass: aClass
  56. "Answer the protocols of aClassthat are not package extensions"
  57. "Issue #143: sort protocol alphabetically"
  58. | map |
  59. map := Dictionary new.
  60. aClass protocolsDo: [:category :methods |
  61. (category match: '^\*') ifFalse: [ map at: category put: methods ]].
  62. ^(map keys sorted: [:a :b | a <= b ]) collect: [:category |
  63. MethodCategory name: category theClass: aClass methods: (map at: category) ]
  64. !
  65. ownCategoriesOfMetaClass: aClass
  66. "Issue #143: sort protocol alphabetically"
  67. ^self ownCategoriesOfClass: aClass class
  68. ! !
  69. !ChunkExporter methodsFor: 'fileOut'!
  70. recipe
  71. "Export a given package."
  72. | exportCategoryRecipe |
  73. exportCategoryRecipe := {
  74. self -> #exportCategoryPrologueOf:on:.
  75. {
  76. self -> #methodsOfCategory:.
  77. self -> #exportMethod:on: }.
  78. self -> #exportCategoryEpilogueOf:on: }.
  79. ^{
  80. self -> #exportPackageDefinitionOf:on:.
  81. {
  82. PluggableExporter -> #ownClassesOfPackage:.
  83. self -> #exportDefinitionOf:on:.
  84. { self -> #ownCategoriesOfClass: }, exportCategoryRecipe.
  85. self -> #exportMetaDefinitionOf:on:.
  86. { self -> #ownCategoriesOfMetaClass: }, exportCategoryRecipe }.
  87. { self -> #extensionCategoriesOfPackage: }, exportCategoryRecipe
  88. }
  89. ! !
  90. !ChunkExporter methodsFor: 'output'!
  91. exportCategoryEpilogueOf: aCategory on: aStream
  92. aStream nextPutAll: ' !!'; lf; lf
  93. !
  94. exportCategoryPrologueOf: aCategory on: aStream
  95. aStream
  96. nextPutAll: '!!', (self classNameFor: aCategory theClass);
  97. nextPutAll: ' methodsFor: ''', aCategory name, '''!!'
  98. !
  99. exportDefinitionOf: aClass on: aStream
  100. "Chunk format."
  101. aStream
  102. nextPutAll: (self classNameFor: aClass superclass);
  103. nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;
  104. tab; nextPutAll: 'instanceVariableNames: '''.
  105. aClass instanceVariableNames
  106. do: [:each | aStream nextPutAll: each]
  107. separatedBy: [aStream nextPutAll: ' '].
  108. aStream
  109. nextPutAll: ''''; lf;
  110. tab; nextPutAll: 'package: ''', aClass category, '''!!'; lf.
  111. aClass comment notEmpty ifTrue: [
  112. aStream
  113. nextPutAll: '!!', (self classNameFor: aClass), ' commentStamp!!';lf;
  114. nextPutAll: (self chunkEscape: aClass comment), '!!';lf].
  115. aStream lf
  116. !
  117. exportMetaDefinitionOf: aClass on: aStream
  118. aClass class instanceVariableNames isEmpty ifFalse: [
  119. aStream
  120. nextPutAll: (self classNameFor: aClass class);
  121. nextPutAll: ' instanceVariableNames: '''.
  122. aClass class instanceVariableNames
  123. do: [:each | aStream nextPutAll: each]
  124. separatedBy: [aStream nextPutAll: ' '].
  125. aStream
  126. nextPutAll: '''!!'; lf; lf]
  127. !
  128. exportMethod: aMethod on: aStream
  129. aStream
  130. lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;
  131. nextPutAll: '!!'
  132. !
  133. exportPackageDefinitionOf: aPackage on: aStream
  134. "Chunk format."
  135. aStream
  136. nextPutAll: 'Smalltalk current createPackage: ''', aPackage name, '''!!';
  137. lf
  138. ! !
  139. AbstractExporter subclass: #Exporter
  140. instanceVariableNames: ''
  141. package: 'Importer-Exporter'!
  142. !Exporter commentStamp!
  143. I am responsible for outputting Amber code into a JavaScript string.
  144. The generated output is enough to reconstruct the exported data, including Smalltalk source code and other metadata.
  145. ## Use case
  146. I am typically used to save code outside of the Amber runtime (committing to disk, etc.).
  147. ## API
  148. Use `#exportAll`, `#exportClass:` or `#exportPackage:` methods.!
  149. !Exporter methodsFor: 'accessing'!
  150. extensionMethodsOfPackage: aPackage
  151. "Issue #143: sort classes and methods alphabetically"
  152. | name result |
  153. name := aPackage name.
  154. result := OrderedCollection new.
  155. (Package sortedClasses: Smalltalk current classes) do: [:each |
  156. {each. each class} do: [:aClass |
  157. result addAll: (((aClass methodDictionary values)
  158. sorted: [:a :b | a selector <= b selector])
  159. select: [:method | method category match: '^\*', name]) ]].
  160. ^result
  161. !
  162. ownMethodsOfClass: aClass
  163. "Issue #143: sort methods alphabetically"
  164. ^((aClass methodDictionary values) sorted: [:a :b | a selector <= b selector])
  165. reject: [:each | (each category match: '^\*')]
  166. !
  167. ownMethodsOfMetaClass: aClass
  168. "Issue #143: sort methods alphabetically"
  169. ^self ownMethodsOfClass: aClass class
  170. ! !
  171. !Exporter methodsFor: 'convenience'!
  172. classNameFor: aClass
  173. ^aClass isMetaclass
  174. ifTrue: [ aClass instanceClass name, '.klass' ]
  175. ifFalse: [
  176. aClass isNil
  177. ifTrue: [ 'nil' ]
  178. ifFalse: [ aClass name ] ]
  179. ! !
  180. !Exporter methodsFor: 'fileOut'!
  181. amdRecipe
  182. "Export a given package with amd transport type."
  183. | legacy result |
  184. legacy := self recipe.
  185. result := (legacy copyFrom: 1 to: 2),
  186. { AmdExporter -> #exportPackageTransportOf:on: },
  187. (legacy copyFrom: 3 to: legacy size).
  188. result first key: AmdExporter.
  189. result last key: AmdExporter.
  190. ^result
  191. !
  192. recipe
  193. "Export a given package."
  194. ^{
  195. self -> #exportPackagePrologueOf:on:.
  196. self -> #exportPackageDefinitionOf:on:.
  197. {
  198. PluggableExporter -> #ownClassesOfPackage:.
  199. self -> #exportDefinitionOf:on:.
  200. {
  201. self -> #ownMethodsOfClass:.
  202. self -> #exportMethod:on: }.
  203. self -> #exportMetaDefinitionOf:on:.
  204. {
  205. self -> #ownMethodsOfMetaClass:.
  206. self -> #exportMethod:on: } }.
  207. {
  208. self -> #extensionMethodsOfPackage:.
  209. self -> #exportMethod:on: }.
  210. self -> #exportPackageEpilogueOf:on:
  211. }
  212. ! !
  213. !Exporter methodsFor: 'output'!
  214. exportDefinitionOf: aClass on: aStream
  215. aStream
  216. lf;
  217. nextPutAll: 'smalltalk.addClass(';
  218. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  219. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  220. nextPutAll: ', ['.
  221. aClass instanceVariableNames
  222. do: [:each | aStream nextPutAll: '''', each, '''']
  223. separatedBy: [aStream nextPutAll: ', '].
  224. aStream
  225. nextPutAll: '], ''';
  226. nextPutAll: aClass category, '''';
  227. nextPutAll: ');'.
  228. aClass comment notEmpty ifTrue: [
  229. aStream
  230. lf;
  231. nextPutAll: 'smalltalk.';
  232. nextPutAll: (self classNameFor: aClass);
  233. nextPutAll: '.comment=';
  234. nextPutAll: aClass comment asJavascript;
  235. nextPutAll: ';'].
  236. aStream lf
  237. !
  238. exportMetaDefinitionOf: aClass on: aStream
  239. aStream lf.
  240. aClass class instanceVariableNames isEmpty ifFalse: [
  241. aStream
  242. nextPutAll: 'smalltalk.', (self classNameFor: aClass class);
  243. nextPutAll: '.iVarNames = ['.
  244. aClass class instanceVariableNames
  245. do: [:each | aStream nextPutAll: '''', each, '''']
  246. separatedBy: [aStream nextPutAll: ','].
  247. aStream nextPutAll: '];', String lf]
  248. !
  249. exportMethod: aMethod on: aStream
  250. aStream
  251. nextPutAll: 'smalltalk.addMethod(';lf;
  252. "nextPutAll: aMethod selector asSelector asJavascript, ',';lf;"
  253. nextPutAll: 'smalltalk.method({';lf;
  254. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  255. nextPutAll: 'category: ''', aMethod category, ''',';lf;
  256. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  257. nextPutAll: 'args: ', aMethod arguments asJavascript, ','; lf;
  258. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf;
  259. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;
  260. nextPutAll: 'referencedClasses: ', aMethod referencedClasses asJavascript.
  261. aStream
  262. lf;
  263. nextPutAll: '}),';lf;
  264. nextPutAll: 'smalltalk.', (self classNameFor: aMethod methodClass);
  265. nextPutAll: ');';lf;lf
  266. !
  267. exportPackageDefinitionOf: aPackage on: aStream
  268. aStream
  269. nextPutAll: 'smalltalk.addPackage(';
  270. nextPutAll: '''', aPackage name, ''');';
  271. lf
  272. !
  273. exportPackageEpilogueOf: aPackage on: aStream
  274. aStream
  275. nextPutAll: '})(global_smalltalk,global_nil,global__st);';
  276. lf
  277. !
  278. exportPackagePrologueOf: aPackage on: aStream
  279. aStream
  280. nextPutAll: '(function(smalltalk,nil,_st){';
  281. lf
  282. ! !
  283. Exporter subclass: #StrippedExporter
  284. instanceVariableNames: ''
  285. package: 'Importer-Exporter'!
  286. !StrippedExporter commentStamp!
  287. I export Amber code into a JavaScript string, but without any optional associated data like the Amber source code.!
  288. !StrippedExporter methodsFor: 'output'!
  289. exportDefinitionOf: aClass on: aStream
  290. aStream
  291. lf;
  292. nextPutAll: 'smalltalk.addClass(';
  293. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  294. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  295. nextPutAll: ', ['.
  296. aClass instanceVariableNames
  297. do: [:each | aStream nextPutAll: '''', each, '''']
  298. separatedBy: [aStream nextPutAll: ', '].
  299. aStream
  300. nextPutAll: '], ''';
  301. nextPutAll: aClass category, '''';
  302. nextPutAll: ');'.
  303. aStream lf
  304. !
  305. exportMethod: aMethod on: aStream
  306. aStream
  307. nextPutAll: 'smalltalk.addMethod(';lf;
  308. "nextPutAll: aMethod selector asSelector asJavascript, ',';lf;"
  309. nextPutAll: 'smalltalk.method({';lf;
  310. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  311. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  312. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript;
  313. nextPutAll: '}),';lf;
  314. nextPutAll: 'smalltalk.', (self classNameFor: aMethod methodClass);
  315. nextPutAll: ');';lf;lf
  316. ! !
  317. Object subclass: #AmdExporter
  318. instanceVariableNames: ''
  319. package: 'Importer-Exporter'!
  320. !AmdExporter class methodsFor: 'exporting-output'!
  321. exportPackageEpilogueOf: aPackage on: aStream
  322. aStream
  323. nextPutAll: '});';
  324. lf
  325. !
  326. exportPackagePrologueOf: aPackage on: aStream
  327. aStream
  328. nextPutAll: 'define("';
  329. nextPutAll: (aPackage amdNamespace ifNil: [ 'amber' ]); "ifNil: only for LegacyPH, it should not happen with AmdPH"
  330. nextPutAll: '/';
  331. nextPutAll: aPackage name;
  332. nextPutAll: '", ';
  333. nextPutAll: (#('amber_vm/smalltalk' 'amber_vm/nil' 'amber_vm/_st'), (self amdNamesOfPackages: aPackage loadDependencies)) asJavascript;
  334. nextPutAll: ', function(smalltalk,nil,_st){';
  335. lf
  336. !
  337. exportPackageTransportOf: aPackage on: aStream
  338. aStream
  339. nextPutAll: 'smalltalk.packages[';
  340. nextPutAll: aPackage name asJavascript;
  341. nextPutAll: '].transport = ';
  342. nextPutAll: aPackage transportJson;
  343. nextPutAll: ';';
  344. lf
  345. ! !
  346. !AmdExporter class methodsFor: 'private'!
  347. amdNamesOfPackages: anArray
  348. | deps depNames |
  349. ^(anArray
  350. select: [ :each | each amdNamespace notNil ])
  351. collect: [ :each | each amdNamespace, '/', each name ]
  352. ! !
  353. Object subclass: #ChunkParser
  354. instanceVariableNames: 'stream'
  355. package: 'Importer-Exporter'!
  356. !ChunkParser commentStamp!
  357. I am responsible for parsing aStream contents in the chunk format.
  358. ## API
  359. ChunkParser new
  360. stream: aStream;
  361. nextChunk!
  362. !ChunkParser methodsFor: 'accessing'!
  363. stream: aStream
  364. stream := aStream
  365. ! !
  366. !ChunkParser methodsFor: 'reading'!
  367. nextChunk
  368. "The chunk format (Smalltalk Interchange Format or Fileout format)
  369. is a trivial format but can be a bit tricky to understand:
  370. - Uses the exclamation mark as delimiter of chunks.
  371. - Inside a chunk a normal exclamation mark must be doubled.
  372. - A non empty chunk must be a valid Smalltalk expression.
  373. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  374. - The object created by the expression then takes over reading chunks.
  375. This metod returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  376. | char result chunk |
  377. result := '' writeStream.
  378. [char := stream next.
  379. char notNil] whileTrue: [
  380. char = '!!' ifTrue: [
  381. stream peek = '!!'
  382. ifTrue: [stream next "skipping the escape double"]
  383. ifFalse: [^result contents trimBoth "chunk end marker found"]].
  384. result nextPut: char].
  385. ^nil "a chunk needs to end with !!"
  386. ! !
  387. !ChunkParser class methodsFor: 'not yet classified'!
  388. on: aStream
  389. ^self new stream: aStream
  390. ! !
  391. Object subclass: #Importer
  392. instanceVariableNames: ''
  393. package: 'Importer-Exporter'!
  394. !Importer commentStamp!
  395. I can import Amber code from a string in the chunk format.
  396. ## API
  397. Importer new import: aString!
  398. !Importer methodsFor: 'fileIn'!
  399. import: aStream
  400. | chunk result parser lastEmpty |
  401. parser := ChunkParser on: aStream.
  402. lastEmpty := false.
  403. [chunk := parser nextChunk.
  404. chunk isNil] whileFalse: [
  405. chunk isEmpty
  406. ifTrue: [lastEmpty := true]
  407. ifFalse: [
  408. result := Compiler new evaluateExpression: chunk.
  409. lastEmpty
  410. ifTrue: [
  411. lastEmpty := false.
  412. result scanFrom: parser]]]
  413. ! !
  414. Object subclass: #MethodCategory
  415. instanceVariableNames: 'methods name theClass'
  416. package: 'Importer-Exporter'!
  417. !MethodCategory commentStamp!
  418. I am an abstraction for a method category in a class / metaclass.
  419. I know of my class, name and methods.
  420. I am used when exporting a package.!
  421. !MethodCategory methodsFor: 'accessing'!
  422. methods
  423. ^methods
  424. !
  425. methods: aCollection
  426. methods := aCollection
  427. !
  428. name
  429. ^name
  430. !
  431. name: aString
  432. name := aString
  433. !
  434. theClass
  435. ^theClass
  436. !
  437. theClass: aClass
  438. theClass := aClass
  439. ! !
  440. !MethodCategory class methodsFor: 'not yet classified'!
  441. name: aString theClass: aClass methods: anArray
  442. ^self new
  443. name: aString;
  444. theClass: aClass;
  445. methods: anArray;
  446. yourself
  447. ! !
  448. InterfacingObject subclass: #PackageHandler
  449. instanceVariableNames: ''
  450. package: 'Importer-Exporter'!
  451. !PackageHandler commentStamp!
  452. I am responsible for handling package loading and committing.
  453. I should not be used directly. Instead, use the corresponding `Package` methods.!
  454. !PackageHandler methodsFor: 'committing'!
  455. commit: aPackage
  456. self commitChannels
  457. do: [ :commitStrategyFactory || fileContents commitStrategy |
  458. commitStrategy := commitStrategyFactory value: aPackage.
  459. fileContents := String streamContents: [ :stream |
  460. (PluggableExporter forRecipe: commitStrategy key) exportPackage: aPackage on: stream ].
  461. self ajaxPutAt: commitStrategy value data: fileContents ]
  462. displayingProgress: 'Committing package ', aPackage name
  463. !
  464. commitChannels
  465. self subclassResponsibility
  466. ! !
  467. !PackageHandler methodsFor: 'private'!
  468. ajaxPutAt: aURL data: aString
  469. self
  470. ajax: #{
  471. 'url' -> aURL.
  472. 'type' -> 'PUT'.
  473. 'data' -> aString.
  474. 'contentType' -> 'text/plain;charset=UTF-8'.
  475. 'error' -> [ :xhr | self error: 'Commiting ' , aURL , ' failed with reason: "' , (xhr responseText) , '"'] }
  476. ! !
  477. PackageHandler class instanceVariableNames: 'registry'!
  478. !PackageHandler class methodsFor: 'accessing'!
  479. classRegisteredFor: aString
  480. ^registry at: aString
  481. !
  482. for: aString
  483. ^(self classRegisteredFor: aString) new
  484. ! !
  485. !PackageHandler class methodsFor: 'initialization'!
  486. initialize
  487. super initialize.
  488. registry := #{}
  489. ! !
  490. !PackageHandler class methodsFor: 'registry'!
  491. register: aClass for: aString
  492. registry at: aString put: aClass
  493. !
  494. registerFor: aString
  495. PackageHandler register: self for: aString
  496. ! !
  497. PackageHandler subclass: #AmdPackageHandler
  498. instanceVariableNames: ''
  499. package: 'Importer-Exporter'!
  500. !AmdPackageHandler commentStamp!
  501. I am responsible for handling package loading and committing.
  502. I should not be used directly. Instead, use the corresponding `Package` methods.!
  503. !AmdPackageHandler methodsFor: 'committing'!
  504. commitChannels
  505. ^{
  506. [ :pkg | Exporter default amdRecipe -> (pkg commitPathJs, '/', pkg name, '.js') ].
  507. [ :pkg | StrippedExporter default amdRecipe -> (pkg commitPathJs, '/', pkg name, '.deploy.js') ].
  508. [ :pkg | ChunkExporter default recipe -> (pkg commitPathSt, '/', pkg name, '.st') ]
  509. }
  510. !
  511. commitPathJsFor: aPackage
  512. ^self toUrl: (self namespaceFor: aPackage)
  513. !
  514. commitPathStFor: aPackage
  515. "if _source is not mapped, .st commit will likely fail"
  516. ^self toUrl: (self namespaceFor: aPackage), '/_source'.
  517. !
  518. namespaceFor: aPackage
  519. ^aPackage amdNamespace
  520. ifNil: [ aPackage amdNamespace: self class defaultNamespace; amdNamespace ]
  521. ! !
  522. !AmdPackageHandler methodsFor: 'private'!
  523. toUrl: aString
  524. (Smalltalk current at: '_amd_require')
  525. ifNil: [ self error: 'AMD loader not present' ]
  526. ifNotNil: [ :require | ^(require basicAt: 'toUrl') value: aString ]
  527. ! !
  528. AmdPackageHandler class instanceVariableNames: 'defaultNamespace'!
  529. !AmdPackageHandler class methodsFor: 'commit paths'!
  530. commitPathsFromLoader
  531. "TODO"
  532. !
  533. defaultNamespace
  534. ^ defaultNamespace ifNil: [ self error: 'AMD default namespace not set.' ]
  535. !
  536. defaultNamespace: aString
  537. defaultNamespace := aString
  538. !
  539. resetCommitPaths
  540. defaultNamespace := nil
  541. ! !
  542. !AmdPackageHandler class methodsFor: 'initialization'!
  543. initialize
  544. super initialize.
  545. self registerFor: 'amd'.
  546. self commitPathsFromLoader
  547. ! !
  548. PackageHandler subclass: #LegacyPackageHandler
  549. instanceVariableNames: ''
  550. package: 'Importer-Exporter'!
  551. !LegacyPackageHandler commentStamp!
  552. I am responsible for handling package loading and committing.
  553. I should not be used directly. Instead, use the corresponding `Package` methods.!
  554. !LegacyPackageHandler methodsFor: 'committing'!
  555. commitChannels
  556. ^{
  557. [ :pkg | Exporter default recipe -> (pkg commitPathJs, '/', pkg name, '.js') ].
  558. [ :pkg | StrippedExporter default recipe -> (pkg commitPathJs, '/', pkg name, '.deploy.js') ].
  559. [ :pkg | ChunkExporter default recipe -> (pkg commitPathSt, '/', pkg name, '.st') ]
  560. }
  561. !
  562. commitPathJsFor: aPackage
  563. ^self class defaultCommitPathJs
  564. !
  565. commitPathStFor: aPackage
  566. ^self class defaultCommitPathSt
  567. ! !
  568. !LegacyPackageHandler methodsFor: 'loading'!
  569. loadPackage: packageName prefix: aString
  570. | url |
  571. url := '/', aString, '/js/', packageName, '.js'.
  572. self
  573. ajax: #{
  574. 'url' -> url.
  575. 'type' -> 'GET'.
  576. 'dataType' -> 'script'.
  577. 'complete' -> [ :jqXHR :textStatus |
  578. jqXHR readyState = 4
  579. ifTrue: [ self setupPackageNamed: packageName prefix: aString ] ].
  580. 'error' -> [ self alert: 'Could not load package at: ', url ]
  581. }
  582. !
  583. loadPackages: aCollection prefix: aString
  584. aCollection do: [ :each |
  585. self loadPackage: each prefix: aString ]
  586. ! !
  587. !LegacyPackageHandler methodsFor: 'private'!
  588. setupPackageNamed: packageName prefix: aString
  589. (Package named: packageName)
  590. setupClasses;
  591. commitPathJs: '/', aString, '/js';
  592. commitPathSt: '/', aString, '/st'
  593. ! !
  594. LegacyPackageHandler class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  595. !LegacyPackageHandler class methodsFor: 'commit paths'!
  596. commitPathsFromLoader
  597. <
  598. var commitPath = typeof amber !!== 'undefined' && amber.commitPath;
  599. if (!!commitPath) return;
  600. if (commitPath.js) self._defaultCommitPathJs_(commitPath.js);
  601. if (commitPath.st) self._defaultCommitPathSt_(commitPath.st);
  602. >
  603. !
  604. defaultCommitPathJs
  605. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  606. !
  607. defaultCommitPathJs: aString
  608. defaultCommitPathJs := aString
  609. !
  610. defaultCommitPathSt
  611. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  612. !
  613. defaultCommitPathSt: aString
  614. defaultCommitPathSt := aString
  615. !
  616. resetCommitPaths
  617. defaultCommitPathJs := nil.
  618. defaultCommitPathSt := nil
  619. ! !
  620. !LegacyPackageHandler class methodsFor: 'initialization'!
  621. initialize
  622. super initialize.
  623. self registerFor: 'unknown'.
  624. self commitPathsFromLoader
  625. ! !
  626. !LegacyPackageHandler class methodsFor: 'loading'!
  627. loadPackages: aCollection prefix: aString
  628. ^ self new loadPackages: aCollection prefix: aString
  629. ! !
  630. Object subclass: #PluggableExporter
  631. instanceVariableNames: 'recipe'
  632. package: 'Importer-Exporter'!
  633. !PluggableExporter commentStamp!
  634. I am an engine for exporting structured data on a Stream.
  635. My instances are created using
  636. PluggableExporter newUsing: recipe,
  637. where recipe is structured description of the exporting algorithm.
  638. The actual exporting is done by interpreting the recipe using a `RecipeInterpreter`.
  639. I am used to export amber packages, so I have a convenience method
  640. `exportPackage: aPackage on: aStream`
  641. which exports `aPackage` using the `recipe`
  642. (it is otherwise no special, so it may be renamed to export:on:)!
  643. !PluggableExporter methodsFor: 'accessing'!
  644. interpreter
  645. ^ RecipeInterpreter new
  646. !
  647. recipe
  648. ^recipe
  649. !
  650. recipe: anArray
  651. recipe := anArray
  652. ! !
  653. !PluggableExporter methodsFor: 'fileOut'!
  654. exportAllPackages
  655. "Export all packages in the system."
  656. ^String streamContents: [:stream |
  657. Smalltalk current packages do: [:pkg |
  658. self exportPackage: pkg on: stream]]
  659. !
  660. exportPackage: aPackage on: aStream
  661. self interpreter interpret: self recipe for: aPackage on: aStream
  662. ! !
  663. !PluggableExporter class methodsFor: 'convenience'!
  664. ownClassesOfPackage: package
  665. "Export classes in dependency order.
  666. Update (issue #171): Remove duplicates for export"
  667. ^package sortedClasses asSet
  668. ! !
  669. !PluggableExporter class methodsFor: 'instance creation'!
  670. forRecipe: aRecipe
  671. ^self new recipe: aRecipe; yourself
  672. ! !
  673. Object subclass: #RecipeInterpreter
  674. instanceVariableNames: ''
  675. package: 'Importer-Exporter'!
  676. !RecipeInterpreter commentStamp!
  677. I am an interpreter for export recipes.
  678. ## Recipe format
  679. Recipe is an array, which can contain two kinds of elements:
  680. - an assocation where the key is the receiver and the value is a two-arguments selector
  681. In this case, `receiver perform: selector withArguments: { data. stream }` is called.
  682. This essentially defines one step of export process.
  683. The key (eg. receiver) is presumed to be some kind of 'repository' of the exporting methods
  684. that just format appropriate aspect of data into a stream; like a class or a singleton,
  685. so that the recipe itself can be decoupled from data.
  686. - a subarray, where first element is special and the rest is recursive recipe.
  687. `subarray first` must be an association similar to one above,
  688. with key being the 'repository' receiver, but value is one-arg selector.
  689. In this case, `receiver perform: selector withArguments: { data }` should create a collection.
  690. Then, the sub-recipe (`subarray allButFirst`) is applied to every element of a collection, eg.
  691. collection do: [ :each | self export: each using: sa allButFirst on: stream ]!
  692. !RecipeInterpreter methodsFor: 'interpreting'!
  693. interpret: aRecipe for: anObject on: aStream
  694. | recipeStream |
  695. recipeStream := aRecipe readStream.
  696. [ recipeStream atEnd ] whileFalse: [
  697. self
  698. interpretStep: recipeStream next
  699. for: anObject
  700. on: aStream ]
  701. !
  702. interpretStep: aRecipeStep for: anObject on: aStream
  703. aRecipeStep value == aRecipeStep ifTrue: [
  704. ^ self interpretSubRecipe: aRecipeStep for: anObject on: aStream ].
  705. aRecipeStep key perform: aRecipeStep value withArguments: { anObject. aStream }
  706. !
  707. interpretSubRecipe: aRecipe for: anObject on: aStream
  708. | selection |
  709. selection := aRecipe first key
  710. perform: aRecipe first value
  711. withArguments: { anObject }.
  712. selection do: [ :each |
  713. self interpret: aRecipe allButFirst for: each on: aStream ]
  714. ! !
  715. !Package methodsFor: '*Importer-Exporter'!
  716. amdNamespace
  717. <return (self.transport && self.transport.amdNamespace) || nil>
  718. !
  719. amdNamespace: aString
  720. <
  721. if (!!self.transport) { self.transport = { type: 'amd' }; }
  722. if (self.transport.type !!== 'amd') { throw new Error('Package '+self._name()+' has transport type '+self.transport.type+', not "amd".'); }
  723. self.transport.amdNamespace = aString;
  724. >
  725. !
  726. commit
  727. ^ self transport commit: self
  728. !
  729. commitPathJs
  730. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathJs ifAbsent: [self transport commitPathJsFor: self]
  731. !
  732. commitPathJs: aString
  733. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathJs put: aString
  734. !
  735. commitPathSt
  736. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathSt ifAbsent: [self transport commitPathStFor: self]
  737. !
  738. commitPathSt: aString
  739. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathSt put: aString
  740. !
  741. transport
  742. ^ PackageHandler for: self transportType
  743. !
  744. transportJson
  745. <return JSON.stringify(self.transport || null);>
  746. !
  747. transportType
  748. <return (self.transport && self.transport.type) || 'unknown';>
  749. ! !