1
0

Importer-Exporter.st 23 KB

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