- name: Clean up {{ code_server_dir }}
  become: yes
  file:
    state: absent
    path: "{{ code_server_dir }}"
  when: code_server_download_needed

- name: Create {{ code_server_dir }}
  become: yes
  file:
    state: directory
    path: "{{ code_server_dir }}"

- name: Download code-server
  become: yes
  unarchive:
    remote_src: yes
    src: "{{ code_server_tar_url }}"
    dest: "{{ code_server_dir }}"
    extra_opts:
      - --strip-components=1
  notify: Restart code-server
  when: code_server_download_needed

- name: Add {{ code_server_bin_dir }}/code-server
  become: yes
  file:
    state: link
    src: "{{ code_server_dir }}{{ '/bin' if code_server_cleaned_ver is version('3.3.0', '>=') else '' }}/code-server"
    dest: "{{ code_server_bin_dir }}/code-server"
    # force is required if upgrade from v1 or v2,
    # in which case dest exists and is a file.
    force: yes
  notify: Restart code-server
