|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Variables for configuration |
| 4 | +HUGO_SITE_DIR="./my-new-site" |
| 5 | +HUGO_SITE_NAME="My Test Site" |
| 6 | +HUGO_CONTENT_NAME="my-first-post" |
| 7 | +HUGO_THEME="ananke" |
| 8 | +HUGO_CONFIG_FILE="$HUGO_SITE_DIR/config.toml" |
| 9 | + |
| 10 | +# Help function |
| 11 | +function usage() { |
| 12 | + echo "Usage: ./hugo_setup_test.sh [--help]" |
| 13 | + echo "Automated Hugo setup testing script. Ensure you are running on Ubuntu in a WSL2 environment." |
| 14 | + exit 0 |
| 15 | +} |
| 16 | + |
| 17 | +# Check for --help option |
| 18 | +if [[ "$1" == "--help" ]]; then |
| 19 | + usage |
| 20 | +fi |
| 21 | + |
| 22 | +# Function to check if Hugo is installed |
| 23 | +function check_hugo_installed() { |
| 24 | + if ! command -v hugo &> /dev/null; then |
| 25 | + echo "Error: Hugo is not installed." |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +# Function to create a new Hugo site |
| 31 | +function create_hugo_site() { |
| 32 | + if [ -d "$HUGO_SITE_DIR" ]; then |
| 33 | + echo "Error: Directory $HUGO_SITE_DIR already exists. Please remove it before running the test." |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + hugo new site "$HUGO_SITE_DIR" |
| 37 | + if [ $? -ne 0 ]; then |
| 38 | + echo "Error: Failed to create Hugo site." |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +# Function to add a theme |
| 44 | +function add_theme() { |
| 45 | + cd "$HUGO_SITE_DIR" || exit |
| 46 | + git init && git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/$HUGO_THEME |
| 47 | + if [ $? -ne 0 ]; then |
| 48 | + echo "Error: Failed to add the theme." |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + echo 'theme = "ananke"' >> config.toml |
| 52 | +} |
| 53 | + |
| 54 | +# Function to create new content |
| 55 | +function create_content() { |
| 56 | + hugo new posts/$HUGO_CONTENT_NAME.md |
| 57 | + if [ $? -ne 0 ]; then |
| 58 | + echo "Error: Failed to create content." |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +} |
| 62 | + |
| 63 | +# Function to verify configuration |
| 64 | +function verify_configuration() { |
| 65 | + if grep -q 'theme = "ananke"' "$HUGO_CONFIG_FILE"; then |
| 66 | + echo "Success: Theme configuration found in $HUGO_CONFIG_FILE" |
| 67 | + else |
| 68 | + echo "Error: Theme configuration missing from $HUGO_CONFIG_FILE" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +# Function to run the server |
| 74 | +function run_hugo_server() { |
| 75 | + hugo server &>/dev/null & |
| 76 | + SERVER_PID=$! |
| 77 | + sleep 3 # Give the server some time to start |
| 78 | + if ps -p $SERVER_PID > /dev/null; then |
| 79 | + echo "Success: Hugo server started." |
| 80 | + kill $SERVER_PID |
| 81 | + else |
| 82 | + echo "Error: Failed to start the Hugo server." |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | +# Main execution |
| 88 | +check_hugo_installed |
| 89 | +create_hugo_site |
| 90 | +add_theme |
| 91 | +create_content |
| 92 | +verify_configuration |
| 93 | +run_hugo_server |
| 94 | + |
| 95 | +echo "All tests passed!" |
| 96 | +exit 0 |
0 commit comments