Class: Grem::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/grem/cli.rb

Class Method Summary collapse

Class Method Details

.browse(stdout, arguments = []) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/grem/cli.rb', line 64

def self.browse(stdout, arguments=[])
  url = 'http://github.com/'

  github = Pathname.new(File.expand_path('~/github'))
  here = Pathname.new(Dir.pwd)
  if self.is_under(here, github) then
    relative = here.relative_path_from(github).to_s
    while true
      break if File.split(File.split(relative)[0])[0] ==
        File.split(File.split(File.split(relative)[0])[0])[0]
      relative = File.split(relative)[0]
    end
    url += relative if relative != '.'
  else
    stdout.puts 'It appears that you are not in ~/github. For usage info, run "grem --help".'
    exit
  end
  Launchy.open(url)
end

.clone(stdout, arguments = []) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/grem/cli.rb', line 46

def self.clone(stdout, arguments=[])
  username = arguments[0]
  reponame = arguments[1]

  user_path = File.join(File.expand_path('~/github'), username)
  repo_path = File.join(user_path, reponame)
  if File.exist?(repo_path)
    stdout.puts "A file already exists at #{repo_path}. Exiting."; exit
  end
  
  remote_path = "git://github.com/#{username}/#{reponame}.git"

  FileUtils.mkdir_p(user_path)
  FileUtils.chdir(user_path) do
    system('git', 'clone', remote_path)
  end
end

.execute(stdout, arguments = []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grem/cli.rb', line 8

def self.execute(stdout, arguments=[])
  parser = OptionParser.new do |opts|
    opts.banner = "      The (GitHub) REpo Manager (grem) clones a github repo into ~/github/username/reponame.\n\n      Cloning:\n\n        \#{File.basename($0)} github_username repo_name\n\n        Clones a repo to ~/github/\\\#{github_username}/\\\#{repo_name}\n\n      Browsing:\n\n        \#{File.basename($0)}\n\n        Browses to a page on GitHub based on the current directory.\n        ~/github goes to http://github.com/\n        ~/github/benatkin goes to http://github.com/benatkin\n        ~/github/benatkin/grem and deeper goes to http://github.com/benatkin/grem\n        outside ~/github prints a friendly error message\n\n      Options are:\n    BANNER\n    opts.separator \"\"\n    opts.on(\"-h\", \"--help\",\n            \"Show this help message.\") { stdout.puts opts; exit }\n    opts.parse!(arguments)\n\n    if arguments.length == 2\n      self.clone(stdout, arguments)\n    elsif arguments.length == 0\n      self.browse(stdout, arguments)\n    else\n      stdout.puts opts; exit\n    end\n  end\nend\n".gsub(/^          /,'')

.is_under(here, github) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/grem/cli.rb', line 84

def self.is_under(here, github)
  while here.parent != here
    return true if here == github
    here = here.parent
  end
  return false
end