From 45a73375e61dba2959c064bfa6eea009b8cf4473 Mon Sep 17 00:00:00 2001 From: Chen Gong Date: Mon, 29 Jan 2018 00:37:16 +0800 Subject: [PATCH] feat(config): save __build_info in compiled config --- src/rime/config/build_info_plugin.cc | 39 ++++++++++++++++++++++++++++ src/rime/config/config_compiler.cc | 7 +++++ src/rime/config/config_compiler.h | 2 ++ src/rime/config/plugins.h | 6 +++++ src/rime/core_module.cc | 1 + 5 files changed, 55 insertions(+) create mode 100644 src/rime/config/build_info_plugin.cc diff --git a/src/rime/config/build_info_plugin.cc b/src/rime/config/build_info_plugin.cc new file mode 100644 index 000000000..238af9f1c --- /dev/null +++ b/src/rime/config/build_info_plugin.cc @@ -0,0 +1,39 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// +#include +#include +#include +#include +#include + +namespace rime { + +bool BuildInfoPlugin::ReviewCompileOutput( + ConfigCompiler* compiler, an resource) { + return true; +} + +bool BuildInfoPlugin::ReviewLinkOutput( + ConfigCompiler* compiler, an resource) { + auto build_info = (*resource)["__build_info"]; + build_info["rime_version"] = RIME_VERSION; + auto timestamps = build_info["timestamps"]; + compiler->EnumerateResources([&](an resource) { + if (!resource->loaded) { + LOG(WARNING) << "resource '" << resource->resource_id << "' not loaded."; + return; + } + auto file_name = resource->data->file_name(); + if (file_name.empty()) { + return; + } + // TODO: store as 64-bit number to avoid the year 2038 problem + timestamps[resource->resource_id] = + (int) boost::filesystem::last_write_time(file_name); + }); + return true; +} + +} // namespace rime diff --git a/src/rime/config/config_compiler.cc b/src/rime/config/config_compiler.cc index 9a4ebcd57..8af3de032 100644 --- a/src/rime/config/config_compiler.cc +++ b/src/rime/config/config_compiler.cc @@ -295,6 +295,13 @@ void ConfigCompiler::Pop() { graph_->Pop(); } +void ConfigCompiler::EnumerateResources( + function resource)> process_resource) { + for (const auto& r : graph_->resources) { + process_resource(r.second); + } +} + an ConfigCompiler::GetCompiledResource( const string& resource_id) const { return graph_->resources[resource_id]; diff --git a/src/rime/config/config_compiler.h b/src/rime/config/config_compiler.h index 26b1595ea..a08fd0169 100644 --- a/src/rime/config/config_compiler.h +++ b/src/rime/config/config_compiler.h @@ -64,6 +64,8 @@ class ConfigCompiler { bool Parse(const string& key, const an& item); void Pop(); + void EnumerateResources( + function resource)> process_resource); an GetCompiledResource(const string& resource_id) const; an Compile(const string& file_name); bool Link(an target); diff --git a/src/rime/config/plugins.h b/src/rime/config/plugins.h index 5ffcb5066..36691fcfe 100644 --- a/src/rime/config/plugins.h +++ b/src/rime/config/plugins.h @@ -45,6 +45,12 @@ class LegacyDictionaryConfigPlugin : public ConfigCompilerPlugin { Review ReviewLinkOutput; }; +class BuildInfoPlugin : public ConfigCompilerPlugin { + public: + Review ReviewCompileOutput; + Review ReviewLinkOutput; +}; + } // namespace rime #endif // RIME_CONFIG_PLUGINS_H_ diff --git a/src/rime/core_module.cc b/src/rime/core_module.cc index 33e7c4301..907d97077 100644 --- a/src/rime/core_module.cc +++ b/src/rime/core_module.cc @@ -25,6 +25,7 @@ static void rime_core_initialize() { config->InstallPlugin(new DefaultConfigPlugin); config->InstallPlugin(new LegacyPresetConfigPlugin); config->InstallPlugin(new LegacyDictionaryConfigPlugin); + config->InstallPlugin(new BuildInfoPlugin); r.Register("config", config); r.Register("schema", new SchemaComponent(config)); }