Created
August 25, 2011 17:35
-
-
Save leandrosilva/1171236 to your computer and use it in GitHub Desktop.
Command line utility to work with project (I don't know)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I've got it from https://github.com/fnando/bash/blob/master/bin/project and added a couple of features. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require "yaml" | |
| require "optparse" | |
| # Save a config file at ~/.projects with something like | |
| # | |
| # project_name: | |
| # home: ~/Sites/some_project | |
| # tabs: | |
| # - rails server thin | |
| # - rails console | |
| # - redis-server | |
| # - mongod --dbpath=tmp/mongodb | |
| # | |
| # To open all tabs for this project, just run | |
| # | |
| # $ project project_name | |
| # | |
| class Project | |
| APPLESCRIPT = <<-APPLESCRIPT | |
| osascript <<EOF | |
| tell application "Terminal" | |
| activate | |
| %s | |
| end tell | |
| EOF | |
| APPLESCRIPT | |
| class NoConfigFileError < StandardError; end | |
| class NoProjectError < StandardError; end | |
| def self.projects | |
| filepath = File.expand_path("~/.projects") | |
| raise NoConfigFileError unless File.exist?(filepath) | |
| @projects ||= YAML.load_file File.expand_path(filepath) | |
| end | |
| attr_accessor :config | |
| def self.run(project_name) | |
| new(project_name).run | |
| end | |
| def self.go(project_name) | |
| new(project_name).go | |
| end | |
| def initialize(name) | |
| @config = self.class.projects[name] | |
| raise NoProjectError unless @config | |
| end | |
| def run | |
| commands = [] | |
| config["tabs"].each_with_index do |tab_command, i| | |
| commands << %[do script "cd #{config["home"]} && #{tab_command}" in last tab of front window] | |
| commands << 'tell application "System Events" to keystroke "t" using command down' if i < config["tabs"].size - 1 | |
| end | |
| command = APPLESCRIPT % commands.join("\n") | |
| system(command) | |
| end | |
| def go | |
| command = APPLESCRIPT % %[do script "cd #{config["home"]}" in last tab of front window] | |
| system(command) | |
| end | |
| end | |
| # Take care about command line options | |
| # | |
| # Current options: | |
| # | |
| # -r, --run [PROJECT] Run project prescriptions contained in ~/.project | |
| # -g, --go [PROJECT] Change to project home directory | |
| # -h, --help Show help | |
| # | |
| class Command | |
| attr_reader :help, :options | |
| def initialize | |
| @help, @options = parse_command_line | |
| end | |
| private | |
| def parse_command_line | |
| options = {} | |
| optparse = OptionParser.new do |opts| | |
| opts.banner = "Usage: project [-r | -g] name" | |
| opts.on("-r", "--run PROJECT", "Run project prescriptions") do |project| | |
| options[:action] = :run | |
| options[:project] = project | |
| end | |
| opts.on("-g", "--go PROJECT", "Change to project home directory") do |project| | |
| options[:action] = :go | |
| options[:project] = project | |
| end | |
| opts.on("-h", "--help", "Show help") do | |
| options[:action] = :help | |
| options[:project] = nil | |
| end | |
| end | |
| help = optparse.help | |
| if ARGV.empty? | |
| options[:action] = :help | |
| options[:project] = nil | |
| else | |
| optparse.parse! | |
| if options.empty? | |
| options[:action] = :run | |
| options[:project] = ARGV.first | |
| end | |
| end | |
| [help, options] | |
| end | |
| end | |
| begin | |
| command = Command.new | |
| action = command.options[:action] | |
| project = command.options[:project] | |
| case action | |
| when :run | |
| Project.run(project) | |
| when :go | |
| Project.go(project) | |
| when :help | |
| puts command.help | |
| end | |
| rescue Project::NoConfigFileError | |
| $stderr << "Make sure your ~/.projects exist\n" | |
| exit 1 | |
| rescue Project::NoProjectError | |
| $stderr << %[Can't find "#{project}" project on ~/.projects\n] | |
| exit 1 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment