Sunday, November 27, 2016

Install any App Store software from command line using mas-cli

   Recently I had a need to create easy onboarding setup.
   Whenever a new member joins the team I want him to be up and running with a local dev environment in a shortest amount of time. I want the process to be error prone, repeatable and involve as less manual interaction as possible. Wouldn't it be nice to just say: "Hey, run this script. And you good!"
   As a part of this effort there is a need to be able to programmatically install apps that are only distributed via App Store and are not available via Homebrew.
   Meet mas-cli utility.
   It allows you to search, install and update App Store distributed software.
   Installing mas-cli on you machine is as easy as running brew install mas in the Terminal window.
The way mas-cli works is that you first need to know the ID of the app you're interested in. You can find it by running mas search <name_here>. This will return a list of available app that have anything to do with the name you provided. Find the one you most interested in and make a note of the ID displayed next to the program name. Now you can run mas install <id_here>. And that's it. The program will be installed without you ever having the need to interact with App Store directly.
   That's cool. But what we really interested here is to come up with a way to put software installation routine in the onboarding script.
   Here is an example of how to do that if you lets say want to install Xcode:
mas install $(mas search xcode | grep 'Xcode' |head -1 | awk '{print $1}')
Here we first search the registry for xcode. Select the result that equals to 'Xcode'. Take only first column - that would be the ID. And we pass it to mas install command.

  So what left here is to add a line like this into your onboarding script. Just make sure you have mas utility installed on the machine prior to invoking such routine.

  Mas-cli GitHub page https://github.com/mas-cli/mas