Class: ManageYaml::MkDirYaml

Inherits:
Object
  • Object
show all
Defined in:
lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb

Instance Method Summary collapse

Constructor Details

#initialize(path: '.', layer: 2, output_file: 'dir.yaml', options: nil) ⇒ MkDirYaml

Returns a new instance of MkDirYaml.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 105

def initialize(path: '.', layer: 2, output_file: 'dir.yaml', options: nil)
  @options = options
  abs_path = File.expand_path(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



117
118
119
120
121
122
123
124
125
126
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 117

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 150

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

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mk_semi_lattice/manage_yaml/mk_semi_lattice_yaml.rb', line 128

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