From 2708e367df532d2cfa4abffa42b8189e1dbeeda2 Mon Sep 17 00:00:00 2001
From: Hyperling <me@hyperling.com>
Date: Wed, 26 Feb 2025 08:29:31 -0700
Subject: [PATCH] Attempt to fix when Sdk/ already exists but latest/sdkmanager
 is not present yet.

---
 tasks/workstation/linux/software/android.yml | 62 +++++++++++++++-----
 1 file changed, 46 insertions(+), 16 deletions(-)

diff --git a/tasks/workstation/linux/software/android.yml b/tasks/workstation/linux/software/android.yml
index 0d3a7d7..050604e 100644
--- a/tasks/workstation/linux/software/android.yml
+++ b/tasks/workstation/linux/software/android.yml
@@ -12,28 +12,41 @@
 # Command-line tools is all Flutter actually needs:
 #   https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
 
-- name: Workstation | Linux | Software | Android | Facts [1/2]
+- name: Workstation | Linux | Software | Android | Facts [1/3]
   set_fact:
     android_url: "https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip"
     android_download_file: "{{ user_user.home }}/Downloads/android-cmdline-tools.tar.xz"
     android_sdk_location: "{{ user_user.home }}/SDKs/Android/Sdk"
 
-- name: Workstation | Linux | Software | Android | Facts [2/2]
+- name: Workstation | Linux | Software | Android | Facts [2/3]
   set_fact:
     android_bin_location: "{{ android_sdk_location }}/platform-tools"
-    android_sdkmanager: "{{ android_sdk_location }}/cmdline-tools/bin/sdkmanager"
+    android_sdk_location_temp: "{{ android_sdk_location }}/ansible"
+    android_sdkmanager_final: "{{ android_sdk_location }}/cmdline-tools/latest/bin/sdkmanager"
+
+- name: Workstation | Linux | Software | Android | Facts [3/3]
+  set_fact:
+    android_sdkmanager_temp: "{{ android_sdk_location_temp }}/cmdline-tools/bin/sdkmanager"
 
 ## Checks ##
 
-- name: Workstation | Linux | Software | Android | Check SDK Exists
+- name: Workstation | Linux | Software | Android | Check SDK Manager Exists [1/2]
   stat:
-    path: "{{ android_bin_location }}"
-  register: android_sdk_stat
+    path: "{{ android_sdkmanager_temp }}"
+  register: android_sdkmanager_temp_stat
+  when: coding == true
+
+- name: Workstation | Linux | Software | Android | Check SDK Manager Exists [2/2]
+  stat:
+    path: "{{ android_sdkmanager_final }}"
+  register: android_sdkmanage_final_stat
+  when: coding == true
 
 - name: Workstation | Linux | Software | Android | Check Download Exists
   stat:
     path: "{{ android_download_file }}"
   register: android_download_stat
+  when: coding == true
 
 ## Packages ##
 
@@ -86,7 +99,7 @@
       owner: "{{ user }}"
       group: "{{ user }}"
 
-  when: coding == true and not android_sdk_stat.stat.exists
+  when: coding == true and not android_sdkmanager_temp_stat.stat.exists
 
 - name: Workstation | Linux | Software | Android | Delete Archive
   file:
@@ -95,13 +108,21 @@
 
 ## Configure Modules ##
 
-- name: Workstation | Linux | Software | Android | Install Modules
-  shell: "yes | {{ android_sdkmanager }} '{{ item }}' --sdk_root={{ android_sdk_location }}"
+# This can only be run once, otherwise cmdline-tools creates latest-* folders.
+- name: Workstation | Linux | Software | Android | Install Consistent Modules
+  shell: "yes | {{ android_sdkmanager_temp }} --install '{{ item }}' --sdk_root={{ android_sdk_location }}"
   loop:
-    # latest
+    # Current
     - cmdline-tools;latest
     - platform-tools
     - emulator
+  become_user: "{{ user }}"
+  when: coding == true and not android_sdkmanager_final_stat.stat.exists
+
+# These are safe to run multiple times.
+- name: Workstation | Linux | Software | Android | Install Modules
+  shell: "yes | {{ android_sdkmanager_final }} --install '{{ item }}' --sdk_root={{ android_sdk_location }}"
+  loop:
     # 34
     - build-tools;34.0.0
     - platforms;android-34
@@ -112,22 +133,31 @@
     - platforms;android-35
     - sources;android-35
     # Images
-    - system-images;android-35;default;x86_64
-    - system-images;android-35;aosp_atd;x86_64
+    #- system-images;android-35;default;x86_64
+    #- system-images;android-35;aosp_atd;x86_64
     - system-images;android-35;google_apis_playstore;x86_64
   become_user: "{{ user }}"
+  when: coding == true and not android_sdkmanager_final_stat.stat.exists
 
 - name: Workstation | Linux | Software | Android | Update Modules
-  shell: "yes | {{ android_sdkmanager }} --update --sdk_root={{ android_sdk_location }}"
+  shell: "yes | {{ android_sdkmanager_final }} --update --sdk_root={{ android_sdk_location }}"
   become_user: "{{ user }}"
+  when: coding == true
 
 - name: Workstation | Linux | Software | Android | Inform Flutter
   shell: "{{ flutter }} config --android-sdk={{ android_sdk_location }}"
   become_user: "{{ user }}"
+  when: coding == true
 
-- name: Workstation | Linux | Software | Android | License Agreements
+- name: Workstation | Linux | Software | Android | Licenses Agreements [1/2]
+  shell: "yes | {{ android_sdkmanager_final }} --licenses --android-sdk={{ android_sdk_location }}"
+  become_user: "{{ user }}"
+  when: coding == true
+
+- name: Workstation | Linux | Software | Android | License Agreements [2/2]
   shell: "yes | {{ flutter }} doctor --android-licenses"
   become_user: "{{ user }}"
+  when: coding == true
 
 - name: Workstation | Linux | Software | Android | Refresh Flutter Doctor Report
   shell: "{{ item }}"
@@ -141,7 +171,7 @@
   blockinfile:
     path: "{{ item }}/.bashrc"
     block: |
-      export PATH="$PATH:{{ android_bin_location }}"
+      export PATH="$PATH:{{ android_bin_location }}:{{ android_sdkmanager_final }}"
     marker: '# {mark} MANAGED BY ANSIBLE | Android'
     state: present
     create: yes
@@ -156,7 +186,7 @@
   blockinfile:
     path: "{{ item }}/.zshrc"
     block: |
-      export PATH="$PATH:{{ android_bin_location }}"
+      export PATH="$PATH:{{ android_bin_location }}:{{ android_sdkmanager_final }}"
     marker: '# {mark} MANAGED BY ANSIBLE | Android'
     state: present
     create: yes