Skip to content

Commit bc66a47

Browse files
committed
feat(dict): relocate binary files to $user_data_dir/build
1 parent 285fbcc commit bc66a47

5 files changed

+38
-17
lines changed

src/rime/dict/dict_compiler.cc

+15-10
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121

2222
namespace rime {
2323

24-
DictCompiler::DictCompiler(Dictionary *dictionary)
24+
DictCompiler::DictCompiler(Dictionary *dictionary, const string& prefix)
2525
: dict_name_(dictionary->name()),
2626
prism_(dictionary->prism()),
27-
table_(dictionary->table()) {
27+
table_(dictionary->table()),
28+
prefix_(prefix) {
2829
}
2930

3031
static string LocateFile(const string& file_name) {
3132
the<ResourceResolver> resolver(
32-
Service::instance().CreateResourceResolver({"", "", ""}));
33+
Service::instance().CreateResourceResolver({"build_source", "", ""}));
3334
return resolver->ResolvePath(file_name).string();
3435
}
3536

@@ -107,7 +108,8 @@ bool DictCompiler::Compile(const string &schema_file) {
107108
LOG(INFO) << schema_file << " (" << schema_file_checksum << ")";
108109
{
109110
the<ResourceResolver> resolver(
110-
Service::instance().CreateResourceResolver({"", "", ".reverse.bin"}));
111+
Service::instance().CreateResourceResolver(
112+
{"find_reverse_db", prefix_, ".reverse.bin"}));
111113
ReverseDb reverse_db(resolver->ResolvePath(dict_name_).string());
112114
if (!reverse_db.Exists() ||
113115
!reverse_db.Load() ||
@@ -130,8 +132,9 @@ bool DictCompiler::Compile(const string &schema_file) {
130132
return true;
131133
}
132134

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

146149
EntryCollector collector;
147150
collector.Configure(settings);
@@ -186,7 +189,8 @@ bool DictCompiler::BuildTable(DictSettings* settings,
186189
}
187190
}
188191
// build .reverse.bin
189-
ReverseDb reverse_db(RelocateToUserDirectory(dict_name_ + ".reverse.bin"));
192+
ReverseDb reverse_db(RelocateToUserDirectory(prefix_,
193+
dict_name_ + ".reverse.bin"));
190194
if (!reverse_db.Build(settings,
191195
collector.syllabary,
192196
vocabulary,
@@ -199,9 +203,10 @@ bool DictCompiler::BuildTable(DictSettings* settings,
199203
}
200204

201205
bool DictCompiler::BuildPrism(const string &schema_file,
202-
uint32_t dict_file_checksum, uint32_t schema_file_checksum) {
206+
uint32_t dict_file_checksum,
207+
uint32_t schema_file_checksum) {
203208
LOG(INFO) << "building prism...";
204-
prism_ = New<Prism>(RelocateToUserDirectory(prism_->file_name()));
209+
prism_ = New<Prism>(RelocateToUserDirectory(prefix_, prism_->file_name()));
205210

206211
// get syllabary from table
207212
Syllabary syllabary;

src/rime/dict/dict_compiler.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DictCompiler {
2727
kDump = 4,
2828
};
2929

30-
RIME_API DictCompiler(Dictionary *dictionary);
30+
RIME_API DictCompiler(Dictionary *dictionary, const string& prefix = "");
3131

3232
RIME_API bool Compile(const string &schema_file);
3333
void set_options(int options) { options_ = options; }
@@ -37,13 +37,15 @@ class DictCompiler {
3737
const vector<string>& dict_files,
3838
uint32_t dict_file_checksum);
3939
bool BuildPrism(const string& schema_file,
40-
uint32_t dict_file_checksum, uint32_t schema_file_checksum);
40+
uint32_t dict_file_checksum,
41+
uint32_t schema_file_checksum);
4142
bool BuildReverseLookupDict(ReverseDb* db, uint32_t dict_file_checksum);
4243

4344
string dict_name_;
4445
an<Prism> prism_;
4546
an<Table> table_;
4647
int options_ = 0;
48+
string prefix_;
4749
};
4850

4951
} // namespace rime

src/rime/dict/dictionary.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ bool Dictionary::loaded() const {
281281
// DictionaryComponent members
282282

283283
static const ResourceType kPrismResourceType = {
284-
"prism", "", ".prism.bin"
284+
"prism", "build/", ".prism.bin"
285285
};
286286

287287
static const ResourceType kTableResourceType = {
288-
"table", "", ".table.bin"
288+
"table", "build/", ".table.bin"
289289
};
290290

291291
DictionaryComponent::DictionaryComponent()
@@ -321,7 +321,6 @@ Dictionary*
321321
DictionaryComponent::CreateDictionaryWithName(const string& dict_name,
322322
const string& prism_name) {
323323
// obtain prism and table objects
324-
boost::filesystem::path path(Service::instance().deployer().user_data_dir);
325324
auto table = table_map_[dict_name].lock();
326325
if (!table) {
327326
auto file_path = table_resource_resolver_->ResolvePath(dict_name).string();

src/rime/dict/reverse_lookup_dictionary.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ an<DictSettings> ReverseLookupDictionary::GetDictSettings() {
226226
}
227227

228228
static const ResourceType kReverseDbResourceType = {
229-
"reverse_db", "", ".reverse.bin"
229+
"reverse_db", "build/", ".reverse.bin"
230230
};
231231

232232
ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent()

src/rime/lever/deployment_tasks.cc

+16-1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ static bool TrashCustomizedCopy(const fs::path& shared_copy,
288288
return false;
289289
}
290290

291+
static bool MaybeCreateDirectory(fs::path dir) {
292+
if (!fs::exists(dir)) {
293+
boost::system::error_code ec;
294+
if (!fs::create_directories(dir, ec)) {
295+
LOG(ERROR) << "error creating directory '" << dir.string() << "'.";
296+
return false;
297+
}
298+
}
299+
return true;
300+
}
301+
291302
bool SchemaUpdate::Run(Deployer* deployer) {
292303
fs::path source_path(schema_file_);
293304
if (!fs::exists(source_path)) {
@@ -314,6 +325,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
314325
LOG(INFO) << "patched copy of schema '" << schema_id
315326
<< "' is moved to trash";
316327
}
328+
317329
// TODO: compile the config file if needs update
318330

319331
string dict_name;
@@ -329,7 +341,10 @@ bool SchemaUpdate::Run(Deployer* deployer) {
329341
return false;
330342
}
331343
LOG(INFO) << "preparing dictionary '" << dict_name << "'.";
332-
DictCompiler dict_compiler(dict.get());
344+
if (!MaybeCreateDirectory(user_data_path / "build")) {
345+
return false;
346+
}
347+
DictCompiler dict_compiler(dict.get(), "build/");
333348
if (verbose_) {
334349
dict_compiler.set_options(DictCompiler::kRebuild | DictCompiler::kDump);
335350
}

0 commit comments

Comments
 (0)