Browse Source

Merge pull request #585 from herby/requirejs-output-dependencies

Requirejs output dependencies
Herbert Vojčík 11 years ago
parent
commit
8a2ba0b5c4
3 changed files with 130 additions and 10 deletions
  1. 47 4
      js/Importer-Exporter.deploy.js
  2. 58 5
      js/Importer-Exporter.js
  3. 25 1
      st/Importer-Exporter.st

+ 47 - 4
js/Importer-Exporter.deploy.js

@@ -1,10 +1,29 @@
-define("amber/Importer-Exporter", ["amber_vm/smalltalk","amber_vm/nil","amber_vm/_st"], function(smalltalk,nil,_st){
+define("amber/Importer-Exporter", ["amber_vm/smalltalk", "amber_vm/nil", "amber_vm/_st", "amber/Kernel-Objects"], function(smalltalk,nil,_st){
 smalltalk.addPackage('Importer-Exporter');
 smalltalk.packages["Importer-Exporter"].transport = {"type":"amd","amdNamespace":"amber"};
 
-
 smalltalk.addClass('AmdExporter', smalltalk.Object, [], 'Importer-Exporter');
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "amdNamesOfPackages:",
+fn: function (anArray){
+var self=this;
+var deps,depNames;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=_st(_st(anArray)._select_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(_st(each)._amdNamespace())._notNil();
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})))._collect_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(_st(_st(each)._amdNamespace()).__comma("/")).__comma(_st(each)._name());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"amdNamesOfPackages:",{anArray:anArray,deps:deps,depNames:depNames},smalltalk.AmdExporter.klass)})},
+messageSends: ["collect:", ",", "name", "amdNamespace", "select:", "notNil"]}),
+smalltalk.AmdExporter.klass);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "exportPackageEpilogueOf:on:",
@@ -38,10 +57,12 @@ $3=$4;
 _st($2)._nextPutAll_($3);
 _st($1)._nextPutAll_("/");
 _st($1)._nextPutAll_(_st(aPackage)._name());
-_st($1)._nextPutAll_("\x22, [\x22amber_vm/smalltalk\x22,\x22amber_vm/nil\x22,\x22amber_vm/_st\x22], function(smalltalk,nil,_st){");
+_st($1)._nextPutAll_("\x22, ");
+_st($1)._nextPutAll_(_st(["amber_vm/smalltalk", "amber_vm/nil", "amber_vm/_st"].__comma(self._amdNamesOfPackages_(_st(aPackage)._loadDependencies())))._asJavascript());
+_st($1)._nextPutAll_(", function(smalltalk,nil,_st){");
 $5=_st($1)._lf();
 return self}, function($ctx1) {$ctx1.fill(self,"exportPackagePrologueOf:on:",{aPackage:aPackage,aStream:aStream},smalltalk.AmdExporter.klass)})},
-messageSends: ["nextPutAll:", "ifNil:", "amdNamespace", "name", "lf"]}),
+messageSends: ["nextPutAll:", "ifNil:", "amdNamespace", "name", "asJavascript", ",", "amdNamesOfPackages:", "loadDependencies", "lf"]}),
 smalltalk.AmdExporter.klass);
 
 smalltalk.addMethod(
@@ -1508,6 +1529,28 @@ return $1;
 messageSends: ["at:put:", "ifNil:"]}),
 smalltalk.Package);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "loadDependencies",
+fn: function (){
+var self=this;
+var root;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2,$3;
+root=_st($Object())._package();
+$1=self.__eq_eq(root);
+if(smalltalk.assert($1)){
+$2=[];
+return $2;
+} else {
+$3=[root];
+return $3;
+};
+return self}, function($ctx1) {$ctx1.fill(self,"loadDependencies",{root:root},smalltalk.Package)})},
+messageSends: ["package", "ifTrue:ifFalse:", "=="]}),
+smalltalk.Package);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "transport",

