Class: Stable::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/stable/registry.rb

Overview

Application registry for managing Rails app configurations

Class Method Summary collapse

Class Method Details

.appsObject



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
# File 'lib/stable/registry.rb', line 9

def self.apps
  apps = []

  # Read legacy apps.yml file for backward compatibility
  legacy_file = Stable::Paths.apps_file
  if File.exist?(legacy_file)
    legacy_apps = load_legacy_apps(legacy_file)
    apps.concat(legacy_apps)
  end

  # Read individual app config files from projects directory
  projects_dir = Stable::Paths.projects_dir
  if Dir.exist?(projects_dir)
    Dir.glob(File.join(projects_dir, '*/')).each do |app_dir|
      app_name = File.basename(app_dir)
      config_file = Stable::Paths.app_config_file(app_name)

      next unless File.exist?(config_file)

      # Skip if we already have this app from legacy file
      next if apps.any? { |app| app[:name] == app_name }

      app_config = load_app_config(app_name)
      apps << app_config if app_config
    end
  end

  apps
end

.load_app_config(app_name) ⇒ Object



59
60
61
62
63
64
# File 'lib/stable/registry.rb', line 59

def self.load_app_config(app_name)
  config_file = Stable::Paths.app_config_file(app_name)
  return nil unless File.exist?(config_file)

  parse_config_file(config_file)
end

.load_legacy_apps(file_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stable/registry.rb', line 39

def self.load_legacy_apps(file_path)
  return [] unless File.exist?(file_path)

  data = YAML.load_file(file_path) || []
  data.map do |entry|
    next entry unless entry.is_a?(Hash)

    entry.each_with_object({}) do |(k, v), memo|
      key = k.is_a?(String) ? k.to_sym : k
      memo[key] = v
    end
  end
end

.parse_config_file(config_file) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/stable/registry.rb', line 86

def self.parse_config_file(config_file)
  data = YAML.load_file(config_file)
  return nil unless data.is_a?(Hash)

  data.each_with_object({}) do |(k, v), memo|
    key = k.is_a?(String) ? k.to_sym : k
    memo[key] = v
  end
end

.remove_app_config(app_name) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/stable/registry.rb', line 66

def self.remove_app_config(app_name)
  config_file = Stable::Paths.app_config_file(app_name)
  FileUtils.rm_f(config_file)

  # Also remove from legacy apps.yml file for backward compatibility
  remove_from_legacy_file(app_name)
end

.remove_from_legacy_file(app_name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stable/registry.rb', line 74

def self.remove_from_legacy_file(app_name)
  legacy_file = Stable::Paths.apps_file
  return unless File.exist?(legacy_file)

  data = YAML.load_file(legacy_file) || []
  filtered_data = data.reject { |entry| entry.is_a?(Hash) && entry['name'] == app_name }

  return unless filtered_data != data

  File.write(legacy_file, filtered_data.to_yaml)
end

.save_app_config(app_name, config) ⇒ Object



53
54
55
56
57
# File 'lib/stable/registry.rb', line 53

def self.save_app_config(app_name, config)
  config_file = Stable::Paths.app_config_file(app_name)
  FileUtils.mkdir_p(File.dirname(config_file))
  File.write(config_file, config.to_yaml)
end