Class: GitPairs::Helper

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

Class Method Summary collapse

Class Method Details

.add(conf, path_to_conf, initials) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/helper.rb', line 90

def self.add(conf, path_to_conf, initials)
  if self.exists?(conf, initials)
    puts ""
    puts Paint["Pairing Partner '#{initials}' already exists", :red]
    puts Paint["To replace '#{initials}', first execute:  git pair -d #{initials}", :yellow]
  else
    puts ""
    puts Paint["Please provide info for: #{initials}", :yellow]
    name = ask("Full Name: ").to_s
    user = ask("Git Username: ")
    #just in case they supply email address
    user = user.split('@')[0].to_s
    email = ask("Email: ").to_s
    partner = {initials => {'name' => name, 'username' => user, 'email' => email}}
    conf["pairs"].update(partner)
    temp_conf = YAML::Store.new(path_to_conf)
    temp_conf.transaction do
      temp_conf["pairs"] = conf["pairs"]
    end
    puts ""
    puts Paint["Added '#{initials}' to list of available pairs", :yellow]
  end
end

.delete(conf, path_to_conf, initials) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/helper.rb', line 185

def self.delete(conf, path_to_conf, initials)
  if conf["pairs"].include?(initials)
    conf["pairs"].delete(initials)
    temp_conf = YAML::Store.new(path_to_conf)
    temp_conf.transaction do
      temp_conf["pairs"] = conf["pairs"]
    end
  end
end

.exists?(conf, initials) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/helper.rb', line 177

def self.exists?(conf, initials)
  return conf["pairs"].include?(initials)
end

.fetch(conf, initials) ⇒ Object



181
182
183
# File 'lib/helper.rb', line 181

def self.fetch(conf, initials)
  return conf["pairs"][initials]
end

.git_installed?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
# File 'lib/helper.rb', line 13

def self.git_installed?
  warning = "Please ensure that git is installed before proceeding"
  # Check if we are in a git repo
  if self.windows?
    Trollop::die warning unless system 'git --version > NUL 2>NUL'
  else
    Trollop::die warning unless system 'git --version > /dev/null 2>/dev/null'
  end
end

.git_repo?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
# File 'lib/helper.rb', line 23

def self.git_repo?
  self.git_installed?
  warning = "Not in a git repo"
  # Check if we are in a git repo
  if self.windows?
    Trollop::die warning unless system 'git status > NUL 2>NUL'
  else
    Trollop::die warning unless system 'git status > /dev/null 2>/dev/null'
  end
end

.git_resetObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/helper.rb', line 34

def self.git_reset
  self.git_installed?
  if self.windows?
    `git config --unset-all user.name > NUL 2>NUL`
    `git config --unset-all user.email > NUL 2>NUL`
    `git config --unset-all user.initials > NUL 2>NUL`
    `git config --remove-section user > NUL 2>NUL`
  else
    `git config --unset-all user.name > /dev/null 2>/dev/null`
    `git config --unset-all user.email > /dev/null 2>/dev/null`
    `git config --unset-all user.initials > /dev/null 2>/dev/null`
    `git config --remove-section user > /dev/null 2>/dev/null`
  end
end

.init(path_to_conf) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/helper.rb', line 49

