Class: Linecook::Cookbook

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

Constant Summary collapse

MANIFEST_KEY =
'manifest'
PATHS_KEY =
'paths'
GEMS_KEY =
'gems'
REWRITE_KEY =
'rewrite'
PATTERNS =
[
  ['attributes', '**/*{.rb,.yaml,.yml,.json}', true],
  ['files',      '**/*'],
  ['recipes',    '**/*.rb', true],
  ['templates',  '**/*']
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, project_dir = '.') ⇒ Cookbook

Returns a new instance of Cookbook.



52
53
54
55
# File 'lib/linecook/cookbook.rb', line 52

def initialize(config={}, project_dir='.')
  @project_dir = project_dir
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



50
51
52
# File 'lib/linecook/cookbook.rb', line 50

def config
  @config
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



49
50
51
# File 'lib/linecook/cookbook.rb', line 49

def project_dir
  @project_dir
end

Class Method Details

.config_file(project_dir = '.') ⇒ Object



7
8
9
# File 'lib/linecook/cookbook.rb', line 7

def config_file(project_dir='.')
  Dir.glob(File.join(project_dir, '{C,c}ookbook')).first
end

.gemsObject



26
27
28
29
30
31
32
33
34
# File 'lib/linecook/cookbook.rb', line 26

def gems
  return [] unless Object.const_defined?(:Gem)
  
  Gem.source_index.latest_specs.select do |spec|
    config_file(spec.full_gem_path) != nil
  end.collect do |spec|
    spec.name
  end
end

.init(project_dir = '.') ⇒ Object



22
23
24
# File 'lib/linecook/cookbook.rb', line 22

def init(project_dir='.')
  setup config_file(project_dir), project_dir
end

.setup(config = {}, project_dir = '.') ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/linecook/cookbook.rb', line 11

def setup(config={}, project_dir='.')
  unless config.kind_of?(Hash)
    config = Utils.load_config(config)
  end
  
  config[PATHS_KEY] ||= [project_dir]
  config[GEMS_KEY]  ||= gems
  
  new(config, project_dir)
end

Instance Method Details

#full_gem_pathsObject



73
74
75
76
77
78
79
80
81
# File 'lib/linecook/cookbook.rb', line 73

def full_gem_paths
  return [] if gems.empty?
  specs = latest_specs
  
  gems.collect do |name| 
    spec = specs[name] or raise "no such gem: #{name.inspect}"
    spec.full_gem_path
  end
end

#gemsObject



61
62
63
# File 'lib/linecook/cookbook.rb', line 61

def gems
  Utils.arrayify config[GEMS_KEY]
end

#glob(*paths) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/linecook/cookbook.rb', line 103

def glob(*paths)
  manifest = Hash.new {|hash, key| hash[key] = {} }
  
  paths.each do |path|
    PATTERNS.each do |(type, glob, chomp_extname)|
      resource_dir = File.expand_path(File.join(path, type), project_dir)
      pattern = File.join(resource_dir, glob)
      
      Dir.glob(pattern).each do |full_path|
        next unless File.file?(full_path)
        
        name = relative_path(resource_dir, full_path)
        name.chomp!(File.extname(full_path)) if chomp_extname
        
        manifest[type][name] = full_path
      end
    end
  end
  
  manifest
end

#manifestObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/linecook/cookbook.rb', line 125

def manifest
  manifest = glob(*(full_gem_paths + paths))
  
  if overrides
    manifest = Utils.deep_merge(manifest, overrides)
  end
  
  manifest.each_key do |key|
    manifest[key] = rewrite manifest[key]
  end
  
  manifest
end

#merge(config = {}) ⇒ Object



139
140
141
142
143
# File 'lib/linecook/cookbook.rb', line 139

def merge(config={})
  duplicate = dup
  dup.config.merge!(config)
  dup
end

#overridesObject



69
70
71
# File 'lib/linecook/cookbook.rb', line 69

def overrides
  config[MANIFEST_KEY]
end

#pathsObject



57
58
59
# File 'lib/linecook/cookbook.rb', line 57

def paths
  Utils.arrayify config[PATHS_KEY]
end

#rewrite(manifest) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/linecook/cookbook.rb', line 83

def rewrite(manifest)
  replacements = {}
  
  rewrites.each_pair do |pattern, substitution|
    manifest.keys.each do |key|
      replacement = key.sub(pattern, substitution)
      next if key == replacement
      raise "multiple replacements for: #{key}" if replacements.has_key?(key)
      
      replacements[key] = replacement
    end
  end if rewrites
  
  replacements.each do |key, replacement|
    manifest[replacement] = manifest.delete(key)
  end
  
  manifest
end

#rewritesObject



65
66
67
# File 'lib/linecook/cookbook.rb', line 65

def rewrites
  config[REWRITE_KEY]
end