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  */
9
10 namespace Palete {
11         
12         public errordomain ValaSourceError {
13                 INVALID_FORMAT 
14         }
15         
16         public delegate  void ValaSourceResult(Json.Object res);
17         
18          
19
20         public class ValaSource : Object {
21  
22
23                 Vala.CodeContext context;
24                  
25                 Project.Gtk project;
26                 public string build_module;
27                 public string filepath;
28                 public string original_filepath;
29                 public int line_offset = 0;
30                 
31                 // file.project , file.path, file.build_module, ""
32                 public ValaSource(
33                         Project.Gtk project, 
34                         string filepath, 
35                         string build_module, 
36                         string original_filepath) {
37                         base();
38                         //this.file = file;
39                         this.filepath = filepath;
40                         this.build_module = build_module;
41                         this.original_filepath = original_filepath;
42                         this.project =  project;
43                         
44                 }
45                 public void dumpCode(string str) 
46                 {
47                         var ls = str.split("\n");
48                         for (var i=0;i < ls.length; i++) {
49                                 print("%d : %s\n", i+1, ls[i]);
50                         }
51                 }
52                 
53                 //public Gee.HashMap<int,string> checkFile()
54                 //{
55                 //      return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
56                 //}
57
58                 public void checkFileWithNodePropChange(
59                                         JsRender.JsRender file,
60                                         JsRender.Node node, 
61                                         string prop,
62                                         string ptype,
63                                         string val,
64                                         ValaSourceResult result_cb)
65                 {
66                         
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