summaryrefslogtreecommitdiffstats
path: root/src/builder.vala
blob: 04c1ed5211d357eb1f96aae406776afe0f1418b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
namespace Wixl {

    class WixBuilder: WixElementVisitor {

        public WixBuilder (WixRoot root) {
            this.root = root;
        }

        WixRoot root;
        MsiDatabase db;

        List<File> path;
        public void add_path (string p) {
            var file = File.new_for_path (p);
            path.append (file);
        }

        delegate void AddSequence (string action, int sequence) throws GLib.Error;

        void sequence_actions () throws GLib.Error {
            AddSequence add = (action, sequence) => {
                db.table_admin_execute_sequence.add (action, sequence);
            };
            add ("CostInitialize", 800);
            add ("FileCost", 900);
            add ("CostFinalize", 1000);
            add ("InstallValidate", 1400);
            add ("InstallInitialize", 1500);
            add ("InstallAdminPackage", 3900);
            add ("InstallFiles", 4000);
            add ("InstallFinalize", 6600);

            add = (action, sequence) => {
                db.table_admin_ui_sequence.add (action, sequence);
            };
            add ("CostInitialize", 800);
            add ("FileCost", 900);
            add ("CostFinalize", 1000);
            add ("ExecuteAction", 1300);

            add = (action, sequence) => {
                db.table_advt_execute_sequence.add (action, sequence);
            };
            add ("CostInitialize", 800);
            add ("CostFinalize", 1000);
            add ("InstallValidate", 1400);
            add ("InstallInitialize", 1500);
            if (db.table_shortcut.records.length () > 0)
                add ("CreateShortcuts", 4500);
            add ("PublishFeatures", 6300);
            add ("PublishProduct", 6400);
            add ("InstallFinalize", 6600);

            add = (action, sequence) => {
                db.table_install_execute_sequence.add (action, sequence);
            };
            add ("ValidateProductID", 700);
            add ("CostInitialize", 800);
            add ("FileCost", 900);
            add ("CostFinalize", 1000);
            add ("InstallValidate", 1400);
            add ("InstallInitialize", 1500);
            add ("ProcessComponents", 1600);
            add ("UnpublishFeatures", 1800);
            if (db.table_registry.records.length () > 0) {
                add ("RemoveRegistryValues", 2600);
                add ("WriteRegistryValues", 5000);
            }
            if (db.table_shortcut.records.length () > 0)
                add ("RemoveShortcuts", 3200);
            if (db.table_remove_file.records.length () > 0)
                add ("RemoveFiles", 3500);
            if (db.table_file.records.length () > 0)
                add ("InstallFiles", 4000);
            if (db.table_shortcut.records.length () > 0)
                add ("CreateShortcuts", 4500);
            add ("RegisterUser", 6000);
            add ("RegisterProduct", 6100);
            add ("PublishFeatures", 6300);
            add ("PublishProduct", 6400);
            add ("InstallFinalize", 6600);

            add = (action, sequence) => {
                db.table_install_ui_sequence.add (action, sequence);
            };
            add ("ValidateProductID", 700);
            add ("CostInitialize", 800);
            add ("FileCost", 900);
            add ("CostFinalize", 1000);
            add ("ExecuteAction", 1300);
        }

        public void build_cabinet () throws GLib.Error {
            var sequence = 0;
            var medias = root.get_elements<WixMedia> ();
            var files = root.get_elements<WixFile> ();

            foreach (var m in medias) {
                var folder = new GCab.Folder (GCab.Compression.MSZIP);

                foreach (var f in files) {
                    if (f.DiskId != m.Id)
                        continue;

                    folder.add_file (new GCab.File.with_file (f.Id, File.new_for_path (f.Name)), false);
                    var rec = f.record;
                    sequence += 1;
                    MsiTableFile.set_sequence (rec, sequence);
                }

                var cab = new GCab.Cabinet ();
                cab.add_folder (folder);
                var output = new MemoryOutputStream (null, realloc, free);
                cab.write (output, null, null, null);
                var input = new MemoryInputStream.from_data (output.get_data ()[0:output.data_size], null);
                if (parse_yesno (m.EmbedCab))
                    db.table_streams.add (m.Cabinet, input, output.data_size);

                db.table_media.set_last_sequence (m.record, sequence);
            }
        }

