Class: Spreen::Wiki::Configuration
- Inherits:
-
Object
- Object
- Spreen::Wiki::Configuration
- Defined in:
- lib/spreen/wiki/configuration.rb,
sig/generated/spreen/wiki/configuration.rbs
Overview
Resolves the runtime configuration for a wiki checkout. Every value is
looked up with the same precedence: explicit keyword argument, then the
.spreen.yml file under base_path (or config_path), then the
ORGANISATION_NAME environment variable (organisation only), then the
built-in defaults.
Constant Summary collapse
- CONFIG_FILENAME =
'.spreen.yml'- EMPTY_HASH =
: Hash[String, untyped]
{}.freeze
- DEFAULT_TEMPLATE_DIR =
File.(File.join(__dir__.to_s, 'templates'))
- DEFAULT_EXCLUDED_DIRS =
%w[spreen-wiki wikis-by-owner].freeze
- DEFAULT_LABELS =
Built-in labels: how the first line of a wiki page is parsed (regexp), which namespace collects pages without a declaration (no_declaration), which namespaces the count report covers (unknown_namespaces) and which namespace the LLM export targets (llm_target_namespace). A config file can override any of these, add languages or add group_by criteria.
{ 'Owner' => { 'regexp' => '[Oo]wner:\s?', 'languages' => { 'English' => { 'no_declaration' => 'Unowned', 'unknown_namespaces' => ['Unknown Owner nor Necessity', 'Unowned but Necessary', 'Unowned'], 'llm_target_namespace' => 'Unknown Owner nor Necessity' }, 'Japanese' => { 'no_declaration' => 'Owner記名なし', 'unknown_namespaces' => [ 'Ownerチームが不明だが必要なページ群', 'Ownerチーム・要or不要が不明なページ群', 'Owner記名なし' ], 'llm_target_namespace' => 'Ownerチーム・要or不要が不明なページ群' } } }, 'Category' => { 'regexp' => '[Cc]ategory:\s?', 'languages' => { 'English' => { 'no_declaration' => 'Uncategorised', 'unknown_namespaces' => ['Uncategorised'], 'llm_target_namespace' => 'Uncategorised' }, 'Japanese' => { 'no_declaration' => 'Category記載なし', 'unknown_namespaces' => ['Category記載なし'], 'llm_target_namespace' => 'Category記載なし' } } } }.freeze
Instance Attribute Summary collapse
-
#excluded_dirs ⇒ Object
readonly
Returns the value of attribute excluded_dirs.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#organisation ⇒ Object
readonly
Returns the value of attribute organisation.
-
#owner_base_url ⇒ Object
readonly
Returns the value of attribute owner_base_url.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
-
#template_dir ⇒ Object
readonly
Returns the value of attribute template_dir.
-
#wiki_url ⇒ Object
readonly
Returns the value of attribute wiki_url.
Instance Method Summary collapse
- #deep_merge(base, override) ⇒ Hash[String, untyped]
- #default_owner_base_url ⇒ String?
- #default_wiki_url ⇒ String?
- #group_bys ⇒ Array[String]
-
#initialize(base_path:, config_path: nil, organisation: nil, repository: nil, wiki_url: nil, owner_base_url: nil, excluded_dirs: nil, template_dir: nil) ⇒ Configuration
constructor
A new instance of Configuration.
- #label_for(group_by) ⇒ Hash[String, untyped]
- #language_labels(group_by, language) ⇒ Hash[String, untyped]
- #languages(group_by) ⇒ Array[String]
- #llm_target_namespace(group_by, language) ⇒ String
- #load_file(path) ⇒ Hash[String, untyped]
- #no_declaration(group_by, language) ⇒ String
- #path_to_template(group_by, language) ⇒ String
-
#presence(value) ⇒ String?
Treats empty strings the same as nil so that callers can pass values straight from optional environment variables or CLI flags.
- #resolve(explicit, key) ⇒ String?
- #resolve_organisation(explicit) ⇒ String?
- #resolve_owner_base_url(explicit) ⇒ String?
- #resolve_wiki_url(explicit) ⇒ String?
- #target_regexp(group_by) ⇒ Regexp
- #unknown_namespaces(group_by, language) ⇒ Array[String]
Constructor Details
#initialize(base_path:, config_path: nil, organisation: nil, repository: nil, wiki_url: nil, owner_base_url: nil, excluded_dirs: nil, template_dir: nil) ⇒ Configuration
Returns a new instance of Configuration.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/spreen/wiki/configuration.rb', line 82 def initialize(base_path:, config_path: nil, organisation: nil, repository: nil, wiki_url: nil, owner_base_url: nil, excluded_dirs: nil, template_dir: nil) @file = load_file(config_path || File.join(base_path, CONFIG_FILENAME)) @organisation = resolve_organisation(organisation) @repository = resolve(repository, 'repository') @wiki_url = resolve_wiki_url(wiki_url) @owner_base_url = resolve_owner_base_url(owner_base_url) @excluded_dirs = excluded_dirs || file.fetch('exclude', DEFAULT_EXCLUDED_DIRS) @template_dir = template_dir || file.fetch('template_dir', DEFAULT_TEMPLATE_DIR) @labels = deep_merge(DEFAULT_LABELS, file.fetch('labels', EMPTY_HASH)) end |
Instance Attribute Details
#excluded_dirs ⇒ Object (readonly)
Returns the value of attribute excluded_dirs.
61 62 63 |
# File 'lib/spreen/wiki/configuration.rb', line 61 def excluded_dirs @excluded_dirs end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
141 142 143 |
# File 'lib/spreen/wiki/configuration.rb', line 141 def file @file end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
141 142 143 |
# File 'lib/spreen/wiki/configuration.rb', line 141 def labels @labels end |
#organisation ⇒ Object (readonly)
Returns the value of attribute organisation.
61 62 63 |
# File 'lib/spreen/wiki/configuration.rb', line 61 def organisation @organisation end |
#owner_base_url ⇒ Object (readonly)
Returns the value of attribute owner_base_url.
61 62 63 |
# File 'lib/spreen/wiki/configuration.rb', line 61 def owner_base_url @owner_base_url end |
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
61 62 63 |
# File 'lib/spreen/wiki/configuration.rb', line 61 def repository @repository end |
#template_dir ⇒ Object (readonly)
Returns the value of attribute template_dir.
61 62 63 |
# File 'lib/spreen/wiki/configuration.rb', line 61 def template_dir @template_dir end |
#wiki_url ⇒ Object (readonly)
Returns the value of attribute wiki_url.
61 62 63 |
# File 'lib/spreen/wiki/configuration.rb', line 61 def wiki_url @wiki_url end |
Instance Method Details
#deep_merge(base, override) ⇒ Hash[String, untyped]
210 211 212 213 214 |
# File 'lib/spreen/wiki/configuration.rb', line 210 def deep_merge(base, override) base.merge(override) do |_key, old_value, new_value| old_value.is_a?(Hash) && new_value.is_a?(Hash) ? deep_merge(old_value, new_value) : new_value end end |
#default_owner_base_url ⇒ String?
203 204 205 |
# File 'lib/spreen/wiki/configuration.rb', line 203 def default_owner_base_url "https://github.com/orgs/#{organisation}/teams/" if organisation end |
#default_wiki_url ⇒ String?
198 199 200 |
# File 'lib/spreen/wiki/configuration.rb', line 198 def default_wiki_url "https://github.com/#{organisation}/#{repository}/wiki" if organisation && repository end |
#group_bys ⇒ Array[String]
95 96 97 |
# File 'lib/spreen/wiki/configuration.rb', line 95 def group_bys labels.keys end |
#label_for(group_by) ⇒ Hash[String, untyped]
186 187 188 |
# File 'lib/spreen/wiki/configuration.rb', line 186 def label_for(group_by) labels.fetch(group_by, EMPTY_HASH) end |
#language_labels(group_by, language) ⇒ Hash[String, untyped]
193 194 195 |
# File 'lib/spreen/wiki/configuration.rb', line 193 def language_labels(group_by, language) label_for(group_by).fetch('languages', EMPTY_HASH).fetch(language, EMPTY_HASH) end |
#languages(group_by) ⇒ Array[String]
101 102 103 |
# File 'lib/spreen/wiki/configuration.rb', line 101 def languages(group_by) label_for(group_by).fetch('languages', EMPTY_HASH).keys end |
#llm_target_namespace(group_by, language) ⇒ String
128 129 130 |
# File 'lib/spreen/wiki/configuration.rb', line 128 def llm_target_namespace(group_by, language) language_labels(group_by, language).fetch('llm_target_namespace', '') end |
#load_file(path) ⇒ Hash[String, untyped]
178 179 180 181 182 |
# File 'lib/spreen/wiki/configuration.rb', line 178 def load_file(path) return {} unless File.exist?(path) YAML.safe_load_file(path) || {} end |
#no_declaration(group_by, language) ⇒ String
114 115 116 |
# File 'lib/spreen/wiki/configuration.rb', line 114 def no_declaration(group_by, language) language_labels(group_by, language).fetch('no_declaration', '') end |
#path_to_template(group_by, language) ⇒ String
135 136 137 |
# File 'lib/spreen/wiki/configuration.rb', line 135 def path_to_template(group_by, language) File.join(template_dir, group_by.downcase, "#{language.downcase}.md") end |
#presence(value) ⇒ String?
Treats empty strings the same as nil so that callers can pass values straight from optional environment variables or CLI flags.
147 148 149 |
# File 'lib/spreen/wiki/configuration.rb', line 147 def presence(value) value unless value.nil? || value.empty? end |
#resolve(explicit, key) ⇒ String?
154 155 156 |
# File 'lib/spreen/wiki/configuration.rb', line 154 def resolve(explicit, key) presence(explicit) || presence(file[key]) end |
#resolve_organisation(explicit) ⇒ String?
160 161 162 |
# File 'lib/spreen/wiki/configuration.rb', line 160 def resolve_organisation(explicit) resolve(explicit, 'organisation') || presence(ENV.fetch('ORGANISATION_NAME', nil)) end |
#resolve_owner_base_url(explicit) ⇒ String?
172 173 174 |
# File 'lib/spreen/wiki/configuration.rb', line 172 def resolve_owner_base_url(explicit) resolve(explicit, 'owner_base_url') || default_owner_base_url end |
#resolve_wiki_url(explicit) ⇒ String?
166 167 168 |
# File 'lib/spreen/wiki/configuration.rb', line 166 def resolve_wiki_url(explicit) resolve(explicit, 'wiki_url') || default_wiki_url end |
#target_regexp(group_by) ⇒ Regexp
107 108 109 |
# File 'lib/spreen/wiki/configuration.rb', line 107 def target_regexp(group_by) Regexp.new(label_for(group_by).fetch('regexp', '')) end |
#unknown_namespaces(group_by, language) ⇒ Array[String]
121 122 123 |
# File 'lib/spreen/wiki/configuration.rb', line 121 def unknown_namespaces(group_by, language) language_labels(group_by, language).fetch('unknown_namespaces', []) end |