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