---

# The following variables will be set by the runner of this playbook:
# project_path: PROJECTS_DIR/_local_path_
# scm_type: git|hg|svn
# scm_url: https://server/repo
# scm_branch: HEAD
# scm_clean: true/false
# scm_delete_on_update: true/false
# scm_username: username (only for svn)
# scm_password: password (only for svn)
# scm_accept_hostkey: true/false (only for git)

- hosts: all
  connection: local
  gather_facts: false
  tasks:

    - name: delete project directory before update
      file: path="{{project_path}}" state=absent
      when: scm_delete_on_update|default('')

    - name: update project using git and accept hostkey
      git: dest="{{project_path}}" repo="{{scm_url}}" version="{{scm_branch}}" force={{scm_clean}} accept_hostkey={{scm_accept_hostkey}}
      when: scm_type == 'git' and scm_accept_hostkey is defined

    - name: update project using git
      git: dest="{{project_path}}" repo="{{scm_url}}" version="{{scm_branch}}" force={{scm_clean}}
      when: scm_type == 'git' and scm_accept_hostkey is not defined

    - name: update project using hg
      hg: dest="{{project_path}}" repo="{{scm_url}}" revision="{{scm_branch}}" force={{scm_clean}}
      when: scm_type == 'hg'

    - name: update project using svn
      subversion: dest="{{project_path}}" repo="{{scm_url}}" revision="{{scm_branch}}" force={{scm_clean}}
      when: scm_type == 'svn' and not scm_username|default('')

    - name: update project using svn with auth
      subversion: dest="{{project_path}}" repo="{{scm_url}}" revision="{{scm_branch}}" force={{scm_clean}} username="{{scm_username}}" password="{{scm_password}}"
      when: scm_type == 'svn' and scm_username|default('')
