Skip to content

Commit

Permalink
feat (common): #15 add common scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaximo33 committed Dec 31, 2023
1 parent 4867be3 commit e180749
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 0 deletions.
18 changes: 18 additions & 0 deletions console/common/tasks/datetime
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail

# Usaged
# Obtener la fecha y hora actual en el formato AAAA-MM-DD HH:MM:SS
# formatted_datetime=$(get_formatted_datetime)
# echo "Fecha y hora actual: $formatted_datetime"
# Obtener la fecha actual en el formato DD/MM/AAAA
# formatted_date=$(get_formatted_datetime "%d-%m-%Y")
# echo "Fecha actual: $formatted_date"
# Obtener la hora actual en el formato HH:MM:SS
# formatted_time=$(get_formatted_datetime "%H:%M:%S")
# echo "Hora actual: $formatted_time"
_mod_.get_formatted_datetime() {
local format="${1:-%Y-%m-%d %H:%M:%S}" # Formato por defecto: AAAA-MM-DD HH:MM:SS
local datetime=$(awk "BEGIN {print strftime(\"$format\")}")
echo "$datetime"
}
10 changes: 10 additions & 0 deletions console/common/tasks/directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail

_mod_.exists() {
[ -d "$1" ]
}

_mod_.create_recursive() {
mkdir -p "$1"
}
6 changes: 6 additions & 0 deletions console/common/tasks/file
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -euo pipefail

_mod_.exists() {
[ -e "$1" ]
}
13 changes: 13 additions & 0 deletions console/common/tasks/os
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail

_mod_.get_operating_system() {
local os=""
case "$OSTYPE" in
linux-gnu) os="Linux" ;;
darwin*) os="macOS" ;;
cygwin|msys) os="Windows" ;;
*) os="Undefined" ;;
esac
echo "$os"
}
53 changes: 53 additions & 0 deletions console/common/tasks/prompt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
set -euo pipefail

# Read a single char from /dev/tty, prompting with "$*"
# Note: pressing enter will return a null string. Perhaps a version terminated with X and then remove it in caller?
# See https://unix.stackexchange.com/a/367880/143394 for dealing with multi-byte, etc.
_mod_.get_keypress() {
local REPLY IFS=
printf >/dev/tty '%s' "$*"
#[[ $ZSH_VERSION ]] && read -rk1 # Use -u0 to read from STDIN
# See https://unix.stackexchange.com/q/383197/143394 regarding '\n' -> ''
[[ $BASH_VERSION ]] && read </dev/tty -rn1
printf '%s' "$REPLY"
}

# Get a y/n from the user, return yes=0, no=1 enter=$2
# Prompt using $1.
# If set, return $2 on pressing enter, useful for cancel or defualting
_mod_.get_yes_keypress() {
local prompt="${1:-Are you sure [y/n]? }"
local enter_return=$2
local REPLY
# [[ ! $prompt ]] && prompt="[y/n]? "
while REPLY=$(_mod_.get_keypress "$prompt"); do
[[ $REPLY ]] && printf '\n' # $REPLY blank if user presses enter
case "$REPLY" in
Y | y) return 0 ;;
N | n) return 1 ;;
'') [[ $enter_return ]] && return "$enter_return" ;;
esac
done
}

# Credit: http://unix.stackexchange.com/a/14444/143394
# Prompt to confirm, defaulting to NO on <enter>
# Usage: _mod_.confirm_default_no "Dangerous. Are you sure?" && action_function
_mod_.confirm_default_no() {
local prompt="${*:-Are you sure} [y/N]? "
_mod_.get_yes_keypress "$prompt" 1
}

# Prompt to confirm, defaulting to YES on <enter>
# Usage:
# _mod_.confirm_default_yes "question?" && action_function
_mod_.confirm_default_yes() {
local prompt="${*:-Are you sure} [Y/n]? "
_mod_.get_yes_keypress "$prompt" 0
}

_mod_.request_input() {
read -r -p "$@" value
echo $value
}
10 changes: 10 additions & 0 deletions console/common/tasks/string
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail

# Usage
# Call: _mod_.validate_project_name "this is test"
# Response: thisIsTest
_mod_.convert_string_to_camelcase(){
local input="$@"
echo "$input" | awk 'BEGIN{OFS=""};{for(j=1;j<=NF;j++){ if(j==1){$j=tolower($j)} else {$j=toupper(substr($j,1,1)) tolower(substr($j,2)) }}}1'
}
11 changes: 11 additions & 0 deletions console/efde/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -euo pipefail

MODULE_PATH=$(dirname "${BASH_SOURCE[0]}")
MODULE_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")"

common.load_module $MODULE_PATH




74 changes: 74 additions & 0 deletions console/efde/tasks/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
set -euo pipefail

CONFIG_FILE="config.env"
CONFIG_PATH="$EFDE_PATH_INSTALL/bin"
CONFIG_PATH_FILE="$CONFIG_PATH/$CONFIG_FILE"

_mod_.check_config() {
if ! common.tasks.file.exists "$CONFIG_PATH_FILE"; then
_mod_.create_config
fi
_mod_.load_config
}

_mod_.create_config() {
common.tasks.message.warning "Crearemos tu configuracion"
_mod_.set_config
}

