Skip to content

Commit 45a7337

Browse files
committed
feat(config): save __build_info in compiled config
1 parent bc66a47 commit 45a7337

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

src/rime/config/build_info_plugin.cc

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright RIME Developers
3+
// Distributed under the BSD License
4+
//
5+
#include <boost/filesystem.hpp>
6+
#include <rime/service.h>
7+
#include <rime/config/config_compiler.h>
8+
#include <rime/config/config_types.h>
9+
#include <rime/config/plugins.h>
10+
11+
namespace rime {
12+
13+
bool BuildInfoPlugin::ReviewCompileOutput(
14+
ConfigCompiler* compiler, an<ConfigResource> resource) {
15+
return true;
16+
}
17+
18+
bool BuildInfoPlugin::ReviewLinkOutput(
19+
ConfigCompiler* compiler, an<ConfigResource> resource) {
20+
auto build_info = (*resource)["__build_info"];
21+
build_info["rime_version"] = RIME_VERSION;
22+
auto timestamps = build_info["timestamps"];
23+
compiler->EnumerateResources([&](an<ConfigResource> resource) {
24+
if (!resource->loaded) {
25+
LOG(WARNING) << "resource '" << resource->resource_id << "' not loaded.";
26+
return;
27+
}
28+
auto file_name = resource->data->file_name();
29+
if (file_name.empty()) {
30+
return;
31+
}
32+
// TODO: store as 64-bit number to avoid the year 2038 problem
33+
timestamps[resource->resource_id] =
34+
(int) boost::filesystem::last_write_time(file_name);
35+
});
36+
return true;
37+
}
38+
39+
} // namespace rime

src/rime/config/config_compiler.cc

+7
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ void ConfigCompiler::Pop() {
295295
graph_->Pop();
296296
}
297297

298+
void ConfigCompiler::EnumerateResources(
299+
function<void (an<ConfigResource> resource)> process_resource) {
300+
for (const auto& r : graph_->resources) {
301+
process_resource(r.second);
302+
}
303+
}
304+
298305
an<ConfigResource> ConfigCompiler::GetCompiledResource(
299306
const string& resource_id) const {
300307
return graph_->resources[resource_id];

src/rime/config/config_compiler.h

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class ConfigCompiler {
6464
bool Parse(const string& key, const an<ConfigItem>& item);
6565
void Pop();
6666

67+
void EnumerateResources(
68+
function<void (an<ConfigResource> resource)> process_resource);
6769
an<ConfigResource> GetCompiledResource(const string& resource_id) const;
6870
an<ConfigResource> Compile(const string& file_name);
6971
bool Link(an<ConfigResource> target);

src/rime/config/plugins.h

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class LegacyDictionaryConfigPlugin : public ConfigCompilerPlugin {
4545
Review ReviewLinkOutput;
4646
};
4747

48+
class BuildInfoPlugin : public ConfigCompilerPlugin {
49+
public:
50+
Review ReviewCompileOutput;
51+
Review ReviewLinkOutput;
52+
};
53+
4854
} // namespace rime
4955

5056
#endif // RIME_CONFIG_PLUGINS_H_

src/rime/core_module.cc

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static void rime_core_initialize() {
2525
config->InstallPlugin(new DefaultConfigPlugin);
2626
config->InstallPlugin(new LegacyPresetConfigPlugin);
2727
config->InstallPlugin(new LegacyDictionaryConfigPlugin);
28+
config->InstallPlugin(new BuildInfoPlugin);
2829
r.Register("config", config);
2930
r.Register("schema", new SchemaComponent(config));
3031
}

0 commit comments

Comments
 (0)