Permalink
Browse files

First version

  • Loading branch information...
0 parents commit a8ee2b151b127c3979d161f815797a2904273580 @lucasfais lucasfais committed Oct 5, 2012
Showing with 165 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 Gemfile
  3. +18 −0 Gemfile.lock
  4. +7 −0 README.md
  5. +19 −0 Rakefile
  6. +71 −0 bin/cb
  7. +29 −0 codebase-cli.gemspec
  8. +6 −0 lib/codebase_cli.rb
  9. +10 −0 lib/codebase_cli/version.rb
@@ -0,0 +1,2 @@
+*.gem
+*.swp
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
@@ -0,0 +1,18 @@
+PATH
+ remote: .
+ specs:
+ codebase-cli (0.0.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ step-up (0.8.1)
+ thor (>= 0.14.6)
+ thor (0.14.6)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ codebase-cli!
+ step-up
@@ -0,0 +1,7 @@
+# Codebase Command Line Interface
+
+Command utilty to open codebase repositories in you browser.
+
+## Authors
+
+* [Lucas Fais](https://github.com/lucasfais)
@@ -0,0 +1,19 @@
+require 'rake/testtask'
+
+desc "Build the gem"
+task :build do
+ opers = Dir.glob('*.gem')
+ opers = ["rm #{ opers.join(' ') }"] unless opers.empty?
+ opers << ["gem build codebase-cli.gemspec"]
+ sh opers.join(" && ")
+end
+
+desc "Build and install the gem, removing old installation"
+task :install => :build do
+ gem = Dir.glob('*.gem').first
+ if gem.nil?
+ puts "could not install the gem"
+ else
+ sh "gem uninstall codebase-cli; gem install #{ gem }"
+ end
+end
71 bin/cb
@@ -0,0 +1,71 @@
+#!/usr/bin/env ruby
+
+CODEBASE_REPOSITORY_URL = "https://%s.codebasehq.com/projects/%s/repositories/%s/tree/master"
+CODEBASE_BLOB_URL = "https://%s.codebasehq.com/projects/%s/repositories/%s/blob/master/%s"
+CODEBASE_COMMIT_URL = "https://%s.codebasehq.com/projects/%s/repositories/%s/commit/%s"
+
+# Helper methods
+#
+def open(url)
+ if RUBY_PLATFORM =~ /darwin/
+ command = 'open'
+ elsif RUBY_PLATFORM =~ /linux/
+ command = 'xgd-open'
+ else
+ command = 'start'
+ end
+
+ `#{command} #{url}`
+end
+
+def repository_info
+ remote_info = `git remote show origin -n`
+
+ if remote_info =~ /Fetch\sURL:\s(.+)\n/
+ remote_url = $1
+
+ if remote_url =~ /codebase/
+ splited_url = remote_url.split('/')
+
+ if splited_url.size == 3
+ company = splited_url[0].split(':')[1]
+ project = splited_url[1]
+ repository = splited_url[2].gsub('.git', '')
+
+ return [company, project, repository]
+ end
+ else
+ puts 'error: this is not a codebase repository'
+ exit 1
+ end
+ else
+ puts 'error: git remote url not found'
+ exit 1
+ end
+end
+
+def help
+ puts "Codebase CLI - Command line utility to open codebase repositories in you browser"
+ puts
+ puts 'cb'
+ puts "\t open the repository homepage"
+ puts 'cb <file>'
+ puts "\t open the specified file"
+ puts 'cb -r | --ref <commit>'
+ puts "\t open the specified commit"
+end
+
+# CLI
+#
+case ARGV[0]
+when '--help', '-h'
+ help
+when '--ref', '-r'
+ ref = ARGV[1]
+ open CODEBASE_COMMIT_URL % (repository_info << ref)
+when nil
+ open CODEBASE_REPOSITORY_URL % repository_info
+else
+ file = ARGV[0]
+ open CODEBASE_BLOB_URL % (repository_info << file)
+end
@@ -0,0 +1,29 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib/', __FILE__)
+$:.unshift lib unless $:.include?(lib)
+
+require 'codebase_cli'
+
+Gem::Specification.new do |s|
+ s.name = "codebase-cli"
+ s.version = CodebaseCli::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Lucas Fais"]
+ s.email = ["lucasfais@gmail.com"]
+ s.homepage = "https://github.com/abril/codebase-cli"
+ s.summary = %q{Command line utility to open codebase repositories in you browser}
+ s.description = %q{Command line utility to open codebase repositories in you browser}
+
+ s.required_rubygems_version = ">= 1.3.6"
+
+ s.add_development_dependency "step-up"
+
+ excepts = %w[
+ .gitignore
+ codebase-cli.gemspec
+ ]
+
+ s.files = `git ls-files`.split("\n") - excepts
+ s.require_paths = ["lib"]
+ s.executables = %w(cb)
+end
@@ -0,0 +1,6 @@
+# encoding: UTF-8
+
+module CodebaseCli
+ autoload :VERSION, 'codebase_cli/version.rb'
+
+end
@@ -0,0 +1,10 @@
+module CodebaseCli
+ version = nil
+ version = $1 if ::File.expand_path('../..', __FILE__) =~ /\/codebase-cli-([\w\.\-]+)/
+ if version.nil? && ::File.exists?(::File.expand_path('../../../.git', __FILE__))
+ require "step-up"
+ version = StepUp::Driver::Git.last_version
+ end
+ version = "0.0.0" if version.nil?
+ VERSION = version.gsub(/^v?([^\+]+)\+?\d*$/, '\1')
+end

0 comments on commit a8ee2b1

Please sign in to comment.