Skip to content

Instantly share code, notes, and snippets.

View Narnach's full-sized avatar

Wes Oldenbeuving Narnach

View GitHub Profile
@Narnach
Narnach / bump.rb
Created January 29, 2009 15:20
Bump a gem's version, tag it and commit it to git
#!/usr/bin/env ruby
# bump.rb - Perform a project version bump
# * If a .gemspec file is present, use it to determine the current project version
# * Scans for the first line that contains .version and a 1.2.3-ish part
# * If no gemspec is present, use the highest tagged version
# * The expected tag format is '1.2.3'
# * Depending on the command line options 'major', 'minor', 'build' it will
# bump the 1st, 2nd, 4th digit of the version. No option implies a 'release',
# which bumps the 3rd digit. Example: 1.2.3 -> 1.2.4 is a release.
# * Overwrites the current gemspec with the new one if a gemspec was found
@Narnach
Narnach / eachfile.rb
Created January 29, 2009 15:11
Execute a command for each of the provided files, or to all files in the current directory
#!/usr/bin/env ruby
# eachfile.rb - Execute a command for each file
def help
puts <<-HELP
Syntax: #{$0} [option] "COMMAND" File1 [File2] .. [FileN]
The command is executed for each of the specified files.
FILE in the command is substituted with the name of each file.
If there is no FILE in command, the filename is given as parameter instead.
When no files are provided, all files in the current directory are used.
require 'rubygems'
require 'net/ssh/gateway'
s1 = {:host=>'foo', :user => 'foobar', :options => {:password => 'barfoo', :port => 2221}}
s2 = {:host=>'bar', :user => 'foobar', :options => {:password => 'barfoo', :port => 2222}}
s3 = {:host=>'baz', :user => 'foobar', :options => {:password => 'barfoo', :port => 2223}}
s4 = {:host=>'moo', :user => 'foobar', :options => {:password => 'barfoo', :port => 2224}}
gw = Net::SSH::Gateway.new(s[:host], s[:user], s[:options)
gw_port = gw.open(s2[:host], s2[:port])
gw2 = Net::SSH::Gateway.new(s2[:host], s2[:user], s[:options].merge({:port => gw_port}))
s1 = {:host=>'foo', :user => 'foobar'}
s2 = {:host=>'bar', :user => 'foobar'}
s3 = {:host=>'baz', :user => 'foobar'}
s4 = {:host=>'moo', :user => 'foobar', :options => {:password => 'barfoo'}}
def gate(s, s2, opts={})
gw = Net::SSH::Gateway.new(s[:host], s[:user], {:port => 22}.merge(opts))
gw_port = gw.open(s2[:host], s2[:port])
yield(gw_port)
ensure
@Narnach
Narnach / gist:51081
Created January 23, 2009 17:05
Remove duplicates from your $PATH environment variable
#!/usr/bin/env ruby
# cleanpath - Cleanup your $PATH!
#
# Reads the current $PATH and outputs a version without duplicate entries.
# It is useful as a last line in ~/.profile:
# export PATH=`/path/to/cleanpath`
path = ENV['PATH']
clean_path = path.split(":").uniq.join(":")
puts clean_path
#!/bin/sh
# Usage: s2b.sh branchname
# Easily switch between branches and their databases for Rails projects.
# Checks out given branch and copies the branch's database.yml to the active database.yml
# Outputs a small list of databases being used.
# Make sure you have set up your branch's database.yml in #{RAILS_ROOT}/config/database.#{branchname}.yml
git checkout $1 && cp config/database.$1.yml config/database.yml
databases=`grep --colour=auto database config/database.yml | grep -v ^# | sed s/database\:// | tr -d '\012' | sed -E s/^\ +\|\ +$// | sed -E s/\ +/,\ /g`
echo "Using databases: $databases"
@Narnach
Narnach / build_gems.rb
Created January 14, 2009 09:12
Build all versions of a rubygem based on git tags for each version
#!/usr/bin/env ruby
# BuildGems
# Created by Wes Oldenbeuving, copyright 2009, licensed under MIT LICENSE.
#
# For a git project, build the gems for all gem tag releases.
# The assumptions made are the following:
# - Version tags have the format 'N.N', 'N.N.N', etc... Only digits and dots.
# - There is a 'rake gem' task that builds the gem for the checked out version
# - Gems are located in pkg/*.gem
# - You want to copy all created gems to ~/gems/ after creating them.
#!/usr/bin/env ruby
require 'rubygems'
require 'active_support'
class John
end
j_c = John.class_eval do
def hi
'hi'
@Narnach
Narnach / cotd.rb
Created January 12, 2009 15:07
Show the manpage of a random executable in your $PATH
#!/usr/bin/env ruby
# Command of the day
# Picks a random executable from your path until it finds one with a manpage.
# Use this to learn about some of the really obscure yet really useful scripts that are already on your system.
files = ENV["PATH"].split(":").uniq.compact.map { |path|
Dir.glob(File.join(path,'*')).select {|file| File.file?(file) && File.executable?(file)}
}.flatten.compact.uniq
cotd = File.basename(files[rand(files.size)])
@Narnach
Narnach / genpassword.rb
Created January 8, 2009 12:15
Generate a random alphanumeric password
#!/usr/bin/env ruby
# Random alphanumeric password generator
# Takes one parameter, which is the password length
# Forces the minimum length to be 10 or more
#
CHARS = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a
class << CHARS
def rand
self[Kernel.rand(size)]
end