Module: Appril::Configs

Extended by:
Configs
Included in:
Configs
Defined in:
app/core/load_configs.rb,
app/core/generate_configs.rb

Instance Method Summary collapse

Instance Method Details

#components(dir) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/core/generate_configs.rb', line 51

def components dir
  components_root = File.expand_path('base/components', dir) + '/'
  RocketIO.controllers.each_with_object([]) do |controller,o|
    next if controller.to_s =~ /BaseController|RTCPController/
    entry = Dir["#{controller.dirname}/index.*"].find {|e| File.file?(e)}
    o << {
      entry_path: entry && File.dirname(entry.sub(components_root, '')),
      url: controller.url,
      url_pattern: url_pattern(controller),
      name: controller.name.gsub('::', '__'),
      controller_name: controller.name,
      api: controller.api.keys
    }
  end.sort do |a,b|
    b[:url].split('/').size <=> a[:url].split('/').size
  end
end

#generate(dir) ⇒ Object



11
12
13
14
15
# File 'app/core/generate_configs.rb', line 11

def generate dir
  components = self.components(dir)
  generate_config_file(dir, components)
  generate_store_modules_file(dir, components)
end

#generate_config_file(dir, components) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/core/generate_configs.rb', line 17

def generate_config_file dir, components
  config = Configs.load("#{dir}/config", env: :development)
  File.open File.expand_path('appril.json', dir), 'w' do |f|
    f << JSON.pretty_generate({
      build_path: config[:build_path],
      client_url: config[:client_url],
      server_url: config[:server_url],
      dev_server_url: config[:dev_server_url],
      default_api: config[:default_api],
      components: components
    })
  end
end

#generate_store_modules_file(dir, components) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/core/generate_configs.rb', line 31

def generate_store_modules_file dir, components

  lines = components.each_with_object([
    '// auto-generated file, do not edit',
  ]) do |c,o|
    next unless c[:name] # skip anonymous controllers
    pattern = "base/components/#{c[:entry_path]}/store.*"
    next unless file = Dir[File.expand_path(pattern, dir)].find {|e| File.file?(e)}
    path = file.sub(dir, 'app')
    o << "
      import #{c[:name]} from '#{path}'
      export {#{c[:name]}}
    "
  end

  file = File.expand_path('base/store/components.js', dir)
  FileUtils.mkdir_p(File.dirname(file))
  File.open(file, 'w') {|f| f << lines.join("\n")}
end

#load(*dirs, env: RocketIO.environment) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/core/load_configs.rb', line 9

def load *dirs, env: RocketIO.environment

  config = RocketIO.indifferent_params({environment: env.to_s.freeze})

  dirs.each do |dir|
    config.update(load_file("#{dir}/config.yml"))
    config.update(load_file("#{dir}/env/#{env}.yml"))

    loaded = [
      File.expand_path("config.yml", dir),
      File.expand_path("env/#{env}.yml", dir)
    ]

    %w[
      *.yml
      **/*.yml
    ].each do |pattern|
      Dir[File.join(dir, pattern)].each do |file|

        path = File.expand_path(file, dir)
        next if loaded.include?(path)
        loaded << path

        key = File.basename(file, '.yml')
        key_config = load_file(file)
        key_config_keys = key_config.keys.map(&:to_s)

        config[key] = if key_config_keys.include?(config[:environment])
          # current environment found, use it
          key_config[config[:environment]]
        else
          if RocketIO::ENVIRONMENTS.keys.find {|k| key_config_keys.include?(k)}
            # there are some environment(s), but current one is missing, so set current environment to nil
            nil
          else
            # there are no environments, so this config is available on any environment
            key_config
          end
        end
      end
    end
  end

  def config.method_missing key
    self[key]
  end

  config
end

#load_file(file) ⇒ Object



59
60
61
# File 'app/core/load_configs.rb', line 59

def load_file file
  RocketIO.indifferent_params(YAML.load(File.read(file)) || {})
end

#url_pattern(controller) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/core/generate_configs.rb', line 69

def url_pattern controller
  controller.url *controller.instance_method(:index).parameters.each_with_object([]) {|param,o|
    pattern = if param[0] == :rest
      "*"
    elsif param[0] == :req
      ":#{param[1]}"
    elsif param[0] == :opt
      ":#{param[1]}?"
    end
    o << pattern if pattern
  }
end