rush, a shell that will yield to you

43
Nicholas Schlueter rush a shell that will yield to you

Upload: guestdd9d06

Post on 29-Oct-2014

8 views

Category:

Technology


4 download

DESCRIPTION

Rush is a shell with ruby syntax and idioms. This presentation goes over how to use it and embed it in other applications.

TRANSCRIPT

Page 1: Rush, a shell that will yield to you

Nicholas Schlueter

rusha shell that will yield to you

Page 2: Rush, a shell that will yield to you

http://flickr.com/photos/exquisitur/2551255999/

Page 3: Rush, a shell that will yield to you

What is rush?

•Unix shell with ruby syntax and idioms

•by Adam Wiggins (Heroku)

Page 4: Rush, a shell that will yield to you

Install and Run$ sudo gem install rush

$ rush

rush> home=> localhost:/Users/schlueter/

Page 5: Rush, a shell that will yield to you

No Working Directory$ home

=> localhost:/Users/schlueter

$ root=> localhost:////

$ rushmate = home["mydev/github/rushmate/"]=> localhost:/Users/schlueter/mydev/github/rushmate/

Page 6: Rush, a shell that will yield to you

But ls Still Works$ home["mydev/github/rushmate/"].ls

/Users/schlueter/mydev/github/rushmate/ coverage/ lib/ test/ History.txt MIT-LICENSE Manifest.txt

Page 7: Rush, a shell that will yield to you

Make a Variable

$ rushmate = home["mydev/github/rushmate/"]

Page 8: Rush, a shell that will yield to you

File Manipulation

http://flickr.com/photos/takashi/18862634/

Page 9: Rush, a shell that will yield to you

Rename

# assign a file to a variable

$ file = rushmate['app/models/rushmate.rb']

$ file.rename 'russian_mate.rb'

Page 10: Rush, a shell that will yield to you

Duplicate a File

$ file = rushmate['README']

$ file.duplicate 'README.markdown'

Page 11: Rush, a shell that will yield to you

Copy Different Directory

$ file = rushmate['README']

$ file.copy_to other_project['README']

Page 12: Rush, a shell that will yield to you

Move

$ file = rushmate['README']

$ file.move_to home['Desktop/']

Page 13: Rush, a shell that will yield to you

Write a File

$ file = rushmate['README']

$ file.write "You Read Me"

Page 14: Rush, a shell that will yield to you

Read a File

$ rushmate["README"].contents

You Read Me

Page 15: Rush, a shell that will yield to you

File Globbing

Page 16: Rush, a shell that will yield to you

Basic Globbing

$ rushmate["**/*.rb"].../mydev/github/rushmate/lib/rushmate.rb.../mydev/github/rushmate/lib/rushmate/command.rb.../mydev/github/rushmate/lib/rushmate/exit.rb...=> 9 x Rush::File

Page 17: Rush, a shell that will yield to you

Glob into an Editor

$ rushmate["**/*.rb"].vi

$ rushmate["**/*.rb"].mate

Page 18: Rush, a shell that will yield to you

Operate on Results

$ rails_proj['**/*.rhtml'].each { |f| f.rename f.name.gsub(/\.rhtml/, '.html.erb') }

Page 19: Rush, a shell that will yield to you

Results Are an Array

$ rushmate["**/*.rb"].reject {|f| f.name.match(/test.rb/)}

Go Nuts!

$ rails_project["**/*.rhtml"] + rails_project["**/*.html.erb"]

Page 20: Rush, a shell that will yield to you

embed

http://flickr.com/photos/liamngls/413522957/

Rush::EmbeddableShell.new.execute_in_shell do #anything in here delegates to rushend

Page 21: Rush, a shell that will yield to you

Sake Quick Index of RDoc

task 'rdoc:local' => 'rush' do shell = Rush::EmbeddableShell.new shell.execute_in_shell do

endend

ruby = root["/Library/Ruby/Gems/1.8/"] indexes = ruby["doc/*/rdoc/index.html"] hrefs = indexes.collect { |f| path = f.full_path text = path.match(/doc\/(.*)\/rdoc/)[1] "<a href=\"#{path}\">#{text}</a>" } home["rdoc.html"].write hrefs.join("<br />") home.bash("open rdoc.html")