+ 58 - 5
js/Importer-Exporter.js

@@ -1,10 +1,34 @@
-define("amber/Importer-Exporter", ["amber_vm/smalltalk","amber_vm/nil","amber_vm/_st"], function(smalltalk,nil,_st){
+define("amber/Importer-Exporter", ["amber_vm/smalltalk", "amber_vm/nil", "amber_vm/_st", "amber/Kernel-Objects"], function(smalltalk,nil,_st){
 smalltalk.addPackage('Importer-Exporter');
 smalltalk.packages["Importer-Exporter"].transport = {"type":"amd","amdNamespace":"amber"};
 
-
 smalltalk.addClass('AmdExporter', smalltalk.Object, [], 'Importer-Exporter');
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "amdNamesOfPackages:",
+category: 'private',
+fn: function (anArray){
+var self=this;
+var deps,depNames;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=_st(_st(anArray)._select_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(_st(each)._amdNamespace())._notNil();
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})))._collect_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(_st(_st(each)._amdNamespace()).__comma("/")).__comma(_st(each)._name());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"amdNamesOfPackages:",{anArray:anArray,deps:deps,depNames:depNames},smalltalk.AmdExporter.klass)})},
+args: ["anArray"],
+source: "amdNamesOfPackages: anArray\x0a\x09| deps depNames |\x0a\x09^(anArray\x0a\x09\x09select: [ :each | each amdNamespace notNil ])\x0a\x09\x09collect: [ :each | each amdNamespace, '/', each name ]",
+messageSends: ["collect:", ",", "name", "amdNamespace", "select:", "notNil"],
+referencedClasses: []
+}),
+smalltalk.AmdExporter.klass);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "exportPackageEpilogueOf:on:",
@@ -44,12 +68,14 @@ $3=$4;
 _st($2)._nextPutAll_($3);
 _st($1)._nextPutAll_("/");
 _st($1)._nextPutAll_(_st(aPackage)._name());
-_st($1)._nextPutAll_("\x22, [\x22amber_vm/smalltalk\x22,\x22amber_vm/nil\x22,\x22amber_vm/_st\x22], function(smalltalk,nil,_st){");
+_st($1)._nextPutAll_("\x22, ");
+_st($1)._nextPutAll_(_st(["amber_vm/smalltalk", "amber_vm/nil", "amber_vm/_st"].__comma(self._amdNamesOfPackages_(_st(aPackage)._loadDependencies())))._asJavascript());
+_st($1)._nextPutAll_(", function(smalltalk,nil,_st){");
 $5=_st($1)._lf();
 return self}, function($ctx1) {$ctx1.fill(self,"exportPackagePrologueOf:on:",{aPackage:aPackage,aStream:aStream},smalltalk.AmdExporter.klass)})},
 args: ["aPackage", "aStream"],
-source: "exportPackagePrologueOf: aPackage on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'define(\x22';\x0a\x09\x09nextPutAll: (aPackage amdNamespace ifNil: [ 'amber' ]); \x22ifNil: only for LegacyPH, it should not happen with AmdPH\x22\x0a\x09\x09nextPutAll: '/';\x0a\x09\x09nextPutAll: aPackage name;\x0a\x09\x09nextPutAll: '\x22, [\x22amber_vm/smalltalk\x22,\x22amber_vm/nil\x22,\x22amber_vm/_st\x22], function(smalltalk,nil,_st){';\x0a\x09\x09lf",
-messageSends: ["nextPutAll:", "ifNil:", "amdNamespace", "name", "lf"],
+source: "exportPackagePrologueOf: aPackage on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'define(\x22';\x0a\x09\x09nextPutAll: (aPackage amdNamespace ifNil: [ 'amber' ]); \x22ifNil: only for LegacyPH, it should not happen with AmdPH\x22\x0a\x09\x09nextPutAll: '/';\x0a\x09\x09nextPutAll: aPackage name;\x0a\x09\x09nextPutAll: '\x22, ';\x0a\x09\x09nextPutAll: (#('amber_vm/smalltalk' 'amber_vm/nil' 'amber_vm/_st'), (self amdNamesOfPackages: aPackage loadDependencies)) asJavascript;\x0a\x09\x09nextPutAll: ', function(smalltalk,nil,_st){';\x0a\x09\x09lf",
+messageSends: ["nextPutAll:", "ifNil:", "amdNamespace", "name", "asJavascript", ",", "amdNamesOfPackages:", "loadDependencies", "lf"],
 referencedClasses: []
 }),
 smalltalk.AmdExporter.klass);
