- name: Install packages
  package:
    name:
      - rust-nightly
    state: present

- name: Install packages
  when: "rust_analyzer_version is defined and rust_analyzer_version == 'package'"
  package:
    name:
      - rust-analyzer
    state: present

- name: Create directories
  when: "rust_analyzer_version is defined and rust_analyzer_version != 'package'"
  file:
    name: "{{ item }}"
    state: directory
    mode: 0755
    owner: root
    group: wheel
  loop:
    - "/opt/rust-analyzer"

- name: Clone rust-analyzer Source
  when: "rust_analyzer_version is defined and rust_analyzer_version != 'package'"
  git:
    repo: "https://github.com/rust-lang/rust-analyzer.git"
    dest: /opt/rust-analyzer
    version: "{{ rust_analyzer_version }}"
    force: true
  diff: false

- name: Build rust-analyzer
  when: "rust_analyzer_version is defined and rust_analyzer_version != 'package'"
  command: "cargo build --release"
  args:
    creates: "/opt/rust-analyzer/target/release/rust-analyzer"
    chdir: /opt/rust-analyzer