        public void shortcut_target () throws GLib.Error {
            var shortcuts = root.get_elements<WixShortcut> ();

            foreach (var sc in shortcuts) {
                var component = sc.get_component ();
                var feature = component.in_feature.first ().data;
                MsiTableShortcut.set_target (sc.record, feature.Id);
            }
        }

        public MsiDatabase build () throws GLib.Error {
            db = new MsiDatabase ();

            root.accept (this);
            shortcut_target ();
            sequence_actions ();
            build_cabinet ();

            return db;
        }

        public override void visit_product (WixProduct product) throws GLib.Error {
            db.info.set_codepage (int.parse (product.Codepage));
            db.info.set_author (product.Manufacturer);

            db.table_property.add ("Manufacturer", product.Manufacturer);
            db.table_property.add ("ProductLanguage", product.Language);
            db.table_property.add ("ProductCode", add_braces (product.Id));
            db.table_property.add ("ProductName", product.Name);
            db.table_property.add ("ProductVersion", product.Version);
            db.table_property.add ("UpgradeCode", add_braces (product.UpgradeCode));
        }

        public override void visit_package (WixPackage package) throws GLib.Error {
            db.info.set_keywords (package.Keywords);
            db.info.set_subject (package.Description);
            db.info.set_comments (package.Comments);
        }

        public override void visit_icon (WixIcon icon) throws GLib.Error {
            db.table_icon.add (icon.Id, icon.SourceFile);
        }

        public override void visit_property (WixProperty prop) throws GLib.Error {
            db.table_property.add (prop.Id, prop.Value);
        }

        public override void visit_media (WixMedia media) throws GLib.Error {
            var cabinet = media.Cabinet;

            if (parse_yesno (media.EmbedCab))
                cabinet = "#" + cabinet;

            var rec = db.table_media.add (media.Id, media.DiskPrompt, cabinet);
            media.record = rec;
        }

        public override void visit_directory (WixDirectory dir) throws GLib.Error {
            if (dir.parent.get_type () == typeof (WixProduct)) {
                if (dir.Id != "TARGETDIR")
                    throw new Wixl.Error.FAILED ("Invalid root directory");
                db.table_directory.add (dir.Id, null, dir.Name);
            } else if (dir.parent.get_type () == typeof (WixDirectory)) {
                var parent = dir.parent as WixDirectory;
                db.table_directory.add (dir.Id, parent.Id, dir.Name);
            } else
                warning ("unhandled parent type %s", dir.parent.name);
        }

        [Flags]
        enum ComponentAttribute {
            LOCAL_ONLY = 0,
            SOURCE_ONLY,
            OPTIONAL,
            REGISTRY_KEY_PATH,
            SHARED_DLL_REF_COUNT,
            PERMANENT,
            ODBC_DATA_SOURCE,
            TRANSITIVE,
            NEVER_OVERWRITE,
            64BIT,
            REGISTRY_REFLECTION,
            UNINSTALL_ON_SUPERSEDENCE,
            SHARED,
        }

        public override void visit_component (WixComponent comp) throws GLib.Error {
            var attr = 0;

            if (comp.key is WixRegistryValue)
                attr |= ComponentAttribute.REGISTRY_KEY_PATH;

            if (comp.parent.get_type () == typeof (WixDirectory)) {
                var parent = comp.parent as WixDirectory;
                db.table_component.add (comp.Id, add_braces (comp.Guid), parent.Id, attr,
                                        comp.key != null ? comp.key.Id : null);
            } else
                warning ("unhandled parent type %s", comp.parent.name);
        }

