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