Module: Git::Multi::Settings

Defined in:
lib/git/multi/utils.rb,
lib/git/multi/settings.rb

Constant Summary collapse

TICK =
['2714'.hex].pack('U*').green.freeze
CROSS =
['2718'.hex].pack('U*').red.freeze
ARROW =
['2794'.hex].pack('U*').blue.freeze

Class Method Summary collapse

Class Method Details

.abbreviate(directory, root_dir = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/git/multi/utils.rb', line 28

def abbreviate(directory, root_dir = nil)
  case root_dir
  when :home
    then directory.gsub(Git::Multi::HOME, '${HOME}')
  when :workarea
    then directory.gsub(Git::Multi::WORKAREA, '${WORKAREA}')
  else abbreviate(abbreviate(directory, :workarea), :home)
  end
end

.describe(token) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/git/multi/utils.rb', line 8

def describe(token)
  if token.nil?
    '(nil)'
  elsif token.empty?
    '(empty)'
  else
    "#{'*' * 36}#{token[36..-1]}"
  end
end

.directory_status(messages, directory) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/git/multi/settings.rb', line 29

def directory_status(messages, directory)
  setting_status(
    messages,
    directory && !directory.empty? && File.directory?(directory),
    false
  )
end

.file_status(file, message = 'File') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/git/multi/settings.rb', line 17

def file_status(file, message = 'File')
  setting_status(
    [
      message,
      abbreviate(file),
      File.file?(file) ? "#{File.size(file).commify} bytes" : nil,
    ],
    file && !file.empty? && File.file?(file),
    false
  )
end

.home_status(home) ⇒ Object



77
78
79
# File 'lib/git/multi/settings.rb', line 77

def home_status(home)
  directory_status(['${HOME}', home], home)
end

.main_workarea_status(workarea) ⇒ Object



81
82
83
# File 'lib/git/multi/settings.rb', line 81

def main_workarea_status(workarea)
  directory_status(['${WORKAREA}', abbreviate(workarea, :home)], workarea)
end

.organization_status(orgs) ⇒ Object



64
65
66
67
68
69
# File 'lib/git/multi/settings.rb', line 64

def organization_status(orgs)
  orgs.each do |org|
    setting_status(['Organization', org], org && !org.empty?, true)
    setting_status(['Organization', 'member?'], Git::Hub.orgs.include?(org), !Git::Hub.connected?)
  end
end

.organization_workarea_status(orgs) ⇒ Object



91
92
93
94
95
# File 'lib/git/multi/settings.rb', line 91

def organization_workarea_status(orgs)
  orgs.each do |org|
    workarea_status("Workarea (org: #{org})", Git::Multi::WORKAREA, org)
  end
end

.setting_status(messages, valid = false, optional = true) ⇒ Object



11
12
13
14
15
# File 'lib/git/multi/settings.rb', line 11

def setting_status(messages, valid = false, optional = true)
  fields = messages.compact.join(' - ')
  icon = valid ? TICK : optional ? ARROW : CROSS
  puts "#{icon}  #{fields}"
end

.symbolize(token) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/git/multi/utils.rb', line 18

def symbolize(token)
  case token
  when Git::Multi.env_var('OCTOKIT_ACCESS_TOKEN')
    then '${OCTOKIT_ACCESS_TOKEN}'
  when Git::Multi.git_option('github.token')
    then 'github.token'
  else '(unset)'
  end
end

.token_status(token) ⇒ Object



71
72
73
74
75
# File 'lib/git/multi/settings.rb', line 71

def token_status(token)
  setting_status(['Token', symbolize(token), describe(token)], !token.nil? && !token.empty?)
  setting_status(['Token', 'valid?'], !token.nil? && !token.empty? && Git::Hub., !Git::Hub.connected?)
  setting_status(['Token', "owned by #{Git::Multi::USER}?"], Git::Hub. == Git::Multi::USER, !Git::Hub.connected?)
end

.user_status(user) ⇒ Object



60
61
62
# File 'lib/git/multi/settings.rb', line 60

def user_status(user)
  setting_status(['User', user], user && !user.empty?)
end

.user_workarea_status(users) ⇒ Object



85
86
87
88
89
# File 'lib/git/multi/settings.rb', line 85

def user_workarea_status(users)
  users.each do |user|
    workarea_status("Workarea (user: #{user})", Git::Multi::WORKAREA, user)
  end
end

.workarea_status(message, workarea, owner) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/git/multi/settings.rb', line 37

def workarea_status(message, workarea, owner)
  directory_status(
    [
      message,
      File.join(abbreviate(workarea, :workarea), owner),
    ],
    File.join(workarea, owner)
  )

  github_count = Git::Multi.repositories_for(owner).count
  cloned_count = Git::Multi.cloned_repositories_for(owner).count
  missing_count = (github_count - cloned_count)
  subdir_count = Dir.new(workarea).git_repos(owner).count
  surplus_count = (subdir_count - cloned_count)

  setting_status(["\tGitHub ", github_count])
  setting_status(["\tcloned ", cloned_count, "(#{missing_count} missing)"])
  Git::Multi.missing_repositories_for(owner).each do |missing|
    setting_status(["\tmissing", missing.full_name], false, false)
  end
  setting_status(["\tsubdirs", subdir_count, "(#{surplus_count} surplus)"])
end