| 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 | */ |
|---|
| 21 | using Gee; |
|---|
| 22 | |
|---|
| 23 | public 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 | |
|---|
| 80 | _commands.add (command); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | if (objects.size == 0) |
|---|
| 85 | return; |
|---|
| 86 | |
|---|
| 87 | var hexfile = _project.name + "-" + _profile.name + ".hex"; |
|---|
| 88 | var hexfile_file = File.new_for_uri (_project.get_file_uri (hexfile)); |
|---|
| 89 | |
|---|
| 90 | if (hexfile_file.query_exists (null) && _commands.size == 0) |
|---|
| 91 | return; |
|---|
| 92 | |
|---|
| 93 | var objects_string = ""; |
|---|
| 94 | foreach (var object in objects) |
|---|
| 95 | objects_string += object.replace (" ", "_") + " "; |
|---|
| 96 | |
|---|
| 97 | var command = device_type.build_command; |
|---|
| 98 | command = command.replace ("%device", _profile.device); |
|---|
| 99 | command = command.replace ("%objects", objects_string); |
|---|
| 100 | command = command.replace ("%hexfile", hexfile.replace (" ", "_")); |
|---|
| 101 | |
|---|
| 102 | _commands.add (command); |
|---|
| 103 | on_command_stopped (true); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | public void clean () throws GLib.Error |
|---|
| 107 | { |
|---|
| 108 | if (_is_building) |
|---|
| 109 | return; |
|---|
| 110 | |
|---|
| 111 | var builder = I4uc.Settings.instance.builders[_profile.builder_id]; |
|---|
| 112 | var device_type = builder.device_types[_profile.device_type]; |
|---|
| 113 | |
|---|
| 114 | foreach (var source in _profile.files) |
|---|
| 115 | { |
|---|
| 116 | var source_type = device_type.get_source_type_from_filename (source); |
|---|
| 117 | if (source_type == null) |
|---|
| 118 | continue; |
|---|
| 119 | |
|---|
| 120 | foreach (var clean_type in source_type.clean_types) |
|---|
| 121 | { |
|---|
| 122 | var filename = source[0:-source_type.source_type.length] + "-" + _profile.name + clean_type; |
|---|
| 123 | var file = File.new_for_uri (_project.get_file_uri (filename.replace (" ", "_"))); |
|---|
| 124 | if (file.query_exists (null)) |
|---|
| 125 | file.delete (null); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | foreach (var clean_type in device_type.clean_types) |
|---|
| 130 | { |
|---|
| 131 | var filename = _project.name + "-" + _profile.name + clean_type; |
|---|
| 132 | var file = File.new_for_uri (_project.get_file_uri (filename.replace (" ", "_"))); |
|---|
| 133 | if (file.query_exists (null)) |
|---|
| 134 | file.delete (null); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | public void stop () |
|---|
| 139 | { |
|---|
| 140 | _commands.clear (); |
|---|
| 141 | _command.stop (); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | private void on_command_stopped (bool exited_ok) |
|---|
| 145 | { |
|---|
| 146 | if (_command_index != 0) |
|---|
| 147 | this.command_finished (_command_output); |
|---|
| 148 | if (_command_index < _commands.size && exited_ok) |
|---|
| 149 | { |
|---|
| 150 | var folder_file = File.new_for_uri (_project.folder_uri); |
|---|
| 151 | _command_output = ""; |
|---|
| 152 | if (_command.run (_commands[_command_index], folder_file.get_path ())) |
|---|
| 153 | { |
|---|
| 154 | this.is_building = true; |
|---|
| 155 | this.command_started (_commands[_command_index]); |
|---|
| 156 | _command_index++; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | else |
|---|
| 160 | { |
|---|
| 161 | this.is_building = false; |
|---|
| 162 | _command_index = 0; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | private void on_message_added (string message) |
|---|
| 167 | { |
|---|
| 168 | _command_output += message + "\n"; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | private bool check_mtime (string newer_uri, string older_uri) throws GLib.Error |
|---|
| 172 | { |
|---|
| 173 | var newer_file = File.new_for_uri (newer_uri); |
|---|
| 174 | var older_file = File.new_for_uri (older_uri); |
|---|
| 175 | |
|---|
| 176 | var newer_info = newer_file.query_info ("time::*", FileQueryInfoFlags.NONE, null); |
|---|
| 177 | var older_info = older_file.query_info ("time::*", FileQueryInfoFlags.NONE, null); |
|---|
| 178 | |
|---|
| 179 | var newer_time = newer_info.get_attribute_uint64 (FILE_ATTRIBUTE_TIME_CHANGED); |
|---|
| 180 | var newer_time_usec = newer_info.get_attribute_uint32 (FILE_ATTRIBUTE_TIME_CHANGED_USEC); |
|---|
| 181 | var older_time = older_info.get_attribute_uint64 (FILE_ATTRIBUTE_TIME_CHANGED); |
|---|
| 182 | var older_time_usec = older_info.get_attribute_uint32 (FILE_ATTRIBUTE_TIME_CHANGED_USEC); |
|---|
| 183 | |
|---|
| 184 | return (newer_time > older_time) || (newer_time == older_time && newer_time_usec > older_time_usec); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|