Class: MRubyPortable::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mruby_portable/config.rb

Constant Summary collapse

CONFIG_FILENAMES =
["mruby-portable.yml", "mruby-p.yml"].freeze
DEFAULT_MAIN =
"main.rb"
DEFAULT_ASSETS =
"assets"
DEFAULT_VERSION =
"01.00"
XMB_ASSET_KEYS =
%w[icon background preview preview_video].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir, data) ⇒ Config

Returns a new instance of Config.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mruby_portable/config.rb', line 29

def initialize(project_dir, data)
  @project_dir = File.expand_path(project_dir)
  @name = string_value(data, "name") || File.basename(@project_dir)
  @title = string_value(data, "title") || @name
  @version = string_value(data, "version") || DEFAULT_VERSION
  @main = string_value(data, "main") || DEFAULT_MAIN
  @assets = string_value(data, "assets") || DEFAULT_ASSETS
  @icon = string_value(data, "icon")
  @background = string_value(data, "background")
  @preview = string_value(data, "preview")
  @preview_video = string_value(data, "preview_video")

  validate!
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def assets
  @assets
end

#backgroundObject (readonly)

Returns the value of attribute background.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def background
  @background
end

#iconObject (readonly)

Returns the value of attribute icon.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def icon
  @icon
end

#mainObject (readonly)

Returns the value of attribute main.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def name
  @name
end

#previewObject (readonly)

Returns the value of attribute preview.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def preview
  @preview
end

#preview_videoObject (readonly)

Returns the value of attribute preview_video.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def preview_video
  @preview_video
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def project_dir
  @project_dir
end

#titleObject (readonly)

Returns the value of attribute title.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def title
  @title
end

#versionObject (readonly)

Returns the value of attribute version.



13
14
15
# File 'lib/mruby_portable/config.rb', line 13

def version
  @version
end

Class Method Details

.load(project_dir) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mruby_portable/config.rb', line 16

def self.load(project_dir)
  project_dir = File.expand_path(project_dir)
  path = CONFIG_FILENAMES.map { |name| File.join(project_dir, name) }.find { |candidate| File.file?(candidate) }
  raise Error, "mruby-portable.yml was not found in #{project_dir}" unless path

  data = YAML.safe_load(File.read(path), aliases: false) || {}
  raise Error, "#{File.basename(path)} must contain a mapping" unless data.is_a?(Hash)

  new(project_dir, data)
rescue Psych::SyntaxError => e
  raise Error, "failed to parse project config: #{e.message}"
end

Instance Method Details

#assets_pathObject



48
49
50
# File 'lib/mruby_portable/config.rb', line 48

def assets_path
  expand_project_path(assets)
end

#background_pathObject



56
57
58
# File 'lib/mruby_portable/config.rb', line 56

def background_path
  optional_project_path(background)
end

#icon_pathObject



52
53
54
# File 'lib/mruby_portable/config.rb', line 52

def icon_path
  optional_project_path(icon)
end

#main_pathObject



44
45
46
# File 'lib/mruby_portable/config.rb', line 44

def main_path
  expand_project_path(main)
end

#preview_pathObject



60
61
62
# File 'lib/mruby_portable/config.rb', line 60

def preview_path
  optional_project_path(preview)
end

#preview_video_pathObject



64
65
66
# File 'lib/mruby_portable/config.rb', line 64

def preview_video_path
  optional_project_path(preview_video)
end

#target_nameObject



68
69
70
71
72
73
# File 'lib/mruby_portable/config.rb', line 68

def target_name
  normalized = name.gsub(/[^0-9A-Za-z_]+/, "_").gsub(/\A_+|_+\z/, "")
  normalized = "game" if normalized.empty?
  normalized = "game_#{normalized}" unless normalized.match?(/\A[A-Za-z_]/)
  normalized
end