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