-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Use inotifywait to run generate in yarn-project (#5168)
Replaces `yarn build:dev` at the root of yarn-packages with a script that leverages inotifywait to re-run `yarn generate` in the packages where it's needed. To achieve this, we define specialized `generate` commands based on the dependencies (l1 artifacts, noir contracts, or noir protocol circuits) and run the one depending on the modified folder. Since typescript watch doesn't seem to like if we modify too many files at a time, we kill it while we're doing codegen, and then restert it. Also, to make sure we don't run codegen before the compilation of the dependent artifacts is completed, we debounce the call by a few seconds.
- Loading branch information
1 parent
b48dd23
commit 137c13e
Showing
13 changed files
with
115 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"scripts": { | ||
"build": "yarn clean && yarn generate", | ||
"generate": "./scripts/generate-types.sh && run -T prettier -w ./src", | ||
"generate": "yarn generate:noir-contracts", | ||
"generate:noir-contracts": "./scripts/generate-types.sh && run -T prettier -w ./src --loglevel warn", | ||
"clean": "rm -rf .tsbuildinfo ./artifacts ./codegenCache.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"scripts": { | ||
"generate:noir-circuits": "echo Noop", | ||
"generate:noir-contracts": "echo Noop", | ||
"generate:l1-contracts": "echo Noop", | ||
"generate:why-these-noops": "echo These noops are here because `yarn workspaces foreach` runs the specified command in the packages that contain it only if two or more packages define it, otherwise it's run everywhere. So we just define these noops as commands to ensure they behave as they should when running watch.sh.", | ||
"generate:why-these-comments": "echo JSON does not support comments, so we just define these commands for documentation sake." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
|
||
DEBOUNCE_DURATION=3 # Set a high duration for debounce since nargo build may pause for a long time during a compilation | ||
INOTIFY_EVENTS="modify,create,delete,move" | ||
NOIR_CONTRACTS_OUT_DIR="../noir-projects/noir-contracts/target/" | ||
NOIR_CIRCUITS_OUT_DIR="../noir-projects/noir-protocol-circuits/target/" | ||
L1_CONTRACTS_OUT_DIR="../l1-contracts/out/" | ||
|
||
# Debounce any command sent here. Grouped by command name and first arg. | ||
debounce() { | ||
local group_id="$1-$2" | ||
local run_id=$(uuidgen) | ||
echo "$run_id" > ".debounce-$group_id" | ||
( | ||
sleep $DEBOUNCE_DURATION; | ||
local current_id=$(cat ".debounce-$group_id"); | ||
if [ "$run_id" = "${current_id}" ]; then | ||
"$@" | ||
fi | ||
) & | ||
} | ||
|
||
# Start typescript watch process in the background and store process ID in a file | ||
start_tsc_watch() { | ||
yarn tsc -b tsconfig.json --watch & | ||
TSC_PID=$! | ||
echo "$TSC_PID" > .tsc.pid | ||
} | ||
|
||
# Stops the typescript watch process | ||
stop_tsc_watch() { | ||
local tsc_pid=$(cat ".tsc.pid"); | ||
kill $tsc_pid || true | ||
} | ||
|
||
# Kill typescript, run a yarn generate, and restart typescript | ||
run_generate() { | ||
echo "Change detected at $1" | ||
stop_tsc_watch | ||
FORCE_COLOR=true yarn workspaces foreach --parallel --topological-dev --verbose run generate:$1 | ||
echo "Generate complete, restarting typescript..." | ||
sleep 3 | ||
start_tsc_watch | ||
} | ||
|
||
# Remove all temp files with process or run ids on exit | ||
cleanup() { | ||
rm .tsc.pid || true | ||
rm .debounce-* || true | ||
} | ||
trap cleanup EXIT | ||
|
||
# Start tsc watch in background | ||
start_tsc_watch | ||
|
||
# Watch for changes in the output directories | ||
while true; do | ||
folder=$(inotifywait --format '%w' --quiet --recursive --event $INOTIFY_EVENTS $NOIR_CONTRACTS_OUT_DIR $NOIR_CIRCUITS_OUT_DIR $L1_CONTRACTS_OUT_DIR) | ||
case $folder in | ||
"$NOIR_CONTRACTS_OUT_DIR") | ||
debounce run_generate "noir-contracts" | ||
;; | ||
"$NOIR_CIRCUITS_OUT_DIR") | ||
debounce run_generate "noir-circuits" | ||
;; | ||
"$L1_CONTRACTS_OUT_DIR"*) | ||
debounce run_generate "l1-contracts" | ||
;; | ||
*) | ||
echo "Change at $folder not matched with any project" | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
|
||
|