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

93 lines
2.5 KiB
D
Raw Normal View History

2020-06-24 08:08:28 +00:00
import std.array;
import std.algorithm;
import std.experimental.logger;
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.
*/
void initPages(T, C)(C *cache, const string directory)
if (isImplicitlyConvertible!(T, Page)) {
2020-06-24 08:08:28 +00:00
bool addPage(string path) {
try {
T newPage = new T(path);
logf("Added %s", newPage.slug);
cache.addItem(newPage);
2020-06-24 08:08:28 +00:00
return true;
} catch (page.ArticleParseException e) {
logf("Could not parse %s: %s", path, e);
return false;
} catch (Exception e) {
logf("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) {
logf("Scanning %s", path.toString());
foreach(file; iterateDirectory(path)) {
if (file.isDirectory) {
scan(path ~ file.name, level + 1);
} else {
addPage((path ~ file.name).toString());
}
}
}
if (!existsFile(getWorkingDirectory() ~ directory)) {
createDirectory(getWorkingDirectory() ~ directory);
}
scan(getWorkingDirectory() ~ directory);
DirectoryWatcher watcher = watchDirectory(getWorkingDirectory() ~ directory, true);
bool shouldStop = false;
while (!shouldStop) {
// Try to reduce changes to only one DirectoryChangeType per change
DirectoryChange[] changes;
shouldStop = !watcher.readChanges(changes);
foreach(change; changes) {
logf("=======[New changes]======");
string[] changeTypes = ["added", "removed", "modified"];
logf("Path: %s, type: %s", change.path.toString(), changeTypes[change.type]);
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) {
warningf("Error while updating %s: %s", change.path.toString(), e.msg);
}
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) {
warningf("Could not parse %s", change.path.toString());
} catch (Exception e) {
warningf("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) {
logf("Error while trying to remove %s: %s", T.stringof, e.msg);
}
2020-06-24 08:08:28 +00:00
break;
default: break;
}
}
}
}