Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve init.sh robustness and IDE configuration handling #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/project.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/runConfigurations/All_tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations/Debug.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/runConfigurations/Integration_tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/runConfigurations/Tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 53 additions & 16 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ function replace() {
sed -E -e "$1" $2 > ${DEST}/${dest}
fi
else
sed -E -e "$1" $2 > $2.new
mv -f $2.new $2
if [[ -f "$2" ]]; then # Only try to replace if file exists
sed -E -e "$1" $2 > $2.new
mv -f $2.new $2
fi
fi
}

Expand All @@ -44,13 +46,18 @@ function move() {
mkdir -p $(dirname "${DEST}/${dest}")
cp -r "$1" ${DEST}/${dest}
else
mv $@
if [[ -e "$1" ]]; then # Only try to move if source exists
mkdir -p $(dirname "$2") # Create target directory if it doesn't exist
mv $@
fi
fi
}

function remove() {
if [[ -z "${TEST}" ]]; then
rm $@
if [[ -e "$1" ]]; then # Only try to remove if file exists
rm $@
fi
fi
}

Expand Down Expand Up @@ -85,24 +92,50 @@ prompt "Remove init script" "y/N"
read removeInit
removeInit=${removeInit:-n}

# Create necessary directories
mkdir -p .idea/runConfigurations
mkdir -p .vscode

# IDE configuration
move .idea/${originalProjectName}.iml .idea/${projectName}.iml
replace "s|.idea/${originalProjectName}.iml|.idea/${projectName}.iml|g" .idea/modules.xml
if [[ -f ".idea/${originalProjectName}.iml" ]]; then
move ".idea/${originalProjectName}.iml" ".idea/${projectName}.iml"
replace "s|${originalProjectName}.iml|${projectName}.iml|g" .idea/modules.xml
fi

# Run configurations
replace 's|name="project"|name="'${projectName}'"|' .idea/runConfigurations/All_tests.xml
replace 's|name="project"|name="'${projectName}'"|; s|value="\$PROJECT_DIR\$\/cmd\/'${originalBinaryName}'\/"|value="$PROJECT_DIR$/cmd/'${binaryName}'/"|' .idea/runConfigurations/Debug.xml
replace 's|name="project"|name="'${projectName}'"|' .idea/runConfigurations/Integration_tests.xml
replace 's|name="project"|name="'${projectName}'"|' .idea/runConfigurations/Tests.xml
replace "s|${originalBinaryName}|${binaryName}|" .vscode/launch.json
if [[ -f ".idea/runConfigurations/All_tests.xml" ]]; then
replace 's|name="'${originalProjectName}'"|name="'${projectName}'"|; s|module name="'${originalProjectName}'"|module name="'${projectName}'"|; s|value="'${originalPackageName}'"|value="'${packageName}'"|' .idea/runConfigurations/All_tests.xml
fi

if [[ -f ".idea/runConfigurations/Debug.xml" ]]; then
replace 's|name="'${originalProjectName}'"|name="'${projectName}'"|; s|module name="'${originalProjectName}'"|module name="'${projectName}'"|; s|value="'${originalPackageName}'/cmd/'${originalBinaryName}'"|value="'${packageName}'/cmd/'${binaryName}'"|' .idea/runConfigurations/Debug.xml
fi

if [[ -f ".idea/runConfigurations/Integration_tests.xml" ]]; then
replace 's|name="'${originalProjectName}'"|name="'${projectName}'"|; s|module name="'${originalProjectName}'"|module name="'${projectName}'"|; s|value="'${originalPackageName}'"|value="'${packageName}'"|' .idea/runConfigurations/Integration_tests.xml
fi

if [[ -f ".idea/runConfigurations/Tests.xml" ]]; then
replace 's|name="'${originalProjectName}'"|name="'${projectName}'"|; s|module name="'${originalProjectName}'"|module name="'${projectName}'"|; s|value="'${originalPackageName}'"|value="'${packageName}'"|' .idea/runConfigurations/Tests.xml
fi

# VSCode configuration
if [[ -f ".vscode/launch.json" ]]; then
replace "s|${originalBinaryName}|${binaryName}|" .vscode/launch.json
fi

# Binary changes:
# - binary name
# - source code
# - variables
move cmd/${originalBinaryName} cmd/${binaryName}
replace "s|${originalAppName}|${appName}|; s|${originalFriendlyAppName}|${friendlyAppName}|" ${DEST}/cmd/${binaryName}/main.go
find ${DEST}/cmd -type f | while read file; do replace "s|${originalPackageName}|${packageName}|" "$file"; done
if [[ -d "cmd/${originalBinaryName}" ]]; then
move cmd/${originalBinaryName} cmd/${binaryName}
fi

if [[ -d "cmd/${binaryName}" ]]; then
replace "s|${originalAppName}|${appName}|; s|${originalFriendlyAppName}|${friendlyAppName}|" ${DEST}/cmd/${binaryName}/main.go
find ${DEST}/cmd -type f | while read file; do replace "s|${originalPackageName}|${packageName}|" "$file"; done
fi

# Other project files
declare -a files=("CHANGELOG.md" "prototool.yaml" "go.mod" ".golangci.yml" "gqlgen.yml")
Expand All @@ -125,8 +158,12 @@ for file in "${files[@]}"; do
done

# Update source code
find .gen -type f | while read file; do replace "s|${originalPackageName}|${packageName}|" "$file"; done
find internal -type f | while read file; do replace "s|${originalPackageName}|${packageName}|" "$file"; done
if [[ -d ".gen" ]]; then # Only try to update .gen if it exists
find .gen -type f | while read file; do replace "s|${originalPackageName}|${packageName}|" "$file"; done
fi
if [[ -d "internal" ]]; then # Only try to update internal if it exists
find internal -type f | while read file; do replace "s|${originalPackageName}|${packageName}|" "$file"; done
fi

if [[ "${removeInit}" != "n" && "${removeInit}" != "N" ]]; then
remove "$0"
Expand Down