Class: MetaCon::Project

Inherits:
Object
  • Object
show all
Includes:
Loaders::Index, Shorthand
Defined in:
lib/metacon/project.rb

Constant Summary

Constants included from Loaders::Index

Loaders::Index::LOADERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shorthand

#common_prefix, #included, #relative_path, #shcmd

Constructor Details

#initialize(relative_to = './', verbose = true) ⇒ Project

Returns a new instance of Project.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/metacon/project.rb', line 21

def initialize(relative_to='./', verbose=true)
  @verbose = verbose
  @rel_dir = File.expand_path(relative_to)
  @mc_dir = Project.find_mc_dir(@rel_dir)
  if @mc_dir.nil?
    @root_dir = nil
    @valid = false
    @state = nil
  else
    @root_dir = File.expand_path(File.join(@mc_dir, '..'))
    @valid = true
    @state = SavedState.new(@mc_dir)
    refresh_conf
  end
end

Instance Attribute Details

#mc_dirObject

Returns the value of attribute mc_dir.



7
8
9
# File 'lib/metacon/project.rb', line 7

def mc_dir
  @mc_dir
end

#rel_dirObject

Returns the value of attribute rel_dir.



7
8
9
# File 'lib/metacon/project.rb', line 7

def rel_dir
  @rel_dir
end

#root_dirObject

Returns the value of attribute root_dir.



7
8
9
# File 'lib/metacon/project.rb', line 7

def root_dir
  @root_dir
end

#validObject

Returns the value of attribute valid.



7
8
9
# File 'lib/metacon/project.rb', line 7

def valid
  @valid
end

Class Method Details

.initialized?(relative_to = './') ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/metacon/project.rb', line 8

def self.initialized?(relative_to='./')
  ! find_mc_dir(relative_to).nil?
end

Instance Method Details

#can_switch?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/metacon/project.rb', line 65

def can_switch?
  # TODO: make sure submodules don't have stuff to stash etc.
  return true
end

#confObject



38
# File 'lib/metacon/project.rb', line 38

def conf; @config[current_state] end

#conf_objObject



37
# File 'lib/metacon/project.rb', line 37

def conf_obj; @config end

#current_stateObject



70
71
72
73
74
75
# File 'lib/metacon/project.rb', line 70

def current_state
  st = @state.readonly
  st[:os] = this_os if (st[:os] == '(this)' || st[:os] == '.')
  st[:host] = this_host if (st[:host] == '(this)' || st[:host] == '.')
  return st
end

#full_contextObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/metacon/project.rb', line 77

def full_context
  res = {}
  res[:root_dir] = @root_dir
  o,e,s = shcmd(["cd \"#{@root_dir}\"",
                 "source '#{MetaCon::shelp_dir}/git-completion.bash'",
                 "export GIT_PS1_SHOWUPSTREAM=\"auto\"",
                 "export GIT_PS1_SHOWDIRTYSTATE=1",
                 "export GIT_PS1_SHOWUNTRACKEDFILES=1",
                 "export GIT_PS1_SHOWSTASHSTATE=1",
                 "__git_ps1 \"%s\""].join(' && '), false)
  git_br = o.strip.split(/\s+/)
  git_codes = git_br.pop
  git_brname = git_br.join(' ')
  res[:git_branch] = git_brname
  res[:git_upstream] =
    case
    when git_codes.include?('<>'); :diverged
    when git_codes.include?('<'); :behind
    when git_codes.include?('>'); :ahead
    else :same
    end
  res[:git_has_stashed] = git_codes.include?('$')
  res[:git_has_unstaged] = git_codes.include?('*')
  res[:git_has_staged] = git_codes.include?('+')
  res[:git_has_changes] = res[:git_has_staged] || res[:git_has_unstaged]
  pwd = File.expand_path(Dir.pwd)
  res[:pwd_from_root] = relative_path(@root_dir,pwd)
  cs = current_state
  res[:runtime_context] = cs[:rtc]
  res[:role] = cs[:role]
  res[:os] = cs[:os]
  res[:machine] = cs[:host]
  res[:name] = res[:root_dir].split('/').last.strip
  res[:metadir] = "{#{res[:name]}}/#{res[:pwd_from_root]}"
  res[:user] = `whoami`.strip
  return res
end

#list(to_list) ⇒ Object



120
121
122
123
124
# File 'lib/metacon/project.rb', line 120

def list(to_list)
  return nil unless @valid
  cs = current_state
  @config.declared[to_list].keys | [cs[to_list]]
end

#summary_strObject



115
116
117
118
# File 'lib/metacon/project.rb', line 115

def summary_str
  # TODO: for last line emitted in most commands and even used for to_s
  # possibly?
end

#switch(changes = {}, opts = {}) ⇒ Object

Options that it cares about:

- :verbose   true/false
- :shell     true/false


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/metacon/project.rb', line 53

def switch(changes={},opts={})
  return :nochange if changes=={}
  return :impossible unless can_switch?
  changed = false
  @state.atomic do |s|
    changes.each{|key, val| s[key]=val unless s[key]==val }
    changed = s.dirty
  end
  if changed then return setup_context(opts)
  else return :nochange end
end

#this_hostObject



45
46
47
48
# File 'lib/metacon/project.rb', line 45

def this_host
  @this_host ||= `uname -n`.strip
  @this_host
end

#this_osObject



40
41
42
43
# File 'lib/metacon/project.rb', line 40

def this_os
  @@this_os ||= `uname -s`.strip.downcase
  @@this_os
end

#uidObject



12
13
14
15
16
17
18
19
# File 'lib/metacon/project.rb', line 12

def uid
  if @uid.nil?
    require 'digest/sha1'
    key = self.this_host + @state[:role] + @state[:rtc] + @state[:host] + @state[:os]
    @uid = Digest::SHA1.hexdigest(key)[0..5]
  end
  @uid
end