Skip to content

Commit 6d43d30

Browse files
committed
Add regex replace to patch.
1 parent e7a66fe commit 6d43d30

10 files changed

Lines changed: 91 additions & 64 deletions

File tree

src/client/main.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ApiResult api_call(const String &cmd, const Strings &args);
5050
void check_spec_file();
5151
void default_run();
5252
void init(const Strings &args, const String &log_level);
53-
void init_service_db();
53+
void init_service_db(bool init);
5454
void load_current_config();
5555
void self_upgrade();
5656

@@ -473,18 +473,20 @@ void init(const Strings &args, const String &log_level)
473473
auto &us = Settings::get_user_settings();
474474

475475
// disable update checks for internal commands
476-
if (args.size() > 1 && args[1].find("internal-") == 0)
476+
bool init = !(args.size() > 1 && args[1].find("internal-") == 0);
477+
if (!init)
477478
us.disable_update_checks = true;
478479

479480
load_current_config();
480-
init_service_db();
481+
init_service_db(init);
481482
}
482483

483-
void init_service_db()
484+
void init_service_db(bool init)
484485
{
485486
// initialize internal db
486-
auto &sdb = getServiceDatabase();
487-
sdb.performStartupActions();
487+
auto &sdb = getServiceDatabase(init);
488+
if (init)
489+
sdb.performStartupActions();
488490
}
489491

490492
void load_current_config()

src/common/access_table.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ struct AccessData
5454

5555
static AccessData data;
5656

57-
AccessTable::AccessTable(const path &cfg_dir)
58-
: root_dir(cfg_dir.parent_path())
57+
AccessTable::AccessTable()
5958
{
6059
data.load();
6160
}
@@ -71,8 +70,6 @@ bool AccessTable::must_update_contents(const path &p) const
7170
return true;
7271
if (data.do_not_update)
7372
return false;
74-
if (!isUnderRoot(p))
75-
return true;
7673
return fs::last_write_time(p) != data.stamps[p];
7774
}
7875

@@ -89,11 +86,6 @@ void AccessTable::update_contents(const path &p, const String &s) const
8986

9087
void AccessTable::write_if_older(const path &p, const String &s) const
9188
{
92-
if (!isUnderRoot(p))
93-
{
94-
write_file_if_different(p, s);
95-
return;
96-
}
9789
if (must_update_contents(p))
9890
update_contents(p, s);
9991
}
@@ -103,11 +95,6 @@ void AccessTable::clear() const
10395
data.clear();
10496
}
10597

106-
bool AccessTable::isUnderRoot(path p) const
107-
{
108-
return is_under_root(p, root_dir);
109-
}
110-
11198
void AccessTable::remove(const path &p) const
11299
{
113100
std::set<path> rm;

src/common/access_table.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class AccessTable
2323
{
2424
public:
25-
AccessTable(const path &cfg_dir);
25+
AccessTable();
2626
~AccessTable();
2727

2828
bool updates_disabled() const;
@@ -33,9 +33,4 @@ class AccessTable
3333
void remove(const path &p) const;
3434

3535
static void do_not_update_files(bool v);
36-
37-
private:
38-
path root_dir;
39-
40-
bool isUnderRoot(path p) const;
4136
};

src/common/config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void Config::post_download() const
194194
p.patchSources();
195195

196196
// remove from table
197-
AccessTable at(directories.storage_dir_etc);
197+
AccessTable at;
198198
at.remove(pkg.getDirSrc());
199199
at.remove(pkg.getDirObj());
200200

src/common/database.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ void writePackagesDbVersion(const path &dir, int version)
257257
write_file(dir / PACKAGES_DB_VERSION_FILE, std::to_string(version));
258258
}
259259

260-
ServiceDatabase &getServiceDatabase()
260+
ServiceDatabase &getServiceDatabase(bool init)
261261
{
262262
#ifdef _WIN32
263263
thread_local
264264
#else
265265
static
266266
#endif
267-
ServiceDatabase db;
267+
ServiceDatabase db(init);
268268
return db;
269269
}
270270

