Class: GitNewlineAtEof::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/git-newline-at-eof.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Application

Returns a new instance of Application.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/git-newline-at-eof.rb', line 6

def initialize(argv)
  @files = files
  @options = {}
  @options[:feed_last_line] = false
  @options[:discard_last_newline] = false
  @options[:treat_all] = false
  @options[:check_all] = false
  @options[:opted] = false

  @opt = OptionParser.new
  [
    [
      '-f', '--feed-last-line',
      'Add newline to line what is not terminated by newline at end of file.',
      proc { |v|
        @options[:feed_last_line] = true
        @options[:opted] = true
      }
    ],
    [
      '-d',
      '--discard-last-newline',
      'Remove discarded newline at end of file.',
      proc { |v|
        @options[:discard_last_newline] = true
        @options[:opted] = true
      }
    ],
    [
      '-a',
      '--treat-all',
      'This is identical with --feed-last-line --discard-last-newline.',
      proc { |v|
        @options[:treat_all] = true
        @options[:opted] = true
      }
    ],
    [
      '-c',
      '--check-all',
      'Check and show warning about newline at end of file.',
      proc { |v|
        @options[:check_all] = true
        @options[:opted] = true
      }
    ],
    [
      '-h',
      '--help',
      'Show this message.',
      proc { |v|
        puts @opt.help
        @options[:opted] = true
      }
    ],
    [
      '-v',
      '--version',
      'Show version.',
      proc {
        @options[:opted] = true
        puts @opt.ver
      }
    ]
  ].each do |short, long, desc, proc_obj|
    @opt.on(short, long, desc, &proc_obj)
  end
  @opt.program_name = 'git newline-at-eof'
  @opt.version = GitNewlineAtEof::VERSION
  @opt.parse!(argv)
end

Instance Method Details

#check_allObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/git-newline-at-eof.rb', line 96

def check_all
  @files.each do |f|
    if no_newline?(f[:last_newlines_num])
      puts "#{f[:filename]}: no newline at end of file"
    elsif discarded_newline?(f[:last_newlines_num])
      discarded_num = f[:last_newlines_num] - 1
      puts "#{f[:filename]}: discarded #{discarded_num} newline#{discarded_num > 1 ? 's' : ''} at end of file"
    end
  end
end

#discard_last_newline_allObject



125
126
127
128
129
130
131
# File 'lib/git-newline-at-eof.rb', line 125

def discard_last_newline_all
  @files.each do |f|
    if discarded_newline?(f[:last_newlines_num])
      discard_last_newline(f[:filename], f[:last_newlines_num] - 1)
    end
  end
end

#feed_last_line_allObject



117
118
119
120
121
122
123
# File 'lib/git-newline-at-eof.rb', line 117

def feed_last_line_all
  @files.each do |f|
    if no_newline?(f[:last_newlines_num])
      feed_last_line(f[:filename])
    end
  end
end

#runObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/git-newline-at-eof.rb', line 78

def run
  unless @options[:opted]
    puts @opt.help
  end
  if @options[:check_all]
    check_all
  elsif @options[:treat_all]
    treat_all
  else
    if @options[:feed_last_line]
      feed_last_line_all
    end
    if @options[:discard_last_newline]
      discard_last_newline_all
    end
  end
end

#treat_allObject



107
108
109
110
111
112
113
114
115
# File 'lib/git-newline-at-eof.rb', line 107

def treat_all
  @files.each do |f|
    if no_newline?(f[:last_newlines_num])
      feed_last_line(f[:filename])
    elsif discarded_newline?(f[:last_newlines_num])
      discard_last_newline(f[:filename], f[:last_newlines_num] - 1)
    end
  end
end