Module: OpenAPISourceTools::ConfigLoader
- Defined in:
- lib/openapi/sourcetools/config.rb
Overview
Configuration file find and load convenience functions. See the first 3 methods. The rest are intended to be internal helpers.
Defined Under Namespace
Classes: ConfigFileInfo
Class Method Summary collapse
-
.contents_array(config_file_infos) ⇒ Object
Maps an array of ConfigFileInfo objects to an array of their contents.
- .convert_path_end(path, splitter, prefix_size, extensions) ⇒ Object
- .find_filenames(root, name_prefix) ⇒ Object
-
.find_files(name_prefix:, extensions: [ '.*' ]) ⇒ Object
A function to find all files with a given prefix.
- .path_splitter(separator) ⇒ Object
- .prepare_prefix(name_prefix, root) ⇒ Object
-
.read_contents(config_file_infos) ⇒ Object
A function to read all YAML files in an array of ConfigFileInfo objects.
- .remove_extension(file_path, extensions) ⇒ Object
Class Method Details
.contents_array(config_file_infos) ⇒ Object
Maps an array of ConfigFileInfo objects to an array of their contents.
45 46 47 |
# File 'lib/openapi/sourcetools/config.rb', line 45 def self.contents_array(config_file_infos) config_file_infos.map(&:content).reject(&:nil?) end |
.convert_path_end(path, splitter, prefix_size, extensions) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/openapi/sourcetools/config.rb', line 139 def self.convert_path_end(path, splitter, prefix_size, extensions) relevant, ext = remove_extension(path[prefix_size..], extensions) pieces = relevant.split(splitter).map do |piece| case piece when '' then nil when '/' then :dir when Gen.separator then :separator else piece end end unless ext.nil? pieces.push(:extension) pieces.push(ext) end pieces.compact! ConfigFileInfo.new(pieces, path) end |
.find_filenames(root, name_prefix) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/openapi/sourcetools/config.rb', line 103 def self.find_filenames(root, name_prefix) full_prefix = File.join(root, name_prefix) file_paths = [] Find.find(root) do |path| next if path.size < full_prefix.size is_dir = File.directory?(path) if path.start_with?(full_prefix) file_paths.push(path) unless is_dir elsif is_dir Find.prune end end file_paths end |
.find_files(name_prefix:, extensions: [ '.*' ]) ⇒ Object
A function to find all files with a given prefix. Prefix is taken from Gen.config if nil. Returns an array of ConfigFileInfo objects.
19 20 21 22 23 24 25 26 27 |
# File 'lib/openapi/sourcetools/config.rb', line 19 def self.find_files(name_prefix:, extensions: [ '.*' ]) name_prefix = Gen.config if name_prefix.nil? raise ArgumentError, 'name_prefix or config must be set' if name_prefix.nil? root, name_prefix = prepare_prefix(name_prefix, Gen.wd) file_paths = find_filenames(root, name_prefix) splitter = path_splitter(Gen.separator) out = file_paths.map { |fp| convert_path_end(fp, splitter, root.size + 1, extensions) } out.sort! end |
.path_splitter(separator) ⇒ Object
118 119 120 121 122 |
# File 'lib/openapi/sourcetools/config.rb', line 118 def self.path_splitter(separator) parts = [ Regexp.quote('/') ] parts.push(Regexp.quote(separator)) if separator.is_a?(String) && !separator.empty? Regexp.new("(#{parts.join('|')})") end |
.prepare_prefix(name_prefix, root) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/openapi/sourcetools/config.rb', line 92 def self.prepare_prefix(name_prefix, root) name_prefix_dir = File.dirname(name_prefix) root = File.realpath(name_prefix_dir, root) unless name_prefix_dir == '.' name_prefix = File.basename(name_prefix) if name_prefix == '.' # Just being nice. Daft argument. name_prefix = File.basename(root) root = File.dirname(root) end [root, name_prefix] end |
.read_contents(config_file_infos) ⇒ Object
A function to read all YAML files in an array of ConfigFileInfo objects. Returns the same as contents_array.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/openapi/sourcetools/config.rb', line 31 def self.read_contents(config_file_infos) config_file_infos.each do |cfi| c = YAML.safe_load_file(cfi.path) # Check allows e.g. copyright and license files be named with config prefix # for clarity, but ignored during config loading. next if cfi.keys.empty? && !c.is_a?(Hash) cfi.bury_content(c) rescue Psych::SyntaxError next # Was not YAML. Other files can be named using config prefix. end contents_array(config_file_infos) end |
.remove_extension(file_path, extensions) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/openapi/sourcetools/config.rb', line 124 def self.remove_extension(file_path, extensions) extensions.each do |e| if e == '.*' idx = file_path.rindex('.') next if idx.nil? # No . anywhere. ext = file_path[idx..] next unless ext.index('/').nil? # Last . is before file name. return [ file_path[0...idx], ext ] elsif file_path.end_with?(e) return [ file_path[0..-(1 + e.size)], e ] end end [ file_path, nil ] end |