Module: Sombrero

Extended by:
Sombrero
Included in:
Sombrero
Defined in:
lib/sombrero.rb,
lib/sombrero/app.rb,
lib/sombrero/cli.rb,
lib/sombrero/cli/app.rb,
lib/sombrero/cli/docker.rb,
lib/sombrero/cli/helpers.rb,
lib/sombrero/cli/generator.rb,
lib/sombrero/cli/app/update.rb,
lib/sombrero/cli/assertions.rb,
lib/sombrero/base_controller.rb,
lib/sombrero/cli/app/install.rb,
lib/sombrero/rtcp_controller.rb,
lib/sombrero/cli/docker/build.rb,
lib/sombrero/cli/docker/update.rb,
lib/sombrero/cli/docker/install.rb

Defined Under Namespace

Classes: App, BaseController, CLI, RTCPController

Constant Summary collapse

BASE_DIR =
Pathname.new(File.expand_path('../../..', __FILE__)).freeze

Instance Method Summary collapse

Instance Method Details

#controllers_map(dir) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sombrero.rb', line 53

def controllers_map dir
  path_to_api = File.expand_path('base/api', dir)
  RocketIO.controllers.each_with_object([]) do |controller,o|
    next unless controller.dirname[path_to_api]

    o << {
      path: controller.dirname.sub(path_to_api, '').gsub(/\A\/|\/\Z/, ''),
      url: controller.url,
      url_pattern: url_pattern(controller),
      name: controller.name.gsub('::', '__'),
      api: controller.api
    }
  end.sort do |a,b|
    b[:url].split('/').size <=> a[:url].split('/').size
  end
end

#generate_controllers_map(dir) ⇒ Object



47
48
49
50
51
# File 'lib/sombrero.rb', line 47

def generate_controllers_map dir
  File.open File.expand_path('controllers.json', dir), 'w' do |f|
    f << controllers_map(dir).to_json
  end
end

#generate_webpack_setup(dir) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sombrero.rb', line 70

def generate_webpack_setup dir
  config = load_config_file(File.expand_path('config/config.yml', dir))

  entries = controllers_map(dir).each_with_object({}) do |controller,o|

    next unless entry = %w[
      ./base/api/%s/client.js
      ./base/api/%s/client.coffee
    ].map {|p| p % controller[:path]}.find {|f| File.file?(File.expand_path(f, dir))}

    o[controller[:path]] = File.join('./base/api', controller[:path], File.basename(entry))
  end

  if core = %w[
      ./base/core.js
      ./base/core.coffee
    ].find {|f| File.file?(File.expand_path(f, dir))}
    entries[:core] = core
  end

  File.open File.expand_path('webpack_setup.json', dir), 'w' do |f|
    f << {
      path: config[:app][:dir],
      url: config[:app][:url][:development],
      entries: entries
    }.to_json
  end
end

#load_config(dir, env = RocketIO.environment) ⇒ Object



6
7
8
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
# File 'lib/sombrero.rb', line 6

def load_config dir, env = RocketIO.environment

  config = load_config_file("#{dir}/config.yml")
  config.update(load_config_file("#{dir}/env/#{env}.yml"))
  config[:environment] = env.to_s.freeze

  Dir["#{dir}/**/*.yml"].each do |file|
    next if File.dirname(file) == './env'

    key = File.basename(file, '.yml')
    next if key == 'config' || key == 'sombrero'

    key_config = load_config_file(file)
    key_config_keys = key_config.keys.map(&:to_s)

    config[key] = if key_config_keys.include?(env.to_s)
      # current environment found, use it
      key_config[env]
    else
      if RocketIO::ENVIRONMENTS.keys.find {|k| key_config_keys.include?(k)}
        # there are some environment(s), but no current one 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

  def config.method_missing key
    self[key]
  end

  config
end

#load_config_file(file) ⇒ Object



43
44
45
# File 'lib/sombrero.rb', line 43

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

#url_pattern(controller) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sombrero.rb', line 99

def url_pattern controller
  controller.url *controller.instance_method(:get).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