# The Docker image to run the workspace in (defaults to gitpod/workspace-full)
image:
  file: Dockerfile

workspaceLocation: canvas/gitpod.code-workspace
checkoutLocation: canvas

vscode:
  extensions:
    - octref.vetur
    - esbenp.prettier-vscode
    - equinusocio.vsc-material-theme
    - equinusocio.vsc-community-material-theme
    - equinusocio.vsc-material-theme-icons
    - eamodio.gitlens
    - github.vscode-pull-request-github

github:
  prebuilds:
    # Enable for the master/default branch (defaults to true)
    master: true
    # Enable for all branches in this repo (defaults to false)
    branches: true
    # Enable for pull requests coming from this repo (defaults to true)
    pullRequests: true
    # Enable for pull requests coming from forks (defaults to false)
    pullRequestsFromForks: true
    # Add a check to pull requests (defaults to true)
    addCheck: true
    # Add a "Review in Gitpod" button as a comment to pull requests (defaults to false)
    addComment: true
    # Add a "Review in Gitpod" button to the pull request's description (defaults to false)
    addBadge: true
    # Add a label once the prebuild is ready to pull requests (defaults to false)
    addLabel: true

# Command to start on workspace startup (optional)
tasks:
  - init: |
      # Reload the .bash_profile to utilize the composer-link() function
      source ~/.bash_profile

      # Remove any existing Composer installation
      sudo rm /usr/bin/composer

      # Install Composer v2.0
      EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
      php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
      ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

      if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
      then
      >&2 echo 'ERROR: Invalid installer checksum'
      rm composer-setup.php
      exit 1
      fi

      php composer-setup.php --quiet
      RESULT=$?
      rm composer-setup.php

      # Make Composer globally accessible
      sudo mv composer.phar /usr/bin/composer

      cd /workspace

      # Install a fresh Laravel app
      composer create-project --prefer-dist laravel/laravel laravel

      cd /workspace/laravel

      # Create a new in-memory database
      touch database/database.sqlite

      # Write the database configuration to the .env file
      sed -i 's/DB_CONNECTION=mysql/DB_CONNECTION=sqlite/g' .env && sed -i 's/DB_DATABASE=laravel/#DB_DATABASE=laravel/g' .env

      # Link Canvas to the Laravel app
      composer-link ../canvas/
      composer require austintoddj/canvas @dev

      # Install Canvas and its resources
      php artisan canvas:install
      php artisan storage:link

      # Remove any existing assets from the Laravel app
      rm -rf public/vendor/canvas/*

      # Create a symlink to the local package assets
      cd public/vendor/canvas
      ln -s ../../../../canvas/public/* .

    command: |
      cd /workspace/laravel

      # Write the local app URL to the .env file
      export url=$(gp url 8000); sed -Ei "s|APP_URL=http://localhost|APP_URL=${url}|g" .env

      # Write the local asset URL to the .env file
      export url=$(gp url 8000); sed -Ei "1i\ASSET_URL=${url}" .env

      # Trust any proxy in a chain of proxies
      export url=$(gp url 8000); sed -Ei "s|APP_URL=http://localhost|APP_URL=${url}|g" .env
      sed -Ei "s|\\\$proxies|\\\$proxies = '*'|g" app/Http/Middleware/TrustProxies.php

      # Clear and cache the app configuration
      php artisan config:clear
      php artisan cache:clear
      php artisan config:cache

      # Serve the Laravel app
      php artisan serve

  - init: |
      # Initializing the Canvas package

      # Install core PHP dependencies
      composer install

      # Install core JS dependencies
      yarn

    command: |
      yarn watch

    openMode: split-right

# Ports to expose on workspace startup (optional)
ports:
  - port: 3306
    onOpen: ignore
  - port: 8000
    onOpen: notify
