From 13ce72f9e5fc3255cf662b467c5e2c83d936dd0f Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Thu, 11 Jun 2026 19:16:12 -0400 Subject: [PATCH] Env: generate .env file in builddir for DB_TARGET and vars --- cmake/CppBeSSOT.cmake | 3 +++ cmake/dbRuntimeEnv.cmake | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 cmake/dbRuntimeEnv.cmake diff --git a/cmake/CppBeSSOT.cmake b/cmake/CppBeSSOT.cmake index a85423e..dce63b9 100644 --- a/cmake/CppBeSSOT.cmake +++ b/cmake/CppBeSSOT.cmake @@ -12,6 +12,7 @@ include("${CMAKE_CURRENT_LIST_DIR}/dbGenMigrations.cmake") include("${CMAKE_CURRENT_LIST_DIR}/dbActionCommon.cmake") include("${CMAKE_CURRENT_LIST_DIR}/dbActionCreateFrom.cmake") include("${CMAKE_CURRENT_LIST_DIR}/dbActionMigrate.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/dbRuntimeEnv.cmake") if(NOT DEFINED CPPBESSOT_WORKDIR) set(CPPBESSOT_WORKDIR "db" CACHE STRING "CppBeSSOT schema root folder") @@ -87,6 +88,8 @@ set(CPPBESSOT_DB_SQLITE_CLONE_PROD_TO_PRODDEV_COMMAND "${CPPBESSOT_DB_SQLITE_CLO set(CPPBESSOT_DB_PGSQL_CLONE_PROD_TO_PRODDEV_COMMAND "${CPPBESSOT_DB_PGSQL_CLONE_PROD_TO_PRODDEV_COMMAND}" CACHE STRING "Parent-supplied command string that clones the prod PostgreSQL DB into proddev") +cppbessot_write_runtime_env_file("${CMAKE_BINARY_DIR}/cppbessot.env") + if(NOT DEFINED CPPBESSOT_AUTO_ENABLE) option(CPPBESSOT_AUTO_ENABLE "Auto-register CppBeSSOT targets when this file is included" ON) endif() diff --git a/cmake/dbRuntimeEnv.cmake b/cmake/dbRuntimeEnv.cmake new file mode 100644 index 0000000..18d8d58 --- /dev/null +++ b/cmake/dbRuntimeEnv.cmake @@ -0,0 +1,41 @@ +include_guard(GLOBAL) + +function(_cppbessot_dotenv_escape output_var value) + set(escaped "${value}") + string(REPLACE "\\" "\\\\" escaped "${escaped}") + string(REPLACE "\"" "\\\"" escaped "${escaped}") + string(REPLACE "\n" "\\n" escaped "${escaped}") + string(REPLACE "\r" "\\r" escaped "${escaped}") + string(REPLACE "\t" "\\t" escaped "${escaped}") + set(${output_var} "${escaped}" PARENT_SCOPE) +endfunction() + +function(_cppbessot_dotenv_append output_var name value) + _cppbessot_dotenv_escape(escaped_value "${value}") + set(line "${name}=\"${escaped_value}\"\n") + set(${output_var} "${${output_var}}${line}" PARENT_SCOPE) +endfunction() + +function(cppbessot_write_runtime_env_file output_path) + set(env_contents + "# Generated by cppbessot during CMake configure. Do not edit.\n") + _cppbessot_dotenv_append(env_contents + "DB_TARGET" "${DB_TARGET}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_PGSQL_PROD_CONNSTR" "${CPPBESSOT_DB_PGSQL_PROD_CONNSTR}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_PGSQL_DEV_CONNSTR" "${CPPBESSOT_DB_PGSQL_DEV_CONNSTR}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR" "${CPPBESSOT_DB_PGSQL_PRODDEV_CONNSTR}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_PGSQL_TESTS_CONNSTR" "${CPPBESSOT_DB_PGSQL_TESTS_CONNSTR}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_SQLITE_PROD_PATH" "${CPPBESSOT_DB_SQLITE_PROD_PATH}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_SQLITE_DEV_PATH" "${CPPBESSOT_DB_SQLITE_DEV_PATH}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_SQLITE_PRODDEV_PATH" "${CPPBESSOT_DB_SQLITE_PRODDEV_PATH}") + _cppbessot_dotenv_append(env_contents + "CPPBESSOT_DB_SQLITE_TESTS_PATH" "${CPPBESSOT_DB_SQLITE_TESTS_PATH}") + file(WRITE "${output_path}" "${env_contents}") +endfunction()