Importer-Exporter.st 26 KB

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