---
-
  name: Ensure drupal_composer_install_dir directory exists.
  ansible.builtin.file:
    path: "{{ drupal_composer_install_dir }}"
    state: directory
    mode: "0775"
  become: false
  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.
  ansible.builtin.copy:
    src: "{{ drupal_composer_path }}"
    dest: "/tmp/drupalvm-composer.json"
    mode: "0644"
  when: drupal_composer_path and not drupal_site_exists
  become: false

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

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

-
  name: Install dependencies with composer require (this may take a while).
  community.general.composer:
    command: require
    arguments: "{{ item }}"
    working_dir: "{{ drupal_composer_install_dir }}"
  with_items: "{{ drupal_composer_dependencies | default([]) }}"
  become: false
  environment:
    COMPOSER_PROCESS_TIMEOUT: '1200'
    COMPOSER_MEMORY_LIMIT: '-1'
