name: Microweber build & upload unstable

on:
  push:

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

jobs:
  microweber-build-and-upload-unstable:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          repository: ${{ github.repository }}


      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"
      - name: Cache composer dependencies
        uses: actions/cache@v3
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          # Use composer.json for key, if composer.lock is not committed.
          # key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - 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: 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 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 }}-unstable.zip `ls -A`
      - name: Upload zip
        run: |
          echo insecure >> ~/.curlrc
          curl -T microweber-${{ env.GITHUB_REF_SLUG }}-unstable.zip ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }} --no-epsv
          cp microweber-${{ env.GITHUB_REF_SLUG }}-unstable.zip microweber.zip
          curl -T microweber.zip ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}-unstable/ --ftp-create-dirs --no-epsv
      - name: Upload composer.json
        run: |
          cp -r composer.json microweber-${{ env.GITHUB_REF_SLUG }}-unstable.composer.json
          curl -T microweber-${{ env.GITHUB_REF_SLUG }}-unstable.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 }}-unstable/ --ftp-create-dirs --no-epsv
          curl -T composer.lock ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}-unstable/ --ftp-create-dirs --no-epsv
      - name: Upload version.txt
        run: |
          cp -r version.txt microweber-${{ env.GITHUB_REF_SLUG }}-unstable.version.txt
          curl -T microweber-${{ env.GITHUB_REF_SLUG }}-unstable.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 }}-unstable/ --ftp-create-dirs --no-epsv
          curl -T CHANGELOG.md ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}-unstable/ --ftp-create-dirs --no-epsv
          curl -T README.md ftp://${{ secrets.FTP_USERNAME }}:${{ secrets.FTP_PASSWORD }}@${{ secrets.FTP_HOST }}/builds/${{ env.GITHUB_REF_SLUG }}-unstable/ --ftp-create-dirs --no-epsv