@@ -315,15 +315,21 @@ void Database::recreate()
315315
created = true;
316316
}
317317

318-
ServiceDatabase::ServiceDatabase()
318+
ServiceDatabase::ServiceDatabase(bool init)
319319
: Database(service_db_name, get_service_tables())
320320
{
321+
if (!init)
322+
return;
321323
createTables();
322324
checkStamp();
323325
increaseNumberOfRuns();
324326
checkForUpdates();
325327
}
326328

329+
ServiceDatabase::~ServiceDatabase()
330+
{
331+
}
332+
327333
void ServiceDatabase::createTables() const
328334
{
329335
// add table hashes
@@ -520,6 +526,11 @@ Stamps ServiceDatabase::getFileStamps() const
520526

521527
void ServiceDatabase::setFileStamps(const Stamps &stamps) const
522528
{
529+
if (stamps.empty())
530+
{
531+
clearFileStamps();
532+
return;
533+
}
523534
String q = "replace into FileStamps values ";
524535
for (auto &s : stamps)
525536
q += "('" + normalize_path(s.first) + "', '" + std::to_string(s.second) + "'),";

src/common/database.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class Database
7171
class ServiceDatabase : public Database
7272
{
7373
public:
74-
ServiceDatabase();
74+
ServiceDatabase(bool init);
75+
~ServiceDatabase();
7576

7677
void performStartupActions() const;
7778

@@ -152,7 +153,7 @@ class PackagesDatabase : public Database
152153
Dependencies getProjectDependencies(ProjectVersionId project_version_id, DependenciesMap &dm) const;
153154
};
154155

155-
ServiceDatabase &getServiceDatabase();
156+
ServiceDatabase &getServiceDatabase(bool init = true);
156157
PackagesDatabase &getPackagesDatabase();
157158

158159
int readPackagesDbSchemaVersion(const path &dir);

src/common/package_store.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ void PackageStore::process(const path &p, Config &root)
6969
processing = false;
7070
};
7171

72+
// main access table holder
73+
AccessTable access_table;
74+
7275
// insert root config
7376
packages[root.pkg].config = &root;
7477

@@ -120,9 +123,6 @@ void PackageStore::process(const path &p, Config &root)
120123
}
121124
}
122125

123-
// main access table holder
124-
AccessTable access_table(directories.storage_dir_etc);
125-
126126
// TODO: if we got a download we might need to refresh configs
127127
// but we do not know what projects we should clear
128128
// so clear the whole AT

src/common/project.cpp

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -327,31 +327,68 @@ Strings BuildSystemConfigInsertions::getStrings()
327327

328328
void Patch::load(const yaml &root)
329329
{
330-
get_map_and_iterate(root, "replace_in_files", [this](auto &v)
330+
auto load_replace = [&root](auto &a, const String &k)
331331
{
332-
if (!v.second.IsMap())
333-
throw std::runtime_error("Members of 'replace_in_files' should be maps");
334-
if (!(v.second["from"].IsDefined() && v.second["to"].IsDefined()))
335-
throw std::runtime_error("There are no 'from' and 'to' inside 'replace_in_files'");
336-
auto from = v.second["from"].template as<String>();
337-
auto to = v.second["to"].template as<String>();
338-
replace_in_files[from] = to;
339-
});
332+
get_map_and_iterate(root, k, [&a, &k](auto &v)
333+
{
334+
auto k = v.first.template as<String>();
335+
if (v.second.IsScalar())
336+
{
337+
auto vv = v.second.template as<String>();
338+
a.emplace_back(k, vv);
339+
}
340+
else if (v.second.IsMap())
341+
{
342+
if (!(v.second["from"].IsDefined() && v.second["to"].IsDefined()))
343+
throw std::runtime_error("There are no 'from' and 'to' inside '" + k + "'");
344+
auto from = v.second["from"].template as<String>();
345+
auto to = v.second["to"].template as<String>();
346+
a.emplace_back(from, to);
347+
}
348+
else
349+
throw std::runtime_error("Members of '" + k + "' must be scalars or maps");
350+
});
351+
};
352+
load_replace(replace, "replace");
353+
load_replace(regex_replace, "regex_replace");
340354
}
341355

