From 9bc52cdeea5f2c4088f95bd7e9367db61b80a107 Mon Sep 17 00:00:00 2001 From: Taj Date: Fri, 16 Dec 2022 23:16:59 +0000 Subject: [PATCH 1/9] action for filename formatter --- .github/workflows/test.yml | 11 +++++++ filename_formatter.sh | 18 ++++++----- formatter/action.yml | 21 +++++++++++++ formatter/filename_formatter.sh | 53 +++++++++++++++++++++++++++++++++ test/some file.cpp | 0 5 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 formatter/action.yml create mode 100755 formatter/filename_formatter.sh create mode 100644 test/some file.cpp diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2afbda2 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,11 @@ +name: Test +on: workflow_dispatch +jobs: + formatter: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: ./formatter + with: + filetypes: .cpp,.hpp + working-directory: ./test diff --git a/filename_formatter.sh b/filename_formatter.sh index 806d191..bfd5c49 100755 --- a/filename_formatter.sh +++ b/filename_formatter.sh @@ -9,6 +9,7 @@ echo -e "Arguments: echo "Changed files:" + # Separate $2 value (filename types) if it has a comma if [[ "$2" == *","* ]]; then @@ -21,10 +22,10 @@ str_value="$2" str_value2="$2" fi -IFS=$'\n' -for fname in `find $1 -type f -name "$str_value2" -o -name "$str_value"` +IFS=$'\n'; set -f +for fname in $(find $1 -type f -name "*$str_value2" -or -name "*$str_value") do - ignored_files="$(echo $3 | tr "," "\n")" + ignored_files="$(echo "$3" | tr "," "\n")" str="${fname}" value=${str%/*} # If the base directory is `.`, check in all directories for the ignored filenames @@ -38,15 +39,16 @@ do done #echo ${fname} - new_fname=`echo ${fname} | tr ' ' '_'` + new_fname=$(echo "${fname}" | tr ' ' '_') #echo " ${new_fname}" - new_fname=`echo ${new_fname} | tr 'A-Z' 'a-z'` + new_fname=$(echo "${new_fname}" | tr '[:upper:]' '[:lower:]') #echo " ${new_fname}" - new_fname=`echo ${new_fname} | tr '-' '_'` + new_fname=$(echo "${new_fname}" | tr '-' '_') #echo " ${new_fname}" - if [ ${fname} != ${new_fname} ] + if [ "${fname}" != "${new_fname}" ] then echo " ${fname} --> ${new_fname}" - git "mv" "${fname}" ${new_fname} # Requires you to be in version control + git "mv" "${fname}" "${new_fname}" # Requires you to be in version control fi done +unset IFS; set +f \ No newline at end of file diff --git a/formatter/action.yml b/formatter/action.yml new file mode 100644 index 0000000..5d586ea --- /dev/null +++ b/formatter/action.yml @@ -0,0 +1,21 @@ +name: "Formatter" +description: "Format filenames into the format acceptable by TheAlgorithms opganisation" +author: "tjgurwara99" +inputs: + filetypes: + description: Filter files by specified file types (comma separated values in a string.) Maximum two values, example .cpp,.hpp + required: true + working-directory: + description: Working directory of the formatter + required: false + default: . + ignore-files: + description: Files that should be ignored + required: false +runs: + using: composite + steps: + - name: Running the formatter + shell: bash + run: | + ./filename_formatter.sh ${{ inputs.working-directory }} ${{ inputs.filetypes }} ${{ inputs.ignore-files }} diff --git a/formatter/filename_formatter.sh b/formatter/filename_formatter.sh new file mode 100755 index 0000000..6d3ace4 --- /dev/null +++ b/formatter/filename_formatter.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +echo -e "Arguments: +[0] - Used by Bash (script filename) +[1] - Base directory +[2] - Filename type (maximum two values) +[3] - Ignored files or folders (optional; use "\""./"\"") +" + +echo "Changed files:" + + +# Separate $2 value (filename types) if it has a comma +if [[ "$2" == *","* ]]; +then +string="$2" + +str_value=${string#*,} +str_value2=${string%%,*} +else +str_value="$2" +str_value2="$2" +fi + +IFS=$'\n'; set -f +for fname in $(find $1 -type f -name "*$str_value2" -or -name "*$str_value") +do + ignored_files="$(echo "$3" | tr "," "\n")" + + str="${fname}" + value=${str%/*} # If the base directory is `.`, check in all directories for the ignored filenames + + for files in $ignored_files + do + if [ "${fname}" == "$value/$files" ] || [ "$value" == "$files" ]; + then + continue 2 + fi + done + + #echo ${fname} + new_fname=$(echo "${fname}" | tr ' ' '_') + #echo " ${new_fname}" + new_fname=$(echo "${new_fname}" | tr '[:upper:]' '[:lower:]') + #echo " ${new_fname}" + new_fname=$(echo "${new_fname}" | tr '-' '_') + #echo " ${new_fname}" + if [ "${fname}" != "${new_fname}" ] + then + echo " ${fname} --> ${new_fname}" + fi +done +unset IFS; set +f \ No newline at end of file diff --git a/test/some file.cpp b/test/some file.cpp new file mode 100644 index 0000000..e69de29 From 45df7684c980c3f442b30476b06ac94970f84a34 Mon Sep 17 00:00:00 2001 From: Taj Date: Fri, 16 Dec 2022 23:57:54 +0000 Subject: [PATCH 2/9] Rename some file.cpp to SOME FILE.cpp --- test/SOME FILE.cpp | 1 + test/some file.cpp | 0 2 files changed, 1 insertion(+) create mode 100644 test/SOME FILE.cpp delete mode 100644 test/some file.cpp diff --git a/test/SOME FILE.cpp b/test/SOME FILE.cpp new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/test/SOME FILE.cpp @@ -0,0 +1 @@ + diff --git a/test/some file.cpp b/test/some file.cpp deleted file mode 100644 index e69de29..0000000 From c902c0edb9619e5760eca9e136bc8622995135d9 Mon Sep 17 00:00:00 2001 From: Taj Date: Sat, 17 Dec 2022 13:07:43 +0000 Subject: [PATCH 3/9] fix review comments --- .github/workflows/test-ignore-files.yml | 13 +++++++++++++ .github/workflows/test.yml | 1 + formatter/action.yml | 10 +++++----- formatter/filename_formatter.sh | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/test-ignore-files.yml diff --git a/.github/workflows/test-ignore-files.yml b/.github/workflows/test-ignore-files.yml new file mode 100644 index 0000000..9ac8128 --- /dev/null +++ b/.github/workflows/test-ignore-files.yml @@ -0,0 +1,13 @@ +name: Test +on: workflow_dispatch +jobs: + formatter: + runs-on: ubuntu-latest + name: Verify that the filename formatter works + steps: + - uses: actions/checkout@v3 + - uses: ./formatter + with: + filetypes: .cpp,.hpp + working-directory: . + ignore-files: ./test diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2afbda2..d26b1af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,6 +3,7 @@ on: workflow_dispatch jobs: formatter: runs-on: ubuntu-latest + name: Verify that the filename formatter works steps: - uses: actions/checkout@v3 - uses: ./formatter diff --git a/formatter/action.yml b/formatter/action.yml index 5d586ea..b97c203 100644 --- a/formatter/action.yml +++ b/formatter/action.yml @@ -1,16 +1,16 @@ name: "Formatter" -description: "Format filenames into the format acceptable by TheAlgorithms opganisation" -author: "tjgurwara99" +description: "Format filenames into the format acceptable by TheAlgorithms opganization" +author: "TheAlgorithms" inputs: filetypes: - description: Filter files by specified file types (comma separated values in a string.) Maximum two values, example .cpp,.hpp + description: Filter files by specified file types (comma separated values in a string.) Maximum two values. E.g. `.cpp,.hpp` required: true working-directory: - description: Working directory of the formatter + description: Working/base directory of the formatter required: false default: . ignore-files: - description: Files that should be ignored + description: Files/folders to ignored required: false runs: using: composite diff --git a/formatter/filename_formatter.sh b/formatter/filename_formatter.sh index 6d3ace4..72f9e87 100755 --- a/formatter/filename_formatter.sh +++ b/formatter/filename_formatter.sh @@ -50,4 +50,4 @@ do echo " ${fname} --> ${new_fname}" fi done -unset IFS; set +f \ No newline at end of file +unset IFS; set +f From db1b14250eaf7a40449e5a3b3fb1d0c9c50c4337 Mon Sep 17 00:00:00 2001 From: Taj Date: Sat, 17 Dec 2022 13:11:06 +0000 Subject: [PATCH 4/9] update name --- formatter/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formatter/action.yml b/formatter/action.yml index b97c203..42fdcd6 100644 --- a/formatter/action.yml +++ b/formatter/action.yml @@ -1,4 +1,4 @@ -name: "Formatter" +name: "Filename Formatter" description: "Format filenames into the format acceptable by TheAlgorithms opganization" author: "TheAlgorithms" inputs: From 31f290199104da26372695ecfd291f066692b5d4 Mon Sep 17 00:00:00 2001 From: Taj Date: Sun, 18 Dec 2022 17:07:12 +0000 Subject: [PATCH 5/9] Update test/SOME FILE.cpp Co-authored-by: David Leal --- test/SOME FILE.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/SOME FILE.cpp b/test/SOME FILE.cpp index d3f5a12..9d51254 100644 --- a/test/SOME FILE.cpp +++ b/test/SOME FILE.cpp @@ -1 +1,2 @@ +// Test file to make sure the filename formatter works as excepted From fb97309e75a680f34e2ba562cf861b3438379d35 Mon Sep 17 00:00:00 2001 From: David Leal Date: Mon, 19 Dec 2022 12:31:09 -0600 Subject: [PATCH 6/9] chore: apply suggestions from code review --- test/SOME FILE.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SOME FILE.cpp b/test/SOME FILE.cpp index 9d51254..c693b2d 100644 --- a/test/SOME FILE.cpp +++ b/test/SOME FILE.cpp @@ -1,2 +1 @@ // Test file to make sure the filename formatter works as excepted - From d037b1108d672e9fb2abc925b21fdcbdf03f6535 Mon Sep 17 00:00:00 2001 From: Taj Date: Mon, 19 Dec 2022 18:33:57 +0000 Subject: [PATCH 7/9] Apply suggestions from code review Co-authored-by: David Leal --- .github/workflows/test-ignore-files.yml | 4 ++-- .github/workflows/test.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-ignore-files.yml b/.github/workflows/test-ignore-files.yml index 9ac8128..7cb3eb7 100644 --- a/.github/workflows/test-ignore-files.yml +++ b/.github/workflows/test-ignore-files.yml @@ -1,9 +1,9 @@ -name: Test +name: Test With ignore directory argument on: workflow_dispatch jobs: formatter: runs-on: ubuntu-latest - name: Verify that the filename formatter works + name: Verify that the filename formatter works (ignoring directory is included) steps: - uses: actions/checkout@v3 - uses: ./formatter diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d26b1af..e96f160 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Test +name: Test without ignore directory argument on: workflow_dispatch jobs: formatter: From 0e734e7ace66896ddb65a15af38d9404f58d3dc5 Mon Sep 17 00:00:00 2001 From: David Leal Date: Mon, 19 Dec 2022 12:53:12 -0600 Subject: [PATCH 8/9] chore: apply suggestions from code review --- .github/workflows/test-ignore-files.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-ignore-files.yml b/.github/workflows/test-ignore-files.yml index 7cb3eb7..54d0c10 100644 --- a/.github/workflows/test-ignore-files.yml +++ b/.github/workflows/test-ignore-files.yml @@ -1,4 +1,4 @@ -name: Test With ignore directory argument +name: Test with ignore directory argument on: workflow_dispatch jobs: formatter: From 80d5cd198001be41b513b034e0ff9e5f1f72f028 Mon Sep 17 00:00:00 2001 From: Taj Date: Wed, 21 Dec 2022 17:12:45 +0000 Subject: [PATCH 9/9] fix missing action path --- formatter/action.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/formatter/action.yml b/formatter/action.yml index 42fdcd6..efa5226 100644 --- a/formatter/action.yml +++ b/formatter/action.yml @@ -15,7 +15,9 @@ inputs: runs: using: composite steps: + - run: echo "${{ github.action_path }}" >> $GITHUB_PATH + shell: bash - name: Running the formatter shell: bash run: | - ./filename_formatter.sh ${{ inputs.working-directory }} ${{ inputs.filetypes }} ${{ inputs.ignore-files }} + filename_formatter.sh ${{ inputs.working-directory }} ${{ inputs.filetypes }} ${{ inputs.ignore-files }}