Skip to content

Commit

Permalink
fix(ts): ensures patches are preserved during code generation (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed May 2, 2024
1 parent 873447f commit c9bfdae
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ clean:
rm -rf $(AKASH_TS_ROOT)/dist

.PHONY: clean-codegen
clean-codegen:
rm -rf $(AKASH_TS_ROOT)/src/generated
clean-codegen: $(AKASH_DEVCACHE_BASE)/tmp/ts
rm -rf $(AKASH_TS_ROOT)/src/generated

$(AKASH_DEVCACHE_BASE)/tmp/ts:
script/preserve-ts-patches.sh
22 changes: 22 additions & 0 deletions script/preserve-ts-patches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

echo "Preserving TypeScript patches..."
generated_dir="$AKASH_TS_ROOT/src/generated"
tmp_dir="$AKASH_DEVCACHE_BASE/tmp/ts"

if [ ! -d "$generated_dir" ]; then
echo "Directory $generated_dir does not exist. Skipping..."
exit 0
fi

find "$generated_dir" -type f -name "*.original.ts" | while read -r src_file; do
src_file=${src_file//.original/}
gen_dir=$(dirname "$src_file")
gen_dir=${gen_dir//$generated_dir\//}
file=$(basename "$src_file" .original.ts)
mkdir -p "$tmp_dir/$gen_dir"
tmp_file="$tmp_dir/$gen_dir/$file"

echo "Preserving $src_file to $tmp_file"
cp "$src_file" "$tmp_file"
done
6 changes: 5 additions & 1 deletion script/protocgen-legacy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env bash

set -eo pipefail

PATH=$(pwd)/.cache/bin/legacy:$PATH
Expand Down Expand Up @@ -98,3 +97,8 @@ cp -rv github.com/akash-network/akash-api/* ./
--doc_out=./docs/proto \
--doc_opt=./docs/protodoc-markdown.tmpl,provider.md \
$(find "./proto/provider" -maxdepth 4 -name '*.proto')

script/restore-ts-patches.sh

cd "$AKASH_TS_ROOT" && npm run format

22 changes: 22 additions & 0 deletions script/restore-ts-patches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

echo "Restoring TypeScript patches..."

tmp_dir="$AKASH_DEVCACHE_BASE/tmp/ts"
generated_dir="$AKASH_TS_ROOT/src/generated"

if [ ! -d "$tmp_dir" ]; then
echo "Directory $tmp_dir does not exist. Skipping..."
exit 0
fi

find "$tmp_dir" -type f -name "*.ts" | while read -r src_file; do
original_file_path=${src_file/$tmp_dir\//}
renamed_original_file_path="${original_file_path/.ts/.original.ts}"
echo "Restoring $original_file_path to $generated_dir/$original_file_path"

mv "$generated_dir/$original_file_path" "$generated_dir/$renamed_original_file_path"
mv "$tmp_dir/$original_file_path" "$generated_dir/$original_file_path"
done

rm -rf "$tmp_dir"
2 changes: 2 additions & 0 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ The files in the `src/generated` directory are auto-generated and should not be
export * from '../../../../patch/cosmos/base/v1beta1/coin';
```

NOTE: Naming and paths are important to prevent the original file from being overwritten when the code is regenerated. See `script/preserve-ts-patches.sh` and `script/restore-ts-patches.sh` for more implementation details.

### License
This package is licensed under the Apache-2.0.

0 comments on commit c9bfdae

Please sign in to comment.