# Dependencies are managed in the Dockerfile in the inkscape-ci-docker
# Git repository. Change them there, wait a bit, and then we'll use
# the new ones here in these builds.
image: registry.gitlab.com/inkscape/inkscape-ci-docker/master

variables:
  GIT_DEPTH: "10"
  GIT_SUBMODULE_STRATEGY: recursive


# Building inkscape
inkscape:linux:
  stage: build
  except:
    - schedules
  cache:
    key: "cache-linux"
    paths:
      - ccache/
  before_script:
    # CCache Config
    - mkdir -p ccache
    - export CCACHE_BASEDIR=${PWD}
    - export CCACHE_DIR=${PWD}/ccache
  script:
    - mkdir -p build/conf
    - ln -s . share/inkscape
    - cd build
    - cmake .. -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX:PATH=$PWD/install_dir/
    - make -j3 install
    - make -j3 tests
    - cpack -G DEB
    - cd ..
  artifacts:
    expire_in: 1 year
    paths:
      - build/

inkscape:mac:
  stage: build
  tags:
    - mac
  only:
    - branches@inkscape/inkscape
  except:
    - schedules
  cache:
    key: "cache-mac"
    paths:
      - ccache/
  before_script:
    - brew update
    - brew upgrade
    # Tools
    - brew install cmake intltool libtool pkg-config ccache
    # Libraries
    - brew install bdw-gc boost gdl gettext gsl gtkmm3 gtkspell3 imagemagick@6 jemalloc libcdr libvisio libyaml libwpg pango popt poppler potrace libsoup
    # Testing tools
    # TODO
    # Cleanup
    - brew cleanup
    # CCache Config
    - mkdir -p ccache
    - export CCACHE_BASEDIR=${PWD}
    - export CCACHE_DIR=${PWD}/ccache
  script:
    - mkdir -p build
    - cd build
    - cmake .. -DCMAKE_PREFIX_PATH="/usr/local/opt/gettext;/usr/local/opt/imagemagick@6/;/usr/local/opt/icu4c/" -DWITH_OPENMP=OFF -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_BUILD_TYPE=Debug
    - make -j1
    - cd ..
  artifacts:
    expire_in: 1 year
    paths:
      - build/
  allow_failure: true


codequality:
  stage: build
  except:
    - schedules
  script:
    - git fetch https://gitlab.com/inkscape/inkscape.git
    - echo "Your commit does not comply exactly with the clang-format style checker.\nPlease review the following diff or apply it with\n \$ git apply <<EOF"
    - git diff FETCH_HEAD -U0 --no-color | clang-format-diff-6.0 -p1 -style file | tee clang_format_diff
    - echo "EOF"
    - if [[ -s clang_format_diff ]]; then false; fi
  allow_failure: true

codeprotect:
  stage: build
  except:
    - schedules
  script:
    - git fetch https://gitlab.com/inkscape/inkscape.git
    - git diff FETCH_HEAD -U0 --no-color src/2geom/ src/3rdparty/adaptagrams/ src/3rdparty/libdepixelize src/3rdparty/libuemf | tee forbidden
    - if [[ -s forbidden ]]; then echo "This commit modifies files that are in an dependency library. Make sure you update upstream first, *then* force the merge in Inkscape."; false; fi

source-doc:
  stage: build
  only:
    variables:
      - $SCHEDULED_JOB_NAME == "source-doc"
  script:
    - cd buildtools
    - doxygen Doxyfile
    - doxygen Doxyfile-extensions
    - cd ..
  artifacts:
    paths:
      - doc/doxygen
      - doc/doxygen-extensions

# This job is a static analysis build by clang.
# It takes MORE THAN 3 HOURS, and depending on worker sometimes 4 hours.
# Make sure the timeout of the build is big enough
clang:
  stage: build
  only:
    variables:
      - $SCHEDULED_JOB_NAME == "clang"
  script:
    - apt-get update -yqq && apt-get install -y clang
    - mkdir -p clang-build
    - cd clang-build
    - scan-build cmake .. -DCMAKE_BUILD_TYPE=Debug
    - VERBOSE=1 scan-build -o ../scan make -j2
  artifacts:
    paths:
      - scan/

test:linux:
  stage: test
  except:
    - schedules
  dependencies:
    - inkscape:linux
  script:
    - cd build
    - ctest -V

translations:
  stage: build
  except:
    - schedules
  script:
    # make sure dependencies for inkscape.pot are up-to-date
    - mkdir build && cd build && cmake .. && make inkscape_pot && cd ..
    - cd po/

    # update POTFILES.in
    - grep -v "^#" POTFILES.in > POTFILES.in.1
    - ./generate_POTFILES.sh
    - grep -v "^#" POTFILES.in > POTFILES.in.2

    # update inkscape.pot and sync all .po files
    - intltool-update --pot && mv untitled.pot inkscape.pot
    - ./update_po_files.sh

    # check if POTFILES.in should be updated in the repo (shall we automate this?)
    - 'if [ -n "$(diff POTFILES.in.1 POTFILES.in.2)" ]; then
        git diff POTFILES.in;
        echo "New translatable strings in source files found.";
        echo "Please add the paths to POTFILES.in so they can be translated";
        echo "(or add them to POTFILES.skip to exlude them from translations).";
        exit 1;
      fi'
    - rm POTFILES.in.1 POTFILES.in.2
  artifacts:
    name: "translations"
    paths:
      - po/
    when: always

# uploads the clang scan to user.gitlab.io/inkscape/
pages:
  stage: deploy
  only:
    - schedules
  dependencies:
    - source-doc
    - clang
  script:
    - mkdir -p public/
    - sed "s/%DATE%/$(date '+%B %d, %Y')/" buildtools/ci-index.html > public/index.html
    - if test -e scan; then cp -rv scan/* public/scan-build; fi
    - if test -e doc/doxygen; then cp -rv doc/doxygen/html public/doxygen; fi
    - if test -e doc/doxygen-extensions; then cp -rv doc/doxygen-extensions/html public/doxygen-extensions; fi
  artifacts:
    paths:
      - public
