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