Class: Pro::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/pro/commands.rb

Constant Summary collapse

DIRTY_MESSAGE =
'uncommitted'.red
UNPUSHED_MESSAGE =
'unpushed'.blue
JOIN_STRING =
' + '

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Commands



35
36
37
# File 'lib/pro/commands.rb', line 35

def initialize(index)
  @index = index
end

Instance Method Details

#find_repo(name) ⇒ Object

Fuzzy search for a git repository by name Returns the full path to the repository.

If name is nil return the pro base.



43
44
45
46
47
# File 'lib/pro/commands.rb', line 43

def find_repo(name)
  return @index.base_dirs.first unless name
  match = FuzzyMatch.new(@index.to_a, :read => :name).find(name)
  match[1] unless match.nil?
end

#install_cdObject

Adds a shell function to the shell config files that allows easy directory changing.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pro/commands.rb', line 119

def install_cd
  puts CD_INFO
  print "Continue with installation (yN)? "
  return unless gets.chomp.downcase == "y"
  # get name
  print "Name of pro cd command (default 'pd'): "
  name = gets.strip
  name = 'pd' if name.empty?
  # sub into function
  func = SHELL_FUNCTION.sub("{{name}}",name)
  did_any = false
  ['~/.profile', '~/.bashrc','~/.zshrc','~/.bash_profile'].each do |rel_path|
    # check if file exists
    path = File.expand_path(rel_path)
    next unless File.exists?(path)
    # ask the user if they want to add it
    print "Install #{name} function to #{rel_path} [yN]: "
    next unless gets.chomp.downcase == "y"
    # add it on to the end of the file
    File.open(path,'a') do |file|
      file.puts func
    end
    did_any = true
  end
  if did_any
    puts "Done! #{name} will be available in new shells."
  else
    STDERR.puts "WARNING: Did not install in any shell dotfiles.".red
    STDERR.puts "Maybe you should create the shell config file you want.".red
  end
end

#list_basesObject

prints out all the base directories



71
72
73
74
75
# File 'lib/pro/commands.rb', line 71

def list_bases
  @index.base_dirs.each do |b|
    puts b
  end
end

#list_reposObject

Prints out the paths to all repositories in all bases



64
65
66
67
68
# File 'lib/pro/commands.rb', line 64

def list_repos()
  @index.each do |r|
    puts r.path
  end
end

#repo_clean?(path) ⇒ Boolean

Checks if there are any uncommitted changes



98
99
100
101
102
103
104
# File 'lib/pro/commands.rb', line 98

def repo_clean?(path)
  status = ""
  Dir.chdir(path) do
    status = `git status 2>/dev/null`
  end
  return status.end_with?("(working directory clean)\n") || status.end_with?("working directory clean\n")
end

#repo_status(path) ⇒ Object

returns a short status message for the repo



90
91
92
93
94
95
# File 'lib/pro/commands.rb', line 90

def repo_status(path)
  messages = []
  messages << DIRTY_MESSAGE unless repo_clean?(path)
  messages << UNPUSHED_MESSAGE if repo_unpushed?(path)
  messages.join(JOIN_STRING)
end

#repo_unpushed?(path) ⇒ Boolean

Finds if there are any commits which have not been pushed to origin



107
108
109
110
111
112
113
114
115
# File 'lib/pro/commands.rb', line 107

def repo_unpushed?(path)
  unpushed = ""
  Dir.chdir(path) do
    branch_ref = `/usr/bin/git symbolic-ref HEAD 2>/dev/null`
    branch = branch_ref.chomp.split('/').last
    unpushed = `git cherry -v origin/#{branch} 2>/dev/null`
  end
  return !(unpushed.empty?)
end

#run_command(command, confirm = true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pro/commands.rb', line 49

def run_command(command, confirm = true)
  if confirm
    print "Do you really want to run '#{command.bold}' on all repos [Yn]? "
    ans = STDIN.gets.chomp.downcase
    return unless ans == 'y' || ans.empty?
  end
  @index.each do |r|
    Dir.chdir(r.path)
    result = `#{command}`
    puts "#{r.name}:".bold.red
    puts result
  end
end

#statusObject

prints a status list showing repos with unpushed commits or uncommitted changes



79
80
81
82
83
84
85
86
87
# File 'lib/pro/commands.rb', line 79

def status()
  max_name = @index.map {|repo| repo.name.length}.max + 1
  @index.each do |r|
    status = repo_status(r.path)
    next if status.empty?
    name = format("%-#{max_name}s",r.name).bold
    puts "#{name} > #{status}"
  end
end