Class: Rake::Application::KeySpace

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

Instance Method Summary collapse

Constructor Details

#initializeKeySpace

Returns a new instance of KeySpace.



13
14
15
16
17
# File 'lib/rake/pro/keystore.rb', line 13

def initialize
  @cfg_files = []
  @kvp_stack = []
  @kvp = {}
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/rake/pro/keystore.rb', line 19

def [](key)
  @kvp[key]
end

#after_invoke(task_details) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/rake/pro/keystore.rb', line 128

def after_invoke task_details
  task_name = task_details[0]
  parts = task_name.split(':')
  if parts.length > 1
    pop_scope
    pop_cfg
  end
end

#before_invoke(task_details) ⇒ Object



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
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rake/pro/keystore.rb', line 86

def before_invoke task_details
  task_name = task_details[0]
  parts = task_name.split(':')
  push_scope(parts[0].to_sym) if parts.length == 1

  load_paths = parts.reduce([
      rpath(root, 'cfg.yml'),
      rpath(root, '.cfg.yml'),
      rpath(root, '.cfg-private.yml')
    ]) { |paths, folder|
    paths.push(rpath(root, folder, 'cfg.yml'))
    paths.push(rpath(root, folder, '.cfg.yml'))
    paths.push(rpath(root, folder, '.cfg-private.yml'))
    paths
  }
  load_paths.push(rpath(home, '.cyborg.yml'))

  load_paths.each { |path|
    load_once(path)
  }

  # promote and prune the key space
  pruned = false
  pruned_stack = []
  @kvp_stack.each_with_index { |kvp, index| 
    @scopes.each_with_index { |scope, i2|
      kvp, didprune = kvp.promote_key(scope)
      siblings = scope_siblings(scope)
      kvp = kvp.prune_keys(scope_siblings(scope)) if siblings
      pruned |= (didprune && scope == task_name.to_sym)
    }
    pruned_stack.push(kvp)
  }

  # merge the kvp stack entries into a single map
  @kvp = pruned_stack.reduce({}) { |acc, kvp|
    acc.recursive_merge(kvp)
  }

  [task_name, pruned]
end

#homeObject



27
28
29
# File 'lib/rake/pro/keystore.rb', line 27

def home()
  Etc.getpwuid.dir;
end

#load_once(cfg_file) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rake/pro/keystore.rb', line 68

def load_once(cfg_file)
  unless @cfg_files.include?(cfg_file)
    if (File.file?(cfg_file))
      Rake.application.active_dir = File.dirname(cfg_file)
      push_cfg(cfg_file)
    end
    @cfg_files.push(cfg_file)
  end
end

#pop_cfgObject



64
65
66
# File 'lib/rake/pro/keystore.rb', line 64

def pop_cfg
  @kvp_stack.pop
end

#pop_scopeObject



43
44
45
# File 'lib/rake/pro/keystore.rb', line 43

def pop_scope
  @scopes.pop
end

#push_cfg(cfg_file) ⇒ Object



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

def push_cfg cfg_file
  #puts "Loading cfg file:  #{cfg_file}..."
  cfg = YAML.load_file(cfg_file).symbolize_keys
  source = if (cfg.has_key?(:default))
              cfg[:default]
            elsif (cfg.has_key?(:source))
              cfg[:source]
            elsif (cfg.has_key?(:system))
              cfg[:system]
            elsif(cfg.has_key?(:target))
              cfg[:target]
            end

  push_scope(source.to_sym) if source
  @kvp_stack.push(cfg)
end

#push_scope(scope) ⇒ Object



39
40
41
# File 'lib/rake/pro/keystore.rb', line 39

def push_scope scope
  (@scopes ||= []).push(scope)
end

#rootObject



31
32
33
# File 'lib/rake/pro/keystore.rb', line 31

def root()
  Rake.original_dir
end

#rpath(*paths) ⇒ Object



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

def rpath(*paths)
  return File.join(paths)
end

#scope_siblings(scope) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rake/pro/keystore.rb', line 78

def scope_siblings scope
  siblings = nil
  Rake.application.scopes.each { |scope_set|
    siblings = scope_set if scope_set.include?(scope)
  }
  siblings
end

#valuesObject



23
24
25
# File 'lib/rake/pro/keystore.rb', line 23

def values()
  @kvp
end