Class: Jekyll::J1EnvVarsGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/starter_web/_plugins/system/load_env.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object

Main plugin action, called by Jekyll-core



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/starter_web/_plugins/system/load_env.rb', line 52

def generate(site)
  @site     = site
  @mode     = site.config['environment']
  @template = site.config['theme']

  # claude - J1 Jekyll plugin #1
  # Resolve config paths relative to the Jekyll source directory.
  # The original `File.dirname(__FILE__).sub('_plugins/system', '')`
  # was fragile (the path segment did not actually exist) and broke
  # whenever the plugin file was moved. `site.source` is authoritative.
  @project_path       = site.source
  @module_data_path   = File.join(@project_path, '_data')
  @module_config_path = File.join(@module_data_path, 'plugins')

  default_config_file = File.join(@module_config_path, 'defaults', 'load-env.yml')
  user_config_file    = File.join(@module_config_path, 'load-env.yml')

  # claude - J1 Jekyll plugin #1
  # Load default + user settings safely. Missing or malformed files are
  # tolerated so the plugin never breaks the build over a config issue.
  default_yaml = load_yaml(default_config_file)
  user_yaml    = load_yaml(user_config_file)

  default_settings = default_yaml['defaults'] || {}
      = user_yaml['settings']    || {}

  # claude - J1 Jekyll plugin #1
  # Non-destructive merge: user settings override defaults without
  # mutating the loaded defaults hash (the original used `merge!`).
  @module_config = default_settings.merge()

  if plugin_disabled?
    Jekyll.logger.info 'J1 Env:', 'disabled'
    return
  end

  Jekyll.logger.info 'J1 Env:', 'enabled'
  Jekyll.logger.info 'J1 Env:', 'exposing whitelisted environment variables'

  # claude - J1 Jekyll plugin #1
  # Optionally pull values from a project-root .env file via the
  # dotenv gem. dotenv is optional; plain ENV still works without it.
  # Moved out of class body so it runs once per build with access to
  # the resolved project path, instead of being unreachable code
  # nested inside `generate` after a `private` declaration.
  load_dotenv(@project_path)

  # claude - J1 Jekyll plugin #2
  # Resolve the whitelist of variables from the merged config. The
  # YAML can legitimately produce nil, an empty array, or entries
  # with stray whitespace, so coerce defensively:
  #   * Array(...)        -> nil becomes [], scalar becomes [scalar]
  #   * to_s.strip        -> tolerate symbols and surrounding spaces
  #   * reject(&:empty?)  -> drop blank entries from typo'd YAML
  #   * uniq              -> avoid logging the same key twice if a
  #                          user accidentally lists it in both files
  allowed_env_vars = Array(config['allowedEnvVars'])
                      .map { |k| k.to_s.strip }
                      .reject(&:empty?)
                      .uniq

  if allowed_env_vars.empty?
    Jekyll.logger.warn 'J1 Env:',
      'allowedEnvVars is empty - no variables will be exposed. ' \
      'Add entries to settings.allowedEnvVars in _data/plugins/load-env.yml.'
    site.config['j1_env'] = {}
    return
  end

  # claude - J1 Jekyll plugin #1
  # Read each whitelisted variable from ENV and expose them via
  # site.config['j1_env'] for use in Liquid as:
  #   {{ site.j1_env.CLAUDE_API_ENDPOINT }}
  # claude - J1 Jekyll plugin #2
  # Iterate over the config-derived list instead of the removed
  # ALLOWED_ENV_VARS constant.
  env_vars = {}
  allowed_env_vars.each do |key|
    value = ENV[key].to_s
    env_vars[key] = value

    if value.empty?
      Jekyll.logger.warn 'J1 Env:', "#{key} is not set (empty string will be used)"
    else
      Jekyll.logger.info 'J1 Env:', "#{key} loaded from process environment"
    end
  end

  site.config['j1_env'] = env_vars
end