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