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