342356
void Patch::save(yaml &node) const
343357
{
344-
yaml root;
345-
for (auto &r : replace_in_files)
346-
root[r.first] = r.second;
347-
if (!replace_in_files.empty())
348-
node["patch"] = root;
358+
auto save_replace = [&node](const auto &a, const auto &k)
359+
{
360+
if (a.empty())
361+
return;
362+
yaml root;
363+
for (auto &r : a)
364+
root[r.first] = r.second;
365+
node["patch"][k] = root;
366+
};
367+
save_replace(replace, "replace");
368+
save_replace(regex_replace, "regex_replace");
369+
}
370+
371+
void Patch::patchSources(const Files &files) const
372+
{
373+
if (replace.empty() && regex_replace.empty())
374+
return;
375+
std::vector<std::pair<std::regex, String>> regex_prepared;
376+
for (auto &p : regex_replace)
377+
regex_prepared.emplace_back(std::regex(p.first), p.second);
378+
for (auto &f : files)
379+
{
380+
auto s = read_file(f, true);
381+
for (auto &p : replace)
382+
boost::algorithm::replace_all(s, p.first, p.second);
383+
for (auto &p : regex_prepared)
384+
s = std::regex_replace(s, p.first, p.second);
385+
write_file_if_different(f, s);
386+
}
349387
}
350388

351389
Project::Project()
352390
: Project(ProjectPath())
353391
{
354-
355392
}
356393

357394
Project::Project(const ProjectPath &root_project)
@@ -930,7 +967,7 @@ void Project::load(const yaml &root)
930967
aliases = get_sequence_set<String>(root, "aliases");
931968
checks.load(root);
932969

933-
auto patch_node = root["patch"];
970+
const auto &patch_node = root["patch"];
934971
if (patch_node.IsDefined())
935972
patch.load(patch_node);
936973

@@ -1176,17 +1213,7 @@ void Project::prepareExports() const
11761213

11771214
void Project::patchSources() const
11781215
{
1179-
if (!patch.replace_in_files.empty())
1180-
{
1181-
auto &srcs = getSources();
1182-
for (auto &f : srcs)
1183-
{
1184-
auto s = read_file(f, true);
1185-
for (auto &p : patch.replace_in_files)
1186-
boost::algorithm::replace_all(s, p.first, p.second);
1187-
write_file_if_different(f, s);
1188-
}
1189-
}
1216+
patch.patchSources(getSources());
11901217
}
11911218

11921219
const Files &Project::getSources() const

src/common/project.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,16 @@ using OptionsMap = std::map<String, Options>;
9898
OptionsMap loadOptionsMap(const yaml &root);
9999
void saveOptionsMap(yaml &root, const OptionsMap &m);
100100

101-
using ReplaceInFiles = std::unordered_map<String, String>;
101+
using ReplaceInFiles = std::vector<std::pair<String, String>>;
102102

103103
struct Patch
104104
{
105-
ReplaceInFiles replace_in_files;
105+
ReplaceInFiles replace;
106+
ReplaceInFiles regex_replace;
106107

107108
void load(const yaml &root);
108109
void save(yaml &root) const;
110+
void patchSources(const Files &files) const;
109111
};
110112

111113
struct Project

src/common/resolver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ void Resolver::read_config(const DownloadDependency &d)
425425
{
426426
if (!fs::exists(d.getDirSrc()))
427427
return;
428+
if (rd.packages.find(d) != rd.packages.end())
429+
return;
428430

429431
try
430432
{

0 commit comments

Comments
 (0)