101 lines
2.6 KiB
D
101 lines
2.6 KiB
D
import std.array;
|
|
import std.algorithm;
|
|
//import std.experimental.logger;
|
|
import std.file;
|
|
import std.stdio;
|
|
import std.traits;
|
|
|
|
import vibe.d;
|
|
|
|
import cache;
|
|
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) @trusted
|
|
if (isImplicitlyConvertible!(T, Page)) {
|
|
|
|
NativePath watchingDir;
|
|
try {
|
|
watchingDir = getWorkingDirectory() ~ directory;
|
|
} catch(PathValidationException) {
|
|
logError("Cannot watch path " ~ directory);
|
|
return;
|
|
}
|
|
|
|
bool addPage(string path) {
|
|
try {
|
|
T newPage = new T(path);
|
|
logInfo("Added %s", newPage.slug);
|
|
cache.addItem(newPage);
|
|
return true;
|
|
} catch (page.ArticleParseException e) {
|
|
logWarn("Could not parse %s: %s", path, e);
|
|
return false;
|
|
} catch (Exception e) {
|
|
logWarn("Other exception while parsing %s: %s", path, e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Initial scan
|
|
void scan(NativePath path, int level = 0) {
|
|
logInfo("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(watchingDir)) {
|
|
createDirectory(watchingDir);
|
|
}
|
|
scan(watchingDir);
|
|
DirectoryWatcher watcher = watchDirectory(watchingDir, 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) {
|
|
logInfo("=======[New changes]======");
|
|
string[] changeTypes = ["added", "removed", "modified"];
|
|
logInfo("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());
|
|
} catch(Exception e) {
|
|
logWarn("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);
|
|
} catch(page.ArticleParseException e) {
|
|
logWarn("Could not parse %s", change.path.toString());
|
|
} catch (Exception e) {
|
|
logWarn("Error while updating %s: %s", change.path.toString(), e.msg);
|
|
}
|
|
break;
|
|
case removed:
|
|
try {
|
|
cache.removeItemByName(change.path.toString());
|
|
} catch(Exception e) {
|
|
logInfo("Error while trying to remove %s: %s", T.stringof, e.msg);
|
|
}
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
}
|
|
}
|