source: libi4uc/i4ucprofilebuilder.vala @ 6e9e55b4c06d6ff5ffa577ade54d8fa719940394

Revision 6e9e55b4c06d6ff5ffa577ade54d8fa719940394, 6.4 KB checked in by Matias De la Puente <mfpuente.ar@…>, 3 years ago (diff)

Builder: Add support for 'post build command'

This command is executed after the build command

  • Property mode set to 100644
Line 
1/* i4ucprofilebuilder.vala
2 *
3 * Copyright (C) 2010  Matias De la Puente
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author:
19 *      Matias De la Puente <mfpuente.ar@gmail.com>
20 */
21using Gee;
22
23public class I4uc.ProfileBuilder : GLib.Object
24{
25        private Command _command = new Command ();
26        private ArrayList<string> _commands = new ArrayList<string> ();
27        private int _command_index = 0;
28        private string _command_output;
29        private ProjectProfile _profile;
30       
31        public bool is_building { private set; get; }
32        public Project project { set; get; }
33
34        public string profile
35        {
36                set { _profile = _project.profiles[value]; }
37                get { return _profile.name; }
38        }
39
40        public signal void command_started (string command);
41        public signal void command_finished (string output);
42
43        public ProfileBuilder ()
44        {
45                _command.stopped.connect (on_command_stopped);
46                _command.stdout_message_added.connect (on_message_added);
47                _command.stderror_message_added.connect (on_message_added);
48        }
49
50        public void build () throws GLib.Error
51        {
52                if (_is_building)
53                        return;
54               
55                var builder = I4uc.Settings.instance.builders[_profile.builder_id];
56                var device_type = builder.device_types[_profile.device_type];
57               
58                var objects = new ArrayList<string> ();
59               
60                _commands.clear ();
61                foreach (var source in _profile.files)
62                {
63                        var source_type = device_type.get_source_type_from_filename (source);
64                        if (source_type == null)
65                                continue;
66                       
67                        var object = source[0:-source_type.source_type.length] + "-" + _profile.name + source_type.object_type;
68                        objects.add (object);
69                       
70                        var source_uri = project.get_file_uri (source);
71                        var object_uri = project.get_file_uri (object);
72                       
73                        if (!FileUtils.test (Filename.from_uri (object_uri), FileTest.EXISTS) || check_mtime (source_uri, object_uri))
74                        {
75                                var command = source_type.build_command;
76                                command = command.replace ("%device", _profile.device);
77                                command = command.replace ("%source", "\"" + source + "\"");
78                                command = command.replace ("%object", object.replace (" ", "_"));
79                                command = command.replace ("%project", _project.name.replace (" ", "_"));
80                                command = command.replace ("%profile", _profile.name.replace (" ", "_"));
81                               
82                                _commands.add (command);
83                        }
84                }
85               
86                if (objects.size == 0)
87                        return;
88               
89                var hexfile = _project.name + "-" + _profile.name + ".hex";
90                var hexfile_file = File.new_for_uri (_project.get_file_uri (hexfile));
91
92                if (hexfile_file.query_exists (null) && _commands.size == 0)
93                        return;
94
95                var objects_string = "";
96                foreach (var object in objects)
97                        objects_string +=  object.replace (" ", "_") + " ";
98               
99                var command = device_type.build_command;
100                command = command.replace ("%device", _profile.device);
101                command = command.replace ("%objects", objects_string);
102                command = command.replace ("%hexfile", hexfile.replace (" ", "_"));
103                command = command.replace ("%project", _project.name.replace (" ", "_"));
104                command = command.replace ("%profile", _profile.name.replace (" ", "_"));
105               
106                _commands.add (command);
107               
108                if (device_type.has_post_build_command)
109                {
110                        command = device_type.post_build_command;
111                        command = command.replace ("%device", _profile.device);
112                        command = command.replace ("%objects", objects_string);
113                        command = command.replace ("%hexfile", hexfile.replace (" ", "_"));
114                        command = command.replace ("%project", _project.name.replace (" ", "_"));
115                        command = command.replace ("%profile", _profile.name.replace (" ", "_"));
116                       
117                        _commands.add (command);
118                }
119               
120                on_command_stopped (true);
121        }
122       
123        public void clean () throws GLib.Error
124        {
125                if (_is_building)
126                        return;
127
128                var builder = I4uc.Settings.instance.builders[_profile.builder_id];
129                var device_type = builder.device_types[_profile.device_type];
130               
131                foreach (var source in _profile.files)
132                {
133                        var source_type = device_type.get_source_type_from_filename (source);
134                        if (source_type == null)
135                                continue;
136                       
137                        foreach (var clean_type in source_type.clean_types)
138                        {
139                                var filename = source[0:-source_type.source_type.length] + "-" + _profile.name + clean_type;
140                                var file = File.new_for_uri (_project.get_file_uri (filename.replace (" ", "_")));
141                                if (file.query_exists (null))
142                                        file.delete (null);
143                        }
144                }
145               
146                foreach (var clean_type in device_type.clean_types)
147                {
148                        var filename = _project.name + "-" + _profile.name + clean_type;
149                        var file = File.new_for_uri (_project.get_file_uri (filename.replace (" ", "_")));
150                        if (file.query_exists (null))
151                                file.delete (null);
152                }
153        }
154
155        public void stop ()
156        {
157                _commands.clear ();
158                _command.stop ();
159        }
160
161        private void on_command_stopped (bool exited_ok)
162        {
163                if (_command_index != 0)
164                        this.command_finished (_command_output);
165                if (_command_index < _commands.size && exited_ok)
166                {
167                        var folder_file = File.new_for_uri (_project.folder_uri);
168                        _command_output = "";
169                        if (_command.run (_commands[_command_index], folder_file.get_path ()))
170                        {
171                                this.is_building = true;
172                                this.command_started (_commands[_command_index]);
173                                _command_index++;
174                        }
175                }
176                else
177                {
178                        this.is_building = false;
179                        _command_index = 0;
180                }
181        }
182
183        private void on_message_added (string message)
184        {
185                _command_output += message + "\n";
186        }
187
188        private bool check_mtime (string newer_uri, string older_uri) throws GLib.Error
189        {
190                var newer_file = File.new_for_uri (newer_uri);
191                var older_file = File.new_for_uri (older_uri);
192               
193                var newer_info = newer_file.query_info ("time::*", FileQueryInfoFlags.NONE, null);
194                var older_info = older_file.query_info ("time::*", FileQueryInfoFlags.NONE, null);
195               
196                var newer_time = newer_info.get_attribute_uint64 (FILE_ATTRIBUTE_TIME_CHANGED);
197                var newer_time_usec = newer_info.get_attribute_uint32 (FILE_ATTRIBUTE_TIME_CHANGED_USEC);
198                var older_time = older_info.get_attribute_uint64 (FILE_ATTRIBUTE_TIME_CHANGED);
199                var older_time_usec = older_info.get_attribute_uint32 (FILE_ATTRIBUTE_TIME_CHANGED_USEC);
200               
201                return (newer_time > older_time) || (newer_time == older_time && newer_time_usec > older_time_usec);
202        }
203}
Note: See TracBrowser for help on using the repository browser.