2
0

Importer-Exporter.st 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. !AbstractExporter methodsFor: 'convenience'!
  8. chunkEscape: aString
  9. "Replace all occurrences of !! with !!!! and trim at both ends."
  10. ^(aString replace: '!!' with: '!!!!') trimBoth
  11. !
  12. classNameFor: aClass
  13. ^aClass isMetaclass
  14. ifTrue: [ aClass instanceClass name, ' class' ]
  15. ifFalse: [
  16. aClass isNil
  17. ifTrue: [ 'nil' ]
  18. ifFalse: [ aClass name ] ]
  19. ! !
  20. !AbstractExporter methodsFor: 'fileOut'!
  21. recipe
  22. "Recipe to export a given package."
  23. self subclassResponsibility
  24. ! !
  25. AbstractExporter class instanceVariableNames: 'default'!
  26. !AbstractExporter class methodsFor: 'instance creation'!
  27. default
  28. ^ default ifNil: [ default := self new ]
  29. ! !
  30. AbstractExporter subclass: #ChunkExporter
  31. instanceVariableNames: ''
  32. package: 'Importer-Exporter'!
  33. !ChunkExporter commentStamp!
  34. I am an exporter dedicated to outputting Amber source code in the classic Smalltalk chunk format.
  35. I do not output any compiled code.!
  36. !ChunkExporter methodsFor: 'accessing'!
  37. extensionCategoriesOfPackage: aPackage
  38. "Issue #143: sort protocol alphabetically"
  39. | name map result |
  40. name := aPackage name.
  41. result := OrderedCollection new.
  42. (Package sortedClasses: Smalltalk current classes) do: [:each |
  43. {each. each class} do: [:aClass |
  44. map := Dictionary new.
  45. aClass protocolsDo: [:category :methods |
  46. category = ('*', name) ifTrue: [ map at: category put: methods ]].
  47. result addAll: ((map keys sorted: [:a :b | a <= b ]) collect: [:category |
  48. MethodCategory name: category theClass: aClass methods: (map at: category)]) ]].
  49. ^result
  50. !
  51. methodsOfCategory: aCategory
  52. "Issue #143: sort methods alphabetically"
  53. ^(aCategory methods) sorted: [:a :b | a selector <= b selector]
  54. !
  55. ownCategoriesOfClass: aClass
  56. "Answer the protocols of aClassthat are not package extensions"
  57. "Issue #143: sort protocol alphabetically"
  58. | map |
  59. map := Dictionary new.
  60. aClass protocolsDo: [:category :methods |
  61. (category match: '^\*') ifFalse: [ map at: category put: methods ]].
  62. ^(map keys sorted: [:a :b | a <= b ]) collect: [:category |
  63. MethodCategory name: category theClass: aClass methods: (map at: category) ]
  64. !
  65. ownCategoriesOfMetaClass: aClass
  66. "Issue #143: sort protocol alphabetically"
  67. ^self ownCategoriesOfClass: aClass class
  68. ! !
  69. !ChunkExporter methodsFor: 'fileOut'!
  70. recipe
  71. "Export a given package."
  72. | exportCategoryRecipe |
  73. exportCategoryRecipe := {
  74. self -> #exportCategoryPrologueOf:on:.
  75. {
  76. self -> #methodsOfCategory:.
  77. self -> #exportMethod:on: }.
  78. self -> #exportCategoryEpilogueOf:on: }.
  79. ^{
  80. self -> #exportPackageDefinitionOf:on:.
  81. {
  82. PluggableExporter -> #ownClassesOfPackage:.
  83. self -> #exportDefinitionOf:on:.
  84. { self -> #ownCategoriesOfClass: }, exportCategoryRecipe.
  85. self -> #exportMetaDefinitionOf:on:.
  86. { self -> #ownCategoriesOfMetaClass: }, exportCategoryRecipe }.
  87. { self -> #extensionCategoriesOfPackage: }, exportCategoryRecipe
  88. }
  89. ! !
  90. !ChunkExporter methodsFor: 'output'!
  91. exportCategoryEpilogueOf: aCategory on: aStream
  92. aStream nextPutAll: ' !!'; lf; lf
  93. !
  94. exportCategoryPrologueOf: aCategory on: aStream
  95. aStream
  96. nextPutAll: '!!', (self classNameFor: aCategory theClass);
  97. nextPutAll: ' methodsFor: ''', aCategory name, '''!!'
  98. !
  99. exportDefinitionOf: aClass on: aStream
  100. "Chunk format."
  101. aStream
  102. nextPutAll: (self classNameFor: aClass superclass);
  103. nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;
  104. tab; nextPutAll: 'instanceVariableNames: '''.
  105. aClass instanceVariableNames
  106. do: [:each | aStream nextPutAll: each]
  107. separatedBy: [aStream nextPutAll: ' '].
  108. aStream
  109. nextPutAll: ''''; lf;
  110. tab; nextPutAll: 'package: ''', aClass category, '''!!'; lf.
  111. aClass comment notEmpty ifTrue: [
  112. aStream
  113. nextPutAll: '!!', (self classNameFor: aClass), ' commentStamp!!';lf;
  114. nextPutAll: (self chunkEscape: aClass comment), '!!';lf].
  115. aStream lf
  116. !
  117. exportMetaDefinitionOf: aClass on: aStream
  118. aClass class instanceVariableNames isEmpty ifFalse: [
  119. aStream
  120. nextPutAll: (self classNameFor: aClass class);
  121. nextPutAll: ' instanceVariableNames: '''.
  122. aClass class instanceVariableNames
  123. do: [:each | aStream nextPutAll: each]
  124. separatedBy: [aStream nextPutAll: ' '].
  125. aStream
  126. nextPutAll: '''!!'; lf; lf]
  127. !
  128. exportMethod: aMethod on: aStream
  129. aStream
  130. lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;
  131. nextPutAll: '!!'
  132. !
  133. exportPackageDefinitionOf: aPackage on: aStream
  134. "Chunk format."
  135. aStream
  136. nextPutAll: 'Smalltalk current createPackage: ''', aPackage name, '''!!';
  137. lf
  138. ! !
  139. AbstractExporter subclass: #Exporter
  140. instanceVariableNames: ''
  141. package: 'Importer-Exporter'!
  142. !Exporter commentStamp!
  143. I am responsible for outputting Amber code into a JavaScript string.
  144. The generated output is enough to reconstruct the exported data, including Smalltalk source code and other metadata.
  145. ## Use case
  146. I am typically used to save code outside of the Amber runtime (committing to disk, etc.).
  147. ## API
  148. Use `#exportAll`, `#exportClass:` or `#exportPackage:` methods.!
  149. !Exporter methodsFor: 'accessing'!
  150. extensionMethodsOfPackage: aPackage
  151. "Issue #143: sort classes and methods alphabetically"
  152. | name result |
  153. name := aPackage name.
  154. result := OrderedCollection new.
  155. (Package sortedClasses: Smalltalk current classes) do: [:each |
  156. {each. each class} do: [:aClass |
  157. result addAll: (((aClass methodDictionary values)
  158. sorted: [:a :b | a selector <= b selector])
  159. select: [:method | method category = ('*', name)]) ]].
  160. ^result
  161. !
  162. ownMethodsOfClass: aClass
  163. "Issue #143: sort methods alphabetically"
  164. ^((aClass methodDictionary values) sorted: [:a :b | a selector <= b selector])
  165. reject: [:each | (each category match: '^\*')]
  166. !
  167. ownMethodsOfMetaClass: aClass
  168. "Issue #143: sort methods alphabetically"
  169. ^self ownMethodsOfClass: aClass class
  170. ! !
  171. !Exporter methodsFor: 'convenience'!
  172. classNameFor: aClass
  173. ^aClass isMetaclass
  174. ifTrue: [ aClass instanceClass name, '.klass' ]
  175. ifFalse: [
  176. aClass isNil
  177. ifTrue: [ 'nil' ]
  178. ifFalse: [ aClass name ] ]
  179. ! !
  180. !Exporter methodsFor: 'fileOut'!
  181. recipe
  182. "Export a given package."
  183. ^{
  184. self -> #exportPackagePrologueOf:on:.
  185. self -> #exportPackageDefinitionOf:on:.
  186. {
  187. PluggableExporter -> #ownClassesOfPackage:.
  188. self -> #exportDefinitionOf:on:.
  189. {
  190. self -> #ownMethodsOfClass:.
  191. self -> #exportMethod:on: }.
  192. self -> #exportMetaDefinitionOf:on:.
  193. {
  194. self -> #ownMethodsOfMetaClass:.
  195. self -> #exportMethod:on: } }.
  196. {
  197. self -> #extensionMethodsOfPackage:.
  198. self -> #exportMethod:on: }.
  199. self -> #exportPackageEpilogueOf:on:
  200. }
  201. ! !
  202. !Exporter methodsFor: 'output'!
  203. exportDefinitionOf: aClass on: aStream
  204. aStream
  205. lf;
  206. nextPutAll: 'smalltalk.addClass(';
  207. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  208. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  209. nextPutAll: ', ['.
  210. aClass instanceVariableNames
  211. do: [:each | aStream nextPutAll: '''', each, '''']
  212. separatedBy: [aStream nextPutAll: ', '].
  213. aStream
  214. nextPutAll: '], ''';
  215. nextPutAll: aClass category, '''';
  216. nextPutAll: ');'.
  217. aClass comment notEmpty ifTrue: [
  218. aStream
  219. lf;
  220. nextPutAll: 'smalltalk.';
  221. nextPutAll: (self classNameFor: aClass);
  222. nextPutAll: '.comment=';
  223. nextPutAll: aClass comment asJavascript;
  224. nextPutAll: ';'].
  225. aStream lf
  226. !
  227. exportMetaDefinitionOf: aClass on: aStream
  228. aStream lf.
  229. aClass class instanceVariableNames isEmpty ifFalse: [
  230. aStream
  231. nextPutAll: 'smalltalk.', (self classNameFor: aClass class);
  232. nextPutAll: '.iVarNames = ['.
  233. aClass class instanceVariableNames
  234. do: [:each | aStream nextPutAll: '''', each, '''']
  235. separatedBy: [aStream nextPutAll: ','].
  236. aStream nextPutAll: '];', String lf]
  237. !
  238. exportMethod: aMethod on: aStream
  239. aStream
  240. nextPutAll: 'smalltalk.addMethod(';lf;
  241. "nextPutAll: aMethod selector asSelector asJavascript, ',';lf;"
  242. nextPutAll: 'smalltalk.method({';lf;
  243. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  244. nextPutAll: 'category: ''', aMethod category, ''',';lf;
  245. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  246. nextPutAll: 'args: ', aMethod arguments asJavascript, ','; lf;
  247. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf;
  248. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;
  249. nextPutAll: 'referencedClasses: ', aMethod referencedClasses asJavascript.
  250. aStream
  251. lf;
  252. nextPutAll: '}),';lf;
  253. nextPutAll: 'smalltalk.', (self classNameFor: aMethod methodClass);
  254. nextPutAll: ');';lf;lf
  255. !
  256. exportPackageDefinitionOf: aPackage on: aStream
  257. aStream
  258. nextPutAll: 'smalltalk.addPackage(';
  259. nextPutAll: '''', aPackage name, ''');';
  260. lf
  261. !
  262. exportPackageEpilogueOf: aPackage on: aStream
  263. aStream
  264. nextPutAll: '})(global_smalltalk,global_nil,global__st);';
  265. lf
  266. !
  267. exportPackagePrologueOf: aPackage on: aStream
  268. aStream
  269. nextPutAll: '(function(smalltalk,nil,_st){';
  270. lf
  271. ! !
  272. Exporter subclass: #StrippedExporter
  273. instanceVariableNames: ''
  274. package: 'Importer-Exporter'!
  275. !StrippedExporter commentStamp!
  276. I export Amber code into a JavaScript string, but without any optional associated data like the Amber source code.!
  277. !StrippedExporter methodsFor: 'output'!
  278. exportDefinitionOf: aClass on: aStream
  279. aStream
  280. lf;
  281. nextPutAll: 'smalltalk.addClass(';
  282. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  283. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  284. nextPutAll: ', ['.
  285. aClass instanceVariableNames
  286. do: [:each | aStream nextPutAll: '''', each, '''']
  287. separatedBy: [aStream nextPutAll: ', '].
  288. aStream
  289. nextPutAll: '], ''';
  290. nextPutAll: aClass category, '''';
  291. nextPutAll: ');'.
  292. aStream lf
  293. !
  294. exportMethod: aMethod on: aStream
  295. aStream
  296. nextPutAll: 'smalltalk.addMethod(';lf;
  297. "nextPutAll: aMethod selector asSelector asJavascript, ',';lf;"
  298. nextPutAll: 'smalltalk.method({';lf;
  299. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  300. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  301. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript;
  302. nextPutAll: '}),';lf;
  303. nextPutAll: 'smalltalk.', (self classNameFor: aMethod methodClass);
  304. nextPutAll: ');';lf;lf
  305. ! !
  306. Object subclass: #ChunkParser
  307. instanceVariableNames: 'stream'
  308. package: 'Importer-Exporter'!
  309. !ChunkParser commentStamp!
  310. I am responsible for parsing aStream contents in the chunk format.
  311. ## API
  312. ChunkParser new
  313. stream: aStream;
  314. nextChunk!
  315. !ChunkParser methodsFor: 'accessing'!
  316. stream: aStream
  317. stream := aStream
  318. ! !
  319. !ChunkParser methodsFor: 'reading'!
  320. nextChunk
  321. "The chunk format (Smalltalk Interchange Format or Fileout format)
  322. is a trivial format but can be a bit tricky to understand:
  323. - Uses the exclamation mark as delimiter of chunks.
  324. - Inside a chunk a normal exclamation mark must be doubled.
  325. - A non empty chunk must be a valid Smalltalk expression.
  326. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  327. - The object created by the expression then takes over reading chunks.
  328. This metod returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  329. | char result chunk |
  330. result := '' writeStream.
  331. [char := stream next.
  332. char notNil] whileTrue: [
  333. char = '!!' ifTrue: [
  334. stream peek = '!!'
  335. ifTrue: [stream next "skipping the escape double"]
  336. ifFalse: [^result contents trimBoth "chunk end marker found"]].
  337. result nextPut: char].
  338. ^nil "a chunk needs to end with !!"
  339. ! !
  340. !ChunkParser class methodsFor: 'instance creation'!
  341. on: aStream
  342. ^self new stream: aStream
  343. ! !
  344. Object subclass: #ExportRecipeInterpreter
  345. instanceVariableNames: ''
  346. package: 'Importer-Exporter'!
  347. !ExportRecipeInterpreter commentStamp!
  348. I am an interpreter for export recipes.
  349. ## Recipe format
  350. Recipe is an array, which can contain two kinds of elements:
  351. - an assocation where the key is the receiver and the value is a two-arguments selector
  352. In this case, `receiver perform: selector withArguments: { data. stream }` is called.
  353. This essentially defines one step of export process.
  354. The key (eg. receiver) is presumed to be some kind of 'repository' of the exporting methods
  355. that just format appropriate aspect of data into a stream; like a class or a singleton,
  356. so that the recipe itself can be decoupled from data.
  357. - a subarray, where first element is special and the rest is recursive recipe.
  358. `subarray first` must be an association similar to one above,
  359. with key being the 'repository' receiver, but value is one-arg selector.
  360. In this case, `receiver perform: selector withArguments: { data }` should create a collection.
  361. Then, the sub-recipe (`subarray allButFirst`) is applied to every element of a collection, eg.
  362. collection do: [ :each | self export: each using: sa allButFirst on: stream ]!
  363. !ExportRecipeInterpreter methodsFor: 'interpreting'!
  364. interpret: aRecipe for: anObject on: aStream
  365. | recipeStream |
  366. recipeStream := aRecipe readStream.
  367. [ recipeStream atEnd ] whileFalse: [
  368. self
  369. interpretStep: recipeStream next
  370. for: anObject
  371. on: aStream ]
  372. !
  373. interpretStep: aRecipeStep for: anObject on: aStream
  374. aRecipeStep value == aRecipeStep ifTrue: [
  375. ^ self interpretSubRecipe: aRecipeStep for: anObject on: aStream ].
  376. aRecipeStep key perform: aRecipeStep value withArguments: { anObject. aStream }
  377. !
  378. interpretSubRecipe: aRecipe for: anObject on: aStream
  379. | selection |
  380. selection := aRecipe first key
  381. perform: aRecipe first value
  382. withArguments: { anObject }.
  383. selection do: [ :each |
  384. self interpret: aRecipe allButFirst for: each on: aStream ]
  385. ! !
  386. Object subclass: #Importer
  387. instanceVariableNames: ''
  388. package: 'Importer-Exporter'!
  389. !Importer commentStamp!
  390. I can import Amber code from a string in the chunk format.
  391. ## API
  392. Importer new import: aString!
  393. !Importer methodsFor: 'fileIn'!
  394. import: aStream
  395. | chunk result parser lastEmpty |
  396. parser := ChunkParser on: aStream.
  397. lastEmpty := false.
  398. [chunk := parser nextChunk.
  399. chunk isNil] whileFalse: [
  400. chunk isEmpty
  401. ifTrue: [lastEmpty := true]
  402. ifFalse: [
  403. result := Compiler new evaluateExpression: chunk.
  404. lastEmpty
  405. ifTrue: [
  406. lastEmpty := false.
  407. result scanFrom: parser]]]
  408. ! !
  409. Object subclass: #MethodCategory
  410. instanceVariableNames: 'methods name theClass'
  411. package: 'Importer-Exporter'!
  412. !MethodCategory commentStamp!
  413. I am an abstraction for a method category in a class / metaclass.
  414. I know of my class, name and methods.
  415. I am used when exporting a package.!
  416. !MethodCategory methodsFor: 'accessing'!
  417. methods
  418. ^methods
  419. !
  420. methods: aCollection
  421. methods := aCollection
  422. !
  423. name
  424. ^name
  425. !
  426. name: aString
  427. name := aString
  428. !
  429. theClass
  430. ^theClass
  431. !
  432. theClass: aClass
  433. theClass := aClass
  434. ! !
  435. !MethodCategory class methodsFor: 'not yet classified'!
  436. name: aString theClass: aClass methods: anArray
  437. ^self new
  438. name: aString;
  439. theClass: aClass;
  440. methods: anArray;
  441. yourself
  442. ! !
  443. InterfacingObject subclass: #PackageHandler
  444. instanceVariableNames: ''
  445. package: 'Importer-Exporter'!
  446. !PackageHandler commentStamp!
  447. I am responsible for handling package loading and committing.
  448. I should not be used directly. Instead, use the corresponding `Package` methods.!
  449. !PackageHandler methodsFor: 'committing'!
  450. commit: aPackage
  451. self commitChannels
  452. do: [ :commitStrategyFactory || fileContents commitStrategy |
  453. commitStrategy := commitStrategyFactory value: aPackage.
  454. fileContents := String streamContents: [ :stream |
  455. (PluggableExporter forRecipe: commitStrategy key) exportPackage: aPackage on: stream ].
  456. self ajaxPutAt: commitStrategy value data: fileContents ]
  457. displayingProgress: 'Committing package ', aPackage name
  458. !
  459. commitChannels
  460. self subclassResponsibility
  461. ! !
  462. !PackageHandler methodsFor: 'private'!
  463. ajaxPutAt: aURL data: aString
  464. self
  465. ajax: #{
  466. 'url' -> aURL.
  467. 'type' -> 'PUT'.
  468. 'data' -> aString.
  469. 'contentType' -> 'text/plain;charset=UTF-8'.
  470. 'error' -> [ :xhr | self error: 'Commiting ' , aURL , ' failed with reason: "' , (xhr responseText) , '"'] }
  471. ! !
  472. PackageHandler class instanceVariableNames: 'registry'!
  473. !PackageHandler class methodsFor: 'accessing'!
  474. classRegisteredFor: aString
  475. ^registry at: aString
  476. !
  477. for: aString
  478. ^(self classRegisteredFor: aString) new
  479. ! !
  480. !PackageHandler class methodsFor: 'initialization'!
  481. initialize
  482. super initialize.
  483. registry := #{}
  484. ! !
  485. !PackageHandler class methodsFor: 'registry'!
  486. register: aClass for: aString
  487. registry at: aString put: aClass
  488. !
  489. registerFor: aString
  490. PackageHandler register: self for: aString
  491. ! !
  492. PackageHandler subclass: #LegacyPackageHandler
  493. instanceVariableNames: ''
  494. package: 'Importer-Exporter'!
  495. !LegacyPackageHandler commentStamp!
  496. I am responsible for handling package loading and committing.
  497. I should not be used directly. Instead, use the corresponding `Package` methods.!
  498. !LegacyPackageHandler methodsFor: 'committing'!
  499. commitChannels
  500. ^{
  501. [ :pkg | Exporter default recipe -> (pkg commitPathJs, '/', pkg name, '.js') ].
  502. [ :pkg | StrippedExporter default recipe -> (pkg commitPathJs, '/', pkg name, '.deploy.js') ].
  503. [ :pkg | ChunkExporter default recipe -> (pkg commitPathSt, '/', pkg name, '.st') ]
  504. }
  505. !
  506. commitPathJsFor: aPackage
  507. ^self class defaultCommitPathJs
  508. !
  509. commitPathStFor: aPackage
  510. ^self class defaultCommitPathSt
  511. ! !
  512. !LegacyPackageHandler methodsFor: 'loading'!
  513. loadPackage: packageName prefix: aString
  514. | url |
  515. url := '/', aString, '/js/', packageName, '.js'.
  516. self
  517. ajax: #{
  518. 'url' -> url.
  519. 'type' -> 'GET'.
  520. 'dataType' -> 'script'.
  521. 'complete' -> [ :jqXHR :textStatus |
  522. jqXHR readyState = 4
  523. ifTrue: [ self setupPackageNamed: packageName prefix: aString ] ].
  524. 'error' -> [ self alert: 'Could not load package at: ', url ]
  525. }
  526. !
  527. loadPackages: aCollection prefix: aString
  528. aCollection do: [ :each |
  529. self loadPackage: each prefix: aString ]
  530. ! !
  531. !LegacyPackageHandler methodsFor: 'private'!
  532. setupPackageNamed: packageName prefix: aString
  533. (Package named: packageName)
  534. setupClasses;
  535. commitPathJs: '/', aString, '/js';
  536. commitPathSt: '/', aString, '/st'
  537. ! !
  538. LegacyPackageHandler class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  539. !LegacyPackageHandler class methodsFor: 'commit paths'!
  540. commitPathsFromLoader
  541. <
  542. var commitPath = typeof amber !!== 'undefined' && amber.commitPath;
  543. if (!!commitPath) return;
  544. if (commitPath.js) self._defaultCommitPathJs_(commitPath.js);
  545. if (commitPath.st) self._defaultCommitPathSt_(commitPath.st);
  546. >
  547. !
  548. defaultCommitPathJs
  549. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  550. !
  551. defaultCommitPathJs: aString
  552. defaultCommitPathJs := aString
  553. !
  554. defaultCommitPathSt
  555. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  556. !
  557. defaultCommitPathSt: aString
  558. defaultCommitPathSt := aString
  559. !
  560. resetCommitPaths
  561. defaultCommitPathJs := nil.
  562. defaultCommitPathSt := nil
  563. ! !
  564. !LegacyPackageHandler class methodsFor: 'initialization'!
  565. initialize
  566. super initialize.
  567. self registerFor: 'unknown'.
  568. self commitPathsFromLoader
  569. ! !
  570. !LegacyPackageHandler class methodsFor: 'loading'!
  571. loadPackages: aCollection prefix: aString
  572. ^ self new loadPackages: aCollection prefix: aString
  573. ! !
  574. Object subclass: #PluggableExporter
  575. instanceVariableNames: 'recipe'
  576. package: 'Importer-Exporter'!
  577. !PluggableExporter commentStamp!
  578. I am an engine for exporting structured data on a Stream.
  579. My instances are created using
  580. PluggableExporter forRecipe: aRecipe,
  581. where recipe is structured description of the exporting algorithm (see `ExportRecipeInterpreter`).
  582. The actual exporting is done by interpreting the recipe using a `RecipeInterpreter`.
  583. I am used to export amber packages, so I have a convenience method
  584. `exportPackage: aPackage on: aStream`
  585. which exports `aPackage` using the `recipe`
  586. (it is otherwise no special, so it may be renamed to export:on:)!
  587. !PluggableExporter methodsFor: 'accessing'!
  588. interpreter
  589. ^ ExportRecipeInterpreter new
  590. !
  591. recipe
  592. ^recipe
  593. !
  594. recipe: anArray
  595. recipe := anArray
  596. ! !
  597. !PluggableExporter methodsFor: 'fileOut'!
  598. exportAllPackages
  599. "Export all packages in the system."
  600. ^String streamContents: [:stream |
  601. Smalltalk current packages do: [:pkg |
  602. self exportPackage: pkg on: stream]]
  603. !
  604. exportPackage: aPackage on: aStream
  605. self interpreter interpret: self recipe for: aPackage on: aStream
  606. ! !
  607. !PluggableExporter class methodsFor: 'convenience'!
  608. ownClassesOfPackage: package
  609. "Export classes in dependency order.
  610. Update (issue #171): Remove duplicates for export"
  611. ^package sortedClasses asSet
  612. ! !
  613. !PluggableExporter class methodsFor: 'instance creation'!
  614. forRecipe: aRecipe
  615. ^self new recipe: aRecipe; yourself
  616. ! !
  617. !Package methodsFor: '*Importer-Exporter'!
  618. commit
  619. ^ self transport commit: self
  620. !
  621. commitPathJs
  622. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathJs ifAbsentPut: [self transport commitPathJsFor: self]
  623. !
  624. commitPathJs: aString
  625. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathJs put: aString
  626. !
  627. commitPathSt
  628. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathSt ifAbsentPut: [self transport commitPathStFor: self]
  629. !
  630. commitPathSt: aString
  631. ^ (extension ifNil: [ extension := #{} ]) at: #commitPathSt put: aString
  632. !
  633. transport
  634. ^ PackageHandler for: self transportType
  635. !
  636. transportType
  637. <return (self.transport && self.transport.type) || 'unknown';>
  638. ! !