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