aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/ctx.c
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2017-02-12 23:44:52 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2017-02-12 23:44:52 +0100
commit4ad218b5710659899ba842da5a597db5052f2d4d (patch)
treebdcc163184030f05513ab77f8779ea8c6f7d1a86 /src/blogc-make/ctx.c
parentcbeba5720d1b98cd7db013038b741b1a6ac9e48c (diff)
downloadblogc-4ad218b5710659899ba842da5a597db5052f2d4d.tar.gz
blogc-4ad218b5710659899ba842da5a597db5052f2d4d.tar.bz2
blogc-4ad218b5710659899ba842da5a597db5052f2d4d.zip
blogc-make: implemented automatic reloader for runserver rule
this is not the best implementation possible, but it works for most use cases
Diffstat (limited to 'src/blogc-make/ctx.c')
-rw-r--r--src/blogc-make/ctx.c110
1 files changed, 107 insertions, 3 deletions
diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c
index 39f1bf4..2dd4f97 100644
--- a/src/blogc-make/ctx.c
+++ b/src/blogc-make/ctx.c
@@ -50,6 +50,49 @@ bm_filectx_new(bm_ctx_t *ctx, const char *filename)
}
+bool
+bm_filectx_changed(bm_filectx_t *ctx, struct timespec *ts)
+{
+ if (ctx == NULL)
+ return false;
+
+ struct stat buf;
+
+ if (0 == stat(ctx->path, &buf)) {
+ if (buf.st_mtim.tv_sec == ctx->timestamp.tv_sec) {
+ if (buf.st_mtim.tv_nsec > ctx->timestamp.tv_nsec) {
+ if (ts != NULL)
+ *ts = buf.st_mtim;
+ return true;
+ }
+ }
+ else if (buf.st_mtim.tv_sec > ctx->timestamp.tv_sec) {
+ if (ts != NULL)
+ *ts = buf.st_mtim;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+void
+bm_filectx_reload(bm_filectx_t *ctx)
+{
+ if (ctx == NULL)
+ return;
+
+ struct timespec ts;
+
+ if (!bm_filectx_changed(ctx, &ts))
+ return;
+
+ ctx->timestamp = ts;
+ ctx->readable = true;
+}
+
+
void
bm_filectx_free(bm_filectx_t *fctx)
{
@@ -62,7 +105,7 @@ bm_filectx_free(bm_filectx_t *fctx)
bm_ctx_t*
-bm_ctx_new(const char *settings_file, bc_error_t **err)
+bm_ctx_new(bm_ctx_t *base, const char *settings_file, bc_error_t **err)
{
if (settings_file == NULL || err == NULL || *err != NULL)
return NULL;
@@ -92,7 +135,14 @@ bm_ctx_new(const char *settings_file, bc_error_t **err)
return NULL;
}
- bm_ctx_t *rv = bc_malloc(sizeof(bm_ctx_t));
+ bm_ctx_t *rv = NULL;
+ if (base == NULL) {
+ rv = bc_malloc(sizeof(bm_ctx_t));
+ }
+ else {
+ bm_ctx_free_internal(base);
+ rv = base;
+ }
rv->settings = settings;
char *real_filename = realpath(settings_file, NULL);
@@ -108,6 +158,8 @@ bm_ctx_new(const char *settings_file, bc_error_t **err)
rv->output_dir = bc_strdup_printf("%s/%s", rv->root_dir, output_dir);
}
+ // can't return null and set error after this!
+
const char *template_dir = bc_trie_lookup(settings->settings,
"template_dir");
@@ -158,25 +210,77 @@ bm_ctx_new(const char *settings_file, bc_error_t **err)
void
-bm_ctx_free(bm_ctx_t *ctx)
+bm_ctx_reload(bm_ctx_t *ctx)
+{
+ if (ctx == NULL || ctx->settings_fctx == NULL)
+ return;
+
+ if (bm_filectx_changed(ctx->settings_fctx, NULL)) {
+ // reload everything! we could just reload settings_fctx, as this
+ // would force rebuilding everything, but we need to know new/deleted
+ // files
+
+ // needs to dup path, because it may be freed when reloading.
+ char *tmp = bc_strdup(ctx->settings_fctx->path);
+ bc_error_t *err = NULL;
+ ctx = bm_ctx_new(ctx, tmp, &err);
+ free(tmp);
+ if (err != NULL) { // failed to reload, keep old ctx
+ bc_error_print(err, "blogc-make");
+ bc_error_free(err);
+ }
+ return;
+ }
+
+ bm_filectx_reload(ctx->main_template_fctx);
+ bm_filectx_reload(ctx->atom_template_fctx);
+
+ for (bc_slist_t *tmp = ctx->posts_fctx; tmp != NULL; tmp = tmp->next)
+ bm_filectx_reload((bm_filectx_t*) tmp->data);
+
+ for (bc_slist_t *tmp = ctx->pages_fctx; tmp != NULL; tmp = tmp->next)
+ bm_filectx_reload((bm_filectx_t*) tmp->data);
+
+ for (bc_slist_t *tmp = ctx->copy_files_fctx; tmp != NULL; tmp = tmp->next)
+ bm_filectx_reload((bm_filectx_t*) tmp->data);
+}
+
+
+void
+bm_ctx_free_internal(bm_ctx_t *ctx)
{
if (ctx == NULL)
return;
bm_settings_free(ctx->settings);
+ ctx->settings = NULL;
free(ctx->root_dir);
+ ctx->root_dir = NULL;
free(ctx->output_dir);
+ ctx->output_dir = NULL;
bm_atom_destroy(ctx->atom_template_fctx->path);
bm_filectx_free(ctx->main_template_fctx);
+ ctx->main_template_fctx = NULL;
bm_filectx_free(ctx->atom_template_fctx);
+ ctx->atom_template_fctx = NULL;
bm_filectx_free(ctx->settings_fctx);
+ ctx->settings_fctx = NULL;
bc_slist_free_full(ctx->posts_fctx, (bc_free_func_t) bm_filectx_free);
+ ctx->posts_fctx = NULL;
bc_slist_free_full(ctx->pages_fctx, (bc_free_func_t) bm_filectx_free);
+ ctx->pages_fctx = NULL;
bc_slist_free_full(ctx->copy_files_fctx, (bc_free_func_t) bm_filectx_free);
+ ctx->copy_files_fctx = NULL;
+}
+
+void
+bm_ctx_free(bm_ctx_t *ctx)
+{
+ bm_ctx_free_internal(ctx);
free(ctx);
}