samdoran / ansible-collection-macos

Ansible collection of roles and modules for use on macOS
8 stars 2 forks source link

[Improvement idea] Check for Xcode CLI tools via `pkgutil` #4

Open webknjaz opened 2 years ago

webknjaz commented 2 years ago

I've been googling about the installer command and stumbled upon this post https://hvops.com/articles/ansible-mac-osx/. Turns out that pkgutil --pkg-info=com.apple.pkg.CLTools_Executables (for macOS X it's com.apple.pkg.DeveloperToolsCLI) exposes metadata that can be used to check whether Xcode CLI tools are present.

$ pkgutil --file-info /Library/Developer/CommandLineTools/usr/bin/python3
volume: /
path: /Library/Developer/CommandLineTools/usr/bin/python3

pkgid: com.apple.pkg.CLTools_Executables
pkg-version: 13.4.0.0.1.1651278267
install-time: 1656705474
uid: 0
gid: 0
mode: 120755

$ pkgutil --file-info /Library/Developer/CommandLineTools
volume: /
path: /Library/Developer/CommandLineTools

pkgid: com.apple.pkg.CLTools_SDK_macOS12
pkg-version: 13.4.0.0.1.1651278267
install-time: 1656705475
uid: 0
gid: 0
mode: 40755

pkgid: com.apple.pkg.CLTools_Executables
pkg-version: 13.4.0.0.1.1651278267
install-time: 1656705474
uid: 0
gid: 0
mode: 40755

pkgid: com.apple.pkg.CLTools_SDK_macOS110
pkg-version: 13.4.0.0.1.1651278267
install-time: 1656705476
uid: 0
gid: 0
mode: 40755

pkgid: com.apple.pkg.CLTools_SwiftBackDeploy
pkg-version: 13.4.0.0.1.1651278267
install-time: 1656705478
uid: 0
gid: 0
mode: 40755

pkgid: com.apple.pkg.CLTools_macOS_SDK
pkg-version: 13.4.0.0.1.1651278267
install-time: 1656705478
uid: 0
gid: 0
mode: 40755

$ pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 13.4.0.0.1.1651278267
volume: /
location: /
install-time: 1656705474
groups: com.apple.FindSystemFiles.pkg-group
$ echo $?
0

$ pkgutil --pkg-info=com.apple.pkg.DeveloperToolsCLI
No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.
$ echo $?
1

It seems like using it could be a more elegant/robust solution for making this check, compared to using stat. Have you considered using it?

samdoran commented 2 years ago

Nope, I hadn't thought to use pkgutil.