Class: SpreenWiki::Configuration

Inherits:
Object
  • Object
show all
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 =

Returns:

  • (::String)
'.spreen.yml'
EMPTY_HASH =

: Hash[String, untyped]

Returns:

  • (Hash[String, untyped])
{}.freeze
DEFAULT_TEMPLATE_DIR =

Returns:

  • (Object)
File.expand_path(File.join(__dir__.to_s, 'templates'))
DEFAULT_EXCLUDED_DIRS =

Returns:

  • (Object)
%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.

Returns:

  • (Object)
{
  '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

Instance Method Summary collapse

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.

Parameters:

  • base_path: (String)
  • config_path: (String, nil) (defaults to: nil)
  • organisation: (String, nil) (defaults to: nil)
  • repository: (String, nil) (defaults to: nil)
  • wiki_url: (String, nil) (defaults to: nil)
  • owner_base_url: (String, nil) (defaults to: nil)
  • excluded_dirs: (Array[String], nil) (defaults to: nil)
  • template_dir: (String, nil) (defaults to: nil)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spreen_wiki/configuration.rb', line 81

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_dirsObject (readonly)

Returns the value of attribute excluded_dirs.

Returns:

  • (Object)


60
61
62
# File 'lib/spreen_wiki/configuration.rb', line 60

def excluded_dirs
  @excluded_dirs
end

#fileObject (readonly)

Returns the value of attribute file.

Returns:

  • (Object)


140
141
142
# File 'lib/spreen_wiki/configuration.rb', line 140

def file
  @file
end

#labelsObject (readonly)

Returns the value of attribute labels.

Returns:

  • (Object)


140
141
142
# File 'lib/spreen_wiki/configuration.rb', line 140

def labels
  @labels
end

#organisationObject (readonly)

Returns the value of attribute organisation.

Returns:

  • (Object)


60
61
62
# File 'lib/spreen_wiki/configuration.rb', line 60

def organisation
  @organisation
end

#owner_base_urlObject (readonly)

Returns the value of attribute owner_base_url.

Returns:

  • (Object)


60
61
62
# File 'lib/spreen_wiki/configuration.rb', line 60

def owner_base_url
  @owner_base_url
end

#repositoryObject (readonly)

Returns the value of attribute repository.

Returns:

  • (Object)


60
61
62
# File 'lib/spreen_wiki/configuration.rb', line 60

def repository
  @repository
end

#template_dirObject (readonly)

Returns the value of attribute template_dir.

Returns:

  • (Object)


60
61
62
# File 'lib/spreen_wiki/configuration.rb', line 60

def template_dir
  @template_dir
end

#wiki_urlObject (readonly)

Returns the value of attribute wiki_url.

Returns:

  • (Object)


60
61
62
# File 'lib/spreen_wiki/configuration.rb', line 60

def wiki_url
  @wiki_url
end

Instance Method Details

#deep_merge(base, override) ⇒ Hash[String, untyped]

Parameters:

  • base (Hash[String, untyped])
  • override (Hash[String, untyped])

Returns:

  • (Hash[String, untyped])


209
210
211
212
213
# File 'lib/spreen_wiki/configuration.rb', line 209

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_urlString?

Returns:

  • (String, nil)


202
203
204
# File 'lib/spreen_wiki/configuration.rb', line 202

def default_owner_base_url
  "https://github.com/orgs/#{organisation}/teams/" if organisation
end

#default_wiki_urlString?

Returns:

  • (String, nil)


197
198
199
# File 'lib/spreen_wiki/configuration.rb', line 197

def default_wiki_url
  "https://github.com/#{organisation}/#{repository}/wiki" if organisation && repository
end

#group_bysArray[String]

Returns:

  • (Array[String])


94
95
96
# File 'lib/spreen_wiki/configuration.rb', line 94

def group_bys
  labels.keys
end

#label_for(group_by) ⇒ Hash[String, untyped]

Parameters:

  • group_by (String)

Returns:

  • (Hash[String, untyped])


185
186
187
# File 'lib/spreen_wiki/configuration.rb', line 185

def label_for(group_by)
  labels.fetch(group_by, EMPTY_HASH)
end

#language_labels(group_by, language) ⇒ Hash[String, untyped]

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (Hash[String, untyped])


192
193
194
# File 'lib/spreen_wiki/configuration.rb', line 192

def language_labels(group_by, language)
  label_for(group_by).fetch('languages', EMPTY_HASH).fetch(language, EMPTY_HASH)
end

#languages(group_by) ⇒ Array[String]

Parameters:

  • group_by (String)

Returns:

  • (Array[String])


100
101
102
# File 'lib/spreen_wiki/configuration.rb', line 100

def languages(group_by)
  label_for(group_by).fetch('languages', EMPTY_HASH).keys
end

#llm_target_namespace(group_by, language) ⇒ String

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (String)


127
128
129
# File 'lib/spreen_wiki/configuration.rb', line 127

def llm_target_namespace(group_by, language)
  language_labels(group_by, language).fetch('llm_target_namespace', '')
end

#load_file(path) ⇒ Hash[String, untyped]

Parameters:

  • path (String)

Returns:

  • (Hash[String, untyped])


177
178
179
180
181
# File 'lib/spreen_wiki/configuration.rb', line 177

def load_file(path)
  return {} unless File.exist?(path)

  YAML.safe_load_file(path) || {}
end

#no_declaration(group_by, language) ⇒ String

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (String)


113
114
115
# File 'lib/spreen_wiki/configuration.rb', line 113

def no_declaration(group_by, language)
  language_labels(group_by, language).fetch('no_declaration', '')
end

#path_to_template(group_by, language) ⇒ String

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (String)


134
135
136
# File 'lib/spreen_wiki/configuration.rb', line 134

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.

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


146
147
148
# File 'lib/spreen_wiki/configuration.rb', line 146

def presence(value)
  value unless value.nil? || value.empty?
end

#resolve(explicit, key) ⇒ String?

Parameters:

  • explicit (String, nil)
  • key (String)

Returns:

  • (String, nil)


153
154
155
# File 'lib/spreen_wiki/configuration.rb', line 153

def resolve(explicit, key)
  presence(explicit) || presence(file[key])
end

#resolve_organisation(explicit) ⇒ String?

Parameters:

  • explicit (String, nil)

Returns:

  • (String, nil)


159
160
161
# File 'lib/spreen_wiki/configuration.rb', line 159

def resolve_organisation(explicit)
  resolve(explicit, 'organisation') || presence(ENV.fetch('ORGANISATION_NAME', nil))
end

#resolve_owner_base_url(explicit) ⇒ String?

Parameters:

  • explicit (String, nil)

Returns:

  • (String, nil)


171
172
173
# File 'lib/spreen_wiki/configuration.rb', line 171

def resolve_owner_base_url(explicit)
  resolve(explicit, 'owner_base_url') || default_owner_base_url
end

#resolve_wiki_url(explicit) ⇒ String?

Parameters:

  • explicit (String, nil)

Returns:

  • (String, nil)


165
166
167
# File 'lib/spreen_wiki/configuration.rb', line 165

def resolve_wiki_url(explicit)
  resolve(explicit, 'wiki_url') || default_wiki_url
end

#target_regexp(group_by) ⇒ Regexp

Parameters:

  • group_by (String)

Returns:

  • (Regexp)


106
107
108
# File 'lib/spreen_wiki/configuration.rb', line 106

def target_regexp(group_by)
  Regexp.new(label_for(group_by).fetch('regexp', ''))
end

#unknown_namespaces(group_by, language) ⇒ Array[String]

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (Array[String])


120
121
122
# File 'lib/spreen_wiki/configuration.rb', line 120

def unknown_namespaces(group_by, language)
  language_labels(group_by, language).fetch('unknown_namespaces', [])
end