From d41c2fcdd9366fb3f1fcb11aceed919bfebbed61 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 6 Mar 2017 21:38:41 +0100 Subject: make: add support to "production" builds this is used by blogc-github-lambda and blogc-git-receiver. --- man/blogc-make.1.ronn | 4 +++ src/blogc-git-receiver/pre-receive.c | 2 +- src/blogc-github-lambda/lambda_function.py | 3 +- src/blogc-make/ctx.c | 1 + src/blogc-make/ctx.h | 2 ++ src/blogc-make/exec.c | 9 +++-- src/blogc-make/exec.h | 2 +- src/blogc-make/main.c | 10 ++++-- tests/blogc-make/check_exec.c | 54 ++++++++++++++++++++++++++---- 9 files changed, 74 insertions(+), 13 deletions(-) diff --git a/man/blogc-make.1.ronn b/man/blogc-make.1.ronn index 10819da..4f9fe8c 100644 --- a/man/blogc-make.1.ronn +++ b/man/blogc-make.1.ronn @@ -20,6 +20,10 @@ EXPAND-ME! * `-f` : Reads as `blogcfile`. + * `-p`: + Builds for production environment. Adds two global variables to all blogc(1) + calls: `MAKE_ENV_PRODUCTION=1` and `MAKE_ENV=production`. + * `-v`: Show program name, version and exit. diff --git a/src/blogc-git-receiver/pre-receive.c b/src/blogc-git-receiver/pre-receive.c index c2fe080..3b05e07 100644 --- a/src/blogc-git-receiver/pre-receive.c +++ b/src/blogc-git-receiver/pre-receive.c @@ -202,7 +202,7 @@ bgr_pre_receive_hook(int argc, char *argv[]) rv = 3; goto cleanup; } - build_cmd = bc_strdup_printf("OUTPUT_DIR=\"%s\" blogc-make -V all", + build_cmd = bc_strdup_printf("OUTPUT_DIR=\"%s\" blogc-make -p -V all", output_dir); } else if ((0 == access("Makefile", F_OK)) || (0 == access("GNUMakefile", F_OK))) { diff --git a/src/blogc-github-lambda/lambda_function.py b/src/blogc-github-lambda/lambda_function.py index f8a259d..fb8492f 100644 --- a/src/blogc-github-lambda/lambda_function.py +++ b/src/blogc-github-lambda/lambda_function.py @@ -165,7 +165,7 @@ def sns_handler(message): if os.path.isfile(blogcfile): # deploy using blogc-make - args = [os.path.join(cwd, 'blogc'), '-m', '-f', blogcfile, + args = [os.path.join(cwd, 'blogc'), '-m', '-p', '-f', blogcfile, 'all'] if debug: args.append('-V') @@ -186,6 +186,7 @@ def sns_handler(message): else: print "Commit not for master branch, skipping: %s" % payload['ref'] + def lambda_handler(event, context): for record in event['Records']: if 'Sns' in record: diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index fc59457..753aadf 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -143,6 +143,7 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0, rv->blogc = bm_exec_find_binary(argv0, "blogc", "BLOGC"); rv->blogc_runserver = bm_exec_find_binary(argv0, "blogc-runserver", "BLOGC_RUNSERVER"); + rv->production = false; rv->verbose = false; } else { diff --git a/src/blogc-make/ctx.h b/src/blogc-make/ctx.h index 8d87163..e36fa4c 100644 --- a/src/blogc-make/ctx.h +++ b/src/blogc-make/ctx.h @@ -31,6 +31,8 @@ typedef struct { typedef struct { char *blogc; char *blogc_runserver; + + bool production; bool verbose; bm_settings_t *settings; diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c index df028c2..db20cae 100644 --- a/src/blogc-make/exec.c +++ b/src/blogc-make/exec.c @@ -205,7 +205,7 @@ list_variables(const char *key, const char *value, bc_string_t *str) char* bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, bc_trie_t *variables, bool listing, const char *template, - const char *output, bool sources_stdin) + const char *output, bool production, bool sources_stdin) { bc_string_t *rv = bc_string_new(); @@ -228,6 +228,11 @@ bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, bc_trie_foreach(variables, (bc_trie_foreach_func_t) list_variables, rv); + if (production) { + bc_string_append(rv, + " -D MAKE_ENV_PRODUCTION=1 -D MAKE_ENV='production'"); + } + if (listing) { bc_string_append(rv, " -l"); } @@ -268,7 +273,7 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing, } char *cmd = bm_exec_build_blogc_cmd(ctx->blogc, ctx->settings, variables, - listing, template->path, output->path, input->len > 0); + listing, template->path, output->path, ctx->production, input->len > 0); if (ctx->verbose) printf("%s\n", cmd); diff --git a/src/blogc-make/exec.h b/src/blogc-make/exec.h index 08cc608..fadaafe 100644 --- a/src/blogc-make/exec.h +++ b/src/blogc-make/exec.h @@ -20,7 +20,7 @@ int bm_exec_command(const char *cmd, const char *input, char **output, char **error, bc_error_t **err); char* bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, bc_trie_t *variables, bool listing, const char *template, - const char *output, bool sources_stdin); + const char *output, bool production, bool sources_stdin); int bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing, bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources, bool only_first_source); diff --git a/src/blogc-make/main.c b/src/blogc-make/main.c index 8e15981..eb7f504 100644 --- a/src/blogc-make/main.c +++ b/src/blogc-make/main.c @@ -36,7 +36,8 @@ print_help(void) " -h show this help message and exit\n" " -v show version and exit\n" " -V be verbose when executing commands\n" - " -f FILE read FILE as blogcfile\n"); + " -f FILE read FILE as blogcfile\n" + " -p build for production environment\n"); bm_rule_print_help(); } @@ -44,7 +45,7 @@ print_help(void) static void print_usage(void) { - printf("usage: blogc-make [-h] [-v] [-V] [-f FILE] [RULE ...]\n"); + printf("usage: blogc-make [-h] [-v] [-V] [-f FILE] [-p] [RULE ...]\n"); } @@ -62,6 +63,7 @@ main(int argc, char **argv) bc_slist_t *rules = NULL; bool verbose = false; + bool production = false; char *blogcfile = NULL; bm_ctx_t *ctx = NULL; @@ -83,6 +85,9 @@ main(int argc, char **argv) else if (i + 1 < argc) blogcfile = bc_strdup(argv[++i]); break; + case 'p': + production = true; + break; #ifdef MAKE_EMBEDDED case 'm': // no-op, for embedding into blogc binary. @@ -112,6 +117,7 @@ main(int argc, char **argv) rv = 3; goto cleanup; } + ctx->production = production; ctx->verbose = verbose; rv = bm_rule_executor(ctx, rules); diff --git a/tests/blogc-make/check_exec.c b/tests/blogc-make/check_exec.c index 506c5f6..081aa25 100644 --- a/tests/blogc-make/check_exec.c +++ b/tests/blogc-make/check_exec.c @@ -81,20 +81,20 @@ test_build_blogc_cmd_with_settings(void **state) bc_trie_insert(variables, "LOL", bc_strdup("HEHE")); char *rv = bm_exec_build_blogc_cmd("blogc", settings, variables, true, - "main.tmpl", "foo.html", true); + "main.tmpl", "foo.html", false, true); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' -l " "-t 'main.tmpl' -o 'foo.html' -i"); free(rv); rv = bm_exec_build_blogc_cmd("blogc", settings, variables, false, NULL, NULL, - false); + false, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE'"); free(rv); rv = bm_exec_build_blogc_cmd("blogc", settings, NULL, false, NULL, NULL, - false); + false, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ'"); free(rv); @@ -106,6 +106,46 @@ test_build_blogc_cmd_with_settings(void **state) } +static void +test_build_blogc_cmd_with_settings_and_production(void **state) +{ + bm_settings_t *settings = bc_malloc(sizeof(bm_settings_t)); + settings->settings = bc_trie_new(free); + bc_trie_insert(settings->settings, "locale", bc_strdup("en_US.utf8")); + settings->global = bc_trie_new(free); + bc_trie_insert(settings->global, "FOO", bc_strdup("BAR")); + bc_trie_insert(settings->global, "BAR", bc_strdup("BAZ")); + bc_trie_t *variables = bc_trie_new(free); + bc_trie_insert(variables, "LOL", bc_strdup("HEHE")); + + char *rv = bm_exec_build_blogc_cmd("blogc", settings, variables, true, + "main.tmpl", "foo.html", true, true); + assert_string_equal(rv, + "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' " + "-D MAKE_ENV_PRODUCTION=1 -D MAKE_ENV='production' -l -t 'main.tmpl' " + "-o 'foo.html' -i"); + free(rv); + + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, false, NULL, NULL, + true, false); + assert_string_equal(rv, + "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' " + "-D MAKE_ENV_PRODUCTION=1 -D MAKE_ENV='production'"); + free(rv); + + rv = bm_exec_build_blogc_cmd("blogc", settings, NULL, false, NULL, NULL, + true, false); + assert_string_equal(rv, + "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' " + "-D MAKE_ENV_PRODUCTION=1 -D MAKE_ENV='production'"); + free(rv); + + bc_trie_free(variables); + bc_trie_free(settings->settings); + bc_trie_free(settings->global); + free(settings); +} + static void test_build_blogc_cmd_without_settings(void **state) @@ -114,18 +154,19 @@ test_build_blogc_cmd_without_settings(void **state) bc_trie_insert(variables, "LOL", bc_strdup("HEHE")); char *rv = bm_exec_build_blogc_cmd("blogc", NULL, variables, true, - "main.tmpl", "foo.html", true); + "main.tmpl", "foo.html", false, true); assert_string_equal(rv, "blogc -D LOL='HEHE' -l -t 'main.tmpl' -o 'foo.html' -i"); free(rv); rv = bm_exec_build_blogc_cmd("blogc", NULL, variables, false, NULL, NULL, - false); + false, false); assert_string_equal(rv, "blogc -D LOL='HEHE'"); free(rv); - rv = bm_exec_build_blogc_cmd("blogc", NULL, NULL, false, NULL, NULL, false); + rv = bm_exec_build_blogc_cmd("blogc", NULL, NULL, false, NULL, NULL, + false, false); assert_string_equal(rv, "blogc"); free(rv); @@ -140,6 +181,7 @@ main(void) const UnitTest tests[] = { unit_test(test_find_binary), unit_test(test_build_blogc_cmd_with_settings), + unit_test(test_build_blogc_cmd_with_settings_and_production), unit_test(test_build_blogc_cmd_without_settings), }; return run_tests(tests); -- cgit v1.2.3-18-g5258