Using Rake Tasks for Mac OSX

Rake is great. It's a clean, high-level API for running shell commands and defining / organizing task functions. I originally started using it for Rails projects, but now I use it for AWS Lambda development, Continuous Development Projects (through Jenkins), and even Mac OSX. Here are a few of my favorite commands.

Networking

Internet speed test

This straightforward example shows how you can use Rakefile to substitute commands in Terminal. This collection of scripts will enable you to run Speedtest.net commands without opening the browser or mistakingly clicking on an advertisement.

You will need homebrew to get started. Read this article.

namespace :speedtest do
  desc %Q{ Install Speedtest.net }
  task :install do
    sh %{ brew update && doctor }
    sh %{ brew install speedtest_cli }
  end 
  
  desc %Q{ Check Download & Upload Speed }
  task :all do
    sh %{ speedtest-cli --simple }
  end 
  
  desc %Q{ Check Upload Speed }
  task :upload do
    sh %{ speedtest-cli --no-download }
  end 
  
  desc %Q{ Check Download Speed }
  task :download do
    sh %{ speedtest-cli --no-upload }
  end 
end

Install Speedtest.net into your local computer.

rake speedtest:install

Check your upload speed. I use this all the time before uploading GB files to AWS S3.

rake speedtest:upload

IP Addresses

Create a Rakefile and paste this code.

namespace :ip do
  desc %Q{ Get Internal IP Address }
  task :internal do
    sh %{ ipconfig getifaddr en0 }
  end 
  
  desc %Q{ Get External IP Address }
  task :external do
    sh %{ curl -s http://checkip.dyndns.org/ | sed 's/[a-zA-Z<>/ :]//g' }
  end 
end

Retrieve your internal IP addresses.

rake ip:internal

Retrieve your external IP addresses.

rake ip:external

Proxy

Modify your Proxy Settings

namespace :proxy do
  desc %Q{ Toggle Web Proxy (HTTP) OFF }
  task :off do
    sh %{ networksetup -setwebproxystate Ethernet off }
  end
  
  desc %Q{ Toggle Web Proxy (HTTP) ON }
  task :on do
    sh %{ networksetup -setwebproxy Ethernet 10.10.10.14 3128 }
  end
  
  desc %Q{ Add *.localhost to Mac OsX Network Proxy Settings }
  task :bypass do
    sh %{ networksetup -setproxybypassdomains Ethernet *.localhost, *.local, 169.254/16 }
  end  
end
rake proxy:on
rake proxy:off

Exittool - Rename Image Files

namespace :photo do
  
  desc %Q{ ›› Install exiftool }
  task :install do |task|
    sh %{ brew install exiftool }
  end
  
  desc %Q{ Run exiftool to capture metadata then rename file accordingly. }
  task :rename, [:dir] do |task, args|
    dir = args.dir
    sh %{ exiftool -d '%y%m%d-%H%M%%-03.c.%%e' '-filename<CreateDate' #{dir} }
  end
end

Video Conversion

Convert FLV to MP4.

namespace :video do
  desc %Q{ ›› Convert FLV to MP4 }
  task :flv, [:flv_path, :mp4_name] do |t, args|
    flv_path = args.flv_path
    mp4_name = args.mp4_name
    # Update Homebrew
    sh %{ brew update && brew doctor }
    # Install FFMPEG converter
    sh %{ brew install ffmpeg }
    # https://gist.github.com/clayton/6196167
    # brew install ffmpeg --with-vpx --with-vorbis --with-libvorbis --with-vpx --with-vorbis --with-theora --with-libogg --with-libvorbis --with-gpl --with-version3 --with-nonfree --with-postproc --with-libaacplus --with-libass --with-libcelt --with-libfaac --with-libfdk-aac --with-libfreetype --with-libmp3lame --with-libopencore-amrnb --with-libopencore-amrwb --with-libopenjpeg --with-openssl --with-libopus --with-libschroedinger --with-libspeex --with-libtheora --with-libvo-aacenc --with-libvorbis --with-libvpx --with-libx264 --with-libxvid

    # Convert FLV file to Mp4
    sh %{ ffmpeg -i "$file" "$mp4name" }
    sh %{  }
  end  
end

Finding files

I prefer not to memorize find and grep commands and instead just use this cheat sheet to find stuff by name, text, or by type. Within your Rakefile paste:

namespace :find do
  desc %Q{ Find by file type. rake find:by_type[wav,~] }
  task :by_type, [:file_type, :dir] do |task, args|
    type = args.file_type
    dir  = args.dir
    sh %{ find #{dir} -iname "*.#{type}" }
  end
  
  desc %Q{ Find by file type ignore error messages. }
  task :by_type_ignore, [:file_type, :dir] do |task, args|
    type = args.file_type
    dir  = args.dir
    sh %{ find #{dir} -type f -name "*.#{type}" 2>/dev/null  }
  end
  
  desc %Q{ Finds [text] in any file in any subfolder of [dir]. }
  # rake find:by_text["chris mendez",~/Desktop/*.txt]
  task :by_text, [:text, :dir] do |task, args|
    text = args.text
    dir  = args.dir
    cmd  = %{ grep -r -l "#{text}" #{dir} }
    sh cmd
  end
  
  desc %Q{ Find a phone number with this pattern 213400 }
  task :by_phone, [:digits, :dir] do |task, args|
    text = args.digits
    dir  = args.dir
    sh %{ grep -r -l [0-9][0-9][0-9][0-9][0-9][0-9] #{dir} }
  end  

  desc %Q{ ›› Find a file in a folder. Args [:text, :folder] }
  task :doc, [:text, :folder] do |task, args|
    text = args.text
    folder = args.folder || "~/Documents/"
    sh %{ mdfind -onlyin #{folder} -live "#{text}" }
    sh %{ done }
  end
  
  
  desc %Q{ ›› Find the size of a specific folder. Args [:path, :threshold] }
  task :size, [:path, :threshold] do |task, args|
    path = args.path || "./"
    threshold = args.threshold || "+50M" 
    puts path
    puts threshold
    sh %{ find #{path} -size #{threshold} | sort -h }
    sh %{ done }
  end

  desc %Q{ ›› Find largests files in directory recursively }
  task :size do |task, args|
    # du estimates the file space usage
    # sort will present the output of du command
    # head will only show top 10 larget files within directory
    sh %{ du -a /dir/ | sort -n -r | head -n 20 }
    sh %{  }
  end
end

Find .WAV files within my home directory.

rake find:by_type[wav,~] 

Find the save .WAV files within my home directory but ignore any errors.

rake find:by_type_ignore[wav,~] 

Find a Verizon cell phone number within a text file.

rake find:by_text_within[213400,~/Desktop/*.txt]

Source


Gatekeeper

namespace :gatekeeper do
  desc %Q{ ›› Turn off Gatekeeper }
  task :off do |t, args|
    sh %{ sudo spctl --master-disable }
  end  

  desc %Q{ ›› Turn on Gatekeeper }
  task :off do |t, args|
    sh %{ sudo spctl --master-enable }
  end  
end

Ringtone Maker

You can even go further and include other OSX commands into your Rakefile and organize it with a namespace:

namespace :ringtone do
  desc %Q{ Convert an Mp3 into a Ringtone. }
  task :create, [:mp3, :name] do |task, args|
    mp3 = args.mp3
    name = args.name
    sh %{ afconvert #{mp3} #{name}.m4r -f m4af }
  end
end
rake ringtone:create[/path/to/file.mp3,"My Ringtone"]

So Much More

There are many more OSX Commands you can create. Read More.