Skip to content

Commit

Permalink
feat(config): save __build_info in compiled config
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Feb 3, 2018
1 parent bc66a47 commit 45a7337
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/rime/config/build_info_plugin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//
#include <boost/filesystem.hpp>
#include <rime/service.h>
#include <rime/config/config_compiler.h>
#include <rime/config/config_types.h>
#include <rime/config/plugins.h>

namespace rime {

bool BuildInfoPlugin::ReviewCompileOutput(
ConfigCompiler* compiler, an<ConfigResource> resource) {
return true;
}

bool BuildInfoPlugin::ReviewLinkOutput(
ConfigCompiler* compiler, an<ConfigResource> resource) {
auto build_info = (*resource)["__build_info"];
build_info["rime_version"] = RIME_VERSION;
auto timestamps = build_info["timestamps"];
compiler->EnumerateResources([&](an<ConfigResource> 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
7 changes: 7 additions & 0 deletions src/rime/config/config_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ void ConfigCompiler::Pop() {
graph_->Pop();
}

void ConfigCompiler::EnumerateResources(
function<void (an<ConfigResource> resource)> process_resource) {
for (const auto& r : graph_->resources) {
process_resource(r.second);
}
}

an<ConfigResource> ConfigCompiler::GetCompiledResource(
const string& resource_id) const {
return graph_->resources[resource_id];
Expand Down
2 changes: 2 additions & 0 deletions src/rime/config/config_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class ConfigCompiler {
bool Parse(const string& key, const an<ConfigItem>& item);
void Pop();

void EnumerateResources(
function<void (an<ConfigResource> resource)> process_resource);
an<ConfigResource> GetCompiledResource(const string& resource_id) const;
an<ConfigResource> Compile(const string& file_name);
bool Link(an<ConfigResource> target);
Expand Down
6 changes: 6 additions & 0 deletions src/rime/config/plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
1 change: 1 addition & 0 deletions src/rime/core_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 45a7337

Please sign in to comment.