# Taken an adapted from gitlab's example repos:
# https://gitlab.com/gitlab-examples/julia

# An example .gitlab-ci.yml file to test (and optionally report the
# coverage results of) your [Julia][1] packages. Please refer to the
# [documentation][2] for more information about package development in
# Julia.
#
# Here, it is assumed that your Julia package is named
# `MyPackage`. Change it to whatever name you have given to your
# package.
#
# [1]: http://julialang.org/
# [2]: https://docs.julialang.org/en/v1/manual/documentation/index.html

variables:
  CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb/src/caosdb-julialib/testenv:latest

stages:
  - code-style
  - test
  - deploy

# Only check style for Julia 1.6 since support for 1.0 may be dropped anyway
code-style:
  stage: code-style
  image: julia:1.6
  tags: [ docker ]
  script:
    # install git
    - apt-get update && apt-get install -y git
    # install JuliaFormatter
    - julia  -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
    # try and format the files
    - julia  -e 'using JuliaFormatter; format(".", verbose=true)'
    # check the git diff for possible changes due to the formatting
    - julia -e '
      out = Cmd(`git diff --name-only`) |> read |> String;
      if out == ""
          exit(0);
      else
          @error "Some files have not been formatted !!!";
          write(stdout, out);
          exit(1);
      end'
  allow_failure: true


# Name a test and select an appropriate image.
# images comes from Docker hub
test:1.0:
  stage: test
  image: julia:1.0
  # Use `docker` runners
  tags: [ docker ]
  # Uncomment below if you would like to run the tests on specific
  # references only, such as the branches `master`, `development`,
  # etc.
  # only:
  #   - master
  #   - development
  script:
    # Let's run the tests. Substitute `coverage = false` below, if you
    # do not want coverage results. Use Pkg.clone for Julia 1.0
    - julia -e 'using Pkg; Pkg.clone(pwd());
      Pkg.build("CaosDB");
      Pkg.test("CaosDB"; coverage = true)'
      # Comment out below if you do not want coverage results.
    - julia -e 'using Pkg; Pkg.add("Coverage");
      import CaosDB;
      cd(joinpath(dirname(pathof(CaosDB)), ".."));
      using Coverage; cl, tl = get_summary(process_folder());
      println("(", cl/tl*100, "%) covered")'

test:1.6:
  stage: test
  image: julia:1.6
  # Use `docker` runners
  tags: [ docker ]
  # Uncomment below if you would like to run the tests on specific
  # references only, such as the branches `master`, `development`,
  # etc.
  # only:
  #   - master
  #   - development
  script:
    # Let's run the tests. Substitute `coverage = false` below, if you
    # do not want coverage results.
    - julia -e 'using Pkg; Pkg.add(path=pwd());
      Pkg.build("CaosDB");
      Pkg.test("CaosDB"; coverage = true)'
      # Comment out below if you do not want coverage results.
    - julia -e 'using Pkg; Pkg.add("Coverage");
      import CaosDB;
      cd(joinpath(dirname(pathof(CaosDB)), ".."));
      using Coverage; cl, tl = get_summary(process_folder());
      println("(", cl/tl*100, "%) covered")'


# REMARK: Do not forget to enable the coverage feature for your
# project, if you are using code coverage reporting above. This can be
# done by
#
# - Navigating to the `CI/CD Pipelines` settings of your project,
# - Copying and pasting the default `Simplecov` regex example
#   provided, i.e., `\(\d+.\d+\%\) covered` in the `test coverage
#   parsing` textfield.

# Example documentation deployment
pages:
  tags: [ cached-dind ]
  image: julia:1.6
  stage: deploy
  script:
    - apt-get update -qq && apt-get install -y git # needed by Documenter
    - julia -e 'using Pkg; Pkg.add(path=pwd()); Pkg.build("CaosDB");' # rebuild Julia (can be put somewhere else I'm sure)
    - julia -e 'using Pkg; import CaosDB; Pkg.add("Documenter")' # install Documenter
    - julia --color=yes docs/make.jl # make documentation
    - mv docs/build public # move to the directory picked up by Gitlab pages
  artifacts:
    paths:
      - public
  only:
    - main

# WARNING: This template is using the `julia` images from [Docker
# Hub][3]. One can use custom Julia images and/or the official ones
# found in the same place. However, care must be taken to correctly
# locate the binary file (`/opt/julia/bin/julia` above), which is
# usually given on the image's description page.
#
# [3]: https://hub.docker.com/_/julia/
