4
4
//
5
5
// 2011-12-10 GONG Chen <[email protected] >
6
6
//
7
+ #include < algorithm>
7
8
#include < boost/algorithm/string.hpp>
8
9
#include < boost/filesystem.hpp>
9
10
#include < boost/uuid/random_generator.hpp>
@@ -30,6 +31,51 @@ namespace fs = boost::filesystem;
30
31
31
32
namespace rime {
32
33
34
+ DetectModifications::DetectModifications (TaskInitializer arg) {
35
+ try {
36
+ data_dirs_ = boost::any_cast<vector<string>>(arg);
37
+ }
38
+ catch (const boost::bad_any_cast&) {
39
+ LOG (ERROR) << " DetectModifications: invalid arguments." ;
40
+ }
41
+ }
42
+
43
+ bool DetectModifications::Run (Deployer* deployer) {
44
+ time_t last_modified = 0 ;
45
+ try {
46
+ for (auto dir : data_dirs_) {
47
+ fs::path p = fs::canonical (dir);
48
+ last_modified = (std::max)(last_modified, fs::last_write_time (p));
49
+ if (fs::is_directory (p)) {
50
+ for (fs::directory_iterator iter (p), end; iter != end; ++iter) {
51
+ fs::path entry (iter->path ());
52
+ if (fs::is_regular_file (fs::canonical (entry)) &&
53
+ entry.extension ().string () == " .yaml" &&
54
+ entry.filename ().string () != " user.yaml" ) {
55
+ last_modified =
56
+ (std::max)(last_modified, fs::last_write_time (entry));
57
+ }
58
+ }
59
+ }
60
+ }
61
+ } catch (const fs::filesystem_error& ex) {
62
+ LOG (ERROR) << " Error reading file information: " << ex.what ();
63
+ return true ;
64
+ }
65
+
66
+ // TODO: store as 64-bit number to avoid the year 2038 problem
67
+ int last_build_time = 0 ;
68
+ {
69
+ the<Config> user_config (Config::Require (" config" )->Create (" user" ));
70
+ user_config->GetInt (" var/last_build_time" , &last_build_time);
71
+ }
72
+ if (last_modified > (time_t )last_build_time) {
73
+ LOG (INFO) << " modifications detected. workspace needs update." ;
74
+ return true ;
75
+ }
76
+ return false ;
77
+ }
78
+
33
79
bool InstallationUpdate::Run (Deployer* deployer) {
34
80
LOG (INFO) << " updating rime installation info." ;
35
81
fs::path shared_data_path (deployer->shared_data_dir );
@@ -188,6 +234,11 @@ bool WorkspaceUpdate::Run(Deployer* deployer) {
188
234
}
189
235
LOG (INFO) << " finished updating schemas: "
190
236
<< success << " success, " << failure << " failure." ;
237
+
238
+ the<Config> user_config (Config::Require (" config" )->Create (" user" ));
239
+ // TODO: store as 64-bit number to avoid the year 2038 problem
240
+ user_config->SetInt (" var/last_build_time" , (int )time (NULL ));
241
+
191
242
return failure == 0 ;
192
243
}
193
244
@@ -263,6 +314,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
263
314
LOG (INFO) << " patched copy of schema '" << schema_id
264
315
<< " ' is moved to trash" ;
265
316
}
317
+ // TODO: compile the config file if needs update
266
318
267
319
string dict_name;
268
320
if (!config->GetString (" translator/dictionary" , &dict_name)) {
@@ -281,6 +333,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
281
333
if (verbose_) {
282
334
dict_compiler.set_options (DictCompiler::kRebuild | DictCompiler::kDump );
283
335
}
336
+ // TODO: use compiled schema instead of the YAML file alone
284
337
if (!dict_compiler.Compile (schema_file_)) {
285
338
LOG (ERROR) << " dictionary '" << dict_name << " ' failed to compile." ;
286
339
return false ;
@@ -317,6 +370,7 @@ bool ConfigFileUpdate::Run(Deployer* deployer) {
317
370
trash)) {
318
371
LOG (INFO) << " patched copy of '" << file_name_ << " ' is moved to trash." ;
319
372
}
373
+ // TODO: compile the config file if needs update
320
374
return true ;
321
375
}
322
376
0 commit comments