Class: Linecook::Commands::Compile

Inherits:
Linecook::Command show all
Defined in:
lib/linecook/commands/compile.rb

Overview

::desc compile recipes

Compiles a list of recipes into a single package and exports the result to the working directory. The recipes are added to the package at their relative path, minus their extname.

For example:

$ echo "write 'echo hello world'" > recipe.rb 
$ linecook compile recipe.rb
$ sh recipe
hello world

Providing ‘-’ as a recipe will cause stdin to be compiled as a recipe to stdout.

Direct Known Subclasses

Build

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Linecook::Command

#call, help, #initialize, signature

Constructor Details

This class inherits a constructor from Linecook::Command

Class Method Details

.cookbook_gem_paths(latest = true) ⇒ Object



66
67
68
69
70
# File 'lib/linecook/commands/compile.rb', line 66

def cookbook_gem_paths(latest=true)
  Cookbook.gemspecs(latest).collect do |gemspec|
    gemspec.full_gem_path
  end
end

.gemspecs(latest = true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/linecook/commands/compile.rb', line 72

def gemspecs(latest=true)
  return [] unless Object.const_defined?(:Gem)

  index = Gem.source_index
  specs = latest ? index.latest_specs : index.gems.values

  specs.select do |spec|
    cookbook_file = File.expand_path(default_file_name, spec.full_gem_path)
    File.exists?(cookbook_file)
  end
end

.parse(argv = ARGV) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/linecook/commands/compile.rb', line 26

def parse(argv=ARGV)
  super(argv) do |options|
    options.on('-I DIRECTORY', 'prepend to LOAD_PATH') do |path|
      $LOAD_PATH.unshift File.expand_path(path)
    end

    options.on('-r LIBRARY', 'require the library') do |path|
      require(path)
    end

    options.on('-G GEMNAME', 'add gem to cookbook path', :option_type => :list) do |name|
      specs = Gem.source_index.find_name(name)
      if specs.empty?
        raise CommandError, "could not find gem: #{name.inspect}"
      end
      (options[:cookbook_path] ||= []) << specs.first.full_gem_path
    end

    options.on('-g', '--gems', 'add latest cookbook gems') do |name|
      (options[:cookbook_path] ||= []).concat cookbook_gem_paths
    end

    options.on('-c', '--common', 'use common flags') do
      set_common_options(options)
    end

    if block_given?
      yield(options)
    end
  end
end

.set_common_options(options) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/linecook/commands/compile.rb', line 58

def set_common_options(options)
  cookbook_path = (options[:cookbook_path] ||= [])
  cookbook_path << '.'
  cookbook_path.concat cookbook_gem_paths
  (options[:helper_dirs] ||= []) << 'helpers'
  options
end

Instance Method Details

#compile_helpers(helper_dir) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/linecook/commands/compile.rb', line 166

def compile_helpers(helper_dir)
  compiler = CompileHelper.new(
    :force => force,
    :quiet => true
  )

  helpers = glob_helpers(helper_dir)
  helpers.each do |(name, sources)|
    compiler.process(name, *sources)
  end

  $LOAD_PATH.unshift compiler.output_dir
end

#glob_helpers(helper_dir) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/linecook/commands/compile.rb', line 149

def glob_helpers(helper_dir)
  sources = {}
  helpers = []

  Dir.glob("#{helper_dir}/*/**/*").each do |source_file|
    next if File.directory?(source_file)
    (sources[File.dirname(source_file)] ||= []) << source_file
  end

  sources.each_pair do |dir, source_files|
    name = dir[(helper_dir.length + 1)..-1]
    helpers << [name, source_files]
  end

  helpers.sort_by {|name, source_files| name }
end

#input_dir=(input) ⇒ Object



92
93
94
# File 'lib/linecook/commands/compile.rb', line 92

def input_dir=(input)
  @input_dir = File.expand_path(input)
end

#load_env(package_file) ⇒ Object



144
145
146
147
# File 'lib/linecook/commands/compile.rb', line 144

def load_env(package_file)
  env = package_file && File.exists?(package_file) ? YAML.load_file(package_file) : nil
  env.nil? ? {} : env
end

#output_dir=(input) ⇒ Object



96
97
98
# File 'lib/linecook/commands/compile.rb', line 96

def output_dir=(input)
  @output_dir = File.expand_path(input)
end

#process(*recipes) ⇒ Object



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
127
128
129
130
131
132
133
134
# File 'lib/linecook/commands/compile.rb', line 100

def process(*recipes)
  helper_dirs.each do |helper_dir|
    compile_helpers(helper_dir)
  end

  package  = Package.new(load_env(package_file))
  cookbook = Cookbook.new(*cookbook_path)
  stdout   = StringIO.new

  recipes.each do |recipe_path|
    recipe = Recipe.new(package, cookbook)

    if recipe_path == '-'
      recipe.instance_eval $stdin.read, 'stdin'
      stdout.print recipe
    else
      recipe_path = File.expand_path(recipe_path)
      recipe.register_as relative_path(input_dir, recipe_path).chomp('.rb')
      recipe.instance_eval File.read(recipe_path), recipe_path
      recipe.register_to package
    end
  end

  package.export(output_dir) do |src, dest|
    unless force
      raise CommandError, "already exists: #{dest.inspect}"
    end

    FileUtils.rm_rf(dest)
    true
  end

  # print to $stdout after export to ensure files will be available
  $stdout << stdout.string
end

#relative_path(dir, path) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/linecook/commands/compile.rb', line 136

def relative_path(dir, path)
  if path.index(dir) == 0 && path != dir
    path[dir.length + 1, path.length - dir.length]
  else
    File.basename(path)
  end
end