image: php:8.0-fpm

stages:
  - quality
  - bundle
  - release

cache:
  paths:
    - vendor/
    - node_modules/

before_script:
  - apt-get update -y

  # Install git which is required by composer (the php image doesn't have it)
  - apt-get install git -y

  - apt-get install -y libicu-dev

  # Install intl PHP extension for tests
  - docker-php-ext-install intl

  # Install composer
  - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  - php composer-setup.php
  - php -r "unlink('composer-setup.php');"

  # Install NodeJS for NPM
  - curl -sL https://deb.nodesource.com/setup_lts.x | bash -
  - apt-get update && apt-get install -y nodejs

  # Install all php dependencies
  - php composer.phar install --prefer-dist --no-ansi --no-interaction --no-progress --ignore-platform-reqs

tests:
  stage: quality
  script:
    # run phpunit without code coverage
    # TODO: add code coverage
    - vendor/bin/phpunit --no-coverage

code-style:
  stage: quality
  script:
    - vendor/bin/ecs check --ansi

static-analysis:
  stage: quality
  script:
    # increase memory limit to 1GB because of script failure
    - php -d memory_limit=1G vendor/bin/phpstan analyse --ansi

code-review:
  stage: quality
  script:
    - vendor/bin/rector process --dry-run --ansi --verbose

bundle_app:
  stage: bundle
  script:
    # make scripts/bundle.sh executable
    - chmod +x ./scripts/bundle-prepare.sh
    - chmod +x ./scripts/bundle.sh

    # bundle castopod-host with commit ref as version
    - ./scripts/bundle-prepare.sh
    - ./scripts/bundle.sh ${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}
  artifacts:
    name: "castopod-host-${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}"
    paths:
      - castopod-host-${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}
  except:
    - main
    - beta
    - alpha

release_app:
  stage: release
  script:
    # make release scripts executable
    - chmod +x ./scripts/bundle-prepare.sh
    - chmod +x ./scripts/bundle.sh
    - chmod +x ./scripts/package.sh

    # prepare bundle before release, both bundle and package scripts will be run by semantic-release
    - ./scripts/bundle-prepare.sh

    # IMPORTANT: delete local git tags before release to prevent eventual script failure (ie. tag already exists)
    - git tag | xargs git tag -d

    # run semantic-release script (configured in `.releaserc.json` file)
    - npm run release
  only:
    - main
    - alpha
    - beta