src/Palete/ValaSource.vala
[app.Builder.js] / src / Palete / ValaSource.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 /**
5  * 
6  *  This just deals with spawning the compiler and getting the results.
7  * 
8  *  each window should have one of these...
9  * 
10  *  x = new ValaSource();
11  *  x.connect.compiled(... do something with results... );
12  *  
13  * x.
14  * 
15  */
16
17 namespace Palete {
18         
19         public errordomain ValaSourceError {
20                 INVALID_FORMAT 
21         }
22         
23         //public delegate  void ValaSourceResult(Json.Object res);
24         
25          
26
27         public class ValaSource : Object {
28  
29                 
30                 public signal void compiled(Json.Object res);
31
32                 
33                  
34                   
35                 JsRender.JsRender file;
36                 public int line_offset = 0;
37                 
38                 // file.project , file.path, file.build_module, ""
39                 public ValaSource(
40                         
41                 
42                 
43                         ) {
44                         base();
45                         //this.file = file;
46                  
47                 }
48                 public void dumpCode(string str) 
49                 {
50                         var ls = str.split("\n");
51                         for (var i=0;i < ls.length; i++) {
52                                 print("%d : %s\n", i+1, ls[i]);
53                         }
54                 }
55                 
56                 //public Gee.HashMap<int,string> checkFile()
57                 //{
58                 //      return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
59                 //}
60
61                 public void checkFileWithNodePropChange(
62                   
63                                         JsRender.JsRender file,
64                                         JsRender.Node node, 
65                                         string prop,
66                                         string ptype,
67                                         string val,
68                                         ValaSourceResult result_cb)
69                 {
70                         
71                         this.project = file.project;
72                         this.filepath = file.path;
73                         this.build_module = file.build_module;
74                         
75                         
76                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
77                         var hash = ptype == "listener" ? node.listeners : node.props;
78                         
79                         // untill we get a smarter renderer..
80                         // we have some scenarios where changing the value does not work
81                         if (prop == "* xns" || prop == "xtype") {
82                                 result_callback(new Json.Object());
83                                 return ;
84                         }
85                                 
86                         
87                         var old = hash.get(prop);
88                         var newval = "/*--VALACHECK-START--*/ " + val ;
89                         
90                         hash.set(prop, newval);
91                         var tmpstring = JsRender.NodeToVala.mungeFile(file);
92                         hash.set(prop, old);
93                         //print("%s\n", tmpstring);
94                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
95                         var offset =0;
96                         if (bits.length > 0) {
97                                 offset = bits[0].split("\n").length +1;
98                         }
99                         
100                         this.line_offset = offset;
101                         
102                         //this.dumpCode(tmpstring);
103                         //print("offset %d\n", offset);
104                         this.result_callback = result_cb;
105                         this.checkStringSpawn(tmpstring );
106                         
107                         // modify report
108                         
109                         
110                         
111                 }
112                 Spawn compiler;
113                 ValaSourceResult result_callback;
114                 public void checkStringSpawn(
115                                         string contents 
116                                 )
117                 {
118                         
119                         FileIOStream iostream;
120                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
121                         tmpfile.ref();
122
123                         OutputStream ostream = iostream.output_stream;
124                         DataOutputStream dostream = new DataOutputStream (ostream);
125                         dostream.put_string (contents);
126                         
127                         
128                         string[] args = {};
129                         args += BuilderApplication._self;
130                         args += "--project";
131                         args += this.project.fn;
132                         args += "--target";
133                         args += this.build_module;
134                         args += "--add-file";
135                         args +=  tmpfile.get_path();
136                         args += "--skip-file";
137                         args += this.filepath;
138                         
139                          
140                         
141                         this.compiler = new Spawn("/tmp", args);
142                         
143                         try {
144                                 this.compiler.run(spawnResult); 
145                         } catch (GLib.SpawnError e) {
146                                 var ret = new Json.Object();
147                                 ret.set_boolean_member("success", false);
148                                 ret.set_string_member("message", e.message);
149                                 this.result_callback(ret);
150                         }
151                          
152                 }
153                 public void spawnResult(int res, string output, string stderr)
154                 {
155                          
156                                 
157                         try { 
158                                 GLib.debug("GOT output %s", output);
159                                 
160                                 var pa = new Json.Parser();
161                                 pa.load_from_data(output);
162                                 var node = pa.get_root();
163
164                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
165                                         throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
166                                 }
167                                 var ret = node.get_object ();
168                                 ret.set_int_member("line_offset", this.line_offset);
169                                 if (result_callback == null) {
170                                         print ("no callback?");
171                                         return;
172                                 }
173                                 this.result_callback(ret);
174                                 
175                                 
176                         } catch (Error e) {
177                                 var ret = new Json.Object();
178                                 ret.set_boolean_member("success", false);
179                                 ret.set_string_member("message", e.message);
180                                 this.result_callback(ret);
181                         }
182                         //compiler.unref();
183                         //tmpfile.unref();
184                          
185                         
186                         
187                 }
188                  
189 }
190 /*
191 int main (string[] args) {
192
193         var a = new ValaSource(file);
194         a.create_valac_tree();
195         return 0;
196 }
197 */
198
199