---
kind: pipeline
name: build
type: docker

workspace:
  path: "/app"

platform:
  os: linux
  arch: amd64

environment:
  PUB_CACHE: /app/.pub_cache

clone:
  git:
    image: plugins/git:next

steps:
  - name: load_packages
    pull: always
    image: jonasfranz/flutter:2.10.1
    commands:
      - flutter packages get

  - name: build
    pull: always
    image: jonasfranz/flutter:2.10.1
    depends_on:
      - load_packages
    commands:
      - flutter build apk

  - name: lint
    pull: always
    image: jonasfranz/flutter:2.10.1
    depends_on:
      - load_packages
    commands:
      - flutter analyze
      - flutter format --set-exit-if-changed lib

  - name: test
    pull: always
    image: jonasfranz/flutter:2.10.1
    depends_on:
      - load_packages
    commands:
      - flutter test -r expanded

---
kind: pipeline
name: metrics
type: docker

depends_on:
  - build

workspace:
  path: "/app"


environment:
  PUB_CACHE: /app/.pub_cache

platform:
  os: linux
  arch: amd64

steps:
  - name: load_packages
    pull: always
    image: jonasfranz/flutter:2.10.1
    commands:
      - flutter packages get
####### BENCHMARKS
  - name: run_benchmarks
    pull: always
    image: jonasfranz/flutter:2.10.1
    depends_on:
      - load_packages
    commands:
      - flutter test --plain-name=Efficiency -r expanded > benchmarks.txt
####### METRICS
  - name: generate_metrics
    pull: always
    image: jonasfranz/flutter:2.10.1
    depends_on:
      - load_packages
    commands:
      - flutter pub run dart_code_metrics:metrics analyze lib -r html
  - name: convert_to_latex
    pull: always
    image: pandoc/core:2.17
    depends_on:
      - generate_metrics
    commands:
      - pandoc --template template.tex -s metrics/index.html -o metrics.tex
      - 'sed "s/All files/Metriken: ${DRONE_COMMIT_BRANCH}/g" > tmp; mv tmp metrics.tex'
      - 'sed "s/all-files/metrics:${DRONE_COMMIT_BRANCH}/g" > tmp; mv tmp metrics.tex'
      - apk --no-cache add bash sed
      - bash /app/headers.sh

  ###### PUBLISHING
  - name: pull_and_prepare_thesis_repo
    image: docker:git
    depends_on:
      - convert_to_latex
      - run_benchmarks
    commands:
      - git clone https://git.jonasfranz.software/KoSI/thesis.git
      - mkdir -p thesis/results/${DRONE_COMMIT_BRANCH}
      - cp metrics.tex thesis/results/${DRONE_COMMIT_BRANCH}/
      - cp benchmarks.txt thesis/results/${DRONE_COMMIT_BRANCH}/
      - cd thesis
      - git add results/
      - git status

  - name: push_matrix
    depends_on:
      - pull_and_prepare_thesis_repo
    image: appleboy/drone-git-push
    settings:
      ssh_key:
        from_secret: ssh_key
      branch: main
      remote: git@git.jonasfranz.software:KoSI/thesis.git
      force: false
      commit: true
      path: /app/thesis
      commit_message: Update metrics