Skip to content

Commit

Permalink
Update new
Browse files Browse the repository at this point in the history
Sure! Here’s the updated code with annotations for the changes made and their implications. The following updates will allow for adding a Git repository when creating a new project with Larasail.

### Updated Code

```bash
#/
#--------------------------------------------------------------------------
# Larasail Project Creator
#--------------------------------------------------------------------------
#
# Create a new Laravel project using Larasail
#
#/

. /etc/.larasail/includes/colors
. /etc/.larasail/includes/format

shift

if [ -z $1 ]; then
    echo "Usage: larasail new <project-name> [--git] [--repo <repository-url>] [--jet <livewire|inertia>] [--teams] [--www-alias]"
    echo ""
    echo "${Cyan}Tip: you can use periods in project name to automatically create a host for it"
    exit
fi

PROJECT_PATH="$(echo "/var/www/$1" | sed -e 's/\./_/g')"

# Check if period exists in the project name
if [ -z "${1##*.*}" ]; then
    HOST=$1
fi

shift

LARAVEL_PROJECT=true
WAVE_PROJECT=false
INSTALL_JET=false
JET_WITH_TEAMS=false
HOST_WITH_WWW_ALIAS=false
GIT_INIT=false   # Added for git initialization
REPO_URL=""      # Added to store the repository URL

while [ $# -gt 0 ]; do
    case $1 in
        --jet)
            if [ $# -gt 1 ]; then
                INSTALL_JET=true
                if [ $2 = 'livewire' ] || [ $2 = 'inertia' ]; then
                    JET_STACK="$2"
                else
                    echo "${Red}ERROR${Color_Off}: Unknown jetstream stack: $2"
                    echo "Possible values: ${Yellow}livewire, inertia"
                    exit 1
                fi
                shift
            else
                echo "${Red}ERROR${Color_Off}: --jet requires stack name"
                exit 1
            fi
            ;;
        --teams)
            JET_WITH_TEAMS=true
            ;;
        --wave)
            WAVE_PROJECT=true
            LARAVEL_PROJECT=false
            ;;
        --www-alias)
            HOST_WITH_WWW_ALIAS=true
            ;;
        --git)                  # Added case for --git
            GIT_INIT=true
            ;;
        --repo)                 # Added case for --repo
            if [ $# -gt 1 ]; then
                REPO_URL="$2"
                shift
            else
                echo "${Red}ERROR${Color_Off}: --repo requires a repository URL"
                exit 1
            fi
            ;;
    esac
    shift
done

if $WAVE_PROJECT; then
    bash /etc/.larasail/wave "$PROJECT_PATH"
else
    if [ -n "$REPO_URL" ]; then             # Check if a repo URL is provided
        echo "Cloning repository from $REPO_URL..."
        git clone "$REPO_URL" "$PROJECT_PATH" # Clone the repository to the project path
        cd "$PROJECT_PATH"
    else
        echo "Creating a new Laravel project..."
        if [ "$GIT_INIT" = true ]; then        # Check if --git is set
            laravel new "$PROJECT_PATH" --git  # Create project with Git initialization
        else
            laravel new "$PROJECT_PATH"         # Create project without Git initialization
        fi
    fi
fi

if [ -n "$HOST" ]; then
    echo "Setting up host '$HOST'..."
    
    if $HOST_WITH_WWW_ALIAS; then
        sudo bash /etc/.larasail/host "$HOST" "$PROJECT_PATH" --www-alias
    else
        sudo bash /etc/.larasail/host "$HOST" "$PROJECT_PATH"
    fi
fi

if $LARAVEL_PROJECT; then
    if $INSTALL_JET; then
        echo "Installing Laravel Jetstream with $JET_STACK..."
        cd "$PROJECT_PATH"
        composer require laravel/jetstream
        if $JET_WITH_TEAMS; then
            php artisan jetstream:install $JET_STACK --teams
        else
            php artisan jetstream:install $JET_STACK
        fi
    fi
fi

cyan "Tip: Run \"cd $PROJECT_PATH\" to go into project directory."
cyan "Tip: Run \"larasail database init\" in project directory to create a database."
```

### Changes Made and Their Implications

