Refactor configuration file installation and template handling

- Update CMakeLists.txt to install the template configuration file as an example, ensuring it is available for reference.
- Modify install-config.cmake to prioritize the installed template file, with fallbacks to source directory templates if the installed one is missing, enhancing the robustness of the configuration setup.
This commit is contained in:
Torsten Schulz (local)
2025-11-18 11:33:33 +01:00
committed by Torsten (PC)
parent e3f46d775a
commit 753c5929e1
2 changed files with 28 additions and 14 deletions

View File

@@ -110,10 +110,10 @@ target_link_libraries(yourpart-daemon PRIVATE
# Installation rules # Installation rules
install(TARGETS yourpart-daemon DESTINATION /usr/local/bin) install(TARGETS yourpart-daemon DESTINATION /usr/local/bin)
# Installiere Template als Referenz ZUERST (wird vom install-Skript benötigt)
install(FILES daemon.conf DESTINATION /etc/yourpart/ RENAME daemon.conf.example)
# Intelligente Konfigurationsdatei-Installation # Intelligente Konfigurationsdatei-Installation
# Verwendet ein CMake-Skript, das nur fehlende Keys hinzufügt, ohne bestehende zu überschreiben # Verwendet ein CMake-Skript, das nur fehlende Keys hinzufügt, ohne bestehende zu überschreiben
# Das Skript liest das Template aus dem Source-Verzeichnis und merged es intelligent # Das Skript liest das Template aus /etc/yourpart/daemon.conf.example oder dem Source-Verzeichnis
install(SCRIPT cmake/install-config.cmake) install(SCRIPT cmake/install-config.cmake)
# Installiere Template als Referenz (optional, wird nicht überschrieben)
install(FILES daemon.conf DESTINATION /etc/yourpart/ RENAME daemon.conf.example)

View File

@@ -3,18 +3,32 @@
# Pfade setzen # Pfade setzen
set(CONFIG_FILE "/etc/yourpart/daemon.conf") set(CONFIG_FILE "/etc/yourpart/daemon.conf")
# Während der Installation ist CMAKE_SOURCE_DIR das Projekt-Root-Verzeichnis set(TEMPLATE_FILE "/etc/yourpart/daemon.conf.example")
# Falls nicht verfügbar, versuchen wir es über CMAKE_CURRENT_LIST_DIR
if(DEFINED CMAKE_SOURCE_DIR)
set(TEMPLATE_FILE "${CMAKE_SOURCE_DIR}/daemon.conf")
else()
get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
set(TEMPLATE_FILE "${PROJECT_ROOT}/daemon.conf")
endif()
# Prüfe ob Template existiert # Prüfe ob Template existiert (wurde von CMake installiert)
if(NOT EXISTS "${TEMPLATE_FILE}") if(NOT EXISTS "${TEMPLATE_FILE}")
message(FATAL_ERROR "Template-Datei ${TEMPLATE_FILE} nicht gefunden!") # Fallback 1: Versuche Template im Source-Verzeichnis zu finden
# CMAKE_CURRENT_LIST_DIR zeigt auf cmake/ während der Installation
get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
set(TEMPLATE_FILE_FALLBACK "${PROJECT_ROOT}/daemon.conf")
# Fallback 2: Versuche über CMAKE_SOURCE_DIR (falls verfügbar)
if(DEFINED CMAKE_SOURCE_DIR AND EXISTS "${CMAKE_SOURCE_DIR}/daemon.conf")
set(TEMPLATE_FILE "${CMAKE_SOURCE_DIR}/daemon.conf")
message(STATUS "Verwende Template aus CMAKE_SOURCE_DIR: ${TEMPLATE_FILE}")
elseif(EXISTS "${TEMPLATE_FILE_FALLBACK}")
set(TEMPLATE_FILE "${TEMPLATE_FILE_FALLBACK}")
message(STATUS "Verwende Template aus Source-Verzeichnis: ${TEMPLATE_FILE}")
else()
message(FATAL_ERROR "Template-Datei nicht gefunden!")
message(FATAL_ERROR " Gesucht in: ${TEMPLATE_FILE}")
message(FATAL_ERROR " Fallback 1: ${TEMPLATE_FILE_FALLBACK}")
if(DEFINED CMAKE_SOURCE_DIR)
message(FATAL_ERROR " Fallback 2: ${CMAKE_SOURCE_DIR}/daemon.conf")
endif()
endif()
else()
message(STATUS "Verwende installierte Template-Datei: ${TEMPLATE_FILE}")
endif() endif()
# Prüfe ob Ziel-Verzeichnis existiert # Prüfe ob Ziel-Verzeichnis existiert