-
-
Save chaitanya79/2f10fd79ef430a877607caecf06d4abf to your computer and use it in GitHub Desktop.
Action to generate Came case
This file contains hidden or 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
name: Generate CamelCase and PascalCase | |
on: | |
workflow_dispatch: | |
inputs: | |
module_name: | |
description: 'Snake case module name (e.g., my_module_name)' | |
required: true | |
type: string | |
jobs: | |
generate_cases: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code (if needed) | |
uses: actions/checkout@v3 # only if you need to access files in your repo | |
- name: Generate CamelCase and PascalCase | |
id: generate_cases | |
run: | | |
module_name="${{ github.event.inputs.module_name }}" | |
camel_case=$(echo "$module_name" | sed 's/_./\U&/g' | sed 's/_//g' | awk '{print tolower(substr($0,1,1)) substr($0,2)}') | |
pascal_case=$(echo "$module_name" | sed 's/_./\U&/g' | sed 's/_//g' | awk '{print toupper(substr($0,1,1)) substr($0,2)}') | |
echo "camel_case=$camel_case" >> $GITHUB_OUTPUT | |
echo "pascal_case=$pascal_case" >> $GITHUB_OUTPUT | |
- name: Output results | |
run: | | |
echo "CamelCase: ${{ steps.generate_cases.outputs.camel_case }}" | |
echo "PascalCase: ${{ steps.generate_cases.outputs.pascal_case }}" | |
- name: Use the values (example) | |
run: | | |
echo "Example use of camelCase: ${{ steps.generate_cases.outputs.camel_case }}" | |
echo "Example use of PascalCase: ${{ steps.generate_cases.outputs.pascal_case }}" | |
jobs: | |
rename_and_zip: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Template Repository | |
uses: actions/checkout@v3 | |
with: | |
path: template-repo # Clone the template repo into a specific directory | |
- name: Transform Module Name | |
run: | | |
# Define the functions | |
camel_case() { | |
local input="$1" | |
local output="" | |
local capitalize=0 | |
for ((i = 0; i < ${#input}; i++)); do | |
local char="${input:$i:1}" | |
if [[ "$char" == "_" ]]; then | |
capitalize=1 | |
elif [[ "$capitalize" -eq 1 ]]; then | |
output+="$(tr '[:lower:]' '[:upper:]' <<< "$char")" | |
capitalize=0 | |
else | |
output+="$char" | |
fi | |
done | |
echo "$output" | |
} | |
pascal_case() { | |
local input="$1" | |
local output="$(tr '[:lower:]' '[:upper:]' <<< "${input:0:1}")" | |
local capitalize=0 | |
for ((i = 1; i < ${#input}; i++)); do | |
local char="${input:$i:1}" | |
if [[ "$char" == "_" ]]; then | |
capitalize=1 | |
elif [[ "$capitalize" -eq 1 ]]; then | |
output+="$(tr '[:lower:]' '[:upper:]' <<< "$char")" | |
capitalize=0 | |
else | |
output+="$char" | |
fi | |
done | |
echo "$output" | |
} | |
lowercase() { | |
local input="$1" | |
echo "$(tr '[:upper:]' '[:lower:]' <<< "$input")" | |
} | |
# Transform the input | |
CAMEL_NAME=$(camel_case "${{ github.event.inputs.module_name }}") | |
PASCAL_NAME=$(pascal_case "${{ github.event.inputs.module_name }}") | |
LOWER_NAME=$(lowercase "${{ github.event.inputs.module_name }}") | |
# Display the transformed names (for debugging) | |
echo "CamelCase: $CAMEL_NAME" | |
echo "PascalCase: $PASCAL_NAME" | |
echo "Lowercase: $LOWER_NAME" | |
# Use the transformed names in subsequent steps (e.g., renaming) | |
find template-repo -type f -exec sed -i "s/OldModule/$CAMEL_NAME/g" {} \; | |
find template-repo -type f -name "*OldModule*" -exec bash -c 'mv "$0" "${0//OldModule/$PASCAL_NAME}"' {} \; | |
find template-repo -type d -name "*OldModule*" -exec bash -c 'mv "$0" "${0//OldModule/$PASCAL_NAME}"' {} \; | |
find template-repo -type f -exec sed -i "s/oldModuleVariable/${LOWER_NAME}Variable/g" {} \; | |
# To Rename directories. | |
find template-repo -type d -name "Old*" -exec bash -c 'mv "$0" "${0//Old/$NEW_NAME}"' {} \; | |
- name: Create ZIP Archive | |
run: zip -r ${{ github.event.inputs.zip_filename }} template-repo | |
- name: Upload ZIP Archive | |
uses: actions/upload-artifact@v3 | |
with: | |
name: ${{ github.event.inputs.zip_filename }} | |
path: ${{ github.event.inputs.zip_filename }} | |
Comments are disabled for this gist.