env_to_h cleaned
This commit is contained in:
15
tools/env_to_h/CMakeLists.txt
Normal file
15
tools/env_to_h/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
function(dusk_env_to_h INPUT_PATH OUTPUT_NAME_RELATIVE)
|
||||
set(DUSK_DEFS_TARGET_NAME "DUSK_DEFS_${OUTPUT_NAME_RELATIVE}")
|
||||
dusk_run_python(
|
||||
${DUSK_DEFS_TARGET_NAME}
|
||||
tools.env_to_h
|
||||
--env ${INPUT_PATH}
|
||||
--output ${DUSK_BUILT_ASSETS_DIR}/${OUTPUT_NAME_RELATIVE}
|
||||
)
|
||||
add_dependencies(${DUSK_LIBRARY_TARGET_NAME} ${DUSK_DEFS_TARGET_NAME})
|
||||
endfunction()
|
||||
46
tools/env_to_h/__main__.py
Normal file
46
tools/env_to_h/__main__.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import argparse
|
||||
import os
|
||||
from dotenv import load_dotenv, dotenv_values
|
||||
|
||||
parser = argparse.ArgumentParser(description="Convert .env to .h defines")
|
||||
parser.add_argument("--env", required=True, help="Path to .env file")
|
||||
parser.add_argument("--output", required=True, help="Path to output .h file")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Load .env file
|
||||
load_dotenv(dotenv_path=args.env)
|
||||
fileDefs = dotenv_values(dotenv_path=args.env)
|
||||
|
||||
|
||||
outHeader = ""
|
||||
outHeader += "#include \"dusk.h\"\n\n"
|
||||
for key, value in fileDefs.items():
|
||||
# Determine type and print out appropriate C type define.
|
||||
|
||||
# Float
|
||||
try:
|
||||
asFloat = float(value)
|
||||
outHeader += f"#define {key} {asFloat}f\n"
|
||||
continue
|
||||
except:
|
||||
pass
|
||||
|
||||
# Integer
|
||||
try:
|
||||
asInt = int(value)
|
||||
outHeader += f"#define {key} {asInt}\n"
|
||||
except:
|
||||
pass
|
||||
|
||||
# Boolean
|
||||
if value.lower() in ['true', 'false']:
|
||||
asBool = '1' if value.lower() == 'true' else '0'
|
||||
outHeader += f"#define {key} {asBool}\n"
|
||||
continue
|
||||
|
||||
# String
|
||||
outHeader += f'#define {key} "{value}"\n'
|
||||
|
||||
# Write to output file
|
||||
with open(args.output, 'w') as outFile:
|
||||
outFile.write(outHeader)
|
||||
Reference in New Issue
Block a user