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