---
- name: Ensure drupal_composer_install_dir directory exists.
  file:
    path: "{{ drupal_composer_install_dir }}"
    state: directory
    mode: 0775
  become: no
  when: drupal_composer_path and not drupal_site_exists

# Use copy-and-move to prevent issues in Windows with VirtualBox. See:
# https://github.com/ansible/ansible/issues/9526#issuecomment-62336962
- name: Copy composer.json into temporary location.
  copy:
    src: "{{ drupal_composer_path }}"
    dest: "/tmp/drupalvm-composer.json"
  when: drupal_composer_path and not drupal_site_exists
  become: no

- name: Move composer.json into place.
  command: "mv /tmp/drupalvm-composer.json {{ drupal_composer_install_dir }}/composer.json"
  when: drupal_composer_path and not drupal_site_exists
  become: no

- name: Run composer install (this may take a while).
  composer:
    command: install
    working_dir: "{{ drupal_composer_install_dir }}"
  when: not drupal_site_exists
  become: no

- name: Install dependencies with composer require (this may take a while).
  composer:
    command: require
    arguments: "{{ item }}"
    working_dir: "{{ drupal_composer_install_dir }}"
  with_items: "{{ drupal_composer_dependencies|default([]) }}"
  become: no
