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