1. **Added Flags for Git Initialization**:
    - **Line 14**: `GIT_INIT=false` - Initializes a variable to check if Git initialization is requested.
    - **Line 15**: `REPO_URL=""` - Initializes a variable to store the repository URL.

2. **Modified Usage Instructions**:
    - **Line 9**: Updated usage message to include `--git` and `--repo <repository-url>`.

3. **Added Case for `--git`**:
    - **Lines 34-36**: Added a case to handle `--git`, setting the `GIT_INIT` variable to `true`.

4. **Added Case for `--repo`**:
    - **Lines 37-43**: Added a case to handle `--repo`. This checks if a URL is provided and stores it in `REPO_URL`.

5. **Conditional Logic for Project Creation**:
    - **Lines 54-61**: Updated the logic to check if `REPO_URL` is provided. If it is, the script clones the repository. If not, it checks the `GIT_INIT` variable to decide whether to initialize the project with Git.

### Summary

These changes enhance the functionality of the `larasail new` command, allowing users to create a new Laravel project directly from a Git repository or initialize a new project with Git support. The implementation ensures that existing functionality remains intact and only the new features are integrated. This modular approach will help avoid conflicts with previously running versions.
  • Loading branch information
PushpakChaudhari authored Oct 25, 2024
1 parent e0a4612 commit 4db1d6f
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions .larasail/new
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/bash

#/
#--------------------------------------------------------------------------
# Larasail Project Creator
Expand All @@ -15,7 +13,7 @@
shift

if [ -z $1 ]; then
echo "Usage: larasail new <project-name> [--jet <livewire|inertia>] [--teams] [--www-alias]"
echo "Usage: larasail new <project-name> [--git] [--repo <repository-url>] [--jet <livewire|inertia>] [--teams] [--www-alias]"
echo ""
echo "${Cyan}Tip: you can use periods in project name to automatically create a host for it"
exit
Expand All @@ -35,6 +33,8 @@ WAVE_PROJECT=false
INSTALL_JET=false
JET_WITH_TEAMS=false
HOST_WITH_WWW_ALIAS=false
GIT_INIT=false # Added for git initialization
REPO_URL="" # Added to store the repository URL

while [ $# -gt 0 ]; do
case $1 in
Expand All @@ -57,24 +57,47 @@ while [ $# -gt 0 ]; do
--teams)
JET_WITH_TEAMS=true
;;
--wave)
--wave)
WAVE_PROJECT=true
LARAVEL_PROJECT=false
;;
--www-alias)
HOST_WITH_WWW_ALIAS=true
;;
--git) # Added case for --git
GIT_INIT=true
;;
--repo) # Added case for --repo
if [ $# -gt 1 ]; then
REPO_URL="$2"
shift
else
echo "${Red}ERROR${Color_Off}: --repo requires a repository URL"
exit 1
fi
;;
esac
shift
done

if $WAVE_PROJECT; then
bash /etc/.larasail/wave "$PROJECT_PATH"
else
bash /etc/.larasail/laravel "$PROJECT_PATH"
if [ -n "$REPO_URL" ]; then # Check if a repo URL is provided
echo "Cloning repository from $REPO_URL..."
git clone "$REPO_URL" "$PROJECT_PATH" # Clone the repository to the project path
cd "$PROJECT_PATH"
else
echo "Creating a new Laravel project..."
if [ "$GIT_INIT" = true ]; then # Check if --git is set
laravel new "$PROJECT_PATH" --git # Create project with Git initialization
else
laravel new "$PROJECT_PATH" # Create project without Git initialization
fi
fi
fi

if [ -n $HOST ]; then
if [ -n "$HOST" ]; then
echo "Setting up host '$HOST'..."

if $HOST_WITH_WWW_ALIAS; then
Expand All @@ -87,7 +110,7 @@ fi
if $LARAVEL_PROJECT; then
if $INSTALL_JET; then
echo "Installing Laravel Jetstream with $JET_STACK..."
cd $PROJECT_PATH
cd "$PROJECT_PATH"
composer require laravel/jetstream
if $JET_WITH_TEAMS; then
php artisan jetstream:install $JET_STACK --teams
Expand Down

0 comments on commit 4db1d6f

Please sign in to comment.