Platform-ImportExport.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. Smalltalk createPackage: 'Platform-ImportExport'!
  2. Object subclass: #AbstractExporter
  3. instanceVariableNames: ''
  4. package: 'Platform-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. classNameFor: aClass
  33. ^ aClass isMetaclass
  34. ifTrue: [ aClass instanceClass name, ' class' ]
  35. ifFalse: [
  36. aClass
  37. ifNil: [ 'nil' ]
  38. ifNotNil: [ aClass name ] ]
  39. ! !
  40. !AbstractExporter methodsFor: 'output'!
  41. exportPackage: aPackage on: aStream
  42. self subclassResponsibility
  43. ! !
  44. AbstractExporter subclass: #ChunkExporter
  45. instanceVariableNames: ''
  46. package: 'Platform-ImportExport'!
  47. !ChunkExporter commentStamp!
  48. I am an exporter dedicated to outputting Amber source code in the classic Smalltalk chunk format.
  49. I do not output any compiled code.!
  50. !ChunkExporter methodsFor: 'accessing'!
  51. extensionCategoriesOfPackage: aPackage
  52. "Issue #143: sort protocol alphabetically"
  53. | name map result |
  54. name := aPackage name.
  55. result := OrderedCollection new.
  56. (Package sortedClasses: Smalltalk classes) do: [ :each |
  57. {each. each class} do: [ :aClass |
  58. map := Dictionary new.
  59. aClass protocolsDo: [ :category :methods |
  60. category = ('*', name) ifTrue: [ map at: category put: methods ] ].
  61. result addAll: ((map keys sorted: [ :a :b | a <= b ]) collect: [ :category |
  62. MethodCategory name: category theClass: aClass methods: (map at: category) ]) ] ].
  63. ^ result
  64. !
  65. ownCategoriesOfClass: aClass
  66. "Answer the protocols of aClass that are not package extensions"
  67. "Issue #143: sort protocol alphabetically"
  68. | map |
  69. map := Dictionary new.
  70. aClass protocolsDo: [ :each :methods |
  71. (each match: '^\*') ifFalse: [ map at: each put: methods ] ].
  72. ^ (map keys sorted: [ :a :b | a <= b ]) collect: [ :each |
  73. MethodCategory name: each theClass: aClass methods: (map at: each) ]
  74. !
  75. ownCategoriesOfMetaClass: aClass
  76. "Issue #143: sort protocol alphabetically"
  77. ^ self ownCategoriesOfClass: aClass class
  78. !
  79. ownMethodProtocolsOfClass: aClass
  80. "Answer a collection of ExportMethodProtocol object of aClass that are not package extensions"
  81. ^ aClass ownProtocols collect: [ :each |
  82. ExportMethodProtocol name: each theClass: aClass ]
  83. ! !
  84. !ChunkExporter methodsFor: 'convenience'!
  85. chunkEscape: aString
  86. "Replace all occurrences of !! with !!!! and trim at both ends."
  87. ^ (aString replace: '!!' with: '!!!!') trimBoth
  88. ! !
  89. !ChunkExporter methodsFor: 'output'!
  90. exportCategoryEpilogueOf: aCategory on: aStream
  91. aStream nextPutAll: ' !!'; lf; lf
  92. !
  93. exportCategoryPrologueOf: aCategory on: aStream
  94. aStream
  95. nextPutAll: '!!', (self classNameFor: aCategory theClass);
  96. nextPutAll: ' methodsFor: ''', aCategory name, '''!!'
  97. !
  98. exportDefinitionOf: aClass on: aStream
  99. "Chunk format."
  100. aStream
  101. nextPutAll: (self classNameFor: aClass superclass);
  102. nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;
  103. tab; nextPutAll: 'instanceVariableNames: '''.
  104. aClass instanceVariableNames
  105. do: [ :each | aStream nextPutAll: each ]
  106. separatedBy: [ aStream nextPutAll: ' ' ].
  107. aStream
  108. nextPutAll: ''''; lf;
  109. tab; nextPutAll: 'package: ''', aClass category, '''!!'; lf.
  110. aClass comment notEmpty ifTrue: [
  111. aStream
  112. nextPutAll: '!!', (self classNameFor: aClass), ' commentStamp!!';lf;
  113. nextPutAll: (self chunkEscape: aClass comment), '!!';lf ].
  114. aStream lf
  115. !
  116. exportMetaDefinitionOf: aClass on: aStream
  117. aClass class instanceVariableNames isEmpty ifFalse: [
  118. aStream
  119. nextPutAll: (self classNameFor: aClass class);
  120. nextPutAll: ' instanceVariableNames: '''.
  121. aClass class instanceVariableNames
  122. do: [ :each | aStream nextPutAll: each ]
  123. separatedBy: [ aStream nextPutAll: ' ' ].
  124. aStream
  125. nextPutAll: '''!!'; lf; lf ]
  126. !
  127. exportMethod: aMethod on: aStream
  128. aStream
  129. lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;
  130. nextPutAll: '!!'
  131. !
  132. exportPackage: aPackage on: aStream
  133. self
  134. exportPackageDefinitionOf: aPackage on: aStream;
  135. exportPackageImportsOf: 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 createPackage: ''', aPackage name, '''!!';
  152. lf
  153. !
  154. exportPackageImportsOf: aPackage on: aStream
  155. aPackage imports ifNotEmpty: [ :imports |
  156. aStream
  157. nextPutAll: '(Smalltalk packageAt: ''';
  158. nextPutAll: aPackage name;
  159. nextPutAll: ''') imports: ';
  160. nextPutAll: (self chunkEscape: aPackage importsDefinition);
  161. nextPutAll: '!!';
  162. lf ]
  163. !
  164. exportProtocol: aProtocol on: aStream
  165. self exportProtocolPrologueOf: aProtocol on: aStream.
  166. aProtocol methods do: [ :method |
  167. self exportMethod: method on: aStream ].
  168. self exportProtocolEpilogueOf: aProtocol on: aStream
  169. !
  170. exportProtocolEpilogueOf: aProtocol on: aStream
  171. aStream nextPutAll: ' !!'; lf; lf
  172. !
  173. exportProtocolPrologueOf: aProtocol on: aStream
  174. aStream
  175. nextPutAll: '!!', (self classNameFor: aProtocol theClass);
  176. nextPutAll: ' methodsFor: ''', aProtocol name, '''!!'
  177. !
  178. exportProtocols: aCollection on: aStream
  179. aCollection do: [ :each |
  180. self exportProtocol: each on: aStream ]
  181. ! !
  182. AbstractExporter subclass: #Exporter
  183. instanceVariableNames: ''
  184. package: 'Platform-ImportExport'!
  185. !Exporter commentStamp!
  186. I am responsible for outputting Amber code into a JavaScript string.
  187. The generated output is enough to reconstruct the exported data, including Smalltalk source code and other metadata.
  188. ## Use case
  189. I am typically used to save code outside of the Amber runtime (committing to disk, etc.).!
  190. !Exporter methodsFor: 'accessing'!
  191. ownMethodsOfClass: aClass
  192. "Issue #143: sort methods alphabetically"
  193. ^ ((aClass methodDictionary values) sorted: [ :a :b | a selector <= b selector ])
  194. reject: [ :each | (each protocol match: '^\*') ]
  195. !
  196. ownMethodsOfMetaClass: aClass
  197. "Issue #143: sort methods alphabetically"
  198. ^ self ownMethodsOfClass: aClass class
  199. ! !
  200. !Exporter methodsFor: 'convenience'!
  201. jsClassNameFor: aClass
  202. ^ aClass isMetaclass
  203. ifTrue: [ (self jsClassNameFor: aClass instanceClass), '.klass' ]
  204. ifFalse: [
  205. aClass
  206. ifNil: [ 'null' ]
  207. ifNotNil: [ '$globals.', aClass name ] ]
  208. ! !
  209. !Exporter methodsFor: 'output'!
  210. exportDefinitionOf: aClass on: aStream
  211. aStream
  212. lf;
  213. nextPutAll: '$core.addClass(';
  214. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  215. nextPutAll: (self jsClassNameFor: aClass superclass);
  216. nextPutAll: ', ['.
  217. aClass instanceVariableNames
  218. do: [ :each | aStream nextPutAll: '''', each, '''' ]
  219. separatedBy: [ aStream nextPutAll: ', ' ].
  220. aStream
  221. nextPutAll: '], ''';
  222. nextPutAll: aClass category, '''';
  223. nextPutAll: ');'.
  224. aClass comment notEmpty ifTrue: [
  225. aStream
  226. lf;
  227. nextPutAll: '//>>excludeStart("ide", pragmas.excludeIdeData);';
  228. lf;
  229. nextPutAll: (self jsClassNameFor: aClass);
  230. nextPutAll: '.comment=';
  231. nextPutAll: aClass comment crlfSanitized asJavascript;
  232. nextPutAll: ';';
  233. lf;
  234. nextPutAll: '//>>excludeEnd("ide");' ].
  235. aStream lf
  236. !
  237. exportMetaDefinitionOf: aClass on: aStream
  238. aStream lf.
  239. aClass class instanceVariableNames isEmpty ifFalse: [
  240. aStream
  241. nextPutAll: (self jsClassNameFor: aClass class);
  242. nextPutAll: '.iVarNames = ['.
  243. aClass class instanceVariableNames
  244. do: [ :each | aStream nextPutAll: '''', each, '''' ]
  245. separatedBy: [ aStream nextPutAll: ',' ].
  246. aStream nextPutAll: '];', String lf ]
  247. !
  248. exportMethod: aMethod on: aStream
  249. aStream
  250. nextPutAll: '$core.addMethod(';lf;
  251. nextPutAll: '$core.method({';lf;
  252. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  253. nextPutAll: 'protocol: ''', aMethod protocol, ''',';lf;
  254. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  255. nextPutAll: '//>>excludeStart("ide", pragmas.excludeIdeData);';lf;
  256. nextPutAll: 'args: ', aMethod arguments asJavascript, ','; lf;
  257. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf;
  258. nextPutAll: 'referencedClasses: ', aMethod referencedClasses asJavascript, ',';lf;
  259. nextPutAll: '//>>excludeEnd("ide");';lf;
  260. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript;lf;
  261. nextPutAll: '}),';lf;
  262. nextPutAll: (self jsClassNameFor: aMethod methodClass);
  263. nextPutAll: ');';lf;lf
  264. !
  265. exportPackage: aPackage on: aStream
  266. self
  267. exportPackagePrologueOf: aPackage on: aStream;
  268. exportPackageDefinitionOf: aPackage on: aStream;
  269. exportPackageContextOf: aPackage on: aStream;
  270. exportPackageImportsOf: aPackage on: aStream;
  271. exportPackageTransportOf: aPackage on: aStream.
  272. aPackage sortedClasses do: [ :each |
  273. self exportDefinitionOf: each on: aStream.
  274. each ownMethods do: [ :method |
  275. self exportMethod: method on: aStream ].
  276. self exportMetaDefinitionOf: each on: aStream.
  277. each class ownMethods do: [ :method |
  278. self exportMethod: method on: aStream ] ].
  279. (self extensionMethodsOfPackage: aPackage) do: [ :each |
  280. self exportMethod: each on: aStream ].
  281. self exportPackageEpilogueOf: aPackage on: aStream
  282. !
  283. exportPackageContextOf: aPackage on: aStream
  284. aStream
  285. nextPutAll: '$core.packages[';
  286. nextPutAll: aPackage name asJavascript;
  287. nextPutAll: '].innerEval = ';
  288. nextPutAll: 'function (expr) { return eval(expr); }';
  289. nextPutAll: ';';
  290. lf
  291. !
  292. exportPackageDefinitionOf: aPackage on: aStream
  293. aStream
  294. nextPutAll: '$core.addPackage(';
  295. nextPutAll: '''', aPackage name, ''');';
  296. lf
  297. !
  298. exportPackageEpilogueOf: aPackage on: aStream
  299. self subclassResponsibility
  300. !
  301. exportPackageImportsOf: aPackage on: aStream
  302. aPackage importsAsJson ifNotEmpty: [ :imports |
  303. aStream
  304. nextPutAll: '$core.packages[';
  305. nextPutAll: aPackage name asJavascript;
  306. nextPutAll: '].imports = ';
  307. nextPutAll: imports asJavascript;
  308. nextPutAll: ';';
  309. lf ]
  310. !
  311. exportPackagePrologueOf: aPackage on: aStream
  312. self subclassResponsibility
  313. !
  314. exportPackageTransportOf: aPackage on: aStream
  315. aStream
  316. nextPutAll: '$core.packages[';
  317. nextPutAll: aPackage name asJavascript;
  318. nextPutAll: '].transport = ';
  319. nextPutAll: aPackage transport asJSONString;
  320. nextPutAll: ';';
  321. lf
  322. ! !
  323. Exporter subclass: #AmdExporter
  324. instanceVariableNames: 'namespace'
  325. package: 'Platform-ImportExport'!
  326. !AmdExporter commentStamp!
  327. I am used to export Packages in an AMD (Asynchronous Module Definition) JavaScript format.!
  328. !AmdExporter methodsFor: 'output'!
  329. exportPackageEpilogueOf: aPackage on: aStream
  330. aStream
  331. nextPutAll: '});';
  332. lf
  333. !
  334. exportPackagePrologueOf: aPackage on: aStream
  335. | importsForOutput loadDependencies pragmaStart pragmaEnd |
  336. pragmaStart := ''.
  337. pragmaEnd := ''.
  338. importsForOutput := self importsForOutput: aPackage.
  339. loadDependencies := self amdNamesOfPackages: aPackage loadDependencies.
  340. importsForOutput value ifNotEmpty: [
  341. pragmaStart := String lf, '//>>excludeStart("imports", pragmas.excludeImports);', String lf.
  342. pragmaEnd := String lf, '//>>excludeEnd("imports");', String lf ].
  343. aStream
  344. nextPutAll: 'define("';
  345. nextPutAll: (self amdNamespaceOfPackage: aPackage);
  346. nextPutAll: '/';
  347. nextPutAll: aPackage name;
  348. nextPutAll: '", ';
  349. nextPutAll: (((
  350. (#('amber/boot' ':1:'), importsForOutput value, #(':2:'), loadDependencies) asJavascript)
  351. replace: ',\s*["'']:1:["'']' with: pragmaStart) replace: ',\s*["'']:2:["'']' with: pragmaEnd);
  352. nextPutAll: ', function(';
  353. nextPutAll: (((
  354. (#('$boot' ':1:'), importsForOutput key, #(':2:')) join: ',')
  355. replace: ',\s*:1:' with: pragmaStart) replace: ',\s*:2:' with: pragmaEnd);
  356. nextPutAll: '){"use strict";';
  357. lf;
  358. nextPutAll: 'var $core=$boot.api,nil=$boot.nil,$recv=$boot.asReceiver,$globals=$boot.globals;';
  359. lf
  360. ! !
  361. !AmdExporter methodsFor: 'private'!
  362. amdNamesOfPackages: anArray
  363. ^ (anArray
  364. select: [ :each | (self amdNamespaceOfPackage: each) notNil ])
  365. collect: [ :each | (self amdNamespaceOfPackage: each), '/', each name ]
  366. !
  367. amdNamespaceOfPackage: aPackage
  368. ^ (aPackage transport type = 'amd')
  369. ifTrue: [ aPackage transport namespace ]
  370. ifFalse: [ nil ]
  371. !
  372. importsForOutput: aPackage
  373. "Returns an association where key is list of import variables
  374. and value is list of external dependencies, with ones imported as variables
  375. put at the beginning with same order as is in key.
  376. For example imports:{'jQuery'->'jquery'. 'bootstrap'} would yield
  377. #('jQuery') -> #('jquery' 'bootstrap')"
  378. | namedImports anonImports importVarNames |
  379. namedImports := #().
  380. anonImports := #().
  381. importVarNames := #().
  382. aPackage imports do: [ :each | each isString
  383. ifTrue: [ anonImports add: each ]
  384. ifFalse: [ namedImports add: each value.
  385. importVarNames add: each key ]].
  386. ^ importVarNames -> (namedImports, anonImports)
  387. ! !
  388. Object subclass: #ChunkParser
  389. instanceVariableNames: 'stream last'
  390. package: 'Platform-ImportExport'!
  391. !ChunkParser commentStamp!
  392. I am responsible for parsing aStream contents in the chunk format.
  393. ## API
  394. ChunkParser new
  395. stream: aStream;
  396. nextChunk!
  397. !ChunkParser methodsFor: 'accessing'!
  398. last
  399. ^ last
  400. !
  401. stream: aStream
  402. stream := aStream
  403. ! !
  404. !ChunkParser methodsFor: 'reading'!
  405. nextChunk
  406. "The chunk format (Smalltalk Interchange Format or Fileout format)
  407. is a trivial format but can be a bit tricky to understand:
  408. - Uses the exclamation mark as delimiter of chunks.
  409. - Inside a chunk a normal exclamation mark must be doubled.
  410. - A non empty chunk must be a valid Smalltalk expression.
  411. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  412. - The object created by the expression then takes over reading chunks.
  413. This method returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  414. | char result chunk |
  415. result := '' writeStream.
  416. [ char := stream next.
  417. char notNil ] whileTrue: [
  418. char = '!!' ifTrue: [
  419. stream peek = '!!'
  420. ifTrue: [ stream next "skipping the escape double" ]
  421. ifFalse: [ ^ last := result contents trimBoth "chunk end marker found" ]].
  422. result nextPut: char ].
  423. ^ last := nil "a chunk needs to end with !!"
  424. ! !
  425. !ChunkParser class methodsFor: 'instance creation'!
  426. on: aStream
  427. ^ self new stream: aStream
  428. ! !
  429. Object subclass: #ClassCommentReader
  430. instanceVariableNames: 'class'
  431. package: 'Platform-ImportExport'!
  432. !ClassCommentReader commentStamp!
  433. I provide a mechanism for retrieving class comments stored on a file.
  434. See also `ClassCategoryReader`.!
  435. !ClassCommentReader methodsFor: 'accessing'!
  436. class: aClass
  437. class := aClass
  438. ! !
  439. !ClassCommentReader methodsFor: 'fileIn'!
  440. scanFrom: aChunkParser
  441. | chunk |
  442. chunk := aChunkParser nextChunk.
  443. chunk isEmpty ifFalse: [
  444. self setComment: chunk ].
  445. ! !
  446. !ClassCommentReader methodsFor: 'initialization'!
  447. initialize
  448. super initialize.
  449. ! !
  450. !ClassCommentReader methodsFor: 'private'!
  451. setComment: aString
  452. class comment: aString
  453. ! !
  454. Object subclass: #ClassProtocolReader
  455. instanceVariableNames: 'class category'
  456. package: 'Platform-ImportExport'!
  457. !ClassProtocolReader commentStamp!
  458. I provide a mechanism for retrieving class descriptions stored on a file in the Smalltalk chunk format.!
  459. !ClassProtocolReader methodsFor: 'accessing'!
  460. class: aClass category: aString
  461. class := aClass.
  462. category := aString
  463. ! !
  464. !ClassProtocolReader methodsFor: 'fileIn'!
  465. scanFrom: aChunkParser
  466. | chunk |
  467. [ chunk := aChunkParser nextChunk.
  468. chunk isEmpty ] whileFalse: [
  469. self compileMethod: chunk ]
  470. ! !
  471. !ClassProtocolReader methodsFor: 'initialization'!
  472. initialize
  473. super initialize.
  474. ! !
  475. !ClassProtocolReader methodsFor: 'private'!
  476. compileMethod: aString
  477. Compiler new install: aString forClass: class protocol: category
  478. ! !
  479. Object subclass: #ExportMethodProtocol
  480. instanceVariableNames: 'name theClass'
  481. package: 'Platform-ImportExport'!
  482. !ExportMethodProtocol commentStamp!
  483. I am an abstraction for a method protocol in a class / metaclass.
  484. I know of my class, name and methods.
  485. I am used when exporting a package.!
  486. !ExportMethodProtocol methodsFor: 'accessing'!
  487. methods
  488. ^ (self theClass methodsInProtocol: self name)
  489. sorted: [ :a :b | a selector <= b selector ]
  490. !
  491. name
  492. ^ name
  493. !
  494. name: aString
  495. name := aString
  496. !
  497. theClass
  498. ^ theClass
  499. !
  500. theClass: aClass
  501. theClass := aClass
  502. ! !
  503. !ExportMethodProtocol class methodsFor: 'instance creation'!
  504. name: aString theClass: aClass
  505. ^ self new
  506. name: aString;
  507. theClass: aClass;
  508. yourself
  509. ! !
  510. Object subclass: #Importer
  511. instanceVariableNames: 'lastSection lastChunk'
  512. package: 'Platform-ImportExport'!
  513. !Importer commentStamp!
  514. I can import Amber code from a string in the chunk format.
  515. ## API
  516. Importer new import: aString!
  517. !Importer methodsFor: 'accessing'!
  518. lastChunk
  519. ^ lastChunk
  520. !
  521. lastSection
  522. ^ lastSection
  523. ! !
  524. !Importer methodsFor: 'fileIn'!
  525. import: aStream
  526. | chunk result parser lastEmpty |
  527. parser := ChunkParser on: aStream.
  528. lastEmpty := false.
  529. lastSection := 'n/a, not started'.
  530. lastChunk := nil.
  531. [
  532. [ chunk := parser nextChunk.
  533. chunk isNil ] whileFalse: [
  534. chunk isEmpty
  535. ifTrue: [ lastEmpty := true ]
  536. ifFalse: [
  537. lastSection := chunk.
  538. result := Compiler new evaluateExpression: chunk.
  539. lastEmpty
  540. ifTrue: [
  541. lastEmpty := false.
  542. result scanFrom: parser ]] ].
  543. lastSection := 'n/a, finished'
  544. ] on: Error do: [:e | lastChunk := parser last. e resignal ].
  545. ! !
  546. Error subclass: #PackageCommitError
  547. instanceVariableNames: ''
  548. package: 'Platform-ImportExport'!
  549. !PackageCommitError commentStamp!
  550. I get signaled when an attempt to commit a package has failed.!
  551. InterfacingObject subclass: #PackageHandler
  552. instanceVariableNames: ''
  553. package: 'Platform-ImportExport'!
  554. !PackageHandler commentStamp!
  555. I am responsible for handling package loading and committing.
  556. I should not be used directly. Instead, use the corresponding `Package` methods.!
  557. !PackageHandler methodsFor: 'accessing'!
  558. chunkContentsFor: aPackage
  559. ^ String streamContents: [ :str |
  560. self chunkExporter exportPackage: aPackage on: str ]
  561. !
  562. chunkExporterClass
  563. ^ ChunkExporter
  564. !
  565. commitPathJsFor: aPackage
  566. self subclassResponsibility
  567. !
  568. commitPathStFor: aPackage
  569. self subclassResponsibility
  570. !
  571. contentsFor: aPackage
  572. ^ String streamContents: [ :str |
  573. self exporter exportPackage: aPackage on: str ]
  574. !
  575. exporterClass
  576. self subclassResponsibility
  577. ! !
  578. !PackageHandler methodsFor: 'committing'!
  579. commit: aPackage
  580. self
  581. commit: aPackage
  582. onSuccess: []
  583. onError: [ :error |
  584. PackageCommitError new
  585. messageText: 'Commiting failed with reason: "' , (error responseText) , '"';
  586. signal ]
  587. !
  588. commit: aPackage onSuccess: aBlock onError: anotherBlock
  589. self
  590. commitJsFileFor: aPackage
  591. onSuccess: [
  592. self
  593. commitStFileFor: aPackage
  594. onSuccess: [ aPackage beClean. aBlock value ]
  595. onError: anotherBlock ]
  596. onError: anotherBlock
  597. !
  598. commitJsFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  599. self
  600. ajaxPutAt: (self commitPathJsFor: aPackage), '/', aPackage name, '.js'
  601. data: (self contentsFor: aPackage)
  602. onSuccess: aBlock
  603. onError: anotherBlock
  604. !
  605. commitStFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  606. self
  607. ajaxPutAt: (self commitPathStFor: aPackage), '/', aPackage name, '.st'
  608. data: (self chunkContentsFor: aPackage)
  609. onSuccess: aBlock
  610. onError: anotherBlock
  611. ! !
  612. !PackageHandler methodsFor: 'error handling'!
  613. onCommitError: anError
  614. PackageCommitError new
  615. messageText: 'Commiting failed with reason: "' , (anError responseText) , '"';
  616. signal
  617. ! !
  618. !PackageHandler methodsFor: 'factory'!
  619. chunkExporter
  620. ^ self chunkExporterClass new
  621. !
  622. exporter
  623. ^ self exporterClass new
  624. ! !
  625. !PackageHandler methodsFor: 'loading'!
  626. load: aPackage
  627. self subclassResponsibility
  628. ! !
  629. !PackageHandler methodsFor: 'private'!
  630. ajaxPutAt: aURL data: aString onSuccess: aBlock onError: anotherBlock
  631. | xhr |
  632. xhr := Platform newXhr.
  633. xhr open: 'PUT' url: aURL async: true.
  634. xhr onreadystatechange: [
  635. xhr readyState = 4 ifTrue: [
  636. (xhr status >= 200 and: [ xhr status < 300 ])
  637. ifTrue: aBlock
  638. ifFalse: anotherBlock ]].
  639. xhr send: aString
  640. ! !
  641. PackageHandler subclass: #AmdPackageHandler
  642. instanceVariableNames: ''
  643. package: 'Platform-ImportExport'!
  644. !AmdPackageHandler commentStamp!
  645. I am responsible for handling package loading and committing.
  646. I should not be used directly. Instead, use the corresponding `Package` methods.!
  647. !AmdPackageHandler methodsFor: 'accessing'!
  648. commitPathJsFor: aPackage
  649. ^ self toUrl: (self namespaceFor: aPackage)
  650. !
  651. commitPathStFor: aPackage
  652. "If _source is not mapped, .st will be committed to .js path.
  653. It is recommended not to use _source as it can be deprecated."
  654. | path pathWithout |
  655. path := self toUrl: (self namespaceFor: aPackage), '/_source'.
  656. pathWithout := self commitPathJsFor: aPackage.
  657. ^ path = (pathWithout, '/_source') ifTrue: [ pathWithout ] ifFalse: [ path ]
  658. !
  659. exporterClass
  660. ^ AmdExporter
  661. ! !
  662. !AmdPackageHandler methodsFor: 'committing'!
  663. namespaceFor: aPackage
  664. ^ aPackage transport namespace
  665. ! !
  666. !AmdPackageHandler methodsFor: 'loading'!
  667. load: aPackage
  668. Smalltalk amdRequire
  669. ifNil: [ self error: 'AMD loader not present' ]
  670. ifNotNil: [ :require |
  671. require value: (Array with: (self namespaceFor: aPackage), '/', aPackage name ) ]
  672. ! !
  673. !AmdPackageHandler methodsFor: 'private'!
  674. toUrl: aString
  675. ^ Smalltalk amdRequire
  676. ifNil: [ self error: 'AMD loader not present' ]
  677. ifNotNil: [ :require | (require basicAt: 'toUrl') value: aString ]
  678. ! !
  679. !AmdPackageHandler class methodsFor: 'commit paths'!
  680. defaultNamespace
  681. ^ Smalltalk defaultAmdNamespace
  682. !
  683. defaultNamespace: aString
  684. Smalltalk defaultAmdNamespace: aString
  685. ! !
  686. Object subclass: #PackageTransport
  687. instanceVariableNames: 'package'
  688. package: 'Platform-ImportExport'!
  689. !PackageTransport commentStamp!
  690. I represent the transport mechanism used to commit a package.
  691. My concrete subclasses have a `#handler` to which committing is delegated.!
  692. !PackageTransport methodsFor: 'accessing'!
  693. commitHandlerClass
  694. self subclassResponsibility
  695. !
  696. definition
  697. ^ ''
  698. !
  699. package
  700. ^ package
  701. !
  702. package: aPackage
  703. package := aPackage
  704. !
  705. type
  706. ^ self class type
  707. ! !
  708. !PackageTransport methodsFor: 'committing'!
  709. commit
  710. self commitHandler commit: self package
  711. !
  712. commitOnSuccess: aBlock onError: anotherBlock
  713. self commitHandler
  714. commit: self package
  715. onSuccess: aBlock
  716. onError: anotherBlock
  717. ! !
  718. !PackageTransport methodsFor: 'converting'!
  719. asJSON
  720. ^ #{ 'type' -> self type }
  721. ! !
  722. !PackageTransport methodsFor: 'factory'!
  723. commitHandler
  724. ^ self commitHandlerClass new
  725. ! !
  726. !PackageTransport methodsFor: 'initialization'!
  727. setupFromJson: anObject
  728. "no op. override if needed in subclasses"
  729. ! !
  730. !PackageTransport methodsFor: 'loading'!
  731. load
  732. self commitHandler load: self package
  733. ! !
  734. PackageTransport class instanceVariableNames: 'registry'!
  735. !PackageTransport class methodsFor: 'accessing'!
  736. classRegisteredFor: aString
  737. ^ registry at: aString
  738. !
  739. defaultType
  740. ^ AmdPackageTransport type
  741. !
  742. type
  743. "Override in subclasses"
  744. ^ nil
  745. ! !
  746. !PackageTransport class methodsFor: 'initialization'!
  747. initialize
  748. super initialize.
  749. self == PackageTransport
  750. ifTrue: [ registry := #{} ]
  751. ifFalse: [ self register ]
  752. ! !
  753. !PackageTransport class methodsFor: 'instance creation'!
  754. for: aString
  755. ^ (self classRegisteredFor: aString) new
  756. !
  757. fromJson: anObject
  758. anObject ifNil: [ ^ self for: self defaultType ].
  759. ^ (self for: anObject type)
  760. setupFromJson: anObject;
  761. yourself
  762. ! !
  763. !PackageTransport class methodsFor: 'registration'!
  764. register
  765. PackageTransport register: self
  766. !
  767. register: aClass
  768. aClass type ifNotNil: [
  769. registry at: aClass type put: aClass ]
  770. ! !
  771. PackageTransport subclass: #AmdPackageTransport
  772. instanceVariableNames: 'namespace'
  773. package: 'Platform-ImportExport'!
  774. !AmdPackageTransport commentStamp!
  775. I am the default transport for committing packages.
  776. See `AmdExporter` and `AmdPackageHandler`.!
  777. !AmdPackageTransport methodsFor: 'accessing'!
  778. commitHandlerClass
  779. ^ AmdPackageHandler
  780. !
  781. definition
  782. ^ String streamContents: [ :stream |
  783. stream
  784. nextPutAll: self class name;
  785. nextPutAll: ' namespace: ';
  786. nextPutAll: '''', self namespace, '''' ]
  787. !
  788. namespace
  789. ^ namespace ifNil: [ self defaultNamespace ]
  790. !
  791. namespace: aString
  792. namespace := aString
  793. ! !
  794. !AmdPackageTransport methodsFor: 'actions'!
  795. setPath: aString
  796. "Set the path the the receiver's `namespace`"
  797. (require basicAt: 'config') value: #{
  798. 'paths' -> #{
  799. self namespace -> aString
  800. }
  801. }.
  802. ! !
  803. !AmdPackageTransport methodsFor: 'converting'!
  804. asJSON
  805. ^ super asJSON
  806. at: 'amdNamespace' put: self namespace;
  807. yourself
  808. ! !
  809. !AmdPackageTransport methodsFor: 'defaults'!
  810. defaultNamespace
  811. ^ Smalltalk defaultAmdNamespace
  812. ! !
  813. !AmdPackageTransport methodsFor: 'initialization'!
  814. setupFromJson: anObject
  815. self namespace: (anObject at: 'amdNamespace')
  816. ! !
  817. !AmdPackageTransport methodsFor: 'printing'!
  818. printOn: aStream
  819. super printOn: aStream.
  820. aStream
  821. nextPutAll: ' (AMD Namespace: ';
  822. nextPutAll: self namespace;
  823. nextPutAll: ')'
  824. ! !
  825. !AmdPackageTransport class methodsFor: 'accessing'!
  826. type
  827. ^ 'amd'
  828. ! !
  829. !AmdPackageTransport class methodsFor: 'instance creation'!
  830. namespace: aString
  831. ^ self new
  832. namespace: aString;
  833. yourself
  834. ! !
  835. !Behavior methodsFor: '*Platform-ImportExport'!
  836. commentStamp
  837. ^ ClassCommentReader new
  838. class: self;
  839. yourself
  840. !
  841. commentStamp: aStamp prior: prior
  842. ^ self commentStamp
  843. !
  844. methodsFor: aString
  845. ^ ClassProtocolReader new
  846. class: self category: aString;
  847. yourself
  848. !
  849. methodsFor: aString stamp: aStamp
  850. "Added for file-in compatibility, ignores stamp."
  851. ^ self methodsFor: aString
  852. ! !
  853. !Package methodsFor: '*Platform-ImportExport'!
  854. commit
  855. ^ self transport commit
  856. !
  857. load
  858. ^ self transport load
  859. !
  860. loadFromNamespace: aString
  861. ^ self transport
  862. namespace: aString;
  863. load
  864. ! !
  865. !Package class methodsFor: '*Platform-ImportExport'!
  866. load: aPackageName
  867. (self named: aPackageName) load
  868. !
  869. load: aPackageName fromNamespace: aString
  870. (self named: aPackageName) loadFromNamespace: aString
  871. ! !