def self.init(path_to_conf)
  # Create config if it doesn't already exist
  unless File.exists?(path_to_conf)
    self.git_installed?
    puts Paint["initializing git-pairing for the first time...", :yellow]
    name = `git config --global --get user.name`
    initials = ""
    name.strip.downcase.split(/ /).each { |n| initials << n.split(//)[0] }
    email = `git config --global --get user.email`
    username = email.split("@")[0]
    default_conf = YAML::Store.new(path_to_conf)
    default_conf.transaction do
      default_conf["pairs"] = {"#{initials}" => {'name'=>"#{name.strip}", 'username'=>"#{username}", 'email'=>"#{email.strip}"} }
      default_conf["delimiters"] = {"name" => " / ", "initials" => " ", "email" => " , "}
    end
  end

  # Update older confs with recently added settings
  tmp_conf = YAML::load(File.open(path_to_conf))
  update_conf = YAML::Store.new(path_to_conf)
  update_conf.transaction do
    update_conf["delimiters"] = tmp_conf["delimiters"] || {}
    update_conf["delimiters"]["name"] = (tmp_conf["delimiters"] && tmp_conf["delimiters"]["name"]) || " / "
    update_conf["delimiters"]["initials"] = (tmp_conf["delimiters"] && tmp_conf["delimiters"]["initials"]) || " "
    update_conf["delimiters"]["email"] = (tmp_conf["delimiters"] && tmp_conf["delimiters"]["email"]) || " , "
  end

  return YAML::load(File.open(path_to_conf))
end

.pairObject



114
115
116
117
118
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
150
151
152
153
154
# File 'lib/helper.rb', line 114

def self.pair
  self.git_installed?
  if self.windows?
    install_with = <<jam
source ~/.git-pairing-prompt.sh
export PS1="\\[\\$(__git_pairing_prompt)\\] "
jam
  else
    install_with = <<jam
source ~/.git-pairing-prompt.sh
export PROMPT_COMMAND='__git_pairing_prompt; echo -ne "\033];${PWD##*/}\007"'
jam
    puts ""
    puts Paint["If you had an older version previously installed, edit your .bash_profile/.bashrc file to remove the following lines:", :yellow]
    puts Paint['source ~/.git-pairing-prompt.sh', :yellow]
    puts Paint['export PS1="\[$(__git_pairing_prompt)\] "', :yellow]
  end

  puts ""
  confirmed = agree("Configure git-pairing bash prompt? ")
  puts ""
  if confirmed
    # copy shell script to user's home dir
    home = File.expand_path('~')
    `curl -o #{home}/.git-pairing-prompt.sh -L https://raw.github.com/glg/git-pairing/master/shell/.git-pairing-prompt.sh`
    # configure prompt
    [ "#{home}/.bash_profile", "#{home}/.bashrc" ].each do |profile|
      if File.exists? profile
        open(profile, 'a') do |f|
          f.puts install_with
        end
      end
    end
    puts ""
    puts Paint["Please re-source your bash_profile or bashrc", :yellow]
    puts Paint["(i.e., source ~/.bash_profile", :yellow]
    puts ""
    puts Paint["Feel free to edit ~/.git-pairing-prompt.sh to suit your prompt preferences", :blue]
    puts ""
  end
end

.set(conf, authors) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/helper.rb', line 156

def self.set(conf, authors)
  self.git_repo?
  authors.sort!
  sorted_authors = ""
  sorted_initials = ""
  sorted_emails = ""
  authors.each do |a|
    sorted_authors << a[0]
    sorted_initials << a[1]
    sorted_emails << a[2]
    if authors.index(a) < authors.size-1
      sorted_authors << conf['delimiters']['name']
      sorted_initials << conf['delimiters']['initials']
      sorted_emails << conf['delimiters']['email']
    end
    cmd_git = `git config user.name "#{sorted_authors}"`
    cmd_git = `git config user.initials "#{sorted_initials}"`
    cmd_git = `git config user.email "#{sorted_emails}"`
  end
end

.whoamiObject



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

def self.whoami
  self.git_installed?
  user = `git config --get user.name`.strip
  email = `git config --get user.email`.strip
  puts ""
  puts "current git config >"
  puts Paint["Name: #{user}", :yellow]
  puts Paint["Email: #{email}", :yellow]
  puts ""
end

.windows?Boolean

Functions for manipulating the .pairs config file

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/helper.rb', line 8

def self.windows?
  windows = (RUBY_PLATFORM.to_s =~ /win32|mswin|mingw|cygwin/ || RUBY_PLATFORM.to_s == 'ruby') ? true : false
  return windows
end