Chris Mendez in DevOps, For Developers, Rake

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 is a very simple example aimed to show how you can use Rakefile to substitute commands in Terminal. This collection of scripts will enable you to run Speedtest.net commands without having to open the browser or mistakingly click 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

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  
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


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.