Class: Craftbelt::Env
- Inherits:
-
Object
- Object
- Craftbelt::Env
- Defined in:
- lib/craftbelt/env.rb
Instance Attribute Summary collapse
-
#build_dir ⇒ Object
readonly
Returns the value of attribute build_dir.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #access ⇒ Object
- #data ⇒ Object
- #erb(template) ⇒ Object
- #field(name) ⇒ Object (also: #f)
-
#initialize(instance_dir, build_dir, extra_settings = {}) ⇒ Env
constructor
A new instance of Env.
-
#player_list(player_setting) ⇒ Object
TODO remove this when players are passed as an array.
- #ram ⇒ Object
- #schema ⇒ Object
- #settings_raw ⇒ Object
- #whitelist? ⇒ Boolean
- #write_player_files ⇒ Object
- #write_templates(templates) ⇒ Object
Constructor Details
#initialize(instance_dir, build_dir, extra_settings = {}) ⇒ Env
Returns a new instance of Env.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/craftbelt/env.rb', line 7 def initialize(instance_dir, build_dir, extra_settings={}) @instance = Craftbelt::Instance.new(instance_dir) @build_dir = build_dir vals = settings_raw.merge( "name" => data[:name], "enable-white-list" => whitelist?, "level-name" => (instance.level_paths.first || 'level') ).merge(extra_settings) @settings = Settings.new(schema, vals) end |
Instance Attribute Details
#build_dir ⇒ Object (readonly)
Returns the value of attribute build_dir.
5 6 7 |
# File 'lib/craftbelt/env.rb', line 5 def build_dir @build_dir end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
5 6 7 |
# File 'lib/craftbelt/env.rb', line 5 def instance @instance end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
5 6 7 |
# File 'lib/craftbelt/env.rb', line 5 def settings @settings end |
Instance Method Details
#access ⇒ Object
31 32 33 34 35 |
# File 'lib/craftbelt/env.rb', line 31 def access @access ||= begin data[:access] || { blacklist: [] } end end |
#data ⇒ Object
19 20 21 22 23 |
# File 'lib/craftbelt/env.rb', line 19 def data @data ||= begin JSON.parse(ENV['DATA'] || '{}', symbolize_names: true) rescue {} end end |
#erb(template) ⇒ Object
78 79 80 |
# File 'lib/craftbelt/env.rb', line 78 def erb(template) ERB.new(template).result(binding) end |
#field(name) ⇒ Object Also known as: f
73 74 75 |
# File 'lib/craftbelt/env.rb', line 73 def field(name) settings.field(name) end |
#player_list(player_setting) ⇒ Object
TODO remove this when players are passed as an array
61 62 63 64 65 66 67 |
# File 'lib/craftbelt/env.rb', line 61 def player_list(player_setting) if player_setting.is_a? Array player_setting.join("\n") else player_setting end end |
#ram ⇒ Object
69 70 71 |
# File 'lib/craftbelt/env.rb', line 69 def ram (ENV['RAM'] || 1024).to_i end |
#schema ⇒ Object
25 26 27 28 29 |
# File 'lib/craftbelt/env.rb', line 25 def schema @schema ||= begin JSON.parse(File.read("#{@build_dir}/funpack.json"))['schema'] end end |
#settings_raw ⇒ Object
41 42 43 44 45 |
# File 'lib/craftbelt/env.rb', line 41 def settings_raw @settings_raw ||= begin data[:settings] || {} end end |
#whitelist? ⇒ Boolean
37 38 39 |
# File 'lib/craftbelt/env.rb', line 37 def whitelist? !!access[:whitelist] end |
#write_player_files ⇒ Object
47 48 49 50 51 |
# File 'lib/craftbelt/env.rb', line 47 def write_player_files File.write('ops.txt', player_list(settings_raw[:ops])) if settings_raw[:ops] File.write('white-list.txt', player_list(access[:whitelist])) if access[:whitelist] File.write('banned-players.txt', player_list(access[:blacklist])) if access[:blacklist] end |
#write_templates(templates) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/craftbelt/env.rb', line 53 def write_templates(templates) templates.each do |src, dest| `mkdir -p #{File.dirname(dest)}` File.write(dest, erb(File.read("#{@build_dir}/templates/#{src}"))) end end |