|
128 | 128 | failed_when: brew_check.rc != 0 |
129 | 129 | become: false |
130 | 130 |
|
131 | | - - name: Link required packages |
| 131 | + - name: Handle yarn linking with conflict resolution |
| 132 | + shell: | |
| 133 | + set -euo pipefail |
| 134 | + |
| 135 | + # Function to check if yarn is installed via brew |
| 136 | + is_yarn_installed() { |
| 137 | + brew list yarn >/dev/null 2>&1 |
| 138 | + } |
| 139 | + |
| 140 | + # Function to check if yarn symlink points to brew installation |
| 141 | + is_yarn_properly_linked() { |
| 142 | + if [ -L "${homebrew_prefix}/bin/yarn" ]; then |
| 143 | + local target=$(readlink "${homebrew_prefix}/bin/yarn") |
| 144 | + # Check if target contains yarn cellar path |
| 145 | + echo "$target" | grep -q "Cellar/yarn/" |
| 146 | + else |
| 147 | + false |
| 148 | + fi |
| 149 | + } |
| 150 | + |
| 151 | + # Function to remove orphaned yarn symlink |
| 152 | + remove_orphaned_yarn_symlink() { |
| 153 | + if [ -L "${homebrew_prefix}/bin/yarn" ] || [ -f "${homebrew_prefix}/bin/yarn" ]; then |
| 154 | + echo "Removing orphaned yarn symlink..." |
| 155 | + rm -f "${homebrew_prefix}/bin/yarn" |
| 156 | + fi |
| 157 | + } |
| 158 | + |
| 159 | + if is_yarn_installed; then |
| 160 | + echo "Yarn is installed via brew" |
| 161 | + if is_yarn_properly_linked; then |
| 162 | + echo "Yarn is already properly linked" |
| 163 | + else |
| 164 | + echo "Yarn is not properly linked, attempting to fix..." |
| 165 | + # Use --overwrite to handle conflicts |
| 166 | + brew link --overwrite yarn |
| 167 | + echo "Yarn linked successfully with --overwrite" |
| 168 | + fi |
| 169 | + else |
| 170 | + echo "Yarn is not installed via brew" |
| 171 | + # If yarn symlink exists but package is not installed, remove it |
| 172 | + if [ -L "${homebrew_prefix}/bin/yarn" ] || [ -f "${homebrew_prefix}/bin/yarn" ]; then |
| 173 | + echo "Found orphaned yarn symlink, removing it..." |
| 174 | + remove_orphaned_yarn_symlink |
| 175 | + fi |
| 176 | + fi |
| 177 | + args: |
| 178 | + executable: /bin/bash |
| 179 | + when: "'yarn' in all_linked_packages" |
| 180 | + become: false |
| 181 | + register: yarn_link_result |
| 182 | + |
| 183 | + - name: Show yarn linking result |
| 184 | + debug: |
| 185 | + msg: "{{ yarn_link_result.stdout_lines }}" |
| 186 | + when: "'yarn' in all_linked_packages and yarn_link_result is defined" |
| 187 | + |
| 188 | + - name: Link other required packages (non-yarn) |
132 | 189 | community.general.homebrew: |
133 | 190 | name: "{{ item }}" |
134 | 191 | state: linked |
135 | | - loop: "{{ all_linked_packages }}" |
136 | | - when: all_linked_packages | length > 0 |
| 192 | + loop: "{{ all_linked_packages | difference(['yarn']) }}" |
| 193 | + when: all_linked_packages | difference(['yarn']) | length > 0 |
137 | 194 | become: false |
138 | 195 |
|
139 | 196 | - name: Run brew doctor |
|
0 commit comments