From c071ac260a24561c889643b7f8df37950a597137 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 30 Apr 2017 17:28:32 +0200 Subject: make: copy rule should support directories, recursively now any directories listed in the [copy] section of blogcfile will have its files copied recursively to the output dir. this is useful for big chunks of generated files, e.g. doxygen documentation. --- src/blogc-make/ctx.c | 49 ++++++++++++++++++++++- src/blogc-make/ctx.h | 1 + src/blogc-make/rules.c | 6 ++- tests/blogc-make/check_blogc_make.sh.in | 70 +++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 4 deletions(-) diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index e062474..ee050a4 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -7,10 +7,13 @@ */ #include +#include +#include #include #include #include #include +#include #include "../common/error.h" #include "../common/file.h" #include "../common/utils.h" @@ -50,6 +53,48 @@ bm_filectx_new(bm_ctx_t *ctx, const char *filename) } +bc_slist_t* +bm_filectx_new_r(bc_slist_t *l, bm_ctx_t *ctx, const char *filename) +{ + if (ctx == NULL || filename == NULL) + return NULL; + + char *f = filename[0] == '/' ? bc_strdup(filename) : + bc_strdup_printf("%s/%s", ctx->root_dir, filename); + + struct stat buf; + if (0 != stat(f, &buf)) { + free(f); + return l; + } + + if (S_ISDIR(buf.st_mode)) { + DIR *dir = opendir(f); + if (dir == NULL) { + free(f); + return l; + } + + struct dirent *e; + while (NULL != (e = readdir(dir))) { + if ((0 == strcmp(e->d_name, ".")) || (0 == strcmp(e->d_name, ".."))) + continue; + char *tmp = bc_strdup_printf("%s/%s", filename, e->d_name); + l = bm_filectx_new_r(l, ctx, tmp); + free(tmp); + } + + closedir(dir); + free(f); + return l; + } + + l = bc_slist_append(l, bm_filectx_new(ctx, filename)); + free(f); + return l; +} + + bool bm_filectx_changed(bm_filectx_t *ctx, time_t *tv_sec, long *tv_nsec) { @@ -208,8 +253,8 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0, rv->copy_fctx = NULL; if (settings->copy != NULL) { for (size_t i = 0; settings->copy[i] != NULL; i++) { - rv->copy_fctx = bc_slist_append(rv->copy_fctx, - bm_filectx_new(rv, settings->copy[i])); + rv->copy_fctx = bm_filectx_new_r(rv->copy_fctx, rv, + settings->copy[i]); } } diff --git a/src/blogc-make/ctx.h b/src/blogc-make/ctx.h index 962c029..1c9d8b0 100644 --- a/src/blogc-make/ctx.h +++ b/src/blogc-make/ctx.h @@ -64,6 +64,7 @@ typedef struct { } bm_ctx_t; bm_filectx_t* bm_filectx_new(bm_ctx_t *ctx, const char *filename); +bc_slist_t* bm_filectx_new_r(bc_slist_t *l, bm_ctx_t *ctx, const char *filename); bool bm_filectx_changed(bm_filectx_t *ctx, time_t *tv_sec, long *tv_nsec); void bm_filectx_reload(bm_filectx_t *ctx); void bm_filectx_free(bm_filectx_t *fctx); diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c index e9755a2..81fda35 100644 --- a/src/blogc-make/rules.c +++ b/src/blogc-make/rules.c @@ -462,9 +462,11 @@ copy_outputlist(bm_ctx_t *ctx) return NULL; bc_slist_t *rv = NULL; - for (size_t i = 0; ctx->settings->copy[i] != NULL; i++) { + // we iterate over ctx->copy_fctx list instead of ctx->settings->copy, + // because bm_ctx_new() expands directories into its files, recursively. + for (bc_slist_t *s = ctx->copy_fctx; s != NULL; s = s->next) { char *f = bc_strdup_printf("%s/%s", ctx->short_output_dir, - ctx->settings->copy[i]); + ((bm_filectx_t*) s->data)->short_path); rv = bc_slist_append(rv, bm_filectx_new(ctx, f)); free(f); } diff --git a/tests/blogc-make/check_blogc_make.sh.in b/tests/blogc-make/check_blogc_make.sh.in index bf71c65..beb1f6c 100755 --- a/tests/blogc-make/check_blogc_make.sh.in +++ b/tests/blogc-make/check_blogc_make.sh.in @@ -794,6 +794,58 @@ Page 2 EOF diff -uN "${TEMP}/proj/_build/page2.html" "${TEMP}/expected-page2.html" +rm -rf "${TEMP}/proj/_build" + + +### copy rule + +mkdir -p "${TEMP}"/proj/{a/b/c,d/e,f} +echo bola > "${TEMP}/proj/a/b/c/foo" +echo guda > "${TEMP}/proj/a/b/bar" +echo chunda > "${TEMP}/proj/a/baz" +echo lol > "${TEMP}/proj/d/e/fuu" +echo hehe > "${TEMP}/proj/d/xd" +echo FFFUUUUUU > "${TEMP}/proj/f/XDDDD" + +cat >> "${TEMP}/proj/blogcfile" <&1 | tee "${TEMP}/output.txt" +grep "_build/posts\\.html" "${TEMP}/output.txt" +grep "_build/atoom/index\\.xml" "${TEMP}/output.txt" +grep "_build/atoom/tag1/index\\.xml" "${TEMP}/output.txt" +grep "_build/atoom/tag2/index\\.xml" "${TEMP}/output.txt" +grep "_build/pagination/1\\.html" "${TEMP}/output.txt" +grep "_build/pagination/2\\.html" "${TEMP}/output.txt" +grep "_build/pagination/3\\.html" "${TEMP}/output.txt" +grep "_build/poost/foo\\.html" "${TEMP}/output.txt" +grep "_build/poost/bar\\.html" "${TEMP}/output.txt" +grep "_build/poost/baz\\.html" "${TEMP}/output.txt" +grep "_build/taag/tag1\\.html" "${TEMP}/output.txt" +grep "_build/taag/tag2\\.html" "${TEMP}/output.txt" +grep "_build/page1\\.html" "${TEMP}/output.txt" +grep "_build/page2\\.html" "${TEMP}/output.txt" +grep "_build/a/b/c/foo" "${TEMP}/output.txt" +grep "_build/a/b/bar" "${TEMP}/output.txt" +grep "_build/a/baz" "${TEMP}/output.txt" +grep "_build/d/e/fuu" "${TEMP}/output.txt" +grep "_build/d/xd" "${TEMP}/output.txt" +grep "_build/f/XDDDD" "${TEMP}/output.txt" + +rm "${TEMP}/output.txt" + +test "$(cat "${TEMP}/proj/_build/a/b/c/foo")" = "bola" +test "$(cat "${TEMP}/proj/_build/a/b/bar")" = "guda" +test "$(cat "${TEMP}/proj/_build/a/baz")" = "chunda" +test "$(cat "${TEMP}/proj/_build/d/e/fuu")" = "lol" +test "$(cat "${TEMP}/proj/_build/d/xd")" = "hehe" +test "$(cat "${TEMP}/proj/_build/f/XDDDD")" = "FFFUUUUUU" + ### clean rule @@ -812,6 +864,12 @@ grep "_build/taag/tag1\\.html" "${TEMP}/output.txt" grep "_build/taag/tag2\\.html" "${TEMP}/output.txt" grep "_build/page1\\.html" "${TEMP}/output.txt" grep "_build/page2\\.html" "${TEMP}/output.txt" +grep "_build/a/b/c/foo" "${TEMP}/output.txt" +grep "_build/a/b/bar" "${TEMP}/output.txt" +grep "_build/a/baz" "${TEMP}/output.txt" +grep "_build/d/e/fuu" "${TEMP}/output.txt" +grep "_build/d/xd" "${TEMP}/output.txt" +grep "_build/f/XDDDD" "${TEMP}/output.txt" rm "${TEMP}/output.txt" @@ -834,6 +892,12 @@ grep "___blogc_build/taag/tag1\\.html" "${TEMP}/output.txt" grep "___blogc_build/taag/tag2\\.html" "${TEMP}/output.txt" grep "___blogc_build/page1\\.html" "${TEMP}/output.txt" grep "___blogc_build/page2\\.html" "${TEMP}/output.txt" +grep "___blogc_build/a/b/c/foo" "${TEMP}/output.txt" +grep "___blogc_build/a/b/bar" "${TEMP}/output.txt" +grep "___blogc_build/a/baz" "${TEMP}/output.txt" +grep "___blogc_build/d/e/fuu" "${TEMP}/output.txt" +grep "___blogc_build/d/xd" "${TEMP}/output.txt" +grep "___blogc_build/f/XDDDD" "${TEMP}/output.txt" rm "${TEMP}/output.txt" @@ -852,6 +916,12 @@ grep "___blogc_build/taag/tag1\\.html" "${TEMP}/output.txt" grep "___blogc_build/taag/tag2\\.html" "${TEMP}/output.txt" grep "___blogc_build/page1\\.html" "${TEMP}/output.txt" grep "___blogc_build/page2\\.html" "${TEMP}/output.txt" +grep "___blogc_build/a/b/c/foo" "${TEMP}/output.txt" +grep "___blogc_build/a/b/bar" "${TEMP}/output.txt" +grep "___blogc_build/a/baz" "${TEMP}/output.txt" +grep "___blogc_build/d/e/fuu" "${TEMP}/output.txt" +grep "___blogc_build/d/xd" "${TEMP}/output.txt" +grep "___blogc_build/f/XDDDD" "${TEMP}/output.txt" rm "${TEMP}/output.txt" -- cgit v1.2.3-18-g5258