-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
Warning Rate limit exceeded@junhaoliao has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 4 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughThe pull request modifies the Changes
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
CMakeLists.txt (1)
14-23
: Improve JSON parsing robustnessThe current regex patterns for extracting name and version are too simplistic and might fail with valid JSON formats. Consider:
- Multiple spaces or tabs between tokens
- Newlines between quotes
- Escaped quotes within values
-if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\":[ ]*\"([^\"]+)\"") +if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\"[[:space:]]*:[[:space:]]*\"([^\"\\]*(\\.[^\"\\]*)*)\""Alternatively, consider using a proper JSON parser like
jq
or Python'sjson
module throughexecute_process
.Also applies to: 24-33
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
CMakeLists.txt
(1 hunks)
🔇 Additional comments (1)
CMakeLists.txt (1)
36-40
: 🛠️ Refactor suggestion
Quote project name and validate version format
- The project name should be quoted to handle names containing spaces
- Consider validating the version format to ensure it follows semantic versioning
project(
- ${CLP_FFI_JS_PROJECT_NAME}
+ "${CLP_FFI_JS_PROJECT_NAME}"
LANGUAGES
C
CXX
VERSION "${CLP_FFI_JS_VERSION}"
)
Run this script to verify the version format:
CMakeLists.txt
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
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() |
We could also have used string(JSON ...) but that is
We're currently enforcing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
CMakeLists.txt (3)
18-27
: Consider using a more robust JSON parsing patternWhile the current regex works for basic JSON, it might fail with complex JSON structures containing nested objects or arrays. Consider a more robust pattern that:
- Handles whitespace more precisely
- Accounts for possible commas and line endings
- Considers the position of the "name" field
-if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\":[ ]*\"([^\"]+)\"") +if(${PACKAGE_JSON_CONTENT} MATCHES "\"name\"[ \t]*:[ \t]*\"([^\"]+)\"[ \t]*[,}]")
28-37
: Add semantic version validationThe current implementation accepts any string as a version. Consider adding validation for semantic versioning format (MAJOR.MINOR.PATCH).
-if(${PACKAGE_JSON_CONTENT} MATCHES "\"version\":[ \t]*\"([^\"]+)\"") +if(${PACKAGE_JSON_CONTENT} MATCHES "\"version\":[ \t]*\"(\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?)\"[ \t]*[,}]")Also, consider adding a message when falling back to the default version:
else() + message(WARNING "Failed to extract version from package.json. Using default version 0.0.0") set(CLP_FFI_JS_VERSION "0.0.0" CACHE STRING "Use a placeholder version." FORCE) endif()
39-44
: Quote the project name parameterWhile the current project name doesn't contain spaces, it's good practice to quote it for consistency and future-proofing.
project( - ${CLP_FFI_JS_PROJECT_NAME} + "${CLP_FFI_JS_PROJECT_NAME}" LANGUAGES C CXX VERSION "${CLP_FFI_JS_VERSION}" )
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
CMakeLists.txt
(1 hunks)
🔇 Additional comments (2)
CMakeLists.txt (2)
12-17
: LGTM: Proper error handling implemented
The implementation correctly checks for the existence of package.json
before attempting to read it, with a clear error message.
12-44
: Verify package.json contents
Let's verify the existence and content of package.json to ensure the build won't fail.
✅ Verification successful
Package.json exists with required fields - build configuration is valid
The package.json file exists and contains both the "name" and "version" fields that are required by the CMake configuration:
- name: "clp-ffi-js"
- version: "0.3.1"
These values will be properly extracted by the CMake regex patterns and set as the project name and version respectively.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify package.json exists and contains required fields
# Check if package.json exists and show its contents
if [ -f "package.json" ]; then
echo "package.json found. Contents:"
cat package.json
else
echo "package.json not found!"
exit 1
fi
# Extract and validate name and version
echo -e "\nValidating fields:"
jq -r '.name + " " + .version' package.json || echo "Failed to extract name/version"
Length of output: 1144
… code review Co-authored-by: kirkrodrigues <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the PR title, how about:
build(cmake): Extract CMake project name and version from package.json.
It's subjective, but I think build
is a more appropriate type since this change feels like more than just a chore.
package.json
.
Description
package.json
.Validation performed
Positive test
task
in the project root.cd build/clp-ffi-js
.cmake -LAH | grep -B 1 CLP_FFI_JS
and saw below in the output:package.json
and found no discrepancy.Negative test
"version": "0.3.1",
frompackage.json
.Summary by CodeRabbit
Summary by CodeRabbit
package.json
, enhancing configurability.package.json
.