Module: CapistranoMulticonfigParallel::InternalHelper

Included in:
ApplicationHelper, Configuration
Defined in:
lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb

Overview

internal helpers for logging mostly

Class Method Summary collapse

Class Method Details

.arg_is_in_default_config?(arg) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 36

def arg_is_in_default_config?(arg)
  default_config_keys.find { |key| key == arg.split('=')[0].tr('--', '') }.present?
end

.check_file(file, filename) ⇒ Object



88
89
90
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 88

def check_file(file, filename)
  file.file? && file.basename.to_s.downcase == filename.to_s.downcase
end

.create_log_file(file_path) ⇒ Object



127
128
129
130
131
132
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 127

def create_log_file(file_path)
  return if file_path.blank?
  directory = File.dirname(file_path)
  FileUtils.mkdir_p(directory) unless File.directory?(directory)
  FileUtils.touch(file_path) unless File.file?(file_path)
end

.custom_commandsObject



123
124
125
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 123

def custom_commands
  ['deploy_multi_stages']
end

.default_config_keysObject



32
33
34
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 32

def default_config_keys
  default_internal_config.map { |array| array[0].to_s }.concat([CapistranoMulticonfigParallel::ENV_KEY_JOB_ID])
end

.default_internal_configObject



19
20
21
22
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 19

def default_internal_config
  @default_config ||= fetch_default_internal_config
  @default_config
end

.default_internal_configuration_params(new_config) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 40

def default_internal_configuration_params(new_config)
  array = []
  new_config.each do |hash|
    array << [hash['name'], sliced_default_config(hash)]
  end
  array
end

.detect_rootObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 102

def detect_root
  if find_env_multi_cap_root
    Pathname.new(find_env_multi_cap_root)
  elsif defined?(::Rails)
    ::Rails.root
  else
    root = try_detect_file
    fail_capfile_not_found(root)
    root
  end
end

.enable_main_log_fileObject



134
135
136
137
138
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 134

def enable_main_log_file
  create_log_file(main_log_file)
  log_file = File.open(main_log_file, 'w')
  log_file.sync = true
end

.fail_capfile_not_found(root) ⇒ Object



76
77
78
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 76

def fail_capfile_not_found(root)
  fail "Can't detect Capfile in the  application root".red if root.blank?
end

.fetch_default_internal_configObject



24
25
26
27
28
29
30
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 24

def fetch_default_internal_config
  config = YAML.load_file(internal_config_file)['default_config']
  new_config = config.map do |hash|
    setup_default_configuration_types(hash)
  end
  default_internal_configuration_params(new_config)
end

.find_config_type(type) ⇒ Object



59
60
61
62
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 59

def find_config_type(type)
  type = type.to_s
  %w(boolean filename string).include?(type) ? type.delete(':').to_sym : type.constantize
end

.find_env_multi_cap_rootObject



64
65
66
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 64

def find_env_multi_cap_root
  ENV['MULTI_CAP_ROOT']
end

.find_file_in_directory(root, filename) ⇒ Object



92
93
94
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 92

def find_file_in_directory(root, filename)
  root.children.find { |file| check_file(file, filename) }.present? || pathname_is_root?(root)
end

.internal_config_directoryObject



11
12
13
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 11

def internal_config_directory
  File.join(root.to_s, 'capistrano_multiconfig_parallel', 'configuration')
end

.internal_config_fileObject



15
16
17
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 15

def internal_config_file
  File.join(internal_config_directory, 'default.yml')
end

.log_directoryObject



114
115
116
117
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 114

def log_directory
  log_dir = configuration.log_dir || detect_root.to_s
  File.join(log_dir.to_s, 'log')
end

.main_log_fileObject



119
120
121
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 119

def main_log_file
  File.join(log_directory, 'multi_cap.log')
end

.multi_level_prop(config, prop) ⇒ Object



6
7
8
9
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 6

def multi_level_prop(config, prop)
  prop.split('.').each { |new_prop| config = config[new_prop] }
  config
end

.pathname_is_root?(root) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 72

def pathname_is_root?(root)
  root.root?
end

.pwd_directoryObject



84
85
86
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 84

def pwd_directory
  Pathname.new(FileUtils.pwd)
end

.pwd_parent_dirObject



80
81
82
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 80

def pwd_parent_dir
  pwd_directory.directory? ? pwd_directory : pwd_directory.parent
end

.rootObject



68
69
70
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 68

def root
  File.expand_path(File.dirname(File.dirname(__dir__)))
end

.setup_default_configuration_types(hash) ⇒ Object



52
53
54
55
56
57
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 52

def setup_default_configuration_types(hash)
  hash.each_with_object({}) do |(key, value), memo|
    memo[key] = (key == 'type') ? find_config_type(value) : value
    memo
  end
end

.sliced_default_config(hash) ⇒ Object



48
49
50
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 48

def sliced_default_config(hash)
  hash.slice('type', 'description', 'default', 'required')
end

.try_detect_file(filename = 'capfile') ⇒ Object



96
97
98
99
100
# File 'lib/capistrano_multiconfig_parallel/helpers/internal_helper.rb', line 96

def try_detect_file(filename = 'capfile')
  root = pwd_parent_dir
  root = root.parent until find_file_in_directory(root, filename)
  pathname_is_root?(root) ? nil : root
end