chris-website/source/nl/netsoj/chris/blog/watcher.d

101 lines
2.6 KiB
D
Raw Permalink Normal View History

2020-06-24 08:08:28 +00:00
import std.array;
import std.algorithm;
2023-12-08 21:52:11 +00:00
//import std.experimental.logger;
2020-06-24 08:08:28 +00:00
import std.file;
import std.stdio;
import std.traits;
2020-06-24 08:08:28 +00:00
import vibe.d;
import cache;
2020-06-24 08:08:28 +00:00
import page;
/**
* Loads pages into memory and sets up a "watcher" to watch a directory for file changes.
*/
2023-12-08 21:52:11 +00:00
void initPages(T, C)(C *cache, const string directory) @trusted
if (isImplicitlyConvertible!(T, Page)) {
2023-12-08 21:52:11 +00:00
NativePath watchingDir;
try {
watchingDir = getWorkingDirectory() ~ directory;
} catch(PathValidationException) {
logError("Cannot watch path " ~ directory);
return;
}
2020-06-24 08:08:28 +00:00
bool addPage(string path) {
try {
T newPage = new T(path);
2023-12-08 21:52:11 +00:00
logInfo("Added %s", newPage.slug);
cache.addItem(newPage);
2020-06-24 08:08:28 +00:00
return true;
} catch (page.ArticleParseException e) {
2023-12-08 21:52:11 +00:00
logWarn("Could not parse %s: %s", path, e);
return false;
} catch (Exception e) {
2023-12-08 21:52:11 +00:00
logWarn("Other exception while parsing %s: %s", path, e);
2020-06-24 08:08:28 +00:00
return false;
}
}
// Initial scan
void scan(NativePath path, int level = 0) {
2023-12-08 21:52:11 +00:00
logInfo("Scanning %s", path.toString());
2020-06-24 08:08:28 +00:00
foreach(file; iterateDirectory(path)) {
if (file.isDirectory) {
scan(path ~ file.name, level + 1);
} else {
addPage((path ~ file.name).toString());
}
}
}
2023-12-08 21:52:11 +00:00
if (!existsFile(watchingDir)) {
createDirectory(watchingDir);
2020-06-24 08:08:28 +00:00
}
2023-12-08 21:52:11 +00:00
scan(watchingDir);
DirectoryWatcher watcher = watchDirectory(watchingDir, true);
2020-06-24 08:08:28 +00:00
bool shouldStop = false;
while (!shouldStop) {
// Try to reduce changes to only one DirectoryChangeType per change
DirectoryChange[] changes;
shouldStop = !watcher.readChanges(changes);
foreach(change; changes) {
2023-12-08 21:52:11 +00:00
logInfo("=======[New changes]======");
2020-06-24 08:08:28 +00:00
string[] changeTypes = ["added", "removed", "modified"];
2023-12-08 21:52:11 +00:00
logInfo("Path: %s, type: %s", change.path.toString(), changeTypes[change.type]);
2020-06-24 08:08:28 +00:00
if (endsWith(change.path.toString(), ".kate-swp")) continue;
switch (change.type) with (DirectoryChangeType){
case added:
try {
addPage(change.path.toString());
2020-06-24 08:08:28 +00:00
} catch(Exception e) {
2023-12-08 21:52:11 +00:00
logWarn("Error while updating %s: %s", change.path.toString(), e.msg);
2020-06-24 08:08:28 +00:00
}
break;
case modified:
T newPage;
try {
newPage = new T(change.path.toString());
cache.changeItem(newPage);
2020-06-24 08:08:28 +00:00
} catch(page.ArticleParseException e) {
2023-12-08 21:52:11 +00:00
logWarn("Could not parse %s", change.path.toString());
} catch (Exception e) {
2023-12-08 21:52:11 +00:00
logWarn("Error while updating %s: %s", change.path.toString(), e.msg);
2020-06-24 08:08:28 +00:00
}
break;
case removed:
try {
cache.removeItemByName(change.path.toString());
} catch(Exception e) {
2023-12-08 21:52:11 +00:00
logInfo("Error while trying to remove %s: %s", T.stringof, e.msg);
}
2020-06-24 08:08:28 +00:00
break;
default: break;
}
}
}
}