Class: Linecook::Commands::Build

Inherits:
Compile show all
Defined in:
lib/linecook/commands/build.rb

Overview

::desc build packages

Builds a list of ‘package’ recipes into packages. Packages are exported to a directory named like the recipe. Build prints the package dir for each recipe to stdout.

Recipes are added to the package as the ‘run’ executable and they are automatically configured with a package file corresponding to the recipe, if it exists.

For example:

$ echo "write 'echo ' + attrs['msg']" > recipe.rb
$ echo "msg: hello world" > recipe.yml
$ linecook build recipe.rb
/path/to/pwd/recipe
$ /path/to/pwd/recipe/run
hello world

The input directory containing the package files and the output directory for the packages may be specified with options.

Package Specs

Package specs can be provided instead of recipes. Specs are comma-separated strings like ‘package_file,recipe_file,export_dir’ that allow full control over the building of packages. Package files

For example:

$ echo "write 'echo ' + attrs['msg']" > recipe.rb
$ echo "msg: hello world" > input.yml
$ linecook build input.yml,recipe.rb,output
/path/to/pwd/output
$ /path/to/pwd/output/run
hello world

Providing ‘-’ as an input will cause stdin to be read for additional inputs. In that way a CSV file can serve as a manifest for the packages built by this command.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Compile

#compile_helpers, cookbook_gem_paths, gemspecs, #glob_helpers, #load_env, #output_dir=, parse, #relative_path

Methods inherited from Linecook::Command

#call, help, #initialize, parse, signature

Constructor Details

This class inherits a constructor from Linecook::Command

Class Method Details

.set_common_options(options) ⇒ Object



48
49
50
51
52
53
# File 'lib/linecook/commands/build.rb', line 48

def set_common_options(options)
  super(options)
  options[:input_dir]  = 'packages'
  options[:output_dir] = 'packages'
  options
end

Instance Method Details

#each_line(lines) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/linecook/commands/build.rb', line 91

def each_line(lines)
  lines.each do |line|
    if line == '-'
      while line = gets
        yield line
      end
    else
      yield line
    end
  end
end

#each_spec(lines) ⇒ Object



103
104
105
106
107
# File 'lib/linecook/commands/build.rb', line 103

def each_spec(lines)
  each_line(lines) do |line|
    yield *parse_spec(line)
  end
end

#input_dir=(input) ⇒ Object



57
58
59
60
# File 'lib/linecook/commands/build.rb', line 57

def input_dir=(input)
  @package_finder = nil
  super(input)
end

#parse_spec(spec) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/linecook/commands/build.rb', line 109

def parse_spec(spec)
  fields = CSV.parse_line(spec)

  case fields.length
  when 1  # short form
    recipe_path = fields.at(0)
    base_name = File.basename(recipe_path).chomp('.rb')
    ["#{base_name}.yml", recipe_path, base_name]
  when 3  # long form
    fields
  else
    raise "invalid spec: #{spec.inspect}"
  end
end

#process(*recipes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/linecook/commands/build.rb', line 62

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

  each_spec(recipes) do |package_file, recipe_file, export_dir|
    export_dir = File.expand_path(export_dir, output_dir)
    if File.exists?(export_dir)
      unless force
        raise CommandError, "already exists: #{export_dir.inspect}"
      end
      FileUtils.rm_r(export_dir)
    end

    package_file = File.expand_path(package_file, input_dir)
    recipe_file = File.expand_path(recipe_file)

    package  = Package.new(load_env(package_file))
    cookbook = Cookbook.new(*cookbook_path)
    recipe   = Recipe.new(package, cookbook)
    recipe.register_as 'run', :mode => 0744
    recipe.instance_eval File.read(recipe_file), recipe_file
    recipe.register_to package

    package.export(export_dir)
    puts export_dir
  end
end