1
0

Importer-Exporter.st 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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: 'output'!
  92. exportCategoryEpilogueOf: aCategory on: aStream
  93. aStream nextPutAll: ' !!'; lf; lf
  94. !
  95. exportCategoryPrologueOf: aCategory on: aStream
  96. aStream
  97. nextPutAll: '!!', (self classNameFor: aCategory theClass);
  98. nextPutAll: ' methodsFor: ''', aCategory name, '''!!'
  99. !
  100. exportDefinitionOf: aClass on: aStream
  101. "Chunk format."
  102. aStream
  103. nextPutAll: (self classNameFor: aClass superclass);
  104. nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;
  105. tab; nextPutAll: 'instanceVariableNames: '''.
  106. aClass instanceVariableNames
  107. do: [:each | aStream nextPutAll: each]
  108. separatedBy: [aStream nextPutAll: ' '].
  109. aStream
  110. nextPutAll: ''''; lf;
  111. tab; nextPutAll: 'package: ''', aClass category, '''!!'; lf.
  112. aClass comment notEmpty ifTrue: [
  113. aStream
  114. nextPutAll: '!!', (self classNameFor: aClass), ' commentStamp!!';lf;
  115. nextPutAll: (self chunkEscape: aClass comment), '!!';lf].
  116. aStream lf
  117. !
  118. exportMetaDefinitionOf: aClass on: aStream
  119. aClass class instanceVariableNames isEmpty ifFalse: [
  120. aStream
  121. nextPutAll: (self classNameFor: aClass class);
  122. nextPutAll: ' instanceVariableNames: '''.
  123. aClass class instanceVariableNames
  124. do: [:each | aStream nextPutAll: each]
  125. separatedBy: [aStream nextPutAll: ' '].
  126. aStream
  127. nextPutAll: '''!!'; lf; lf]
  128. !
  129. exportMethod: aMethod on: aStream
  130. aStream
  131. lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;
  132. nextPutAll: '!!'
  133. !
  134. exportPackage: aPackage on: aStream
  135. self exportPackageDefinitionOf: aPackage on: aStream.
  136. aPackage sortedClasses do: [ :each |
  137. self exportDefinitionOf: each on: aStream.
  138. self
  139. exportProtocols: (self ownMethodProtocolsOfClass: each)
  140. on: aStream.
  141. self exportMetaDefinitionOf: each on: aStream.
  142. self
  143. exportProtocols: (self ownMethodProtocolsOfClass: each class)
  144. on: aStream ].
  145. self
  146. exportProtocols: (self extensionProtocolsOfPackage: aPackage)
  147. on: aStream
  148. !
  149. exportPackageDefinitionOf: aPackage on: aStream
  150. aStream
  151. nextPutAll: 'Smalltalk current createPackage: ''', aPackage name, '''!!';
  152. lf
  153. !
  154. exportProtocol: aProtocol on: aStream
  155. self exportProtocolPrologueOf: aProtocol on: aStream.
  156. aProtocol methods do: [ :method |
  157. self exportMethod: method on: aStream ].
  158. self exportProtocolEpilogueOf: aProtocol on: aStream
  159. !
  160. exportProtocolEpilogueOf: aProtocol on: aStream
  161. aStream nextPutAll: ' !!'; lf; lf
  162. !
  163. exportProtocolPrologueOf: aProtocol on: aStream
  164. aStream
  165. nextPutAll: '!!', (self classNameFor: aProtocol theClass);
  166. nextPutAll: ' methodsFor: ''', aProtocol name, '''!!'
  167. !
  168. exportProtocols: aCollection on: aStream
  169. aCollection do: [ :each |
  170. self exportProtocol: each on: aStream ]
  171. ! !
  172. AbstractExporter subclass: #Exporter
  173. instanceVariableNames: ''
  174. package: 'Importer-Exporter'!
  175. !Exporter commentStamp!
  176. I am responsible for outputting Amber code into a JavaScript string.
  177. The generated output is enough to reconstruct the exported data, including Smalltalk source code and other metadata.
  178. ## Use case
  179. I am typically used to save code outside of the Amber runtime (committing to disk, etc.).!
  180. !Exporter methodsFor: 'accessing'!
  181. ownMethodsOfClass: aClass
  182. "Issue #143: sort methods alphabetically"
  183. ^((aClass methodDictionary values) sorted: [:a :b | a selector <= b selector])
  184. reject: [:each | (each category match: '^\*')]
  185. !
  186. ownMethodsOfMetaClass: aClass
  187. "Issue #143: sort methods alphabetically"
  188. ^self ownMethodsOfClass: aClass class
  189. ! !
  190. !Exporter methodsFor: 'convenience'!
  191. classNameFor: aClass
  192. ^aClass isMetaclass
  193. ifTrue: [ aClass instanceClass name, '.klass' ]
  194. ifFalse: [
  195. aClass isNil
  196. ifTrue: [ 'nil' ]
  197. ifFalse: [ aClass name ] ]
  198. ! !
  199. !Exporter methodsFor: 'output'!
  200. exportDefinitionOf: aClass on: aStream
  201. aStream
  202. lf;
  203. nextPutAll: 'smalltalk.addClass(';
  204. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  205. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  206. nextPutAll: ', ['.
  207. aClass instanceVariableNames
  208. do: [:each | aStream nextPutAll: '''', each, '''']
  209. separatedBy: [aStream nextPutAll: ', '].
  210. aStream
  211. nextPutAll: '], ''';
  212. nextPutAll: aClass category, '''';
  213. nextPutAll: ');'.
  214. aClass comment notEmpty ifTrue: [
  215. aStream
  216. lf;
  217. nextPutAll: 'smalltalk.';
  218. nextPutAll: (self classNameFor: aClass);
  219. nextPutAll: '.comment=';
  220. nextPutAll: aClass comment asJavascript;
  221. nextPutAll: ';'].
  222. aStream lf
  223. !
  224. exportMetaDefinitionOf: aClass on: aStream
  225. aStream lf.
  226. aClass class instanceVariableNames isEmpty ifFalse: [
  227. aStream
  228. nextPutAll: 'smalltalk.', (self classNameFor: aClass class);
  229. nextPutAll: '.iVarNames = ['.
  230. aClass class instanceVariableNames
  231. do: [:each | aStream nextPutAll: '''', each, '''']
  232. separatedBy: [aStream nextPutAll: ','].
  233. aStream nextPutAll: '];', String lf]
  234. !
  235. exportMethod: aMethod on: aStream
  236. aStream
  237. nextPutAll: 'smalltalk.addMethod(';lf;
  238. "nextPutAll: aMethod selector asSelector asJavascript, ',';lf;"
  239. nextPutAll: 'smalltalk.method({';lf;
  240. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  241. nextPutAll: 'category: ''', aMethod category, ''',';lf;
  242. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  243. nextPutAll: 'args: ', aMethod arguments asJavascript, ','; lf;
  244. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf;
  245. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;
  246. nextPutAll: 'referencedClasses: ', aMethod referencedClasses asJavascript.
  247. aStream
  248. lf;
  249. nextPutAll: '}),';lf;
  250. nextPutAll: 'smalltalk.', (self classNameFor: aMethod methodClass);
  251. nextPutAll: ');';lf;lf
  252. !
  253. exportPackage: aPackage on: aStream
  254. self
  255. exportPackagePrologueOf: aPackage on: aStream;
  256. exportPackageDefinitionOf: aPackage on: aStream;
  257. exportPackageTransportOf: aPackage on: aStream.
  258. aPackage sortedClasses do: [ :each |
  259. self exportDefinitionOf: each on: aStream.
  260. each ownMethods do: [ :method |
  261. self exportMethod: method on: aStream ].
  262. self exportMetaDefinitionOf: each on: aStream.
  263. each class ownMethods do: [ :method |
  264. self exportMethod: method on: aStream ] ].
  265. (self extensionMethodsOfPackage: aPackage) do: [ :each |
  266. self exportMethod: each on: aStream ].
  267. self exportPackageEpilogueOf: aPackage on: aStream
  268. !
  269. exportPackageDefinitionOf: aPackage on: aStream
  270. aStream
  271. nextPutAll: 'smalltalk.addPackage(';
  272. nextPutAll: '''', aPackage name, ''');';
  273. lf
  274. !
  275. exportPackageEpilogueOf: aPackage on: aStream
  276. aStream
  277. nextPutAll: '})(global_smalltalk,global_nil,global__st);';
  278. lf
  279. !
  280. exportPackagePrologueOf: aPackage on: aStream
  281. aStream
  282. nextPutAll: '(function(smalltalk,nil,_st){';
  283. lf
  284. !
  285. exportPackageTransportOf: aPackage on: aStream
  286. | json |
  287. json := aPackage transportJson.
  288. json = 'null' ifFalse: [
  289. aStream
  290. nextPutAll: 'smalltalk.packages[';
  291. nextPutAll: aPackage name asJavascript;
  292. nextPutAll: '].transport = ';
  293. nextPutAll: json;
  294. nextPutAll: ';';
  295. lf ]
  296. ! !
  297. Exporter subclass: #AmdExporter
  298. instanceVariableNames: 'namespace'
  299. package: 'Importer-Exporter'!
  300. !AmdExporter commentStamp!
  301. I am used to export Packages in an AMD (Asynchronous Module Definition) JavaScript format.!
  302. !AmdExporter methodsFor: 'output'!
  303. exportPackageEpilogueOf: aPackage on: aStream
  304. aStream
  305. nextPutAll: '});';
  306. lf
  307. !
  308. exportPackagePrologueOf: aPackage on: aStream
  309. aStream
  310. nextPutAll: 'define("';
  311. nextPutAll: (self amdNamespaceOfPackage: aPackage);
  312. nextPutAll: '/';
  313. nextPutAll: aPackage name;
  314. nextPutAll: '", ';
  315. nextPutAll: (#('amber_vm/smalltalk' 'amber_vm/nil' 'amber_vm/_st'), (self amdNamesOfPackages: aPackage loadDependencies)) asJavascript;
  316. nextPutAll: ', function(smalltalk,nil,_st){';
  317. lf
  318. ! !
  319. !AmdExporter methodsFor: 'private'!
  320. amdNamesOfPackages: anArray
  321. ^ (anArray
  322. select: [ :each | (self amdNamespaceOfPackage: each) notNil ])
  323. collect: [ :each | (self amdNamespaceOfPackage: each), '/', each name ]
  324. !
  325. amdNamespaceOfPackage: aPackage
  326. ^ (aPackage transport type = 'amd')
  327. ifTrue: [ aPackage transport namespace ]
  328. ifFalse: [ nil ]
  329. ! !
  330. Object subclass: #ChunkParser
  331. instanceVariableNames: 'stream'
  332. package: 'Importer-Exporter'!
  333. !ChunkParser commentStamp!
  334. I am responsible for parsing aStream contents in the chunk format.
  335. ## API
  336. ChunkParser new
  337. stream: aStream;
  338. nextChunk!
  339. !ChunkParser methodsFor: 'accessing'!
  340. stream: aStream
  341. stream := aStream
  342. ! !
  343. !ChunkParser methodsFor: 'reading'!
  344. nextChunk
  345. "The chunk format (Smalltalk Interchange Format or Fileout format)
  346. is a trivial format but can be a bit tricky to understand:
  347. - Uses the exclamation mark as delimiter of chunks.
  348. - Inside a chunk a normal exclamation mark must be doubled.
  349. - A non empty chunk must be a valid Smalltalk expression.
  350. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  351. - The object created by the expression then takes over reading chunks.
  352. This method returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  353. | char result chunk |
  354. result := '' writeStream.
  355. [char := stream next.
  356. char notNil] whileTrue: [
  357. char = '!!' ifTrue: [
  358. stream peek = '!!'
  359. ifTrue: [stream next "skipping the escape double"]
  360. ifFalse: [^result contents trimBoth "chunk end marker found"]].
  361. result nextPut: char].
  362. ^nil "a chunk needs to end with !!"
  363. ! !
  364. !ChunkParser class methodsFor: 'instance creation'!
  365. on: aStream
  366. ^self new stream: aStream
  367. ! !
  368. Object subclass: #ExportMethodProtocol
  369. instanceVariableNames: 'name theClass'
  370. package: 'Importer-Exporter'!
  371. !ExportMethodProtocol commentStamp!
  372. I am an abstraction for a method protocol in a class / metaclass.
  373. I know of my class, name and methods.
  374. I am used when exporting a package.!
  375. !ExportMethodProtocol methodsFor: 'accessing'!
  376. methods
  377. ^ self theClass methodsInProtocol: self name
  378. !
  379. name
  380. ^name
  381. !
  382. name: aString
  383. name := aString
  384. !
  385. sortedMethods
  386. ^ self methods sorted: [ :a :b | a selector <= b selector ]
  387. !
  388. theClass
  389. ^theClass
  390. !
  391. theClass: aClass
  392. theClass := aClass
  393. ! !
  394. !ExportMethodProtocol class methodsFor: 'instance creation'!
  395. name: aString theClass: aClass
  396. ^self new
  397. name: aString;
  398. theClass: aClass;
  399. yourself
  400. ! !
  401. Object subclass: #Importer
  402. instanceVariableNames: ''
  403. package: 'Importer-Exporter'!
  404. !Importer commentStamp!
  405. I can import Amber code from a string in the chunk format.
  406. ## API
  407. Importer new import: aString!
  408. !Importer methodsFor: 'fileIn'!
  409. import: aStream
  410. | chunk result parser lastEmpty |
  411. parser := ChunkParser on: aStream.
  412. lastEmpty := false.
  413. [chunk := parser nextChunk.
  414. chunk isNil] whileFalse: [
  415. chunk isEmpty
  416. ifTrue: [lastEmpty := true]
  417. ifFalse: [
  418. result := Compiler new evaluateExpression: chunk.
  419. lastEmpty
  420. ifTrue: [
  421. lastEmpty := false.
  422. result scanFrom: parser]]]
  423. ! !
  424. Object subclass: #MethodCategory
  425. instanceVariableNames: 'methods name theClass'
  426. package: 'Importer-Exporter'!
  427. !MethodCategory commentStamp!
  428. I am an abstraction for a method category in a class / metaclass.
  429. I know of my class, name and methods.
  430. I am used when exporting a package.!
  431. !MethodCategory methodsFor: 'accessing'!
  432. methods
  433. ^methods
  434. !
  435. methods: aCollection
  436. methods := aCollection
  437. !
  438. name
  439. ^name
  440. !
  441. name: aString
  442. name := aString
  443. !
  444. theClass
  445. ^theClass
  446. !
  447. theClass: aClass
  448. theClass := aClass
  449. ! !
  450. !MethodCategory class methodsFor: 'not yet classified'!
  451. name: aString theClass: aClass methods: anArray
  452. ^self new
  453. name: aString;
  454. theClass: aClass;
  455. methods: anArray;
  456. yourself
  457. ! !
  458. InterfacingObject subclass: #PackageHandler
  459. instanceVariableNames: ''
  460. package: 'Importer-Exporter'!
  461. !PackageHandler commentStamp!
  462. I am responsible for handling package loading and committing.
  463. I should not be used directly. Instead, use the corresponding `Package` methods.!
  464. !PackageHandler methodsFor: 'accessing'!
  465. chunkContentsFor: aPackage
  466. ^ String streamContents: [ :str |
  467. self chunkExporter exportPackage: aPackage on: str ]
  468. !
  469. chunkExporterClass
  470. ^ ChunkExporter
  471. !
  472. commitPathJsFor: aPackage
  473. self subclassResponsibility
  474. !
  475. commitPathStFor: aPackage
  476. self subclassResponsibility
  477. !
  478. contentsFor: aPackage
  479. ^ String streamContents: [ :str |
  480. self exporter exportPackage: aPackage on: str ]
  481. !
  482. exporterClass
  483. ^ Exporter
  484. ! !
  485. !PackageHandler methodsFor: 'committing'!
  486. commit: aPackage
  487. {
  488. [ self commitStFileFor: aPackage ].
  489. [ self commitJsFileFor: aPackage ]
  490. }
  491. do: [ :each | each value ]
  492. displayingProgress: 'Committing package ', aPackage name
  493. !
  494. commitJsFileFor: aPackage
  495. self
  496. ajaxPutAt: (self commitPathJsFor: aPackage), '/', aPackage name, '.js'
  497. data: (self contentsFor: aPackage)
  498. !
  499. commitStFileFor: aPackage
  500. self
  501. ajaxPutAt: (self commitPathStFor: aPackage), '/', aPackage name, '.st'
  502. data: (self chunkContentsFor: aPackage)
  503. ! !
  504. !PackageHandler methodsFor: 'factory'!
  505. chunkExporter
  506. ^ self chunkExporterClass new
  507. !
  508. exporter
  509. ^ self exporterClass new
  510. ! !
  511. !PackageHandler methodsFor: 'private'!
  512. ajaxPutAt: aURL data: aString
  513. self
  514. ajax: #{
  515. 'url' -> aURL.
  516. 'type' -> 'PUT'.
  517. 'data' -> aString.
  518. 'contentType' -> 'text/plain;charset=UTF-8'.
  519. 'error' -> [ :xhr | self error: 'Commiting ' , aURL , ' failed with reason: "' , (xhr responseText) , '"'] }
  520. ! !
  521. PackageHandler subclass: #AmdPackageHandler
  522. instanceVariableNames: ''
  523. package: 'Importer-Exporter'!
  524. !AmdPackageHandler commentStamp!
  525. I am responsible for handling package loading and committing.
  526. I should not be used directly. Instead, use the corresponding `Package` methods.!
  527. !AmdPackageHandler methodsFor: 'accessing'!
  528. commitPathJsFor: aPackage
  529. ^self toUrl: (self namespaceFor: aPackage)
  530. !
  531. commitPathStFor: aPackage
  532. "if _source is not mapped, .st commit will likely fail"
  533. ^self toUrl: (self namespaceFor: aPackage), '/_source'.
  534. !
  535. exporterClass
  536. ^ AmdExporter
  537. ! !
  538. !AmdPackageHandler methodsFor: 'committing'!
  539. namespaceFor: aPackage
  540. ^ aPackage transport namespace
  541. ! !
  542. !AmdPackageHandler methodsFor: 'private'!
  543. toUrl: aString
  544. ^ Smalltalk current amdRequire
  545. ifNil: [ self error: 'AMD loader not present' ]
  546. ifNotNil: [ :require | (require basicAt: 'toUrl') value: aString ]
  547. ! !
  548. !AmdPackageHandler class methodsFor: 'commit paths'!
  549. defaultNamespace
  550. ^ Smalltalk current defaultAmdNamespace
  551. !
  552. defaultNamespace: aString
  553. Smalltalk current defaultAmdNamespace: aString
  554. ! !
  555. Object subclass: #PackageTransport
  556. instanceVariableNames: 'package'
  557. package: 'Importer-Exporter'!
  558. !PackageTransport commentStamp!
  559. I represent the transport mechanism used to commit a package.
  560. My concrete subclasses have a `#handler` to which committing is delegated.!
  561. !PackageTransport methodsFor: 'accessing'!
  562. commitHandlerClass
  563. self subclassResponsibility
  564. !
  565. package
  566. ^ package
  567. !
  568. package: aPackage
  569. package := aPackage
  570. !
  571. type
  572. ^ self class type
  573. ! !
  574. !PackageTransport methodsFor: 'committing'!
  575. commit
  576. self commitHandler commit: self package
  577. ! !
  578. !PackageTransport methodsFor: 'factory'!
  579. commitHandler
  580. ^ self commitHandlerClass new
  581. ! !
  582. !PackageTransport methodsFor: 'initialization'!
  583. setupFromJson: anObject
  584. "no op. override if needed in subclasses"
  585. ! !
  586. PackageTransport class instanceVariableNames: 'registry'!
  587. !PackageTransport class methodsFor: 'accessing'!
  588. classRegisteredFor: aString
  589. ^ registry at: aString
  590. !
  591. for: aString
  592. ^ (self classRegisteredFor: aString) new
  593. !
  594. type
  595. "Override in subclasses"
  596. ^ nil
  597. ! !
  598. !PackageTransport class methodsFor: 'initialization'!
  599. initialize
  600. super initialize.
  601. registry := #{}.
  602. self register
  603. ! !
  604. !PackageTransport class methodsFor: 'registration'!
  605. register
  606. PackageTransport register: self
  607. !
  608. register: aClass
  609. aClass type ifNotNil: [
  610. registry at: aClass type put: aClass ]
  611. ! !
  612. PackageTransport subclass: #AmdPackageTransport
  613. instanceVariableNames: 'namespace'
  614. package: 'Importer-Exporter'!
  615. !AmdPackageTransport commentStamp!
  616. I am the default transport for committing packages.
  617. See `AmdExporter` and `AmdPackageHandler`.!
  618. !AmdPackageTransport methodsFor: 'accessing'!
  619. commitHandlerClass
  620. ^ AmdPackageHandler
  621. !
  622. namespace
  623. ^ namespace ifNil: [ self defaultNamespace ]
  624. !
  625. namespace: aString
  626. namespace := aString
  627. ! !
  628. !AmdPackageTransport methodsFor: 'defaults'!
  629. defaultNamespace
  630. ^ Smalltalk current defaultAmdNamespace
  631. ! !
  632. !AmdPackageTransport methodsFor: 'initialization'!
  633. setupFromJson: anObject
  634. self namespace: (anObject at: 'amdNamespace')
  635. ! !
  636. !AmdPackageTransport class methodsFor: 'accessing'!
  637. type
  638. ^ 'amd'
  639. ! !
  640. !AmdPackageTransport class methodsFor: 'instance creation'!
  641. namespace: aString
  642. ^ self new
  643. namespace: aString;
  644. yourself
  645. ! !
  646. !Package methodsFor: '*Importer-Exporter'!
  647. commit
  648. ^ self transport commit
  649. !
  650. transportJson
  651. <return JSON.stringify(self.transport || null);>
  652. !
  653. transportType
  654. <return (self.transport && self.transport.type) || 'unknown';>
  655. !
  656. transport
  657. ^ (PackageTransport for: self transportType)
  658. setupFromJson: self basicTransport;
  659. package: self;
  660. yourself
  661. !
  662. basicTransport
  663. <return self.transport>
  664. ! !