Importer-Exporter.st 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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 methodsFor: 'output'!
  358. exportPackageEpilogueOf: aPackage on: aStream
  359. aStream
  360. nextPutAll: '});';
  361. lf
  362. !
  363. exportPackagePrologueOf: aPackage on: aStream
  364. aStream
  365. nextPutAll: 'define("';
  366. nextPutAll: (aPackage amdNamespace ifNil: [ 'amber' ]); "ifNil: only for LegacyPH, it should not happen with AmdPH"
  367. nextPutAll: '/';
  368. nextPutAll: aPackage name;
  369. nextPutAll: '", ';
  370. nextPutAll: (#('amber_vm/smalltalk' 'amber_vm/nil' 'amber_vm/_st'), (self amdNamesOfPackages: aPackage loadDependencies)) asJavascript;
  371. nextPutAll: ', function(smalltalk,nil,_st){';
  372. lf
  373. ! !
  374. !AmdExporter methodsFor: 'private'!
  375. amdNamesOfPackages: anArray
  376. ^ (anArray
  377. select: [ :each | each amdNamespace notNil ])
  378. collect: [ :each | each amdNamespace, '/', each name ]
  379. ! !
  380. !AmdExporter class methodsFor: 'exporting-output'!
  381. exportPackageEpilogueOf: aPackage on: aStream
  382. aStream
  383. nextPutAll: '});';
  384. lf
  385. !
  386. exportPackagePrologueOf: aPackage on: aStream
  387. aStream
  388. nextPutAll: 'define("';
  389. nextPutAll: (aPackage amdNamespace ifNil: [ 'amber' ]); "ifNil: only for LegacyPH, it should not happen with AmdPH"
  390. nextPutAll: '/';
  391. nextPutAll: aPackage name;
  392. nextPutAll: '", ';
  393. nextPutAll: (#('amber_vm/smalltalk' 'amber_vm/nil' 'amber_vm/_st'), (self amdNamesOfPackages: aPackage loadDependencies)) asJavascript;
  394. nextPutAll: ', function(smalltalk,nil,_st){';
  395. lf
  396. ! !
  397. !AmdExporter class methodsFor: 'private'!
  398. amdNamesOfPackages: anArray
  399. | deps depNames |
  400. ^(anArray
  401. select: [ :each | each amdNamespace notNil ])
  402. collect: [ :each | each amdNamespace, '/', each name ]
  403. ! !
  404. Object subclass: #ChunkParser
  405. instanceVariableNames: 'stream'
  406. package: 'Importer-Exporter'!
  407. !ChunkParser commentStamp!
  408. I am responsible for parsing aStream contents in the chunk format.
  409. ## API
  410. ChunkParser new
  411. stream: aStream;
  412. nextChunk!
  413. !ChunkParser methodsFor: 'accessing'!
  414. stream: aStream
  415. stream := aStream
  416. ! !
  417. !ChunkParser methodsFor: 'reading'!
  418. nextChunk
  419. "The chunk format (Smalltalk Interchange Format or Fileout format)
  420. is a trivial format but can be a bit tricky to understand:
  421. - Uses the exclamation mark as delimiter of chunks.
  422. - Inside a chunk a normal exclamation mark must be doubled.
  423. - A non empty chunk must be a valid Smalltalk expression.
  424. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  425. - The object created by the expression then takes over reading chunks.
  426. This method returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  427. | char result chunk |
  428. result := '' writeStream.
  429. [char := stream next.
  430. char notNil] whileTrue: [
  431. char = '!!' ifTrue: [
  432. stream peek = '!!'
  433. ifTrue: [stream next "skipping the escape double"]
  434. ifFalse: [^result contents trimBoth "chunk end marker found"]].
  435. result nextPut: char].
  436. ^nil "a chunk needs to end with !!"
  437. ! !
  438. !ChunkParser class methodsFor: 'instance creation'!
  439. on: aStream
  440. ^self new stream: aStream
  441. ! !
  442. Object subclass: #ExportMethodProtocol
  443. instanceVariableNames: 'name theClass'
  444. package: 'Importer-Exporter'!
  445. !ExportMethodProtocol commentStamp!
  446. I am an abstraction for a method protocol in a class / metaclass.
  447. I know of my class, name and methods.
  448. I am used when exporting a package.!
  449. !ExportMethodProtocol methodsFor: 'accessing'!
  450. methods
  451. ^ self theClass methodsInProtocol: self name
  452. !
  453. name
  454. ^name
  455. !
  456. name: aString
  457. name := aString
  458. !
  459. theClass
  460. ^theClass
  461. !
  462. theClass: aClass
  463. theClass := aClass
  464. !
  465. sortedMethods
  466. ^ self methods sorted: [ :a :b | a selector <= b selector ]
  467. ! !
  468. !ExportMethodProtocol class methodsFor: 'instance creation'!
  469. name: aString theClass: aClass
  470. ^self new
  471. name: aString;
  472. theClass: aClass;
  473. yourself
  474. ! !
  475. Object subclass: #Importer
  476. instanceVariableNames: ''
  477. package: 'Importer-Exporter'!
  478. !Importer commentStamp!
  479. I can import Amber code from a string in the chunk format.
  480. ## API
  481. Importer new import: aString!
  482. !Importer methodsFor: 'fileIn'!
  483. import: aStream
  484. | chunk result parser lastEmpty |
  485. parser := ChunkParser on: aStream.
  486. lastEmpty := false.
  487. [chunk := parser nextChunk.
  488. chunk isNil] whileFalse: [
  489. chunk isEmpty
  490. ifTrue: [lastEmpty := true]
  491. ifFalse: [
  492. result := Compiler new evaluateExpression: chunk.
  493. lastEmpty
  494. ifTrue: [
  495. lastEmpty := false.
  496. result scanFrom: parser]]]
  497. ! !
  498. Object subclass: #MethodCategory
  499. instanceVariableNames: 'methods name theClass'
  500. package: 'Importer-Exporter'!
  501. !MethodCategory commentStamp!
  502. I am an abstraction for a method category in a class / metaclass.
  503. I know of my class, name and methods.
  504. I am used when exporting a package.!
  505. !MethodCategory methodsFor: 'accessing'!
  506. methods
  507. ^methods
  508. !
  509. methods: aCollection
  510. methods := aCollection
  511. !
  512. name
  513. ^name
  514. !
  515. name: aString
  516. name := aString
  517. !
  518. theClass
  519. ^theClass
  520. !
  521. theClass: aClass
  522. theClass := aClass
  523. ! !
  524. !MethodCategory class methodsFor: 'not yet classified'!
  525. name: aString theClass: aClass methods: anArray
  526. ^self new
  527. name: aString;
  528. theClass: aClass;
  529. methods: anArray;
  530. yourself
  531. ! !
  532. InterfacingObject subclass: #PackageHandler
  533. instanceVariableNames: ''
  534. package: 'Importer-Exporter'!
  535. !PackageHandler commentStamp!
  536. I am responsible for handling package loading and committing.
  537. I should not be used directly. Instead, use the corresponding `Package` methods.!
  538. !PackageHandler methodsFor: 'accessing'!
  539. commitPathJsFor: aPackage
  540. self subclassResponsibility
  541. !
  542. commitPathStFor: aPackage
  543. self subclassResponsibility
  544. !
  545. exporterClass
  546. ^ Exporter
  547. !
  548. chunkExporterClass
  549. ^ ChunkExporter
  550. !
  551. chunkContentsFor: aPackage
  552. ^ String streamContents: [ :str |
  553. self chunkExporter exportPackage: aPackage on: str ]
  554. !
  555. contentsFor: aPackage
  556. ^ String streamContents: [ :str |
  557. self exporter exportPackage: aPackage on: str ]
  558. ! !
  559. !PackageHandler methodsFor: 'committing'!
  560. commit: aPackage
  561. {
  562. [ self commitStFileFor: aPackage ].
  563. [ self commitJsFileFor: aPackage ]
  564. }
  565. do: [ :each | each value ]
  566. displayingProgress: 'Committing package ', aPackage name
  567. !
  568. oldCommit: aPackage
  569. self commitChannels
  570. do: [ :commitStrategyFactory || fileContents commitStrategy |
  571. commitStrategy := commitStrategyFactory value: aPackage.
  572. fileContents := String streamContents: [ :stream |
  573. (PluggableExporter forRecipe: commitStrategy key) exportPackage: aPackage on: stream ].
  574. self ajaxPutAt: commitStrategy value data: fileContents ]
  575. displayingProgress: 'Committing package ', aPackage name
  576. !
  577. commitStFileFor: aPackage
  578. self
  579. ajaxPutAt: (self commitPathStFor: aPackage), '/', aPackage name, '.st'
  580. data: (self chunkContentsFor: aPackage)
  581. !
  582. commitJsFileFor: aPackage
  583. self
  584. ajaxPutAt: (self commitPathJsFor: aPackage), '/', aPackage name, '.js'
  585. data: (self contentsFor: aPackage)
  586. ! !
  587. !PackageHandler methodsFor: 'factory'!
  588. chunkExporter
  589. ^ self chunkExporterClass default
  590. !
  591. exporter
  592. ^ self exporterClass default
  593. ! !
  594. !PackageHandler methodsFor: 'private'!
  595. ajaxPutAt: aURL data: aString
  596. self
  597. ajax: #{
  598. 'url' -> aURL.
  599. 'type' -> 'PUT'.
  600. 'data' -> aString.
  601. 'contentType' -> 'text/plain;charset=UTF-8'.
  602. 'error' -> [ :xhr | self error: 'Commiting ' , aURL , ' failed with reason: "' , (xhr responseText) , '"'] }
  603. ! !
  604. PackageHandler class instanceVariableNames: 'registry'!
  605. !PackageHandler class methodsFor: 'accessing'!
  606. classRegisteredFor: aString
  607. ^ registry at: aString
  608. !
  609. for: aString
  610. ^ (self classRegisteredFor: aString) new
  611. ! !
  612. !PackageHandler class methodsFor: 'initialization'!
  613. initialize
  614. super initialize.
  615. registry := #{}
  616. ! !
  617. !PackageHandler class methodsFor: 'registry'!
  618. register: aClass for: aString
  619. registry at: aString put: aClass
  620. !
  621. registerFor: aString
  622. PackageHandler register: self for: aString
  623. ! !
  624. PackageHandler subclass: #AmdPackageHandler
  625. instanceVariableNames: ''
  626. package: 'Importer-Exporter'!
  627. !AmdPackageHandler commentStamp!
  628. I am responsible for handling package loading and committing.
  629. I should not be used directly. Instead, use the corresponding `Package` methods.!
  630. !AmdPackageHandler methodsFor: 'accessing'!
  631. commitPathJsFor: aPackage
  632. ^self toUrl: (self namespaceFor: aPackage)
  633. !
  634. commitPathStFor: aPackage
  635. "if _source is not mapped, .st commit will likely fail"
  636. ^self toUrl: (self namespaceFor: aPackage), '/_source'.
  637. !
  638. exporterClass
  639. ^ AmdExporter
  640. ! !
  641. !AmdPackageHandler methodsFor: 'committing'!
  642. namespaceFor: aPackage
  643. ^ aPackage amdNamespace
  644. ifNil: [ aPackage amdNamespace: self class defaultNamespace; amdNamespace ]
  645. ! !
  646. !AmdPackageHandler methodsFor: 'private'!
  647. toUrl: aString
  648. ^ Smalltalk current amdRequire
  649. ifNil: [ self error: 'AMD loader not present' ]
  650. ifNotNil: [ :require | (require basicAt: 'toUrl') value: aString ]
  651. ! !
  652. !AmdPackageHandler class methodsFor: 'commit paths'!
  653. defaultNamespace
  654. ^ Smalltalk current defaultAMDNamespace
  655. !
  656. defaultNamespace: aString
  657. Smalltalk current defaultAMDNamespace: aString
  658. !
  659. resetCommitPaths
  660. defaultNamespace := nil
  661. ! !
  662. !AmdPackageHandler class methodsFor: 'initialization'!
  663. initialize
  664. super initialize.
  665. self registerFor: 'amd'
  666. ! !
  667. PackageHandler subclass: #LegacyPackageHandler
  668. instanceVariableNames: ''
  669. package: 'Importer-Exporter'!
  670. !LegacyPackageHandler commentStamp!
  671. I am responsible for handling package loading and committing.
  672. I should not be used directly. Instead, use the corresponding `Package` methods.!
  673. !LegacyPackageHandler methodsFor: 'committing'!
  674. commitChannels
  675. ^{
  676. [ :pkg | Exporter default recipe -> (pkg commitPathJs, '/', pkg name, '.js') ].
  677. [ :pkg | StrippedExporter default recipe -> (pkg commitPathJs, '/', pkg name, '.deploy.js') ].
  678. [ :pkg | ChunkExporter default recipe -> (pkg commitPathSt, '/', pkg name, '.st') ]
  679. }
  680. !
  681. commitPathJsFor: aPackage
  682. ^self class defaultCommitPathJs
  683. !
  684. commitPathStFor: aPackage
  685. ^self class defaultCommitPathSt
  686. ! !
  687. !LegacyPackageHandler methodsFor: 'loading'!
  688. loadPackage: packageName prefix: aString
  689. | url |
  690. url := '/', aString, '/js/', packageName, '.js'.
  691. self
  692. ajax: #{
  693. 'url' -> url.
  694. 'type' -> 'GET'.
  695. 'dataType' -> 'script'.
  696. 'complete' -> [ :jqXHR :textStatus |
  697. jqXHR readyState = 4
  698. ifTrue: [ self setupPackageNamed: packageName prefix: aString ] ].
  699. 'error' -> [ self alert: 'Could not load package at: ', url ]
  700. }
  701. !
  702. loadPackages: aCollection prefix: aString
  703. aCollection do: [ :each |
  704. self loadPackage: each prefix: aString ]
  705. ! !
  706. !LegacyPackageHandler methodsFor: 'private'!
  707. setupPackageNamed: packageName prefix: aString
  708. (Package named: packageName)
  709. setupClasses;
  710. commitPathJs: '/', aString, '/js';
  711. commitPathSt: '/', aString, '/st'
  712. ! !
  713. LegacyPackageHandler class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  714. !LegacyPackageHandler class methodsFor: 'commit paths'!
  715. commitPathsFromLoader
  716. <
  717. var commitPath = typeof amber !!== 'undefined' && amber.commitPath;
  718. if (!!commitPath) return;
  719. if (commitPath.js) self._defaultCommitPathJs_(commitPath.js);
  720. if (commitPath.st) self._defaultCommitPathSt_(commitPath.st);
  721. >
  722. !
  723. defaultCommitPathJs
  724. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  725. !
  726. defaultCommitPathJs: aString
  727. defaultCommitPathJs := aString
  728. !
  729. defaultCommitPathSt
  730. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  731. !
  732. defaultCommitPathSt: aString
  733. defaultCommitPathSt := aString
  734. !
  735. resetCommitPaths
  736. defaultCommitPathJs := nil.
  737. defaultCommitPathSt := nil
  738. ! !
  739. !LegacyPackageHandler class methodsFor: 'initialization'!
  740. initialize
  741. super initialize.
  742. self registerFor: 'unknown'.
  743. self commitPathsFromLoader
  744. ! !
  745. !LegacyPackageHandler class methodsFor: 'loading'!
  746. loadPackages: aCollection prefix: aString
  747. ^ self new loadPackages: aCollection prefix: aString
  748. ! !
  749. Object subclass: #PluggableExporter
  750. instanceVariableNames: 'recipe'
  751. package: 'Importer-Exporter'!
  752. !PluggableExporter commentStamp!
  753. I am an engine for exporting structured data on a Stream.
  754. My instances are created using
  755. PluggableExporter forRecipe: aRecipe,
  756. where recipe is structured description of the exporting algorithm (see `ExportRecipeInterpreter`).
  757. The actual exporting is done by interpreting the recipe using a `RecipeInterpreter`.
  758. I am used to export amber packages, so I have a convenience method
  759. `exportPackage: aPackage on: aStream`
  760. which exports `aPackage` using the `recipe`
  761. (it is otherwise no special, so it may be renamed to export:on:)!
  762. !PluggableExporter methodsFor: 'accessing'!
  763. interpreter
  764. ^ ExportRecipeInterpreter new
  765. !
  766. recipe
  767. ^recipe
  768. !
  769. recipe: anArray
  770. recipe := anArray
  771. ! !
  772. !PluggableExporter methodsFor: 'fileOut'!
  773. exportAllPackages
  774. "Export all packages in the system."
  775. ^String streamContents: [:stream |
  776. Smalltalk current packages do: [:pkg |
  777. self exportPackage: pkg on: stream]]
  778. !
  779. exportPackage: aPackage on: aStream
  780. self interpreter interpret: self recipe for: aPackage on: aStream
  781. ! !
  782. !PluggableExporter class methodsFor: 'convenience'!
  783. ownClassesOfPackage: package
  784. "Export classes in dependency order.
  785. Update (issue #171): Remove duplicates for export"
  786. ^package sortedClasses asSet
  787. ! !
  788. !PluggableExporter class methodsFor: 'instance creation'!
  789. forRecipe: aRecipe
  790. ^self new recipe: aRecipe; yourself
  791. ! !
  792. !Package methodsFor: '*Importer-Exporter'!
  793. amdNamespace
  794. <return (self.transport && self.transport.amdNamespace) || nil>
  795. !
  796. amdNamespace: aString
  797. <
  798. if (!!self.transport) { self.transport = { type: 'amd' }; }
  799. if (self.transport.type !!== 'amd') { throw new Error('Package '+self._name()+' has transport type '+self.transport.type+', not "amd".'); }
  800. self.transport.amdNamespace = aString;
  801. >
  802. !
  803. commit
  804. ^ self handler commit: self
  805. !
  806. commitPathJs
  807. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathJs ifAbsent: [ self handler commitPathJsFor: self ]
  808. !
  809. commitPathJs: aString
  810. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathJs put: aString
  811. !
  812. commitPathSt
  813. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathSt ifAbsent: [ self handler commitPathStFor: self ]
  814. !
  815. commitPathSt: aString
  816. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathSt put: aString
  817. !
  818. handler
  819. ^ PackageHandler for: self transportType
  820. !
  821. transportJson
  822. <return JSON.stringify(self.transport || null);>
  823. !
  824. transportType
  825. <return (self.transport && self.transport.type) || 'unknown';>
  826. ! !