Class: Craftbelt::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/craftbelt/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dirObject (readonly)

Returns the value of attribute build_dir.



5
6
7
# File 'lib/craftbelt/env.rb', line 5

def build_dir
  @build_dir
end

#instanceObject (readonly)

Returns the value of attribute instance.



5
6
7
# File 'lib/craftbelt/env.rb', line 5

def instance
  @instance
end

#settingsObject (readonly)

Returns the value of attribute settings.



5
6
7
# File 'lib/craftbelt/env.rb', line 5

def settings
  @settings
end

Instance Method Details

#accessObject



31
32
33
34
35
# File 'lib/craftbelt/env.rb', line 31

def access
  @access ||= begin
    data[:access] || { blacklist: [] }
  end
end

#dataObject



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

#ramObject



69
70
71
# File 'lib/craftbelt/env.rb', line 69

def ram
  (ENV['RAM'] || 1024).to_i
end

#schemaObject



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_rawObject



41
42
43
44
45
# File 'lib/craftbelt/env.rb', line 41

def settings_raw
  @settings_raw ||= begin
    data[:settings] || {}
  end
end

#whitelist?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/craftbelt/env.rb', line 37

def whitelist?
  !!access[:whitelist]
end

#write_player_filesObject



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