Class: ManageYaml::MkDirYaml
- Inherits:
-
Object
- Object
- ManageYaml::MkDirYaml
- Defined in:
- lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb
Instance Method Summary collapse
- #build_ignore_regex(ignore_pattern) ⇒ Object
- #dir_tree(path, depth) ⇒ Object
-
#initialize(path: '.', layer: 2, output_file: 'dir.yaml', options: nil) ⇒ MkDirYaml
constructor
A new instance of MkDirYaml.
- #skip_name?(entry, full, ignore_regex) ⇒ Boolean
Constructor Details
#initialize(path: '.', layer: 2, output_file: 'dir.yaml', options: nil) ⇒ MkDirYaml
Returns a new instance of MkDirYaml.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 129 def initialize(path: '.', layer: 2, output_file: 'dir.yaml', options: nil) @options = abs_path = File.(path) root_key = File.basename(abs_path) + '/' # layerの数だけ深さを調整 result = { root_key => dir_tree(path, layer - 1) } p ["MkDirYaml result", result] File.write(output_file, result.to_yaml) puts "Directory structure exported to #{output_file}" end |
Instance Method Details
#build_ignore_regex(ignore_pattern) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 141 def build_ignore_regex(ignore_pattern) return nil unless ignore_pattern # '_stack_*|*.yaml' → /(?:\A_stack_.*\z|\A.*\.yaml\z)/ regex_str = ignore_pattern.split('|').map do |pat| pat = pat.strip pat = Regexp.escape(pat).gsub('\*', '.*').gsub('\?', '.') "\\A#{pat}\\z" end.join('|') Regexp.new("(?:#{regex_str})") end |
#dir_tree(path, depth) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 174 def dir_tree(path, depth) return nil if depth < 0 tree = {} entries = if @options && (@options[:visibility] == 'all' || @options[:visibility] == 'dir_and_hidden_dir') Dir.entries(path) - ['.', '..'] else Dir.children(path) end ignore_pattern = @options && @options[:ignore] ? @options[:ignore] : nil ignore_regex = build_ignore_regex(ignore_pattern) entries.each do |entry| full = File.join(path, entry) p [entry, skip_name?(entry, full, ignore_regex)] next if skip_name?(entry, full, ignore_regex) if File.symlink?(full) target = File.readlink(full) tree[entry] = "-> #{target}" elsif File.directory?(full) subtree = dir_tree(full, depth - 1) if subtree tree["#{entry}/"] = subtree else tree["#{entry}/"] = nil end else tree[entry] = nil unless @options && (@options[:visibility] == 'dir_only' || @options[:visibility] == 'dir_and_hidden_dir') end end tree end |
#skip_name?(entry, full, ignore_regex) ⇒ Boolean
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 152 def skip_name?(entry, full, ignore_regex) return true if entry.end_with?('~') return true if ignore_regex && entry.match(ignore_regex) case @options && @options[:visibility] when 'dir_only' return true if entry.start_with?('.') # 隠しディレクトリ除外 return true unless File.directory?(full) when 'dir_and_hidden_dir' return true unless File.directory?(full) # 隠しも含めてディレクトリのみ when 'all' # すべて含める(除外なし) when 'yaml_exclude' return true if entry.start_with?('.') return true if File.extname(entry) == '.yaml' else # 'normal' return true if entry.start_with?('.') # return true if File.file?(full) && File.extname(entry) == '.yaml' && # entry != 'semi_lattice.yaml' end false end |