Page 22: Rush, a shell that will yield to you

sake rdoc:local

Page 23: Rush, a shell that will yield to you
Page 24: Rush, a shell that will yield to you

Refactoring

http://flickr.com/photos/tonyjcase/2262229518/

Page 25: Rush, a shell that will yield to you

Search$ work["**/*.rb"].search(/\bRushmate\b/)

.../test/user_input_test.rb => assert_equal("foo", Rushmate::.../lib/rushmate/command.rb => module Rushmate.../lib/rushmate/exit.rb => module Rushmate...7 matching files with 25 matching lines

$ work["**/*.rb"].search(/\bRushmate\b/).mate

Page 26: Rush, a shell that will yield to you

and Replace$ rushmate["**/*.rb"].replace_contents!(/\bRushmate\b/, "RussianMate").../lib/rushmate.rb.../lib/rushmate/command.rb.../lib/rushmate/exit.rb=> 9 x Rush::File

Page 27: Rush, a shell that will yield to you

Regex is your friend

$ project_files.replace_contents! (/User.authenticate\(\s*(.*),\s*(.*)\)/, 'User.authenticate(\\2, \\1)')

User.authenticate(user, password)User.authenticate(password, user)

Page 28: Rush, a shell that will yield to you

Processes

$ ff = processes.filter(:cmdline => /firefox/).first

• ff.alive?

• ff.mem

• ff.pid

• ff.cpu

• ff.kill

Page 29: Rush, a shell that will yield to you

Real World

Rush::EmbeddableShell.new.execute_in_shell do

end

ff_processes = processes.filter(:cmdline => /firefox/) if ff = ff_processes.select{|p| p.mem > 400000}.first ff_cmdline = ff.cmdline ff.kill home.bash ff_cmdline, :background => true end

Page 30: Rush, a shell that will yield to you

Permissions$ file.access = {:user_can => :read_and_write, :group_and_other_can => :read }

$ file.access[:user_can_read]=> true

Page 31: Rush, a shell that will yield to you

Use BASH if you must

$ rushmate.bash "rm -rf ."

# D'oh!

Page 32: Rush, a shell that will yield to you

Shell Locally Act Globally

Page 33: Rush, a shell that will yield to you

Remote$ remote = Rush::Box.new('[email protected]')

$ remote["/u/app/current/REVISION"].contents=> 160

Page 34: Rush, a shell that will yield to you

Starts a Mongrel on Remote Server

Use in production with caution

Page 35: Rush, a shell that will yield to you

Customize# setup variables to common directoriesrushmate = home["mydev/github/rushmate/"]

# setup variables to common serversqa = Rush::Box.new('[email protected]')

~/.rush/evn.rb

Page 36: Rush, a shell that will yield to you

Cheap Backupdef cheap_backup(directory) unless directory.dir? puts "cheap backup only works on dirs" return end backup_path = directory.path + "/" + directory.name + ".bak" if self[backup_path].exists? puts "backup already exists" else directory.duplicate(directory.name + ".bak") endend

Page 37: Rush, a shell that will yield to you

Extend

# allows you to run # directory.coveragedef coverage self.rake("coverage")end

~/.rush/commands.rb

Page 38: Rush, a shell that will yield to you

Another Command

def ruby_files self["**/*.rb"]end

$ rushmate.ruby_files.../mydev/github/rushmate/lib/rushmate.rb.../mydev/github/rushmate/lib/rushmate/command.rb.../mydev/github/rushmate/lib/rushmate/exit.rb...=> 9 x Rush::File

Page 39: Rush, a shell that will yield to you
Page 40: Rush, a shell that will yield to you

TextMateRushmate::Command.new { # find ruby files with the current word in textmate found_files = project_directory["**/#{current_word.downcase}.rb"] if found_files.size == 1 found_files.mate else menu_files = found_files.collect { |f| f.full_path.gsub(project_directory.full_path, "") } project_directory[user_input.quick_menu_from_array(menu_files)].mate end}

Page 41: Rush, a shell that will yield to you

The Take Away

Page 42: Rush, a shell that will yield to you

Questions

?

Page 43: Rush, a shell that will yield to you

Nicholas Schlueter

Thank [email protected]

http://www.simpltry.com