prepare prefixed vendor:
  image: gitlab.wpdesk.dev:5050/wpdesk/docker-tests/php-box:8.3.2
  artifacts:
    name: "vendor_prefixed"
    paths:
      - assets
      - vendor
      - vendor_prefixed
      # wpdesk/wp-init default cache directory for plugin.php file
      - generated
  tags:
    - vendor
  stage: prepare-vendor
  except:
    variables:
      - $IS_NPM_LIBRARY
  interruptible: true
  before_script:
    - !reference [.composer-auth, script]
    - echo ${WPDESK_CI_VERSION}
    - php --version
  script:
    - composer install
    # For plugins using wpdesk/wp-init, but only after v0.10. This has to be changed later, when library hits v1
    - composer show -q wpdesk/wp-init ^0.10 >/dev/null 2>&1 && vendor/bin/wpinit ./generated/ || echo "Not using wpdesk/wp-init"
    - rm -f auth.json

prepare npm assets:
  image: node:20.18-slim
  artifacts:
    name: "node_assets"
    paths:
      - assets
      - build
  needs:
      - prepare prefixed vendor
  cache:
    key: ${CI_PROJECT_NAME}-node
    paths:
      - node_modules/
  tags:
    - vendor
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - exists:
      - package.json
      - tests/codeception/prepare_npm.sh
    - if: $IS_NPM_LIBRARY
      when: never
  stage: prepare-vendor
  interruptible: true
  before_script:
    - mkdir -p /cache/npm-cache
    - npm config set cache /cache/npm-cache --global
    - npm config set -- //${CI_SERVER_HOST}/:_authToken=${CI_DEPLOY_TOKEN}
    - npm config set @octolize:registry=https://${CI_SERVER_HOST}/api/v4/packages/npm/
    - npm config set @wpdesk:registry=https://${CI_SERVER_HOST}/api/v4/packages/npm/
    - npm config list
  script:
    - if [[ -f package.json ]]; then npm install --no-audit --progress=false; fi
    - if [[ -f package.json ]]; then npm run prod; fi
    - if [[ -f tests/codeception/prepare_npm.sh ]]; then sh tests/codeception/prepare_npm.sh; fi

prepare tests:
  image:
    name: gitlab.wpdesk.dev:5050/wpdesk/docker-tests/codeception:13.1.0
    entrypoint: [ "" ]
  tags:
    - vendor
  stage: prepare-vendor
  artifacts:
    name: "dependent_plugins"
    paths:
      - tests/dependent_plugins
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - exists:
      - tests/integration/prepare.sh
      - tests/codeception/prepare.sh
    - if: $IS_LIBRARY || $IS_NPM_LIBRARY
      when: never
  interruptible: true
  before_script:
    - cp /usr/local/bin/clone /tmp/clone.sh
    - !reference [.composer-auth, script]
    - echo ${WPDESK_CI_VERSION}
    - php --version
  script:
    - echo "xdebug.max_nesting_level=2560" >> /usr/local/etc/php/php.ini
    - export DEPENDENT_PLUGINS_DIR=${CI_PROJECT_DIR}/tests/dependent_plugins
    - echo ${DEPENDENT_PLUGINS_DIR}
    - echo ${WPDESK_CI_VERSION}
    - mkdir -p ${DEPENDENT_PLUGINS_DIR}
    - if [[ -f tests/integration/prepare.sh ]]; then sh tests/integration/prepare.sh; fi
    - if [[ -f tests/codeception/prepare.sh ]]; then sh tests/codeception/prepare.sh; fi