---
- name: Fetch and unpack TensorFlow Text source archive
  when: tensorflow_text_version != 'git'
  block:
    - name: Fetch TensorFlow Text {{ tensorflow_text_version }} source
      ansible.builtin.get_url:
        url: "https://github.com/tensorflow/text/archive/refs/tags/v{{ tensorflow_text_version }}.tar.gz"
        dest: "{{ build_dir }}/text-{{ tensorflow_text_version }}.tar.gz"
        mode: 0600

    - name: Unpack TensorFlow Text {{ tensorflow_text_version }} source
      ansible.builtin.unarchive:
        src: "{{ build_dir }}/text-{{ tensorflow_text_version }}.tar.gz"
        dest: "{{ build_dir }}/"
        remote_src: true

- name: Fetch TensorFlow Text from git
  ansible.builtin.git:
    repo: "https://github.com/tensorflow/text.git"  # noqa latest[git]
    dest: "{{ build_dir }}/text-{{ tensorflow_text_version }}/"
    depth: 1
    recursive: true
    force: true
    version: "{{ tensorflow_text_git_version }}"
  when: tensorflow_text_version == 'git'

- name: Patch the source to work
  ansible.builtin.lineinfile:
    path:  "{{ build_dir }}/text-{{ tensorflow_text_version }}/oss_scripts/prepare_tf_dep.sh"
    regexp: '^(.*)(Browse the repository)(.*)$'
    line: '\1permalink\3'
    backrefs: true
