Class: Reality::Zapwhite

Inherits:
Object
  • Object
show all
Defined in:
lib/reality/zapwhite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_directory) ⇒ Zapwhite

Returns a new instance of Zapwhite.



21
22
23
24
25
26
27
28
# File 'lib/reality/zapwhite.rb', line 21

def initialize(base_directory)
  @base_directory = base_directory
  @attributes = Reality::Git::Attributes.parse(@base_directory)
  # .ijwb is Bazel-IDEA directory while .idea is IDEAs normal directory structure.
  @exclude_patterns = %w(vendor/.* node_modules/.* .ijwb/.* .idea/.*) + load_braid_mirrors
  @check_only = false
  @additional_gitattribute_rules = []
end

Instance Attribute Details

#additional_gitattribute_rulesObject

Returns the value of attribute additional_gitattribute_rules.



34
35
36
# File 'lib/reality/zapwhite.rb', line 34

def additional_gitattribute_rules
  @additional_gitattribute_rules
end

#check_only=(value) ⇒ Object (writeonly)

Sets the attribute check_only

Parameters:

  • value

    the value to set the attribute check_only to.



46
47
48
# File 'lib/reality/zapwhite.rb', line 46

def check_only=(value)
  @check_only = value
end

#generate_gitattributes=(value) ⇒ Object (writeonly)

Sets the attribute generate_gitattributes

Parameters:

  • value

    the value to set the attribute generate_gitattributes to.



36
37
38
# File 'lib/reality/zapwhite.rb', line 36

def generate_gitattributes=(value)
  @generate_gitattributes = value
end

Instance Method Details

#check_only?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/reality/zapwhite.rb', line 42

def check_only?
  !!@check_only
end

#exclude_patternsObject



30
31
32
# File 'lib/reality/zapwhite.rb', line 30

def exclude_patterns
  @exclude_patterns
end

#generate_gitattributes?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/reality/zapwhite.rb', line 38

def generate_gitattributes?
  @generate_gitattributes
end

#runObject

Run normalization process across directory. Return the number of files that need normalization



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/reality/zapwhite.rb', line 50

def run
  normalize_count = 0

  if generate_gitattributes?
    output_options = {:prefix => '# DO NOT EDIT: File is auto-generated', :normalize => true}
    new_gitattributes = generate_gitattributes!
    new_content = new_gitattributes.as_file_contents(output_options)
    old_content = File.exist?(@attributes.attributes_file) ? IO.read(@attributes.attributes_file) : nil
    if new_content != old_content
      @attributes = new_gitattributes
      if check_only?
        puts 'Non-normalized .gitattributes file'
      else
        puts 'Fixing: .gitattributes'
        @attributes.write(output_options)
      end
      normalize_count += 1
    end
  end

  files = {}

  collect_file_attributes(files)

  files.each_pair do |filename, config|
    full_filename = "#{@base_directory}/#{filename}"
    original_bin_content = File.binread(full_filename)

    encoding = config[:encoding].nil? ? 'utf-8' : config[:encoding].gsub(/^UTF/,'utf-')

    content = File.read(full_filename, :encoding => "bom|#{encoding}")

    content =
      config[:dos] ?
        clean_dos_whitespace(filename, content, config[:eofnl], config[:allow_empty]) :
        clean_whitespace(filename, content, config[:eofnl], config[:allow_empty])
    if config[:nodupnl]
      while content.gsub!(/\n\n\n/, "\n\n")
        # Keep removing duplicate new lines till they have gone
      end
    end
    if content.bytes != original_bin_content.bytes
      normalize_count += 1
      if check_only?
        puts "Non-normalized whitespace in #{filename}"
      else
        puts "Fixing: #{filename}"
        File.open(full_filename, 'wb') do |out|
          out.write content
        end
      end
    end
  end

  normalize_count
end