_mod_.set_config(){
#autoupdate #days
#lenguaje #es_ES en_US pt_PT
#my_options=("es_ES" "en_US" "pt_PT")
#common.tasks.menu.show_menu --question="¿Que lenguaje?" --options="es_ES,en_US,pt_PT"
result=$(common.tasks.menu.show_menu --question="¿Que lenguaje?" --options="es_ES,en_US,pt_PT")
echo $result
exit
# Definir el array asociativo con los pares "key=value"
declare -A env_vars=(
[VERSION]=$(_mod_.set_config_version)
[SO]=$(common.tasks.os.get_operating_system)
[LAST_UPDATE]=$(common.tasks.datetime.get_formatted_datetime "%Y-%m-%d")
)

echo -e "$(_mod_.prepare_string_to_env env_vars)" > $CONFIG_PATH_FILE
}

_mod_.set_config_version() {
echo $(efde.tasks.git.get_current_tag)
}

_mod_.load_config() {
echo "load config"
}

_mod_.show_config() {
echo "t"
}

# USAGED
# Call:
# SETUP_ENVIRONMENT=(
# [NAME]="MyName"
# [CREATE_AT]="2000-01-01"
# [IMPLEMENTION]="efde"
# )
# _mod_.prepare_string_to_env SETUP_ENVIRONMENT
#
# Response:
# 'NAME="MyName"\nCREATE_AT="2000-01-01"\nIMPLEMENTION="efde"'
_mod_.prepare_string_to_env(){
local -n env_array=$1
local env_str=""

for key in "${!env_array[@]}"; do
value="${env_array[$key]}"
env_str+="\n$key=\"$value\""
done

env_str="${env_str:2}" # Remove firts \n

echo "$env_str"
}

7 changes: 7 additions & 0 deletions console/efde/tasks/git
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail

_mod_.get_current_tag(){
cd $EFDE_PATH_INSTALL
echo $(git describe --tags --abbrev=0)
}
53 changes: 53 additions & 0 deletions console/efde/tasks/implemention
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
set -euo pipefail
IMPLEMENTION_EFDE_FOLDER=".efde"

declare -gA SETUP_ENVIRONMENT=(
[NAME]=""
[CREATE_AT]=""
[IMPLEMENTION]=""
)

_mod_.has_folder_implementation(){
common.tasks.directory.exists "$(pwd)/$IMPLEMENTION_EFDE_FOLDER"
}

_mod_.create_folder(){
common.tasks.directory.create_recursive "$(pwd)/$1"
}

_mod_.setup_environment(){
local FUNCTION_INSTALL=$(printf "%s.tasks.install.environment" $RESPONSE_CODE)
local STRING_ENV=""


if ! common.tasks.module.exists_function "$FUNCTION_INSTALL" ;then
common.tasks.message.danger "The installation of the selected environment is not yet available '$RESPONSE_TITLE'"
common.tasks.prompt.confirm_default_yes "Back to menu?" && menu_init
fi

SETUP_ENVIRONMENT=(
[NAME]=$(_mod_.project_name)
[CREATE_AT]=$(common.tasks.datetime.get_formatted_datetime "%Y-%m-%d")
[IMPLEMENTION]=$RESPONSE_CODE
)

_mod_.create_folder "${SETUP_ENVIRONMENT['NAME']}/$IMPLEMENTION_EFDE_FOLDER"
STRING_ENV="$(efde.tasks.config.prepare_string_to_env SETUP_ENVIRONMENT)"
echo -e "$STRING_ENV" > "${SETUP_ENVIRONMENT['NAME']}/$IMPLEMENTION_EFDE_FOLDER/env"

cd "${SETUP_ENVIRONMENT['NAME']}"
$FUNCTION_INSTALL
}

_mod_.project_name(){
local value=$(common.tasks.prompt.request_input "$(common.tasks.message.warning "Enter the project name\nProject name: ")")

if ! [[ "$value" =~ ^[[:alnum:][:space:]]+$ ]]; then
common.tasks.message.danger "The entered value can only contain letters or numbers."
common.tasks.message.info "Cancel to [ctrl + c] "
_mod_.project_name
fi

echo "$(common.tasks.string.convert_string_to_camelcase "$value")"
}
7 changes: 7 additions & 0 deletions console/symfony/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail

MODULE_PATH=$(dirname "${BASH_SOURCE[0]}")
MODULE_NAME="$(basename "$(dirname "${BASH_SOURCE[0]}")")"

common.load_module $MODULE_PATH
16 changes: 16 additions & 0 deletions console/symfony/props/menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

MENU_INIT=(
'tools,Tools, '
'symfony,Install Symfony, '
'magento,Coming soon - Install Magento | Wordpress | Prestashop | Drupal | React | Angular | Vue | Next, '
# 'magento,Coming soon - Install Magento, '
# 'wordpress,Coming soon - Install Wordpress, '
# 'woocommerce,Coming soon - Install WooCommerce, '
# 'prestashop,Coming soon - Install Prestashop, '
# 'drupal,Coming soon - Install Drupal, '
# 'react,Coming soon - Install React, '
# 'angular,Coming soon - Install Angular, '
# 'vue,Coming soon - Install Vue, '
# 'next,Coming soon - Install Next, '
)
12 changes: 12 additions & 0 deletions console/symfony/tasks/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail

_mod_.environment(){
echo "INSTALAR SYMFONY $(pwd)"
echo $PATH_CONSOLE
}

_mod_.move_environment(){
#mv $PATH_SETUP/symfony $(pwd)/
echo "te"
}

0 comments on commit e180749

Please sign in to comment.