name: Microweber build & upload

on:
  push:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

jobs:

  microweber-test-before-build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          repository: ${{ github.repository }}


      - name: Install Dependencies
        run: composer install

      - name: Dump autoload dependecies
        run: composer dump-autoload
      - name: Npm install
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: 'npm'
          cache-dependency-path: ./package-lock.json
      - run: npm install
      - run: npm run build
      - name: Test before build
        run: composer test:phpunit



  microweber-build-and-upload:
    runs-on: ubuntu-latest
    #needs: microweber-test-before-build
    steps:
      - uses: actions/checkout@v3
        with:
          repository: ${{ github.repository }}
      - name: Npm install
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: 'npm'
          cache-dependency-path: ./package-lock.json
      - run: npm install
      - run: npm run build

      - name: Install Dependencies
        run: composer install --no-dev

      - name: Dump autoload dependecies
        run: composer dump-autoload


      - name: Inject slug/short variables
        uses: rlespinasse/github-slug-action@v3.x

      - name: Zip the files
        run: |
          rm -rf .git
          rm -rf .github
          rm -rf .nmp
          rm -rf .idea
          rm -rf node_modules
          rm -rf .phpunit.cache
          rm -rf vendor/composer/tmp-*.zip
          rm -rf userfiles/modules/microweber/api/libs/mw-ui/grunt/plugins/tabler-ui/demo
          rm -rf userfiles/modules/microweber/api/libs/mw-ui/grunt/plugins/tabler-ui/src/static
          find . \( -name ".git" -o -name ".gitignore" -o -name ".gitmodules" -o -name ".gitattributes" \) -exec rm -rf -- {} +
          zip -r microweber-${{ env.GITHUB_REF_SLUG }}.zip `ls -A`
      - name: Upload zip
        run: |
          echo insecure >> ~/.curlrc
          curl -T microweber-${{ env.GITHUB_REF_SLUG }}.zip ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }} --no-epsv
          cp microweber-${{ env.GITHUB_REF_SLUG }}.zip microweber.zip
          curl -T microweber.zip ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}/ --ftp-create-dirs --no-epsv
      - name: Upload composer.json
        run: |
          cp -r composer.json microweber-${{ env.GITHUB_REF_SLUG }}.composer.json
          curl -T microweber-${{ env.GITHUB_REF_SLUG }}.composer.json ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}
          curl -T composer.json ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}/ --ftp-create-dirs --no-epsv
          curl -T composer.lock ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}/ --ftp-create-dirs --no-epsv
      - name: Upload version.txt
        run: |
          cp -r version.txt microweber-${{ env.GITHUB_REF_SLUG }}.version.txt
          curl -T microweber-${{ env.GITHUB_REF_SLUG }}.version.txt ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }} --no-epsv
          curl -T version.txt ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}/ --ftp-create-dirs --no-epsv
          curl -T CHANGELOG.md ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}/ --ftp-create-dirs --no-epsv
          curl -T README.md ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}/ --ftp-create-dirs --no-epsv


  microweber-build-and-upload-update-zip:
    runs-on: ubuntu-latest
    needs: microweber-build-and-upload
    steps:
      - uses: actions/checkout@v3
        with:
          repository: ${{ github.repository }}
      - name: Npm install
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: 'npm'
          cache-dependency-path: ./package-lock.json
      - run: npm install
      - run: npm run build

      - name: Install Dependencies
        run: composer install --no-dev

      - name: Dump autoload dependecies
        run: composer dump-autoload


      - name: Inject slug/short variables
        uses: rlespinasse/github-slug-action@v3.x

      - name: Zip the update files
        run: |
          rm -rf .git
          rm -rf .github
          rm -rf .idea
          rm -rf .nmp
          rm -rf node_modules
          rm -rf .phpunit.cache
          rm -rf vendor/composer/tmp-*.zip
          rm -rf userfiles/modules/microweber/api/libs/mw-ui/grunt/plugins/tabler-ui/demo
          rm -rf userfiles/modules/microweber/api/libs/mw-ui/grunt/plugins/tabler-ui/src/static
          rm -rf config/*
          rm -rf storage/*
          rm -rf app/*
          rm -rf bootstrap/*
          rm -rf database/*
          rm -rf tests/*
          rm -rf routes/*
          rm -rf userfiles/cache/*
          rm -rf userfiles/css/*
          rm -rf userfiles/media/*
          rm -rf userfiles/templates/*
          rm -rf favicon.ico
          rm -f *.php
          rm -f *.yml
          find . \( -name ".git" -o -name ".gitignore" -o -name ".gitmodules" -o -name ".gitattributes" \) -exec rm -rf -- {} +
          zip -r microweber-update-${{ env.GITHUB_REF_SLUG }}.zip `ls -A`
      - name: Upload zip
        run: |
          echo insecure >> ~/.curlrc
          cp microweber-update-${{ env.GITHUB_REF_SLUG }}.zip microweber-update.zip
          curl -T microweber-update.zip ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}/ --ftp-create-dirs --no-epsv
