Platform-ImportExport.st 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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: (((
  346. (#('amber/boot' ':1:'), importsForOutput value, #(':2:'), loadDependencies asArray sorted) asJavascript)
  347. replace: ',\s*["'']:1:["'']' with: pragmaStart) replace: ',\s*["'']:2:["'']' with: pragmaEnd);
  348. nextPutAll: ', function(';
  349. nextPutAll: (((
  350. (#('$boot' ':1:'), importsForOutput key, #(':2:')) join: ',')
  351. replace: ',\s*:1:' with: pragmaStart) replace: ',\s*:2:' with: pragmaEnd);
  352. nextPutAll: '){"use strict";';
  353. lf;
  354. nextPutAll: 'var $core=$boot.api,nil=$boot.nil,$recv=$boot.asReceiver,$globals=$boot.globals;';
  355. lf;
  356. nextPutAll: 'if(!!$boot.nilAsClass)$boot.nilAsClass=$boot.dnu;';
  357. lf
  358. ! !
  359. !AmdExporter methodsFor: 'private'!
  360. amdNamesOfPackages: anArray
  361. ^ (anArray
  362. select: [ :each | (self amdNamespaceOfPackage: each) notNil ])
  363. collect: [ :each | (self amdNamespaceOfPackage: each), '/', each name ]
  364. !
  365. amdNamespaceOfPackage: aPackage
  366. ^ (aPackage transport type = 'amd')
  367. ifTrue: [ aPackage transport namespace ]
  368. ifFalse: [ nil ]
  369. !
  370. importsForOutput: aPackage
  371. "Returns an association where key is list of import variables
  372. and value is list of external dependencies, with ones imported as variables
  373. put at the beginning with same order as is in key.
  374. For example imports:{'jQuery'->'jquery'. 'bootstrap'} would yield
  375. #('jQuery') -> #('jquery' 'bootstrap')"
  376. | namedImports anonImports importVarNames |
  377. namedImports := #().
  378. anonImports := #().
  379. importVarNames := #().
  380. aPackage imports do: [ :each | each isString
  381. ifTrue: [ anonImports add: each ]
  382. ifFalse: [ namedImports add: each value.
  383. importVarNames add: each key ]].
  384. ^ importVarNames -> (namedImports, anonImports)
  385. ! !
  386. Object subclass: #ChunkParser
  387. instanceVariableNames: 'stream last'
  388. package: 'Platform-ImportExport'!
  389. !ChunkParser commentStamp!
  390. I am responsible for parsing aStream contents in the chunk format.
  391. ## API
  392. ChunkParser new
  393. stream: aStream;
  394. nextChunk!
  395. !ChunkParser methodsFor: 'accessing'!
  396. last
  397. ^ last
  398. !
  399. stream: aStream
  400. stream := aStream
  401. ! !
  402. !ChunkParser methodsFor: 'reading'!
  403. nextChunk
  404. "The chunk format (Smalltalk Interchange Format or Fileout format)
  405. is a trivial format but can be a bit tricky to understand:
  406. - Uses the exclamation mark as delimiter of chunks.
  407. - Inside a chunk a normal exclamation mark must be doubled.
  408. - A non empty chunk must be a valid Smalltalk expression.
  409. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  410. - The object created by the expression then takes over reading chunks.
  411. This method returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  412. | char result chunk |
  413. result := '' writeStream.
  414. [ char := stream next.
  415. char notNil ] whileTrue: [
  416. char = '!!' ifTrue: [
  417. stream peek = '!!'
  418. ifTrue: [ stream next "skipping the escape double" ]
  419. ifFalse: [ ^ last := result contents trimBoth "chunk end marker found" ]].
  420. result nextPut: char ].
  421. ^ last := nil "a chunk needs to end with !!"
  422. ! !
  423. !ChunkParser class methodsFor: 'instance creation'!
  424. on: aStream
  425. ^ self new stream: aStream
  426. ! !
  427. Object subclass: #ClassCommentReader
  428. instanceVariableNames: 'class'
  429. package: 'Platform-ImportExport'!
  430. !ClassCommentReader commentStamp!
  431. I provide a mechanism for retrieving class comments stored on a file.
  432. See also `ClassCategoryReader`.!
  433. !ClassCommentReader methodsFor: 'accessing'!
  434. class: aClass
  435. class := aClass
  436. ! !
  437. !ClassCommentReader methodsFor: 'fileIn'!
  438. scanFrom: aChunkParser
  439. | chunk |
  440. chunk := aChunkParser nextChunk.
  441. chunk isEmpty ifFalse: [
  442. self setComment: chunk ].
  443. ! !
  444. !ClassCommentReader methodsFor: 'initialization'!
  445. initialize
  446. super initialize.
  447. ! !
  448. !ClassCommentReader methodsFor: 'private'!
  449. setComment: aString
  450. class comment: aString
  451. ! !
  452. Object subclass: #ClassProtocolReader
  453. instanceVariableNames: 'class category'
  454. package: 'Platform-ImportExport'!
  455. !ClassProtocolReader commentStamp!
  456. I provide a mechanism for retrieving class descriptions stored on a file in the Smalltalk chunk format.!
  457. !ClassProtocolReader methodsFor: 'accessing'!
  458. class: aClass category: aString
  459. class := aClass.
  460. category := aString
  461. ! !
  462. !ClassProtocolReader methodsFor: 'fileIn'!
  463. scanFrom: aChunkParser
  464. | chunk |
  465. [ chunk := aChunkParser nextChunk.
  466. chunk isEmpty ] whileFalse: [
  467. self compileMethod: chunk ]
  468. ! !
  469. !ClassProtocolReader methodsFor: 'initialization'!
  470. initialize
  471. super initialize.
  472. ! !
  473. !ClassProtocolReader methodsFor: 'private'!
  474. compileMethod: aString
  475. Compiler new install: aString forClass: class protocol: category
  476. ! !
  477. Object subclass: #ExportMethodProtocol
  478. instanceVariableNames: 'name theClass'
  479. package: 'Platform-ImportExport'!
  480. !ExportMethodProtocol commentStamp!
  481. I am an abstraction for a method protocol in a class / metaclass.
  482. I know of my class, name and methods.
  483. I am used when exporting a package.!
  484. !ExportMethodProtocol methodsFor: 'accessing'!
  485. methods
  486. ^ (self theClass methodsInProtocol: self name)
  487. sorted: [ :a :b | a selector <= b selector ]
  488. !
  489. name
  490. ^ name
  491. !
  492. name: aString
  493. name := aString
  494. !
  495. theClass
  496. ^ theClass
  497. !
  498. theClass: aClass
  499. theClass := aClass
  500. ! !
  501. !ExportMethodProtocol class methodsFor: 'instance creation'!
  502. name: aString theClass: aClass
  503. ^ self new
  504. name: aString;
  505. theClass: aClass;
  506. yourself
  507. ! !
  508. Object subclass: #Importer
  509. instanceVariableNames: 'lastSection lastChunk'
  510. package: 'Platform-ImportExport'!
  511. !Importer commentStamp!
  512. I can import Amber code from a string in the chunk format.
  513. ## API
  514. Importer new import: aString!
  515. !Importer methodsFor: 'accessing'!
  516. lastChunk
  517. ^ lastChunk
  518. !
  519. lastSection
  520. ^ lastSection
  521. ! !
  522. !Importer methodsFor: 'fileIn'!
  523. import: aStream
  524. | chunk result parser lastEmpty |
  525. parser := ChunkParser on: aStream.
  526. lastEmpty := false.
  527. lastSection := 'n/a, not started'.
  528. lastChunk := nil.
  529. [
  530. [ chunk := parser nextChunk.
  531. chunk isNil ] whileFalse: [
  532. chunk isEmpty
  533. ifTrue: [ lastEmpty := true ]
  534. ifFalse: [
  535. lastSection := chunk.
  536. result := Compiler new evaluateExpression: chunk.
  537. lastEmpty
  538. ifTrue: [
  539. lastEmpty := false.
  540. result scanFrom: parser ]] ].
  541. lastSection := 'n/a, finished'
  542. ] on: Error do: [:e | lastChunk := parser last. e resignal ].
  543. ! !
  544. Error subclass: #PackageCommitError
  545. instanceVariableNames: ''
  546. package: 'Platform-ImportExport'!
  547. !PackageCommitError commentStamp!
  548. I get signaled when an attempt to commit a package has failed.!
  549. Object subclass: #PackageHandler
  550. instanceVariableNames: ''
  551. package: 'Platform-ImportExport'!
  552. !PackageHandler commentStamp!
  553. I am responsible for handling package loading and committing.
  554. I should not be used directly. Instead, use the corresponding `Package` methods.!
  555. !PackageHandler methodsFor: 'accessing'!
  556. chunkContentsFor: aPackage
  557. ^ String streamContents: [ :str |
  558. self chunkExporter exportPackage: aPackage on: str ]
  559. !
  560. chunkExporterClass
  561. ^ ChunkExporter
  562. !
  563. commitPathJsFor: aPackage
  564. self subclassResponsibility
  565. !
  566. commitPathStFor: aPackage
  567. self subclassResponsibility
  568. !
  569. contentsFor: aPackage
  570. ^ String streamContents: [ :str |
  571. self exporter exportPackage: aPackage on: str ]
  572. !
  573. exporterClass
  574. self subclassResponsibility
  575. ! !
  576. !PackageHandler methodsFor: 'committing'!
  577. commit: aPackage
  578. self
  579. commit: aPackage
  580. onSuccess: []
  581. onError: [ :error |
  582. PackageCommitError new
  583. messageText: 'Commiting failed with reason: "' , (error responseText) , '"';
  584. signal ]
  585. !
  586. commit: aPackage onSuccess: aBlock onError: anotherBlock
  587. self
  588. commitJsFileFor: aPackage
  589. onSuccess: [
  590. self
  591. commitStFileFor: aPackage
  592. onSuccess: [ aPackage beClean. aBlock value ]
  593. onError: anotherBlock ]
  594. onError: anotherBlock
  595. !
  596. commitJsFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  597. self
  598. ajaxPutAt: (self commitPathJsFor: aPackage), '/', aPackage name, '.js'
  599. data: (self contentsFor: aPackage)
  600. onSuccess: aBlock
  601. onError: anotherBlock
  602. !
  603. commitStFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  604. self
  605. ajaxPutAt: (self commitPathStFor: aPackage), '/', aPackage name, '.st'
  606. data: (self chunkContentsFor: aPackage)
  607. onSuccess: aBlock
  608. onError: anotherBlock
  609. ! !
  610. !PackageHandler methodsFor: 'error handling'!
  611. onCommitError: anError
  612. PackageCommitError new
  613. messageText: 'Commiting failed with reason: "' , (anError responseText) , '"';
  614. signal
  615. ! !
  616. !PackageHandler methodsFor: 'factory'!
  617. chunkExporter
  618. ^ self chunkExporterClass new
  619. !
  620. exporter
  621. ^ self exporterClass new
  622. ! !
  623. !PackageHandler methodsFor: 'loading'!
  624. load: aPackage
  625. self subclassResponsibility
  626. ! !
  627. !PackageHandler methodsFor: 'private'!
  628. ajaxPutAt: aURL data: aString onSuccess: aBlock onError: anotherBlock
  629. | xhr |
  630. xhr := Platform newXhr.
  631. xhr open: 'PUT' url: aURL async: true.
  632. xhr onreadystatechange: [
  633. xhr readyState = 4 ifTrue: [
  634. (xhr status >= 200 and: [ xhr status < 300 ])
  635. ifTrue: aBlock
  636. ifFalse: anotherBlock ]].
  637. xhr send: aString
  638. ! !
  639. PackageHandler subclass: #AmdPackageHandler
  640. instanceVariableNames: ''
  641. package: 'Platform-ImportExport'!
  642. !AmdPackageHandler commentStamp!
  643. I am responsible for handling package loading and committing.
  644. I should not be used directly. Instead, use the corresponding `Package` methods.!
  645. !AmdPackageHandler methodsFor: 'accessing'!
  646. commitPathJsFor: aPackage
  647. ^ self toUrl: (self namespaceFor: aPackage)
  648. !
  649. commitPathStFor: aPackage
  650. "If _source is not mapped, .st will be committed to .js path.
  651. It is recommended not to use _source as it can be deprecated."
  652. | path pathWithout |
  653. path := self toUrl: (self namespaceFor: aPackage), '/_source'.
  654. pathWithout := self commitPathJsFor: aPackage.
  655. ^ path = (pathWithout, '/_source') ifTrue: [ pathWithout ] ifFalse: [ path ]
  656. !
  657. exporterClass
  658. ^ AmdExporter
  659. ! !
  660. !AmdPackageHandler methodsFor: 'committing'!
  661. namespaceFor: aPackage
  662. ^ aPackage transport namespace
  663. ! !
  664. !AmdPackageHandler methodsFor: 'loading'!
  665. load: aPackage
  666. Smalltalk amdRequire
  667. ifNil: [ self error: 'AMD loader not present' ]
  668. ifNotNil: [ :require |
  669. require value: (Array with: (self namespaceFor: aPackage), '/', aPackage name ) ]
  670. ! !
  671. !AmdPackageHandler methodsFor: 'private'!
  672. toUrl: aString
  673. ^ Smalltalk amdRequire
  674. ifNil: [ self error: 'AMD loader not present' ]
  675. ifNotNil: [ :require | (require basicAt: 'toUrl') value: aString ]
  676. ! !
  677. !AmdPackageHandler class methodsFor: 'commit paths'!
  678. defaultNamespace
  679. ^ Smalltalk defaultAmdNamespace
  680. !
  681. defaultNamespace: aString
  682. Smalltalk defaultAmdNamespace: aString
  683. ! !
  684. Object subclass: #PackageTransport
  685. instanceVariableNames: 'package'
  686. package: 'Platform-ImportExport'!
  687. !PackageTransport commentStamp!
  688. I represent the transport mechanism used to commit a package.
  689. My concrete subclasses have a `#handler` to which committing is delegated.!
  690. !PackageTransport methodsFor: 'accessing'!
  691. commitHandlerClass
  692. self subclassResponsibility
  693. !
  694. definition
  695. ^ ''
  696. !
  697. package
  698. ^ package
  699. !
  700. package: aPackage
  701. package := aPackage
  702. !
  703. type
  704. ^ self class type
  705. ! !
  706. !PackageTransport methodsFor: 'committing'!
  707. commit
  708. self commitHandler commit: self package
  709. !
  710. commitOnSuccess: aBlock onError: anotherBlock
  711. self commitHandler
  712. commit: self package
  713. onSuccess: aBlock
  714. onError: anotherBlock
  715. ! !
  716. !PackageTransport methodsFor: 'converting'!
  717. asJSON
  718. ^ #{ 'type' -> self type }
  719. ! !
  720. !PackageTransport methodsFor: 'factory'!
  721. commitHandler
  722. ^ self commitHandlerClass new
  723. ! !
  724. !PackageTransport methodsFor: 'initialization'!
  725. setupFromJson: anObject
  726. "no op. override if needed in subclasses"
  727. ! !
  728. !PackageTransport methodsFor: 'loading'!
  729. load
  730. self commitHandler load: self package
  731. ! !
  732. PackageTransport class instanceVariableNames: 'registry'!
  733. !PackageTransport class methodsFor: 'accessing'!
  734. classRegisteredFor: aString
  735. ^ registry at: aString
  736. !
  737. defaultType
  738. ^ AmdPackageTransport type
  739. !
  740. type
  741. "Override in subclasses"
  742. ^ nil
  743. ! !
  744. !PackageTransport class methodsFor: 'initialization'!
  745. initialize
  746. super initialize.
  747. self == PackageTransport
  748. ifTrue: [ registry := #{} ]
  749. ifFalse: [ self register ]
  750. ! !
  751. !PackageTransport class methodsFor: 'instance creation'!
  752. for: aString
  753. ^ (self classRegisteredFor: aString) new
  754. !
  755. fromJson: anObject
  756. anObject ifNil: [ ^ self for: self defaultType ].
  757. ^ (self for: anObject type)
  758. setupFromJson: anObject;
  759. yourself
  760. ! !
  761. !PackageTransport class methodsFor: 'registration'!
  762. register
  763. PackageTransport register: self
  764. !
  765. register: aClass
  766. aClass type ifNotNil: [
  767. registry at: aClass type put: aClass ]
  768. ! !
  769. PackageTransport subclass: #AmdPackageTransport
  770. instanceVariableNames: 'namespace'
  771. package: 'Platform-ImportExport'!
  772. !AmdPackageTransport commentStamp!
  773. I am the default transport for committing packages.
  774. See `AmdExporter` and `AmdPackageHandler`.!
  775. !AmdPackageTransport methodsFor: 'accessing'!
  776. commitHandlerClass
  777. ^ AmdPackageHandler
  778. !
  779. definition
  780. ^ String streamContents: [ :stream |
  781. stream
  782. nextPutAll: self class name;
  783. nextPutAll: ' namespace: ';
  784. nextPutAll: '''', self namespace, '''' ]
  785. !
  786. namespace
  787. ^ namespace ifNil: [ self defaultNamespace ]
  788. !
  789. namespace: aString
  790. namespace := aString
  791. ! !
  792. !AmdPackageTransport methodsFor: 'actions'!
  793. setPath: aString
  794. "Set the path the the receiver's `namespace`"
  795. (require basicAt: 'config') value: #{
  796. 'paths' -> #{
  797. self namespace -> aString
  798. }
  799. }.
  800. ! !
  801. !AmdPackageTransport methodsFor: 'converting'!
  802. asJSON
  803. ^ super asJSON
  804. at: 'amdNamespace' put: self namespace;
  805. yourself
  806. ! !
  807. !AmdPackageTransport methodsFor: 'defaults'!
  808. defaultNamespace
  809. ^ Smalltalk defaultAmdNamespace
  810. ! !
  811. !AmdPackageTransport methodsFor: 'initialization'!
  812. setupFromJson: anObject
  813. self namespace: (anObject at: 'amdNamespace')
  814. ! !
  815. !AmdPackageTransport methodsFor: 'printing'!
  816. printOn: aStream
  817. super printOn: aStream.
  818. aStream
  819. nextPutAll: ' (AMD Namespace: ';
  820. nextPutAll: self namespace;
  821. nextPutAll: ')'
  822. ! !
  823. !AmdPackageTransport class methodsFor: 'accessing'!
  824. type
  825. ^ 'amd'
  826. ! !
  827. !AmdPackageTransport class methodsFor: 'instance creation'!
  828. namespace: aString
  829. ^ self new
  830. namespace: aString;
  831. yourself
  832. ! !
  833. !Behavior methodsFor: '*Platform-ImportExport'!
  834. commentStamp
  835. ^ ClassCommentReader new
  836. class: self;
  837. yourself
  838. !
  839. commentStamp: aStamp prior: prior
  840. ^ self commentStamp
  841. !
  842. methodsFor: aString
  843. ^ ClassProtocolReader new
  844. class: self category: aString;
  845. yourself
  846. !
  847. methodsFor: aString stamp: aStamp
  848. "Added for file-in compatibility, ignores stamp."
  849. ^ self methodsFor: aString
  850. ! !
  851. !Package methodsFor: '*Platform-ImportExport'!
  852. commit
  853. ^ self transport commit
  854. !
  855. load
  856. ^ self transport load
  857. !
  858. loadFromNamespace: aString
  859. ^ self transport
  860. namespace: aString;
  861. load
  862. ! !
  863. !Package class methodsFor: '*Platform-ImportExport'!
  864. load: aPackageName
  865. (self named: aPackageName) load
  866. !
  867. load: aPackageName fromNamespace: aString
  868. (self named: aPackageName) loadFromNamespace: aString
  869. ! !