Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(cmake): Extract CMake project name and version from package.json. #45

Merged
merged 6 commits into from
Dec 29, 2024
27 changes: 25 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,35 @@ Emscripten.cmake"
)
endif()

# Extract the project name & version from package.json
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for package.json read operation

The file(READ ...) command doesn't verify if the file exists or is readable. Consider adding error handling:

-file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT)
+if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/package.json")
+    file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT)
+else()
+    message(FATAL_ERROR "package.json not found in ${CMAKE_CURRENT_SOURCE_DIR}")
+endif()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/package.json")
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON_CONTENT)
else()
message(FATAL_ERROR "package.json not found in ${CMAKE_CURRENT_SOURCE_DIR}")
endif()

if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\":[ ]*\"([^\"]+)\"")
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
set(CLP_FFI_JS_PROJECT_NAME
${CMAKE_MATCH_1}
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
CACHE STRING
"The name of the project parsed from `package.json`."
FORCE
kirkrodrigues marked this conversation as resolved.
Show resolved Hide resolved
)
else()
set(CLP_FFI_JS_PROJECT_NAME "clp-ffi-js" CACHE STRING "Use a placeholder project name." FORCE)
endif()
if(${PACKAGE_JSON_CONTENT} MATCHES "\"version\":[ ]*\"([^\"]+)\"")
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
set(CLP_FFI_JS_VERSION
${CMAKE_MATCH_1}
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
CACHE STRING
"The version of the project parsed from `package.json`."
FORCE
)
else()
set(CLP_FFI_JS_VERSION "0.0.0" CACHE STRING "Use a placeholder version." FORCE)
endif()

project(
clp-ffi-js
${CLP_FFI_JS_PROJECT_NAME}
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
LANGUAGES
C
CXX
VERSION 0.3.1
VERSION "${CLP_FFI_JS_VERSION}"
)

# Enable exporting compile commands
Expand Down
Loading