@@ -1911,6 +1937,33 @@ referencedClasses: []
 }),
 smalltalk.Package);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "loadDependencies",
+category: '*Importer-Exporter',
+fn: function (){
+var self=this;
+var root;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2,$3;
+root=_st($Object())._package();
+$1=self.__eq_eq(root);
+if(smalltalk.assert($1)){
+$2=[];
+return $2;
+} else {
+$3=[root];
+return $3;
+};
+return self}, function($ctx1) {$ctx1.fill(self,"loadDependencies",{root:root},smalltalk.Package)})},
+args: [],
+source: "loadDependencies\x0a\x09\x22Returns list of packages that need to be present\x0a\x09before loading this package.\x0a\x09These are determined as set of packages covering\x0a\x09all classes used either for subclassing or for defining\x0a\x09extension methods on.\x22\x0a\x09\x0a\x09\x22Fake one for now. TODO\x22\x0a\x09| root |\x0a\x09root := Object package.\x0a\x09self == root ifTrue: [ ^#() ] ifFalse: [ ^{root} ]",
+messageSends: ["package", "ifTrue:ifFalse:", "=="],
+referencedClasses: ["Object"]
+}),
+smalltalk.Package);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "transport",

+ 25 - 1
st/Importer-Exporter.st

@@ -17,7 +17,9 @@ exportPackagePrologueOf: aPackage on: aStream
 		nextPutAll: (aPackage amdNamespace ifNil: [ 'amber' ]); "ifNil: only for LegacyPH, it should not happen with AmdPH"
 		nextPutAll: '/';
 		nextPutAll: aPackage name;
-		nextPutAll: '", ["amber_vm/smalltalk","amber_vm/nil","amber_vm/_st"], function(smalltalk,nil,_st){';
+		nextPutAll: '", ';
+		nextPutAll: (#('amber_vm/smalltalk' 'amber_vm/nil' 'amber_vm/_st'), (self amdNamesOfPackages: aPackage loadDependencies)) asJavascript;
+		nextPutAll: ', function(smalltalk,nil,_st){';
 		lf
 !
 
@@ -31,6 +33,15 @@ exportPackageTransportOf: aPackage on: aStream
 		lf
 ! !
 
+!AmdExporter class methodsFor: 'private'!
+
+amdNamesOfPackages: anArray
+	| deps depNames |
+	^(anArray
+		select: [ :each | each amdNamespace notNil ])
+		collect: [ :each | each amdNamespace, '/', each name ]
+! !
+
 Object subclass: #ChunkExporter
 	instanceVariableNames: ''
 	package: 'Importer-Exporter'!
@@ -790,6 +801,19 @@ commitPathSt: aString
 	^ (extension ifNil: [ extension := #{} ]) at: #commitPathSt put: aString
 !
 
+loadDependencies
+	"Returns list of packages that need to be present
+	before loading this package.
+	These are determined as set of packages covering
+	all classes used either for subclassing or for defining
+	extension methods on."
+	
+	"Fake one for now. TODO"
+	| root |
+	root := Object package.
+	self == root ifTrue: [ ^#() ] ifFalse: [ ^{root} ]
+!
+
 transport
 	^ PackageHandler for: self transportType
 !