module XcodeInstall
  class Command
    class InstallCLITools < Command
      self.command = 'install-cli-tools'
      self.summary = 'Installs Xcode Command Line Tools.'

      def run
        fail Informative, 'Xcode CLI Tools are already installed.' if installed?
        install
      end

      def installed?
        `xcode-select -p`
        $?.success?
      end

      def install
        cli_placeholder_file = '/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress'
        # create the placeholder file that's checked by CLI updates' .dist code in Apple's SUS catalog
        FileUtils.touch(cli_placeholder_file)
        # find the CLI Tools update
        product = `softwareupdate -l | grep "\*.*Command Line" | head -n 1 | awk -F"*" '{print $2}' | sed -e 's/^ *//' | tr -d '\n'`
        `softwareupdate -i "#{product}" -v`
        FileUtils.rm(cli_placeholder_file)
      end
    end
  end
end

module XcodeInstall
  class Command
    class InstallCLITools < Command
      self.command = 'install-cli-tools'
      self.summary = 'Installs Xcode Command Line Tools.'

      def run
        fail Informative, 'Xcode CLI Tools are already installed.' if installed?
        install
      end

      def installed?
        File.exist?('/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib')
      end

      def install
        cli_placeholder_file = '/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress'
        # create the placeholder file that's checked by CLI updates' .dist code in Apple's SUS catalog
        FileUtils.touch(cli_placeholder_file)
        # find the CLI Tools update
        product = `softwareupdate -l | grep "\*.*Command Line" | head -n 1 | awk -F"*" '{print $2}' | sed -e 's/^ *//' | tr -d '\n'`
        `softwareupdate -i "#{product}" -v`
        FileUtils.rm(cli_placeholder_file)
      end
    end
  end
end

Check CLI tools installed by libxcrun.dylib instead of `xcode-select -p`
Check CLI tools installed by libxcrun.dylib instead of `xcode-select -p`

- Success of `xcode-select -p` command will be true if any Xcode.app
  is installed, or an alternate Xcode.app version has been selected,
  even if CLI tools have never been installed
- More details:
  https://macops.ca/developer-binaries-on-os-x-xcode-select-and-xcrun
