Class: Exerb::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/exerb/recipe.rb

Overview

#

Defined Under Namespace

Classes: FileBlock, GeneralBlock, PathBlock, ResourceBlock

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blocks, basedir = Dir.pwd, filename = '-') ⇒ Recipe

Returns a new instance of Recipe.

Raises:



21
22
23
24
25
26
27
28
29
# File 'lib/exerb/recipe.rb', line 21

def initialize(blocks, basedir = Dir.pwd, filename = '-')
  @basedir  = basedir
  @filename = filename
  @general  = Exerb::Recipe::GeneralBlock.analyze(blocks, @filename)
  @path     = Exerb::Recipe::PathBlock.analyze(blocks, @filename)
  @resource = Exerb::Recipe::ResourceBlock.analyze(blocks, @filename)
  @file     = Exerb::Recipe::FileBlock.analyze(blocks, @filename)
  raise(Exerb::ExerbError, "#{@filename}: found unknown field in the recipe -- #{blocks.keys.join(', ')}") unless blocks.empty?
end

Class Method Details

.load(filepath) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/exerb/recipe.rb', line 31

def self.load(filepath)
  basedir  = File.dirname(File.expand_path(filepath))
  filename = File.basename(filepath)

  begin
    blocks = YAML.load(File.read(filepath))
  rescue Errno::ENOENT => e
    raise(Exerb::ExerbError, "no such file -- #{filepath}")
  rescue ArgumentError => e
    msg = e.message.gsub(/\n/, '\n')
    msg.gsub!(/\r/, '\r')
    msg.gsub!(/\t/, '\t')
    raise(Exerb::ExerbError, format('%s: %s', @filename, msg))
  end

  return self.new(blocks, basedir, filename)
end

Instance Method Details

#archive_filepath(filename = nil) ⇒ Object



49
50
51
# File 'lib/exerb/recipe.rb', line 49

def archive_filepath(filename = nil)
  return self.output_filepath(filename).sub(/(\.exe)?$/i, '.exa')
end

#core_filepath(filepath = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/exerb/recipe.rb', line 60

def core_filepath(filepath = nil)
  unless @general.core.nil?
    core   = Exerb::Utility.find_core_by_filepath(File.expand_path(@general.core, @basedir))
    core ||= Exerb::Utility.find_core_by_filename(@general.core)
    core ||= Exerb::Utility.find_core_by_name(@general.core)
    raise(Exerb::ExerbError, "#{@filename}: specified core isn't found -- #{@general.core}") if core.nil?
  end

  filepath ||= core
  filepath ||= Exerb::Utility.find_core_by_name('cui', true)

  return filepath
end

#create_archive(kcode = nil) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/exerb/recipe.rb', line 74

def create_archive(kcode = nil)
  archive = Exerb::Archive.new(kcode || @general.kcode)

  search_path  = [@basedir]
  search_path += @path.path.collect { |dir| File.expand_path(dir, @basedir) } if @path
  search_path += $:

  startup_entry = @file.delete(@general.startup)
  raise(Exerb::ExerbError, "#{@filename}: specified startup script isn't found in the file block -- #{@general.startup}") if startup_entry.nil?
  add_file_entry(archive, search_path, @general.startup, startup_entry)

  @file.keys.sort.each { |internal_name|
    add_file_entry(archive, search_path, internal_name, @file[internal_name])
  }

  return archive
end

#output_filepath(filename = nil) ⇒ Object



53
54
55
56
57
58
# File 'lib/exerb/recipe.rb', line 53

def output_filepath(filename = nil)
  filename ||= @general.output
  filename ||= @filename.sub(/(\.exy)?$/i, '.exe')

  return File.expand_path(filename, @basedir)
end

#update_resource(rsrc) ⇒ Object



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
127
128
129
130
131
132
133
134
# File 'lib/exerb/recipe.rb', line 92

def update_resource(rsrc)
  if @resource
    if @resource.icon
      rsrc.remove(Exerb::Win32::Const::RT_ICON)
      rsrc.remove(Exerb::Win32::Const::RT_GROUP_ICON)

      group_icon = Exerb::Resource::GroupIcon.new

      @resource.icon.entries.each_with_index { |entry, index|
        id   = index + 1
        icon = Exerb::Resource::Icon.read(entry.file, entry.width, entry.height, entry.color)
        rsrc.add(Exerb::Win32::Const::RT_ICON, id, icon)
        group_icon.add(id, icon)
      }

      rsrc.add(Exerb::Win32::Const::RT_GROUP_ICON, 1, group_icon)
    end

    if @resource.version
      fv = (0..3).collect { |i| @resource.version.file_version_number[i]    || 0 }
      pv = (0..3).collect { |i| @resource.version.product_version_number[i] || 0 }

      ver = Exerb::Resource::VersionInfo.new
      ver.file_version_number    = Exerb::Resource::VersionInfo.make_version(*fv)
      ver.product_version_number = Exerb::Resource::VersionInfo.make_version(*pv)
      ver.comments               = @resource.version.comments
      ver.company_name           = @resource.version.company_name
      ver.legal_copyright        = @resource.version.legal_copyright
      ver.legal_trademarks       = @resource.version.legal_trademarks
      ver.file_version           = @resource.version.file_version
      ver.product_version        = @resource.version.product_version
      ver.product_name           = @resource.version.product_name
      ver.file_description       = @resource.version.file_description
      ver.internal_name          = @resource.version.internal_name
      ver.original_filename      = @resource.version.original_filename
      ver.private_build          = @resource.version.private_build
      ver.special_build          = @resource.version.special_build

      rsrc.remove(Exerb::Win32::Const::RT_VERSION)
      rsrc.add(Exerb::Win32::Const::RT_VERSION, 1, ver)
    end
  end
end