Kernel-ImportExport.st 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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: 'smalltalk.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: (self jsClassNameFor: aClass);
  215. nextPutAll: '.comment=';
  216. nextPutAll: aClass comment asJavascript;
  217. nextPutAll: ';' ].
  218. aStream lf
  219. !
  220. exportMetaDefinitionOf: aClass on: aStream
  221. aStream lf.
  222. aClass class instanceVariableNames isEmpty ifFalse: [
  223. aStream
  224. nextPutAll: (self jsClassNameFor: aClass class);
  225. nextPutAll: '.iVarNames = ['.
  226. aClass class instanceVariableNames
  227. do: [ :each | aStream nextPutAll: '''', each, '''' ]
  228. separatedBy: [ aStream nextPutAll: ',' ].
  229. aStream nextPutAll: '];', String lf ]
  230. !
  231. exportMethod: aMethod on: aStream
  232. aStream
  233. nextPutAll: 'smalltalk.addMethod(';lf;
  234. "nextPutAll: aMethod selector asSelector asJavascript, ',';lf;"
  235. nextPutAll: 'smalltalk.method({';lf;
  236. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  237. nextPutAll: 'protocol: ''', aMethod protocol, ''',';lf;
  238. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  239. nextPutAll: 'args: ', aMethod arguments asJavascript, ','; lf;
  240. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf;
  241. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;
  242. nextPutAll: 'referencedClasses: ', aMethod referencedClasses asJavascript.
  243. aStream
  244. lf;
  245. nextPutAll: '}),';lf;
  246. nextPutAll: (self jsClassNameFor: aMethod methodClass);
  247. nextPutAll: ');';lf;lf
  248. !
  249. exportPackage: aPackage on: aStream
  250. self
  251. exportPackagePrologueOf: aPackage on: aStream;
  252. exportPackageDefinitionOf: aPackage on: aStream;
  253. exportPackageTransportOf: aPackage on: aStream.
  254. aPackage sortedClasses do: [ :each |
  255. self exportDefinitionOf: each on: aStream.
  256. each ownMethods do: [ :method |
  257. self exportMethod: method on: aStream ].
  258. self exportMetaDefinitionOf: each on: aStream.
  259. each class ownMethods do: [ :method |
  260. self exportMethod: method on: aStream ] ].
  261. (self extensionMethodsOfPackage: aPackage) do: [ :each |
  262. self exportMethod: each on: aStream ].
  263. self exportPackageEpilogueOf: aPackage on: aStream
  264. !
  265. exportPackageDefinitionOf: aPackage on: aStream
  266. aStream
  267. nextPutAll: 'smalltalk.addPackage(';
  268. nextPutAll: '''', aPackage name, ''');';
  269. lf
  270. !
  271. exportPackageEpilogueOf: aPackage on: aStream
  272. aStream
  273. nextPutAll: '})(global_smalltalk,global_nil,global__st);';
  274. lf
  275. !
  276. exportPackagePrologueOf: aPackage on: aStream
  277. aStream
  278. nextPutAll: '(function(smalltalk,nil,_st){';
  279. lf
  280. !
  281. exportPackageTransportOf: aPackage on: aStream
  282. aStream
  283. nextPutAll: 'smalltalk.packages[';
  284. nextPutAll: aPackage name asJavascript;
  285. nextPutAll: '].transport = ';
  286. nextPutAll: aPackage transport asJSONString;
  287. nextPutAll: ';';
  288. lf
  289. ! !
  290. Exporter subclass: #AmdExporter
  291. instanceVariableNames: 'namespace'
  292. package: 'Kernel-ImportExport'!
  293. !AmdExporter commentStamp!
  294. I am used to export Packages in an AMD (Asynchronous Module Definition) JavaScript format.!
  295. !AmdExporter methodsFor: 'output'!
  296. exportPackageEpilogueOf: aPackage on: aStream
  297. aStream
  298. nextPutAll: '});';
  299. lf
  300. !
  301. exportPackagePrologueOf: aPackage on: aStream
  302. aStream
  303. nextPutAll: 'define("';
  304. nextPutAll: (self amdNamespaceOfPackage: aPackage);
  305. nextPutAll: '/';
  306. nextPutAll: aPackage name;
  307. nextPutAll: '", ';
  308. nextPutAll: (#('amber/boot'), (self amdNamesOfPackages: aPackage loadDependencies)) asJavascript;
  309. nextPutAll: ', function($boot){';
  310. lf;
  311. nextPutAll: 'var smalltalk=$boot.vm,nil=$boot.nil,_st=$boot.asReceiver,globals=$boot.globals;';
  312. lf
  313. ! !
  314. !AmdExporter methodsFor: 'private'!
  315. amdNamesOfPackages: anArray
  316. ^ (anArray
  317. select: [ :each | (self amdNamespaceOfPackage: each) notNil ])
  318. collect: [ :each | (self amdNamespaceOfPackage: each), '/', each name ]
  319. !
  320. amdNamespaceOfPackage: aPackage
  321. ^ (aPackage transport type = 'amd')
  322. ifTrue: [ aPackage transport namespace ]
  323. ifFalse: [ nil ]
  324. ! !
  325. Object subclass: #ChunkParser
  326. instanceVariableNames: 'stream last'
  327. package: 'Kernel-ImportExport'!
  328. !ChunkParser commentStamp!
  329. I am responsible for parsing aStream contents in the chunk format.
  330. ## API
  331. ChunkParser new
  332. stream: aStream;
  333. nextChunk!
  334. !ChunkParser methodsFor: 'accessing'!
  335. last
  336. ^ last
  337. !
  338. stream: aStream
  339. stream := aStream
  340. ! !
  341. !ChunkParser methodsFor: 'reading'!
  342. nextChunk
  343. "The chunk format (Smalltalk Interchange Format or Fileout format)
  344. is a trivial format but can be a bit tricky to understand:
  345. - Uses the exclamation mark as delimiter of chunks.
  346. - Inside a chunk a normal exclamation mark must be doubled.
  347. - A non empty chunk must be a valid Smalltalk expression.
  348. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  349. - The object created by the expression then takes over reading chunks.
  350. This method returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  351. | char result chunk |
  352. result := '' writeStream.
  353. [ char := stream next.
  354. char notNil ] whileTrue: [
  355. char = '!!' ifTrue: [
  356. stream peek = '!!'
  357. ifTrue: [ stream next "skipping the escape double" ]
  358. ifFalse: [ ^ last := result contents trimBoth "chunk end marker found" ]].
  359. result nextPut: char ].
  360. ^ last := nil "a chunk needs to end with !!"
  361. ! !
  362. !ChunkParser class methodsFor: 'instance creation'!
  363. on: aStream
  364. ^ self new stream: aStream
  365. ! !
  366. Object subclass: #ExportMethodProtocol
  367. instanceVariableNames: 'name theClass'
  368. package: 'Kernel-ImportExport'!
  369. !ExportMethodProtocol commentStamp!
  370. I am an abstraction for a method protocol in a class / metaclass.
  371. I know of my class, name and methods.
  372. I am used when exporting a package.!
  373. !ExportMethodProtocol methodsFor: 'accessing'!
  374. methods
  375. ^ (self theClass methodsInProtocol: self name)
  376. sorted: [ :a :b | a selector <= b selector ]
  377. !
  378. name
  379. ^ name
  380. !
  381. name: aString
  382. name := aString
  383. !
  384. theClass
  385. ^ theClass
  386. !
  387. theClass: aClass
  388. theClass := aClass
  389. ! !
  390. !ExportMethodProtocol class methodsFor: 'instance creation'!
  391. name: aString theClass: aClass
  392. ^ self new
  393. name: aString;
  394. theClass: aClass;
  395. yourself
  396. ! !
  397. Object subclass: #Importer
  398. instanceVariableNames: 'lastSection lastChunk'
  399. package: 'Kernel-ImportExport'!
  400. !Importer commentStamp!
  401. I can import Amber code from a string in the chunk format.
  402. ## API
  403. Importer new import: aString!
  404. !Importer methodsFor: 'accessing'!
  405. lastChunk
  406. ^ lastChunk
  407. !
  408. lastSection
  409. ^ lastSection
  410. ! !
  411. !Importer methodsFor: 'fileIn'!
  412. import: aStream
  413. | chunk result parser lastEmpty |
  414. parser := ChunkParser on: aStream.
  415. lastEmpty := false.
  416. lastSection := 'n/a, not started'.
  417. lastChunk := nil.
  418. [
  419. [ chunk := parser nextChunk.
  420. chunk isNil ] whileFalse: [
  421. chunk isEmpty
  422. ifTrue: [ lastEmpty := true ]
  423. ifFalse: [
  424. lastSection := chunk.
  425. result := Compiler new evaluateExpression: chunk.
  426. lastEmpty
  427. ifTrue: [
  428. lastEmpty := false.
  429. result scanFrom: parser ]] ].
  430. lastSection := 'n/a, finished'
  431. ] on: Error do: [:e | lastChunk := parser last. e signal ].
  432. ! !
  433. InterfacingObject subclass: #PackageHandler
  434. instanceVariableNames: ''
  435. package: 'Kernel-ImportExport'!
  436. !PackageHandler commentStamp!
  437. I am responsible for handling package loading and committing.
  438. I should not be used directly. Instead, use the corresponding `Package` methods.!
  439. !PackageHandler methodsFor: 'accessing'!
  440. chunkContentsFor: aPackage
  441. ^ String streamContents: [ :str |
  442. self chunkExporter exportPackage: aPackage on: str ]
  443. !
  444. chunkExporterClass
  445. ^ ChunkExporter
  446. !
  447. commitPathJsFor: aPackage
  448. self subclassResponsibility
  449. !
  450. commitPathStFor: aPackage
  451. self subclassResponsibility
  452. !
  453. contentsFor: aPackage
  454. ^ String streamContents: [ :str |
  455. self exporter exportPackage: aPackage on: str ]
  456. !
  457. exporterClass
  458. ^ Exporter
  459. ! !
  460. !PackageHandler methodsFor: 'committing'!
  461. commit: aPackage
  462. self
  463. commit: aPackage
  464. onSuccess: []
  465. onError: [ :error |
  466. PackageCommitError new
  467. messageText: 'Commiting failed with reason: "' , (error responseText) , '"';
  468. signal ]
  469. !
  470. commit: aPackage onSuccess: aBlock onError: anotherBlock
  471. self
  472. commitJsFileFor: aPackage
  473. onSuccess: [
  474. self
  475. commitStFileFor: aPackage
  476. onSuccess: [ aPackage beClean. aBlock value ]
  477. onError: anotherBlock ]
  478. onError: anotherBlock
  479. !
  480. commitJsFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  481. self
  482. ajaxPutAt: (self commitPathJsFor: aPackage), '/', aPackage name, '.js'
  483. data: (self contentsFor: aPackage)
  484. onSuccess: aBlock
  485. onError: anotherBlock
  486. !
  487. commitStFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  488. self
  489. ajaxPutAt: (self commitPathStFor: aPackage), '/', aPackage name, '.st'
  490. data: (self chunkContentsFor: aPackage)
  491. onSuccess: aBlock
  492. onError: anotherBlock
  493. ! !
  494. !PackageHandler methodsFor: 'error handling'!
  495. onCommitError: anError
  496. PackageCommitError new
  497. messageText: 'Commiting failed with reason: "' , (anError responseText) , '"';
  498. signal
  499. ! !
  500. !PackageHandler methodsFor: 'factory'!
  501. chunkExporter
  502. ^ self chunkExporterClass new
  503. !
  504. exporter
  505. ^ self exporterClass new
  506. ! !
  507. !PackageHandler methodsFor: 'loading'!
  508. load: aPackage
  509. self subclassResponsibility
  510. ! !
  511. !PackageHandler methodsFor: 'private'!
  512. ajaxPutAt: aURL data: aString onSuccess: aBlock onError: anotherBlock
  513. self
  514. ajax: #{
  515. 'url' -> aURL.
  516. 'type' -> 'PUT'.
  517. 'data' -> aString.
  518. 'contentType' -> 'text/plain;charset=UTF-8'.
  519. 'success' -> aBlock.
  520. 'error' -> anotherBlock
  521. }
  522. ! !
  523. PackageHandler subclass: #AmdPackageHandler
  524. instanceVariableNames: ''
  525. package: 'Kernel-ImportExport'!
  526. !AmdPackageHandler commentStamp!
  527. I am responsible for handling package loading and committing.
  528. I should not be used directly. Instead, use the corresponding `Package` methods.!
  529. !AmdPackageHandler methodsFor: 'accessing'!
  530. commitPathJsFor: aPackage
  531. ^ self toUrl: (self namespaceFor: aPackage)
  532. !
  533. commitPathStFor: aPackage
  534. "If _source is not mapped, .st will be committed to .js path.
  535. It is recommended not to use _source as it can be deprecated."
  536. | path pathWithout |
  537. path := self toUrl: (self namespaceFor: aPackage), '/_source'.
  538. pathWithout := self commitPathJsFor: aPackage.
  539. ^ path = (pathWithout, '/_source') ifTrue: [ pathWithout ] ifFalse: [ path ]
  540. !
  541. exporterClass
  542. ^ AmdExporter
  543. ! !
  544. !AmdPackageHandler methodsFor: 'committing'!
  545. namespaceFor: aPackage
  546. ^ aPackage transport namespace
  547. ! !
  548. !AmdPackageHandler methodsFor: 'loading'!
  549. load: aPackage
  550. Smalltalk amdRequire
  551. ifNil: [ self error: 'AMD loader not present' ]
  552. ifNotNil: [ :require |
  553. require value: (Array new: (self namespaceFor: aPackage), '/', aPackage name ) ]
  554. ! !
  555. !AmdPackageHandler methodsFor: 'private'!
  556. toUrl: aString
  557. ^ Smalltalk amdRequire
  558. ifNil: [ self error: 'AMD loader not present' ]
  559. ifNotNil: [ :require | (require basicAt: 'toUrl') value: aString ]
  560. ! !
  561. !AmdPackageHandler class methodsFor: 'commit paths'!
  562. defaultNamespace
  563. ^ Smalltalk defaultAmdNamespace
  564. !
  565. defaultNamespace: aString
  566. Smalltalk defaultAmdNamespace: aString
  567. ! !
  568. Object subclass: #PackageTransport
  569. instanceVariableNames: 'package'
  570. package: 'Kernel-ImportExport'!
  571. !PackageTransport commentStamp!
  572. I represent the transport mechanism used to commit a package.
  573. My concrete subclasses have a `#handler` to which committing is delegated.!
  574. !PackageTransport methodsFor: 'accessing'!
  575. commitHandlerClass
  576. self subclassResponsibility
  577. !
  578. definition
  579. ^ ''
  580. !
  581. package
  582. ^ package
  583. !
  584. package: aPackage
  585. package := aPackage
  586. !
  587. type
  588. ^ self class type
  589. ! !
  590. !PackageTransport methodsFor: 'committing'!
  591. commit
  592. self commitHandler commit: self package
  593. !
  594. commitOnSuccess: aBlock onError: anotherBlock
  595. self commitHandler
  596. commit: self package
  597. onSuccess: aBlock
  598. onError: anotherBlock
  599. ! !
  600. !PackageTransport methodsFor: 'converting'!
  601. asJSON
  602. ^ #{ 'type' -> self type }
  603. ! !
  604. !PackageTransport methodsFor: 'factory'!
  605. commitHandler
  606. ^ self commitHandlerClass new
  607. ! !
  608. !PackageTransport methodsFor: 'initialization'!
  609. setupFromJson: anObject
  610. "no op. override if needed in subclasses"
  611. ! !
  612. !PackageTransport methodsFor: 'loading'!
  613. load
  614. self commitHandler load: self package
  615. ! !
  616. PackageTransport class instanceVariableNames: 'registry'!
  617. !PackageTransport class methodsFor: 'accessing'!
  618. classRegisteredFor: aString
  619. ^ registry at: aString
  620. !
  621. defaultType
  622. ^ AmdPackageTransport type
  623. !
  624. type
  625. "Override in subclasses"
  626. ^ nil
  627. ! !
  628. !PackageTransport class methodsFor: 'initialization'!
  629. initialize
  630. super initialize.
  631. registry := #{}.
  632. self register
  633. ! !
  634. !PackageTransport class methodsFor: 'instance creation'!
  635. for: aString
  636. ^ (self classRegisteredFor: aString) new
  637. !
  638. fromJson: anObject
  639. anObject ifNil: [ ^ self for: self defaultType ].
  640. ^ (self for: anObject type)
  641. setupFromJson: anObject;
  642. yourself
  643. ! !
  644. !PackageTransport class methodsFor: 'registration'!
  645. register
  646. PackageTransport register: self
  647. !
  648. register: aClass
  649. aClass type ifNotNil: [
  650. registry at: aClass type put: aClass ]
  651. ! !
  652. PackageTransport subclass: #AmdPackageTransport
  653. instanceVariableNames: 'namespace'
  654. package: 'Kernel-ImportExport'!
  655. !AmdPackageTransport commentStamp!
  656. I am the default transport for committing packages.
  657. See `AmdExporter` and `AmdPackageHandler`.!
  658. !AmdPackageTransport methodsFor: 'accessing'!
  659. commitHandlerClass
  660. ^ AmdPackageHandler
  661. !
  662. definition
  663. ^ String streamContents: [ :stream |
  664. stream
  665. nextPutAll: self class name;
  666. nextPutAll: ' namespace: ';
  667. nextPutAll: '''', self namespace, '''' ]
  668. !
  669. namespace
  670. ^ namespace ifNil: [ self defaultNamespace ]
  671. !
  672. namespace: aString
  673. namespace := aString
  674. ! !
  675. !AmdPackageTransport methodsFor: 'actions'!
  676. setPath: aString
  677. "Set the path the the receiver's `namespace`"
  678. (require basicAt: 'config') value: #{
  679. 'paths' -> #{
  680. self namespace -> aString
  681. }
  682. }.
  683. ! !
  684. !AmdPackageTransport methodsFor: 'converting'!
  685. asJSON
  686. ^ super asJSON
  687. at: 'amdNamespace' put: self namespace;
  688. yourself
  689. ! !
  690. !AmdPackageTransport methodsFor: 'defaults'!
  691. defaultNamespace
  692. ^ Smalltalk defaultAmdNamespace
  693. ! !
  694. !AmdPackageTransport methodsFor: 'initialization'!
  695. setupFromJson: anObject
  696. self namespace: (anObject at: 'amdNamespace')
  697. ! !
  698. !AmdPackageTransport methodsFor: 'printing'!
  699. printOn: aStream
  700. super printOn: aStream.
  701. aStream
  702. nextPutAll: ' (AMD Namespace: ';
  703. nextPutAll: self namespace;
  704. nextPutAll: ')'
  705. ! !
  706. !AmdPackageTransport class methodsFor: 'accessing'!
  707. type
  708. ^ 'amd'
  709. ! !
  710. !AmdPackageTransport class methodsFor: 'instance creation'!
  711. namespace: aString
  712. ^ self new
  713. namespace: aString;
  714. yourself
  715. ! !
  716. !Package methodsFor: '*Kernel-ImportExport'!
  717. commit
  718. ^ self transport commit
  719. !
  720. load
  721. ^ self transport load
  722. !
  723. loadFromNamespace: aString
  724. ^ self transport
  725. namespace: aString;
  726. load
  727. ! !
  728. !Package class methodsFor: '*Kernel-ImportExport'!
  729. load: aPackageName
  730. (self named: aPackageName) load
  731. !
  732. load: aPackageName fromNamespace: aString
  733. (self named: aPackageName) loadFromNamespace: aString
  734. ! !