From 199fb0f7f4c9da289a6f36cd3fb6fd5e70e44286 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Fri, 17 Feb 2023 21:17:56 +0600 Subject: [PATCH] Fast formatting (#2658) Signed-off-by: Denis Bykhov --- .github/workflows/main.yml | 2 +- common/config/rush/command-line.json | 8 +++++ common/scripts/each-diff.sh | 44 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100755 common/scripts/each-diff.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fd7af84a06..c5d537475f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -154,7 +154,7 @@ jobs: - name: Install... run: node common/scripts/install-run-rush.js install - name: Formatting... - run: node common/scripts/install-run-rush.js format + run: node common/scripts/install-run-rush.js fast-format - name: Check files formatting run: | echo '================================================================' diff --git a/common/config/rush/command-line.json b/common/config/rush/command-line.json index 66de5ee15d..481d9bb4fb 100644 --- a/common/config/rush/command-line.json +++ b/common/config/rush/command-line.json @@ -316,6 +316,14 @@ "description": "Clean package-deps-*.json files", "safeForSimultaneousRushProcesses": true, "shellCommand": "find .|grep .rush/temp/package-deps_ | xargs rm" + }, + { + "commandKind": "global", + "name": "fast-format", + "summary": "Format changed projects", + "description": "Format and autofix linting issues in changed projects", + "safeForSimultaneousRushProcesses": true, + "shellCommand": "./common/scripts/each-diff.sh rushx format" } ], diff --git a/common/scripts/each-diff.sh b/common/scripts/each-diff.sh new file mode 100755 index 0000000000..35b367f18b --- /dev/null +++ b/common/scripts/each-diff.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +FILES=$(git diff origin/main --name-only --diff-filter=ACMR | sed 's| |\\ |g') +[ -z "$FILES" ] && exit 0 + +roots=$(rush list -p --json | grep "path" | cut -f 2 -d ':' | cut -f 2 -d '"') + +declare -a changed_roots + +pushd () { + command pushd "$@" > /dev/null +} + +popd () { + command popd "$@" > /dev/null +} + +if [ -n "$FILES" ]; then + for file in $FILES; do + # Match file to root + for r in $roots; do + if [[ "$file" == ${r}* ]]; then + echo -e "${file}" + if [[ ! " ${changed_roots[@]} " =~ " ${r} " ]]; then + changed_roots+=("$r") + fi + fi + done + done + for value in "${changed_roots[@]}"; do + echo -e "\033[0;34mProcessing \033[0;31m${value}\033[0m" + exec 3>&1 + exec 1> >(paste /dev/null -) + pushd $value > /dev/null + $@ + retVal=$? + if [ $retVal -ne 0 ]; then + echo "Error" + exit 1 + fi + popd > /dev/null + exec 1>&3 3>&- + done +fi \ No newline at end of file