Fix #8039 - library support and add back roopacker dependancy
[roobuilder] / src / Project / GtkValaSettings.vala
1 namespace Project 
2 {
3 // an object describing a build config (or generic ...)
4         public class GtkValaSettings : Object {
5                 public string name { get; set; }
6  
7                 
8                 Gtk project;
9
10                 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
11  
12
13                 public string execute_args;
14                 
15                 public bool loading_ui = true;
16                 public bool is_library = false;
17                 
18                 public GtkValaSettings(Gtk project, string name) 
19                 {
20                         this.name = name;
21                         this.project = project;
22  
23                  
24                         this.sources = new Gee.ArrayList<string>();
25                         this.execute_args = "";
26                                 
27                 }
28                  
29                 public GtkValaSettings.from_json(Gtk project, Json.Object el) {
30
31                         this.project = project;
32                         this.name = el.get_string_member("name");
33                         if (el.has_member("is_library")) {
34                                 this.is_library = el.get_boolean_member("is_library");
35                         }
36                         if ( el.has_member("execute_args")) {
37                                 this.execute_args = el.get_string_member("execute_args");
38                         } else {
39                                 this.execute_args = "";
40                    }
41                         // sources and packages.
42                         this.sources = this.filterFiles(this.project.readArray(el.get_array_member("sources")));
43                         
44
45                 }
46                 
47                 // why not array of strings?
48                 
49                 
50                 
51                 public Json.Object toJson()
52                 {
53                         var ret = new Json.Object();
54                         ret.set_string_member("name", this.name);
55                         ret.set_boolean_member("is_library", this.is_library);
56                         ret.set_string_member("execute_args", this.execute_args);
57  
58                         ret.set_array_member("sources", this.writeArray( this.filterFiles(this.sources)));
59  
60
61                         return ret;
62                 }
63                 public Json.Array writeArray(Gee.ArrayList<string> ar) {
64                         var ret = new Json.Array();
65                         for(var i =0; i< ar.size; i++) {
66                                 ret.add_string_element(ar.get(i));
67                         }
68                         return ret;
69                 }
70                 public bool has_file(JsRender.JsRender file)
71                 {
72                         
73                         //GLib.debug("Checking %s has file %s", this.name, file.path);
74                         var pr = (Gtk) file.project;
75                         for(var i = 0; i < this.sources.size;i++) {
76                                 var path = pr.path + "/" +  this.sources.get(i);
77                                 //GLib.debug("check %s =%s or %s", path , file.path, file.targetName());
78                                 
79                                 if (path == file.path || path == file.targetName()) {
80                                         //GLib.debug("GOT IT");
81                                         return true;
82                                 }
83                         }
84                         //GLib.debug("CANT FIND IT");
85                         return false;
86                 
87                 }
88                 
89                 public Gee.ArrayList<string> filterFiles( Gee.ArrayList<string> ar)
90                 {
91                         var ret = new Gee.ArrayList<string>();
92                         foreach(var f in ar) {
93                                 if (null == this.project.getByRelPath(f)) {
94                                         continue;
95                                 }
96                                 ret.add(f);
97                         }
98                         return ret;
99                 }
100                 
101                 public string writeMesonExe(string resources)
102                 {
103                 
104                         var cgname = this.name;
105                         if (!this.is_library) {
106                                 return @"
107 $cgname = executable('$cgname',
108     dependencies: deps,
109     sources: [  $(cgname)_src $resources ],
110     install: true
111 )
112 ";
113                         }
114                         string[]  deps = {};
115                         foreach(var p in this.project.packages) {
116                                 if (p == "posix" ) {
117                                         continue;
118                                 } 
119                                 deps += "'" + p  + "'";
120                                 
121                         }
122                         var depstr = deps.length < 1 ? "" : ( "["  + string.joinv(",", deps) + "]");
123
124                         
125                         var version = this.project.version;
126                         // it's a library..
127                         return @"
128 $(cgname)_lib = shared_library('$cgname',  
129     sources : [ $(cgname)_src $resources ],
130     vala_vapi: '$(cgname)-$(version).vapi',
131     dependencies: deps,
132     install: true,
133     install_dir: [true, true, true]
134 )
135 pkg = import('pkgconfig')
136 pkg.generate( $(cgname)_lib,
137     filebase: '$(cgname)-$(version)',
138     requires : $(depstr)
139 )
140
141 ";
142
143                 
144                 }
145                 
146         }
147  }