Skip to content

Commit

Permalink
feat(dict): relocate binary files to $user_data_dir/build
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Jan 18, 2018
1 parent 285fbcc commit bc66a47
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
25 changes: 15 additions & 10 deletions src/rime/dict/dict_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@

namespace rime {

DictCompiler::DictCompiler(Dictionary *dictionary)
DictCompiler::DictCompiler(Dictionary *dictionary, const string& prefix)
: dict_name_(dictionary->name()),
prism_(dictionary->prism()),
table_(dictionary->table()) {
table_(dictionary->table()),
prefix_(prefix) {
}

static string LocateFile(const string& file_name) {
the<ResourceResolver> resolver(
Service::instance().CreateResourceResolver({"", "", ""}));
Service::instance().CreateResourceResolver({"build_source", "", ""}));
return resolver->ResolvePath(file_name).string();
}

Expand Down Expand Up @@ -107,7 +108,8 @@ bool DictCompiler::Compile(const string &schema_file) {
LOG(INFO) << schema_file << " (" << schema_file_checksum << ")";
{
the<ResourceResolver> resolver(
Service::instance().CreateResourceResolver({"", "", ".reverse.bin"}));
Service::instance().CreateResourceResolver(
{"find_reverse_db", prefix_, ".reverse.bin"}));
ReverseDb reverse_db(resolver->ResolvePath(dict_name_).string());
if (!reverse_db.Exists() ||
!reverse_db.Load() ||
Expand All @@ -130,8 +132,9 @@ bool DictCompiler::Compile(const string &schema_file) {
return true;
}

static string RelocateToUserDirectory(const string& file_name) {
ResourceResolver resolver(ResourceType{"", "", ""});
static string RelocateToUserDirectory(const string& prefix,
const string& file_name) {
ResourceResolver resolver(ResourceType{"build_target", prefix, ""});
resolver.set_root_path(Service::instance().deployer().user_data_dir);
auto resource_id = boost::filesystem::path(file_name).filename().string();
return resolver.ResolvePath(resource_id).string();
Expand All @@ -141,7 +144,7 @@ bool DictCompiler::BuildTable(DictSettings* settings,
const vector<string>& dict_files,
uint32_t dict_file_checksum) {
LOG(INFO) << "building table...";
table_ = New<Table>(RelocateToUserDirectory(table_->file_name()));
table_ = New<Table>(RelocateToUserDirectory(prefix_, table_->file_name()));

EntryCollector collector;
collector.Configure(settings);
Expand Down Expand Up @@ -186,7 +189,8 @@ bool DictCompiler::BuildTable(DictSettings* settings,
}
}
// build .reverse.bin
ReverseDb reverse_db(RelocateToUserDirectory(dict_name_ + ".reverse.bin"));
ReverseDb reverse_db(RelocateToUserDirectory(prefix_,
dict_name_ + ".reverse.bin"));
if (!reverse_db.Build(settings,
collector.syllabary,
vocabulary,
Expand All @@ -199,9 +203,10 @@ bool DictCompiler::BuildTable(DictSettings* settings,
}

bool DictCompiler::BuildPrism(const string &schema_file,
uint32_t dict_file_checksum, uint32_t schema_file_checksum) {
uint32_t dict_file_checksum,
uint32_t schema_file_checksum) {
LOG(INFO) << "building prism...";
prism_ = New<Prism>(RelocateToUserDirectory(prism_->file_name()));
prism_ = New<Prism>(RelocateToUserDirectory(prefix_, prism_->file_name()));

// get syllabary from table
Syllabary syllabary;
Expand Down
6 changes: 4 additions & 2 deletions src/rime/dict/dict_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DictCompiler {
kDump = 4,
};

RIME_API DictCompiler(Dictionary *dictionary);
RIME_API DictCompiler(Dictionary *dictionary, const string& prefix = "");

RIME_API bool Compile(const string &schema_file);
void set_options(int options) { options_ = options; }
Expand All @@ -37,13 +37,15 @@ class DictCompiler {
const vector<string>& dict_files,
uint32_t dict_file_checksum);
bool BuildPrism(const string& schema_file,
uint32_t dict_file_checksum, uint32_t schema_file_checksum);
uint32_t dict_file_checksum,
uint32_t schema_file_checksum);
bool BuildReverseLookupDict(ReverseDb* db, uint32_t dict_file_checksum);

string dict_name_;
an<Prism> prism_;
an<Table> table_;
int options_ = 0;
string prefix_;
};

} // namespace rime
Expand Down
5 changes: 2 additions & 3 deletions src/rime/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ bool Dictionary::loaded() const {
// DictionaryComponent members

static const ResourceType kPrismResourceType = {
"prism", "", ".prism.bin"
"prism", "build/", ".prism.bin"
};

static const ResourceType kTableResourceType = {
"table", "", ".table.bin"
"table", "build/", ".table.bin"
};

DictionaryComponent::DictionaryComponent()
Expand Down Expand Up @@ -321,7 +321,6 @@ Dictionary*
DictionaryComponent::CreateDictionaryWithName(const string& dict_name,
const string& prism_name) {
// obtain prism and table objects
boost::filesystem::path path(Service::instance().deployer().user_data_dir);
auto table = table_map_[dict_name].lock();
if (!table) {
auto file_path = table_resource_resolver_->ResolvePath(dict_name).string();
Expand Down
2 changes: 1 addition & 1 deletion src/rime/dict/reverse_lookup_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ an<DictSettings> ReverseLookupDictionary::GetDictSettings() {
}

static const ResourceType kReverseDbResourceType = {
"reverse_db", "", ".reverse.bin"
"reverse_db", "build/", ".reverse.bin"
};

ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent()
Expand Down
17 changes: 16 additions & 1 deletion src/rime/lever/deployment_tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ static bool TrashCustomizedCopy(const fs::path& shared_copy,
return false;
}

static bool MaybeCreateDirectory(fs::path dir) {
if (!fs::exists(dir)) {
boost::system::error_code ec;
if (!fs::create_directories(dir, ec)) {
LOG(ERROR) << "error creating directory '" << dir.string() << "'.";
return false;
}
}
return true;
}

bool SchemaUpdate::Run(Deployer* deployer) {
fs::path source_path(schema_file_);
if (!fs::exists(source_path)) {
Expand All @@ -314,6 +325,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
LOG(INFO) << "patched copy of schema '" << schema_id
<< "' is moved to trash";
}

// TODO: compile the config file if needs update

string dict_name;
Expand All @@ -329,7 +341,10 @@ bool SchemaUpdate::Run(Deployer* deployer) {
return false;
}
LOG(INFO) << "preparing dictionary '" << dict_name << "'.";
DictCompiler dict_compiler(dict.get());
if (!MaybeCreateDirectory(user_data_path / "build")) {
return false;
}
DictCompiler dict_compiler(dict.get(), "build/");
if (verbose_) {
dict_compiler.set_options(DictCompiler::kRebuild | DictCompiler::kDump);
}
Expand Down

0 comments on commit bc66a47

Please sign in to comment.