Kernel-ImportExport.st 22 KB

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