-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): config compiler plugins that port legacy features to th…
…e new YAML syntax
- Loading branch information
Showing
9 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright RIME Developers | ||
// Distributed under the BSD License | ||
// | ||
#include <boost/algorithm/string.hpp> | ||
#include <rime/config/config_compiler_impl.h> | ||
#include <rime/config/plugins.h> | ||
|
||
namespace rime { | ||
|
||
static string remove_suffix(const string& input, const string& suffix) { | ||
return boost::ends_with(input, suffix) ? | ||
input.substr(0, input.length() - suffix.length()) : input; | ||
} | ||
|
||
// auto-patch applies to all loaded config resources, including dependencies. | ||
// therefore it's done at the end of Compile phase. | ||
bool AutoPatchConfigPlugin::ReviewCompileOutput(ConfigCompiler* compiler, | ||
an<ConfigResource> resource) { | ||
if (boost::ends_with(resource->resource_id, ".custom")) | ||
return true; | ||
// skip auto-patch if there is already an explicit `__patch` at the root node | ||
auto root_deps = compiler->GetDependencies(resource->resource_id + ":"); | ||
if (!root_deps.empty() && root_deps.back()->priority() >= kPatch) | ||
return true; | ||
auto patch_resource_id = | ||
remove_suffix(resource->resource_id, ".schema") + ".custom"; | ||
LOG(INFO) << "auto-patch " << resource->resource_id << ":/__patch: " | ||
<< patch_resource_id << ":/patch?"; | ||
compiler->Push(resource); | ||
compiler->AddDependency( | ||
New<PatchReference>(Reference{patch_resource_id, "patch", true})); | ||
compiler->Pop(); | ||
return true; | ||
} | ||
|
||
bool AutoPatchConfigPlugin::ReviewLinkOutput(ConfigCompiler* compiler, | ||
an<ConfigResource> resource) { | ||
return true; | ||
} | ||
|
||
} // namespace rime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Copyright RIME Developers | ||
// Distributed under the BSD License | ||
// | ||
#include <rime/config/config_compiler_impl.h> | ||
#include <rime/config/plugins.h> | ||
|
||
namespace rime { | ||
|
||
bool LegacyDictionaryConfigPlugin::ReviewCompileOutput( | ||
ConfigCompiler* compiler, an<ConfigResource> resource) { | ||
// TODO: unimplemented | ||
return true; | ||
} | ||
|
||
bool LegacyDictionaryConfigPlugin::ReviewLinkOutput( | ||
ConfigCompiler* compiler, an<ConfigResource> resource) { | ||
// TODO: unimplemented | ||
return true; | ||
} | ||
|
||
} // namespace rime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// Copyright RIME Developers | ||
// Distributed under the BSD License | ||
// | ||
#include <boost/algorithm/string.hpp> | ||
#include <rime/config/config_compiler_impl.h> | ||
#include <rime/config/config_cow_ref.h> | ||
#include <rime/config/plugins.h> | ||
|
||
namespace rime { | ||
|
||
bool LegacyPresetConfigPlugin::ReviewCompileOutput( | ||
ConfigCompiler* compiler, an<ConfigResource> resource) { | ||
return true; | ||
} | ||
|
||
bool LegacyPresetConfigPlugin::ReviewLinkOutput( | ||
ConfigCompiler* compiler, an<ConfigResource> resource) { | ||
if (!boost::ends_with(resource->resource_id, ".schema")) | ||
return true; | ||
if (auto preset = resource->data->Traverse("key_binder/import_preset")) { | ||
if (!Is<ConfigValue>(preset)) | ||
return false; | ||
auto preset_config_id = As<ConfigValue>(preset)->str(); | ||
LOG(INFO) << "interpreting key_binder/import_preset: " << preset_config_id; | ||
auto target = Cow(resource, "key_binder"); | ||
auto map = As<ConfigMap>(**target); | ||
if (map && map->HasKey("bindings")) { | ||
// append to included list `key_binder/bindings/+` instead of overwriting | ||
auto appended = map->Get("bindings"); | ||
*Cow(target, "bindings/+") = appended; | ||
// `*target` is already referencing a copied map, safe to edit directly | ||
(*target)["bindings"] = nullptr; | ||
} | ||
Reference reference{preset_config_id, "key_binder", false}; | ||
if (!IncludeReference{reference} | ||
.TargetedAt(target).Resolve(compiler)) { | ||
LOG(ERROR) << "failed to include section " << reference; | ||
return false; | ||
} | ||
} | ||
// NOTE: in the following cases, Cow() is not strictly necessary because | ||
// we know for sure that no other ConfigResource is going to reference the | ||
// root map node that will be modified. But other than the root node of the | ||
// resource being linked, it's possbile a map or list has multiple references | ||
// in the node tree, therefore Cow() is recommended to make sure the | ||
// modifications only happen to one place. | ||
if (auto preset = resource->data->Traverse("punctuator/import_preset")) { | ||
if (!Is<ConfigValue>(preset)) | ||
return false; | ||
auto preset_config_id = As<ConfigValue>(preset)->str(); | ||
LOG(INFO) << "interpreting punctuator/import_preset: " << preset_config_id; | ||
Reference reference{preset_config_id, "punctuator", false}; | ||
if (!IncludeReference{reference} | ||
.TargetedAt(Cow(resource, "punctuator")).Resolve(compiler)) { | ||
LOG(ERROR) << "failed to include section " << reference; | ||
return false; | ||
} | ||
} | ||
if (auto preset = resource->data->Traverse("recognizer/import_preset")) { | ||
if (!Is<ConfigValue>(preset)) | ||
return false; | ||
auto preset_config_id = As<ConfigValue>(preset)->str(); | ||
LOG(INFO) << "interpreting recognizer/import_preset: " << preset_config_id; | ||
Reference reference{preset_config_id, "recognizer", false}; | ||
if (!IncludeReference{reference} | ||
.TargetedAt(Cow(resource, "recognizer")).Resolve(compiler)) { | ||
LOG(ERROR) << "failed to include section " << reference; | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace rime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Copyright RIME Developers | ||
// Distributed under the BSD License | ||
// | ||
#ifndef RIME_CONFIG_PLUGINS_H_ | ||
#define RIME_CONFIG_PLUGINS_H_ | ||
|
||
#include <rime/common.h> | ||
|
||
namespace rime { | ||
|
||
class ConfigCompiler; | ||
struct ConfigResource; | ||
|
||
class ConfigCompilerPlugin { | ||
public: | ||
typedef bool Review(ConfigCompiler* compiler, | ||
an<ConfigResource> resource); | ||
|
||
virtual Review ReviewCompileOutput = 0; | ||
virtual Review ReviewLinkOutput = 0; | ||
}; | ||
|
||
class AutoPatchConfigPlugin : public ConfigCompilerPlugin { | ||
public: | ||
Review ReviewCompileOutput; | ||
Review ReviewLinkOutput; | ||
}; | ||
|
||
class LegacyPresetConfigPlugin : public ConfigCompilerPlugin { | ||
public: | ||
Review ReviewCompileOutput; | ||
Review ReviewLinkOutput; | ||
}; | ||
|
||
class LegacyDictionaryConfigPlugin : public ConfigCompilerPlugin { | ||
public: | ||
Review ReviewCompileOutput; | ||
Review ReviewLinkOutput; | ||
}; | ||
|
||
} // namespace rime | ||
|
||
#endif // RIME_CONFIG_PLUGINS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters