Skip to content

Commit

Permalink
refactor(setup): simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Oct 22, 2024
1 parent 172262b commit 9d4923b
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 538 deletions.
10 changes: 4 additions & 6 deletions src/first_time_setup/checks.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/bin/bash
# @name checks
# @deps utils/boolean utils/git
# @deps utils/boolean
# @brief A package for checks and validations.

set -euo pipefail

source 'src/first_time_setup/utils/boolean.sh'
source 'src/first_time_setup/utils/git.sh'

# @description Check if the script should run in non-interactive mode.
# @arg $1 The jurisdiction name.
Expand All @@ -23,15 +22,14 @@ is::ci() {

# @description Check if the repository exists.
is::repo() {
is::inside_working_tree
git rev-parse --is-inside-work-tree &>/dev/null && echo true || echo false
}

# @description Check if the setup should persevere.
# @arg $1 If we are in a CI environment.
# @arg $2 If the repository exists.
setup::persevere() {
local is_ci is_repo
is_ci=$(is::true "${1:-}")
is_repo=$(is::true "${2:-}")
local -r is_ci=$(is::true "${1:-}")
local -r is_repo=$(is::true "${2:-}")
if ! "${is_ci}" && "${is_repo}"; then echo false; else echo true; fi
}
28 changes: 12 additions & 16 deletions src/first_time_setup/envvars.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/bin/bash
# @name envvars
# @deps utils/dir
# @brief A package for setting the environment variables.

set -euo pipefail

source 'src/first_time_setup/utils/dir.sh'

if [[ -z ${JURISDICTION_NAME+A} ]]; then
readonly JURISDICTION_NAME="${JURISDICTION_NAME:-}"
fi
Expand All @@ -24,32 +21,31 @@ if [[ -z ${DRY_MODE+A} ]]; then
fi

if [[ -z ${SCRIPT_PATH+A} ]]; then
_script_path=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
readonly SCRIPT_PATH=${_script_path}
unset _script_path
readonly SCRIPT_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
fi

if [[ -z ${DEV_MODE+A} ]]; then
_dev_mode=$(is::dev "${SCRIPT_PATH}")
readonly DEV_MODE=${_dev_mode}
unset _dev_mode
if [[ -f "${SCRIPT_PATH}/README.md" ]]; then
readonly DEV_MODE='false'
else
readonly DEV_MODE='true'
fi
fi

if [[ -z ${ROOT_PATH+A} ]]; then
_root_path=${SCRIPT_PATH}
if ${DEV_MODE}; then _root_path=$(cd "${_root_path}/../.." && pwd); fi
readonly ROOT_PATH=${_root_path}
unset _root_dir
if ${DEV_MODE}; then
readonly ROOT_PATH=$(cd "${SCRIPT_PATH}/../.." && pwd);
else
readonly ROOT_PATH=${SCRIPT_PATH}
fi
fi

if [[ -z ${ROOT_DIR+A} ]]; then
readonly ROOT_DIR=${ROOT_PATH##*/}
fi

if [[ -z ${PARENT_PATH+A} ]]; then
_parent_path=$(cd "${ROOT_PATH}/.." && pwd)
readonly PARENT_PATH=${_parent_path}
unset _parent_path
readonly PARENT_PATH=$(cd "${ROOT_PATH}/.." && pwd)
fi

if [[ -z ${PARENT_DIR+A} ]]; then
Expand Down
39 changes: 5 additions & 34 deletions src/first_time_setup/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

source 'src/first_time_setup/utils/boolean.sh'
source 'src/first_time_setup/utils/colours.sh'
source 'src/first_time_setup/utils/dir.sh'
source 'src/first_time_setup/utils/file.sh'
source 'src/first_time_setup/utils/git.sh'
source 'src/first_time_setup/utils/string.sh'
source 'src/first_time_setup/utils/subshell.sh'
source 'src/first_time_setup/utils/url.sh'
Expand Down Expand Up @@ -84,11 +82,9 @@ main() {
local -r first_commit='Initial import from OpenFisca-Extension_Template'
local -r second_commit='Customise extension_template through script'

# Print a welcome message.
# Check if we can continue.
colour::info "$(msg::welcome)"
# Gather information from environment variables.
status::gather_info "${name}" "${url}" "${interactive}"
# Check if we can continue.
status::check_continue "${ci}" "${repo}" "${persevere}" "${dry}"
if ! "${persevere}" && ! "${dry}"; then
echo ''
Expand All @@ -97,35 +93,28 @@ main() {
exit 2
fi

# Print a message to let the user know we are waiting for input.
echo ''
colour::task 'We will now start setting up your new package'

# Ask for the jurisdiction name.
# Process the jurisdiction name.
if [[ -z ${name} ]]; then echo ''; fi
while [[ -z ${name} ]]; do
echo -e -n "$(colour::user "$(msg::prompt_name)")"
IFS= read -r -p ' ' name
done
readonly name

# Remove spaces from the jurisdiction name.
local -r label=$(setup::name_label "${name}")
# Snake case the jurisdiction name.
local -r snake=$(setup::name_snake "${label}")

# Ask for the repository URL.
# Process the repository URL.
if [[ -z ${url} ]]; then echo ''; fi
while [[ -z ${url} ]]; do
echo -e -n "$(colour::user "$(msg::prompt_url)")"
IFS= read -r -p ' ' url
done
readonly url

# Process the repository folder.
local -r folder=$(setup::repository_folder "${url}")

# Print a summary of the information gathered.
status::pre_summary "${name}" "${snake}" "${url}"

# Shall we proceed?
Expand All @@ -144,23 +133,17 @@ main() {
echo -e "$(colour::logs "${continue}")"
if ! "${continue}"; then echo '' && exit 3; fi

# Now we can proceed with the setup
echo ''
colour::task 'Now we can proceed with the setup'

# Set package name.
local -r package="openfisca_${snake}"

# Set line to change in README.md
local -r lineno_readme=$(setup::readme_lineno)
if [[ ${lineno_readme} -eq -1 ]]; then
echo ''
main.error 'Could not find the last line number of the README.md section'
echo ''
exit 4
fi

# Set line to change in CHANGELOG.md
local -r lineno_changelog=$(setup::changelog_lineno)
if [[ ${lineno_changelog} -eq -1 ]]; then
echo ''
Expand All @@ -182,37 +165,25 @@ main() {
colour::logs "${first_commit}"
fi

# Replace default country_template references.
# And go on...
colour::pass 'Replace default extension_template references'
local -r files=$(git::ls_files "src/${module}")
setup::replace_references "${label}" "${snake}" "${name}" "${files}"

# Remove bootstrap instructions.
colour::pass 'Remove bootstrap instructions'
setup::remove_bootstrap_instructions "${lineno_readme}"

# Prepare README.md and CONTRIBUTING.md.
colour::pass 'Prepare README.md and CONTRIBUTING.md'
setup::prepare_readme_contributing "${url}"

# Prepare CHANGELOG.md.
colour::pass 'Prepare CHANGELOG.md'
setup::prepare_changelog "${lineno_changelog}"

# Prepare pyproject.toml.
colour::pass 'Prepare pyproject.toml'
setup::prepare_pyproject "${url}" "${folder}"

# Rename package to jurisdiction name.
colour::pass 'Rename package to:'
colour::logs "${package}"
if ! "${dry}"; then
setup::rename_package "${package}"
else
colour::warn 'Skipping renaming of package because of dry run'
fi

# Remove single use first time setup files.
colour::pass 'Remove single use first time setup files'
if ! "${dry}"; then
setup::remove_files
Expand All @@ -238,7 +209,7 @@ main() {
colour::pass "Second commit and first tag made on 'main' branch:"
colour::logs "${second_commit}"

# Print a goodbye message.
# And finish! :)
echo ''
colour::done "$(msg::byebye "${label}")"
echo ''
Expand Down
34 changes: 13 additions & 21 deletions src/first_time_setup/setup.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/bin/bash
# @name setup
# @deps utils/string utils/url
# @deps utils/file utils/string utils/url
# @brief Functions for setting up the new package.

set -euo pipefail

source 'src/first_time_setup/utils/colours.sh'
source 'src/first_time_setup/utils/file.sh'
source 'src/first_time_setup/utils/git.sh'
source 'src/first_time_setup/utils/string.sh'
source 'src/first_time_setup/utils/url.sh'

Expand All @@ -24,31 +22,23 @@ setup::name_label() {
# @description Snake case the jurisdiction name.
# @arg $1 The jurisdiction name label.
setup::name_snake() {
local name
name=$(string::snake "${1}")
echo "${name}"
string::snake "${1}"
}

# @description Get the repository folder.
# @arg $1 The repository URL.
setup::repository_folder() {
local folder
folder=$(url::folder "${1}")
echo "${folder}"
url::folder "${1}"
}

# @description Get the last line number of the bootstrapping section.
setup::readme_lineno() {
local lineno
lineno=$(file::find_lineno README.md '## Writing the Legislation')
echo "${lineno}"
file::find_lineno README.md '## Writing the Legislation'
}

# @description Get the last line number of the changelog section.
setup::changelog_lineno() {
local lineno
lineno=$(file::find_lineno CHANGELOG.md '# Example Entry')
echo "${lineno}"
file::find_lineno CHANGELOG.md '# Example Entry'
}

# @description First commit.
Expand All @@ -59,9 +49,10 @@ setup::first_commit() {
cd ..
mv "${1}" openfisca-"${2}"
cd openfisca-"${2}"
git::init 'main'
git::add '.'
git::commit "${3}" 'OpenFisca Bot <bot@openfisca.org>'
git init --initial-branch=main &>/dev/null 2>&1
git add .
git commit --no-gpg-sign --quiet --message "${3}" \
--author='OpenFisca Bot <bot@openfisca.org>'
}

# @description Replace default extension_template references
Expand Down Expand Up @@ -136,7 +127,8 @@ setup::remove_files() {
# @description Second commit and first tag.
# @arg $1 The second commit message.
setup::second_commit() {
git::add '.'
git::commit "${1}" 'OpenFisca Bot <bot@openfisca.org>'
git::tag '0.0.1'
git add .
git commit --no-gpg-sign --quiet --message "${1}" \
--author='OpenFisca Bot <bot@openfisca.org>'
git tag '0.0.1'
}
4 changes: 1 addition & 3 deletions src/first_time_setup/utils/boolean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ is::false() {
# @description Check if a value is true.
# @arg $1 The value to check.
is::true() {
local is_false
is_false=$(is::false "${1}")
if [[ ${is_false} == 'false' ]]; then echo true; else echo false; fi
[[ $(is::false "${1}") == 'false' ]] && echo true || echo false
}
Loading

0 comments on commit 9d4923b

Please sign in to comment.