Scafold Project Structure

Create Project Structure

nano <PROJECT_NAME>.txt
wallpaper_manager # Init Folder
    main.py                 # The application's entry point.
    assets/                 # For icons.
    src/
        __init__.py         # Makes 'src' a package.
        main_window.py      # The main application window.
        wallpaper_handler.py# Logic for finding and setting wallpapers.
        widgets/
            __init__.py     # Makes 'widgets' a package.
            wallpaper_grid.py# The custom widget.

Create SH script to Scafold Project

nano create_project_structure.sh
#!/bin/bash

# A script to scaffold a project structure defined by indentation or lack thereof.

# --- Setup and Input Check ---

INPUT=$(cat)
if [ -z "$INPUT" ]; then
    echo "Error: No input provided. Please pipe a structure into the script."
    exit 1
fi

# START Folder
PROJECT_NAME=$(echo "$INPUT" | head -n 1 | sed -E 's/^[[:space:]]*([^/[:space:]]+)\/?.*/\1/')

# --- Sequential Execution Logic (Wrapped in Subshell) ---
(
    # 1. Create and navigate into the root directory immediately.
    mkdir -p "$PROJECT_NAME"
    if [ $? -ne 0 ]; then
        echo "❌ Error: Could not create the root directory '$PROJECT_NAME'."
        exit 1
    fi
    cd "$PROJECT_NAME"
    echo "   Navigated to: $(pwd)"

    # Variables for tracking state
    declare -a PATH_STACK=() # Holds the current directory path hierarchy (e.g., [src, widgets])
    LAST_INDENT=-1           # Tracks the indentation of the previous line

    # 2. Start at second line and process all lines sequentially 
    echo "$INPUT" | tail -n +2 | while IFS='#' read -r line_content comment; do
        
        # a) Calculate indentation
        if [[ "$line_content" =~ ^([[:space:]]*) ]]; then
            INDENT=${#BASH_REMATCH[1]}
        else
            INDENT=0
        fi
        
        # b) Clean the item name
        ITEM_NAME=$(echo "$line_content" | sed -E 's/^[[:space:]]*//; s/[[:space:]]*$//')
        comment=$(echo "$comment" | sed 's/^[[:space:]]*//')

        if [[ -z "$ITEM_NAME" ]]; then
            continue # Skip empty lines
        fi

        # c) Adjust PATH_STACK based on indentation change
        if [ "$INDENT" -lt "$LAST_INDENT" ]; then
            # Pop elements off the stack if we moved to a shallower indent
            LEVELS_TO_POP=$((LAST_INDENT - INDENT))
            PATH_STACK=("${PATH_STACK[@]:0:${#PATH_STACK[@]}-LEVELS_TO_POP}")
        fi
        
        # d) Construct the full path and create the item
        
        # Determine the current path context based on the stack
        CURRENT_CONTEXT=$(IFS=/; echo "${PATH_STACK[*]}")
        
        # The path for creation combines the context and the item name
        RELATIVE_PATH="$CURRENT_CONTEXT/$ITEM_NAME"
        RELATIVE_PATH="${RELATIVE_PATH#*/}" # Clean leading slash if stack is empty
        
        if [[ "$ITEM_NAME" =~ /$ ]]; then
            # Directory: Create and push onto stack
            DIR_PATH="${ITEM_NAME%%/}" # Remove trailing slash
            echo "   Creating dir:  $RELATIVE_PATH"
            mkdir -p "$RELATIVE_PATH"
            PATH_STACK+=("$DIR_PATH") # Add the new directory to the stack
        else
            # File: Ensure parent dir exists and create file
            DIR=$(dirname "$RELATIVE_PATH")
            mkdir -p "$DIR" 2>/dev/null
            
            echo "   Creating file: $RELATIVE_PATH"
            echo "# $comment" > "$RELATIVE_PATH"
        fi

        # e) Update state for the next line
        LAST_INDENT=$INDENT
    done

# END of the subshell (reverts the directory change)
)

# --- Final Confirmation ---
echo ""
echo "✅ Project structure for '$PROJECT_NAME' created successfully!"
echo "Current directory is: $(pwd)"

Make Script Executable

chmod +x ./create_project_structure.sh

Execute Scafolding of Project

  • Version 1
cat <PROJECT_NAME>.txt | ./create_project_structure.sh
  • Version 2
./create_project_structure.sh << 'EOF'
wallpaper_manager
    main.py                 # The application's entry point.
    assets/                 # For icons.
    src/
        __init__.py         # Makes 'src' a package.
        main_window.py      # The main application window.
        wallpaper_handler.py# Logic for finding and setting wallpapers.
        widgets/
            __init__.py     # Makes 'widgets' a package.
            wallpaper_grid.py# The custom widget.
EOF

Status

Completed.