Skip to content

Commit

Permalink
Wrap the script in a main() function
Browse files Browse the repository at this point in the history
  • Loading branch information
jamonholmgren committed Nov 29, 2023
1 parent 44d503a commit da0496b
Showing 1 changed file with 151 additions and 148 deletions.
299 changes: 151 additions & 148 deletions src/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,182 +2,185 @@

# Qub CLI
# By @jamonholmgren & @knewter
main() {
VERSION="0.1.0"

VERSION="0.1.0"
# What OS are we running on?

# What OS are we running on?
OS=$(uname -s)

OS=$(uname -s)
# Colors

# Colors
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
DKGRAY='\033[1;30m'
CYAN='\033[0;36m'
GREEN='\033[0;32m'
END='\033[0m' # End Color

BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
DKGRAY='\033[1;30m'
CYAN='\033[0;36m'
GREEN='\033[0;32m'
END='\033[0m' # End Color
function replace_in_file {
local filename=$1
local variable_name=$2
local replace_with=$3
local os_type=$(uname -s)

function replace_in_file {
local filename=$1
local variable_name=$2
local replace_with=$3
local os_type=$(uname -s)
if [[ $os_type == "Darwin" ]]; then
sed -i '' "s/\$${variable_name}/${replace_with}/g" "$filename"
elif [[ $os_type == "Linux" ]]; then
sed -i "s/\$${variable_name}/${replace_with}/g" "$filename"
fi
}

if [[ $os_type == "Darwin" ]]; then
sed -i '' "s/\$${variable_name}/${replace_with}/g" "$filename"
elif [[ $os_type == "Linux" ]]; then
sed -i "s/\$${variable_name}/${replace_with}/g" "$filename"
fi
}

# Print header

echo ""
echo -e "${BLUE}Qub -- QBasic Website Generator${END}"
echo ""

# Print version and exit
# Print header

if [[ $1 == "-v" || $1 == "--version" ]]; then
echo "${VERSION}"
exit 0
fi

# Help command if provided -h or --help

if [[ $1 == "-h" || $1 == "--help" ]]; then
echo "Usage: qub [command] [options]"
echo ""
echo "Commands:"
echo " create Create a new Qub QB64 web project"
echo " setup-server Set up remote server for deployment (coming soon)"
echo ""
echo "Options:"
echo " -h, --help Show help"
echo " -v, --version Show version number"
echo ""
echo "Examples:"
echo " qub create"
echo -e "${BLUE}Qub -- QBasic Website Generator${END}"
echo ""
exit 0
fi

# Create command
# Print version and exit

if [[ $1 == "create" ]]; then
echo "Creating new Qub QB64 website project..."
echo ""

# If $DOMAIN isn't set, ask for it
if [[ -z $DOMAIN ]]; then
echo -e "${YELLOW}What domain will this be hosted on?${END} ${DKGRAY}(e.g. jamon.dev)${END}"
read DOMAIN
if [[ $1 == "-v" || $1 == "--version" ]]; then
echo "${VERSION}"
exit 0
fi

# If $DOMAIN is still empty, exit
if [[ -z $DOMAIN ]]; then
echo ""
echo -e "${RED}Domain name cannot be empty.${END}"
echo ""
exit 1
fi
# Help command if provided -h or --help

# Check for anything but numbers, letters, dashes, and periods
if [[ $DOMAIN =~ [^a-zA-Z0-9\.\-] ]]; then
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo "Usage: qub [command] [options]"
echo ""
echo -e "${RED}Domain name can only contain numbers, letters, dashes, and periods.${END}"
echo "Commands:"
echo " create Create a new Qub QB64 web project"
echo " setup-server Set up remote server for deployment (coming soon)"
echo ""
exit 1
fi

# Check if the folder exists (DOMAIN)
if [[ -d $DOMAIN ]]; then
echo "Options:"
echo " -h, --help Show help"
echo " -v, --version Show version number"
echo ""
echo -e "${RED}Folder already exists.${END}"
echo "Examples:"
echo " qub create"
echo ""
exit 1
exit 0
fi

# Make the folders
mkdir $DOMAIN
mkdir $DOMAIN/bin
mkdir -p $DOMAIN/web/pages
mkdir -p $DOMAIN/web/static
# Create command

GITHUB_TEMPLATE="https://raw.githubusercontent.com/jamonholmgren/qub/main/template"
if [[ $1 == "create" ]]; then
echo "Creating new Qub QB64 website project..."
echo ""

echo ""
echo -e "${GREEN}Creating project...${END}"
echo ""
# If $DOMAIN isn't set, ask for it
if [[ -z $DOMAIN ]]; then
echo -e "${YELLOW}What domain will this be hosted on?${END} ${DKGRAY}(e.g. jamon.dev)${END}"
read DOMAIN
fi

# If $DOMAIN is still empty, exit
if [[ -z $DOMAIN ]]; then
echo ""
echo -e "${RED}Domain name cannot be empty.${END}"
echo ""
exit 1
fi

# Check for anything but numbers, letters, dashes, and periods
if [[ $DOMAIN =~ [^a-zA-Z0-9\.\-] ]]; then
echo ""
echo -e "${RED}Domain name can only contain numbers, letters, dashes, and periods.${END}"
echo ""
exit 1
fi

# Check if the folder exists (DOMAIN)
if [[ -d $DOMAIN ]]; then
echo ""
echo -e "${RED}Folder already exists.${END}"
echo ""
exit 1
fi

# Make the folders
mkdir $DOMAIN
mkdir $DOMAIN/bin
mkdir -p $DOMAIN/web/pages
mkdir -p $DOMAIN/web/static

GITHUB_TEMPLATE="https://raw.githubusercontent.com/jamonholmgren/qub/main/template"

# Copy files from Github
curl -s $GITHUB_TEMPLATE/README.md > $DOMAIN/README.md
replace_in_file "$DOMAIN/README.md" "DOMAIN" "$DOMAIN"
echo -e "${GREEN}${END} README.md"
curl -s $GITHUB_TEMPLATE/app.bas > $DOMAIN/app.bas
echo -e "${GREEN}${END} app.bas"
curl -s $GITHUB_TEMPLATE/.gitignore > $DOMAIN/.gitignore
echo -e "${GREEN}${END} .gitignore"
curl -s $GITHUB_TEMPLATE/bin/install_qb64 > $DOMAIN/bin/install_qb64
echo -e "${GREEN}${END} bin/install_qb64"
curl -s $GITHUB_TEMPLATE/bin/build > $DOMAIN/bin/build
echo -e "${GREEN}${END} bin/build"
curl -s $GITHUB_TEMPLATE/web/pages/home.html > $DOMAIN/web/pages/home.html
replace_in_file "$DOMAIN/web/pages/home.html" "DOMAIN" "$DOMAIN"
echo -e "${GREEN}${END} web/pages/home.html"
curl -s $GITHUB_TEMPLATE/web/pages/contact.html > $DOMAIN/web/pages/contact.html
echo -e "${GREEN}${END} web/pages/contact.html"
curl -s $GITHUB_TEMPLATE/web/pages/404.html > $DOMAIN/web/pages/404.html
echo -e "${GREEN}${END} web/pages/404.html"
curl -s $GITHUB_TEMPLATE/web/static/scripts.js > $DOMAIN/web/static/scripts.js
echo -e "${GREEN}${END} web/static/scripts.js"
curl -s $GITHUB_TEMPLATE/web/static/styles.css > $DOMAIN/web/static/styles.css
echo -e "${GREEN}${END} web/static/styles.css"
curl -s $GITHUB_TEMPLATE/web/footer.html > $DOMAIN/web/footer.html
replace_in_file "$DOMAIN/web/footer.html" "DOMAIN" "$DOMAIN"
echo -e "${GREEN}${END} web/footer.html"
curl -s $GITHUB_TEMPLATE/web/header.html > $DOMAIN/web/header.html
echo -e "${GREEN}${END} web/header.html"
curl -s $GITHUB_TEMPLATE/web/head.html > $DOMAIN/web/head.html
echo -e "${GREEN}${END} web/head.html"

# Make the binary files executable
chmod +x $DOMAIN/bin/*

# Ask if the user wants to install QB64
echo ""
echo -e "${GREEN}Creating project...${END}"
echo ""

echo ""
echo -e "${YELLOW}Do you want to install QB64?${END} ${DKGRAY}(y/n)${END}"
read INSTALL_QB64
# Copy files from Github
curl -s $GITHUB_TEMPLATE/README.md > $DOMAIN/README.md
replace_in_file "$DOMAIN/README.md" "DOMAIN" "$DOMAIN"
echo -e "${GREEN}${END} README.md"
curl -s $GITHUB_TEMPLATE/app.bas > $DOMAIN/app.bas
echo -e "${GREEN}${END} app.bas"
curl -s $GITHUB_TEMPLATE/.gitignore > $DOMAIN/.gitignore
echo -e "${GREEN}${END} .gitignore"
curl -s $GITHUB_TEMPLATE/bin/install_qb64 > $DOMAIN/bin/install_qb64
echo -e "${GREEN}${END} bin/install_qb64"
curl -s $GITHUB_TEMPLATE/bin/build > $DOMAIN/bin/build
echo -e "${GREEN}${END} bin/build"
curl -s $GITHUB_TEMPLATE/web/pages/home.html > $DOMAIN/web/pages/home.html
replace_in_file "$DOMAIN/web/pages/home.html" "DOMAIN" "$DOMAIN"
echo -e "${GREEN}${END} web/pages/home.html"
curl -s $GITHUB_TEMPLATE/web/pages/contact.html > $DOMAIN/web/pages/contact.html
echo -e "${GREEN}${END} web/pages/contact.html"
curl -s $GITHUB_TEMPLATE/web/pages/404.html > $DOMAIN/web/pages/404.html
echo -e "${GREEN}${END} web/pages/404.html"
curl -s $GITHUB_TEMPLATE/web/static/scripts.js > $DOMAIN/web/static/scripts.js
echo -e "${GREEN}${END} web/static/scripts.js"
curl -s $GITHUB_TEMPLATE/web/static/styles.css > $DOMAIN/web/static/styles.css
echo -e "${GREEN}${END} web/static/styles.css"
curl -s $GITHUB_TEMPLATE/web/footer.html > $DOMAIN/web/footer.html
replace_in_file "$DOMAIN/web/footer.html" "DOMAIN" "$DOMAIN"
echo -e "${GREEN}${END} web/footer.html"
curl -s $GITHUB_TEMPLATE/web/header.html > $DOMAIN/web/header.html
echo -e "${GREEN}${END} web/header.html"
curl -s $GITHUB_TEMPLATE/web/head.html > $DOMAIN/web/head.html
echo -e "${GREEN}${END} web/head.html"

# Make the binary files executable
chmod +x $DOMAIN/bin/*

# Ask if the user wants to install QB64

if [[ $INSTALL_QB64 == "y" ]]; then
echo ""
echo -e "${YELLOW}Installing QB64...${END}"
echo -e "${YELLOW}Do you want to install QB64?${END} ${DKGRAY}(y/n)${END}"
read INSTALL_QB64

if [[ $INSTALL_QB64 == "y" ]]; then
echo ""
echo -e "${YELLOW}Installing QB64...${END}"
echo ""
pushd $DOMAIN
./bin/install_qb64
popd
fi

echo ""
echo -e "${GREEN}New QB64 website project created!${END}"
echo ""
echo -e "${YELLOW}Next steps:${END}"
echo ""
echo -e " ${CYAN}cd ${DOMAIN}${END}"
if [[ $INSTALL_QB64 != "y" ]]; then
echo -e " ${CYAN}./bin/install_qb64${END}"
fi
echo -e " ${CYAN}./bin/build${END}"
echo -e " ${CYAN}./app${END}"
echo ""
echo -e "${YELLOW}Support Qub development:${END}"
echo ""
echo -e " ${CYAN}Star the repo: ${DKGRAY}https://github.com/jamonholmgren/qub${END}"
echo -e " ${CYAN}Tell me what you're making: ${DKGRAY}https://twitter.com/jamonholmgren${END}"
echo ""
pushd $DOMAIN
./bin/install_qb64
popd
fi

echo ""
echo -e "${GREEN}New QB64 website project created!${END}"
echo ""
echo -e "${YELLOW}Next steps:${END}"
echo ""
echo -e " ${CYAN}cd ${DOMAIN}${END}"
if [[ $INSTALL_QB64 != "y" ]]; then
echo -e " ${CYAN}./bin/install_qb64${END}"
exit 0
fi
echo -e " ${CYAN}./bin/build${END}"
echo -e " ${CYAN}./app${END}"
echo ""
echo -e "${YELLOW}Support Qub development:${END}"
echo ""
echo -e " ${CYAN}Star the repo: ${DKGRAY}https://github.com/jamonholmgren/qub${END}"
echo -e " ${CYAN}Tell me what you're making: ${DKGRAY}https://twitter.com/jamonholmgren${END}"
echo ""
}

exit 0
fi
main "$@"

0 comments on commit da0496b

Please sign in to comment.