        public override void visit_feature (WixFeature feature) throws GLib.Error {
            db.table_feature.add (feature.Id, 2, int.parse (feature.Level), 0);
        }

        public override void visit_component_ref (WixComponentRef ref) throws GLib.Error {
            if (ref.parent is WixFeature) {
                var feature = ref.parent as WixFeature;

                var component = root.find_element (typeof (WixComponent), @ref.Id) as WixComponent;
                component.in_feature.append (feature);
                db.table_feature_components.add (feature.Id, @ref.Id);
            } else
                warning ("unhandled parent type %s", @ref.parent.name);
        }

        enum RemoveFileInstallMode {
            INSTALL = 1,
            UNINSTALL,
            BOTH
        }

        public override void visit_remove_folder (WixRemoveFolder rm) throws GLib.Error {
            var on = enum_from_string (typeof (RemoveFileInstallMode), rm.On);
            var comp = rm.parent as WixComponent;
            var dir = comp.parent as WixDirectory;

            db.table_remove_file.add (rm.Id, comp.Id, dir.Id, on);
        }

        void visit_key_element (WixKeyElement key) throws GLib.Error {
            var component = key.parent as WixComponent;

            if (!parse_yesno (key.KeyPath))
                return;

            component.key = key;
        }

        enum RegistryValueType {
            STRING,
            INTEGER,
            BINARY,
            EXPANDABLE,
            MULTI_STRING
        }

        enum RegistryRoot {
            HKCR,
            HKCU,
            HKLM,
            HKU,
            HKMU
        }

        public override void visit_registry_value (WixRegistryValue reg) throws GLib.Error {
            var comp = reg.parent as WixComponent;
            var value = reg.Value;
            var t = enum_from_string (typeof (RegistryValueType), reg.Type);
            var r = enum_from_string (typeof (RegistryRoot), reg.Root.down ());
            if (reg.Id == null) {
                reg.Id = generate_id ("reg", 4,
                                      comp.Id,
                                      reg.Root,
                                      reg.Key != null ? reg.Key.down () : null,
                                      reg.Name != null ? reg.Name.down () : null);
            }

            switch (t) {
            case RegistryValueType.STRING:
                value = value[0] == '#' ? "#" + value : value;
                break;
            }

            db.table_registry.add (reg.Id, r, reg.Key, comp.Id);

            visit_key_element (reg);
        }

        [Flags]
        enum FileAttribute {
            READ_ONLY = 1 << 0,
            HIDDEN = 1 << 1,
            SYSTEM = 1 << 2,
            VITAL = 1 << 9,
            CHECKSUM = 1 << 10,
            PATCH_ADDED = 1 << 11,
            NON_COMPRESSED = 1 << 12,
            COMPRESSED = 1 << 13
        }

        public override void visit_file (WixFile file) throws GLib.Error {
            return_if_fail (file.DiskId == "1");

            var comp = file.parent as WixComponent;
            var gfile = File.new_for_path (file.Name);
            var info = gfile.query_info ("standard::*", 0, null);
            var attr = FileAttribute.VITAL;

            var rec = db.table_file.add (file.Id, comp.Id, file.Name, (int)info.get_size (), attr);
            file.record = rec;

            visit_key_element (file);
        }

        public override void visit_shortcut (WixShortcut shortcut) throws GLib.Error {
            if (!parse_yesno (shortcut.Advertise))
                throw new Wixl.Error.FIXME ("unimplemented");

            var component = shortcut.get_component ();
            var rec = db.table_shortcut.add (shortcut.Id, shortcut.Directory, shortcut.Name, component.Id);
            shortcut.record = rec;

            if (shortcut.Icon != null)
                MsiTableShortcut.set_icon (rec, shortcut.Icon, int.parse (shortcut.IconIndex));
            if (shortcut.WorkingDirectory != null)
                MsiTableShortcut.set_working_dir (rec, shortcut.WorkingDirectory);
        }

        public override void visit_create_folder (WixCreateFolder folder) throws GLib.Error {
        }
    }

} // Wixl