jodybrewster / macbook

shell script to setup a new macbook
5 stars 4 forks source link

Method for tracking installed homebrew packages #1

Open stickystyle opened 6 years ago

stickystyle commented 6 years ago

Hey Jody, just noticed that you do something very similar to myself with a script to set up a new laptop. I took a slightly different route to make it automatic, perhaps you could use some of my ideas...

In ~/Library/LaunchAgents I added the below plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>net.stickystyle.launched.save_packages</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>/Users/rparrish/Dropbox/home/backup_packages.sh</string>
    </array>
    <key>StandardErrorPath</key>
    <string>/tmp/net.stickystyle.launched.save_packages.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/net.stickystyle.launched.save_packages.out</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>12</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <key>WatchPaths</key>
    <array>
                 <string>/usr/local/lib/python3.6/site-packages</string>
        <string>/usr/local/Cellar</string>
        <string>/opt/homebrew-cask/Caskroom</string>
    </array>
</dict>
</plist>

What this does is monitor the three dirs in the WatchPaths array, and whenever there is a change in the filesystem, such as installing a new package, updating or removing a package it runs the backup_packages.sh script which looks like this...

#!/bin/bash
UPDATED="#Updated: $(date)"
PATH=/usr/local/bin:/usr/local/sbin:$PATH
echo $UPDATED > ~/Dropbox/home/python/requirements.txt
pip3 freeze >> ~/Dropbox/home/python/requirements.txt

echo $UPDATED > ~/Dropbox/home/homebrew/leaves.txt
brew leaves >> ~/Dropbox/home/homebrew/leaves.txt
echo $UPDATED > ~/Dropbox/home/homebrew/casks.txt
brew cask list >> ~/Dropbox/home/homebrew/casks.txt

This makes it so I don't have to remember to add a package to the list, the system just automatically dumps them to the .txt files on Dropbox.

Then to set up a new system I just do the following, For python packages - $pip install -r ~/Dropbox/home/python/requirements.txt For homebrew - $ cat ~/Dropbox/home/homebrew/leaves.txt | xargs brew install

I'm sure that it could be extended to global nodejs packages, and actually now that I'm thinking about it, I really need to add that.

jodybrewster commented 6 years ago

haha, that's awesome @stickystyle. I'm not sure if I should have automatic backups or what. I update all of this every time I get a new computer. I take it that if I have to go to git and manually make the change it must be important enough to commit. My zshrc is constantly changing.

Good to hear from you! Hope you're doing well.