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