Module: AbstractFeatureBranch::FileBeautifier

Defined in:
lib/abstract_feature_branch/file_beautifier.rb

Class Method Summary collapse

Class Method Details

.beautify(content) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/abstract_feature_branch/file_beautifier.rb', line 38

def self.beautify(content)
  #Not relying on ordered hashes for backwards compatibility with Ruby 1.8.7
  sections = []
  section_features = []
  content.split("\n").each do |line|
    if line.strip.empty? || (line.strip.start_with?('#') && !section_features.flatten.empty?)
      # ignore empty line
    elsif line.start_with?(" ")
      section_features.last << line
    else
      sections << line
      section_features << []
    end
  end
  beautified_file_content = ''
  sections.each_with_index do |section, i|
    if section.start_with?('#')
      beautified_file_content << "#{section}\n"
    else
      beautified_file_content << "#{section}\n"
      beautified_file_content << "#{section_features[i].sort.join("\n")}\n\n"
    end
  end
  beautified_file_content
end

.process(file_path = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/abstract_feature_branch/file_beautifier.rb', line 2

def self.process(file_path=nil)
  if file_path.nil?
    feature_directory_path = File.join(AbstractFeatureBranch.application_root, 'config', 'features')
    feature_file_path = File.join(AbstractFeatureBranch.application_root, 'config', 'features.yml')
    local_feature_file_path = File.join(AbstractFeatureBranch.application_root, 'config', 'features.local.yml')
    process_directory(feature_directory_path)
    process_file(feature_file_path)
    process_file(local_feature_file_path)
  else
    if File.directory?(file_path)
      process_directory(file_path)
    else
      process_file(file_path)
    end
  end
  AbstractFeatureBranch.logger.info "File beautification done."
end

.process_directory(directory_path) ⇒ Object



20
21
22
23
24
# File 'lib/abstract_feature_branch/file_beautifier.rb', line 20

def self.process_directory(directory_path)
  Dir.glob(File.join(directory_path, '**', '*.yml')).each do |file_path|
    process_file(file_path)
  end
end

.process_file(file_path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/abstract_feature_branch/file_beautifier.rb', line 26

def self.process_file(file_path)
  AbstractFeatureBranch.logger.info "Beautifying #{file_path}..."
  file_content = nil
  File.open(file_path, 'r') do |file|
    file_content = file.readlines.join
  end
  beautified_file_content = beautify(file_content)
  File.open(file_path, 'w+') do |file|
    file << beautified_file_content
  end
end