Class: Raketeer::FilesBumper

Inherits:
Object
  • Object
show all
Defined in:
lib/raketeer/files_bumper.rb

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, bump_ver, dry_run, strict, &init_per_file) ⇒ FilesBumper

Returns a new instance of FilesBumper.

Since:

  • 0.2.4



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/raketeer/files_bumper.rb', line 39

def initialize(files,bump_ver,dry_run,strict,&init_per_file)
  @bump_ver = bump_ver
  @bump_ver_empty = bump_ver.empty?
  @dry_run = dry_run
  @files = files.to_a
  @init_per_file = init_per_file
  @sem_ver = nil
  @strict = strict
  @version = nil
  @version_bumped = false
end

Instance Attribute Details

#bump_verObject (readonly)

Since:

  • 0.2.4



21
22
23
# File 'lib/raketeer/files_bumper.rb', line 21

def bump_ver
  @bump_ver
end

#bump_ver_emptyObject (readonly) Also known as: bump_ver_empty?

Since:

  • 0.2.4



22
23
24
# File 'lib/raketeer/files_bumper.rb', line 22

def bump_ver_empty
  @bump_ver_empty
end

#changesObject

Since:

  • 0.2.4



23
24
25
# File 'lib/raketeer/files_bumper.rb', line 23

def changes
  @changes
end

#dry_runObject (readonly) Also known as: dry_run?

Since:

  • 0.2.4



24
25
26
# File 'lib/raketeer/files_bumper.rb', line 24

def dry_run
  @dry_run
end

#filesObject (readonly)

Since:

  • 0.2.4



25
26
27
# File 'lib/raketeer/files_bumper.rb', line 25

def files
  @files
end

#init_per_fileObject

Since:

  • 0.2.4



26
27
28
# File 'lib/raketeer/files_bumper.rb', line 26

def init_per_file
  @init_per_file
end

#lineObject

Since:

  • 0.2.4



27
28
29
# File 'lib/raketeer/files_bumper.rb', line 27

def line
  @line
end

#linesObject

Since:

  • 0.2.4



28
29
30
# File 'lib/raketeer/files_bumper.rb', line 28

def lines
  @lines
end

#sem_verObject

Since:

  • 0.2.4



29
30
31
# File 'lib/raketeer/files_bumper.rb', line 29

def sem_ver
  @sem_ver
end

#strictObject Also known as: strict?

Since:

  • 0.2.4



30
31
32
# File 'lib/raketeer/files_bumper.rb', line 30

def strict
  @strict
end

#versionObject

Since:

  • 0.2.4



31
32
33
# File 'lib/raketeer/files_bumper.rb', line 31

def version
  @version
end

#version_bumpedObject Also known as: version_bumped?

Was the version actually bumped?

Since:

  • 0.2.4



32
33
34
# File 'lib/raketeer/files_bumper.rb', line 32

def version_bumped
  @version_bumped
end

Instance Method Details

#add_change(line, push: true) ⇒ Object

Since:

  • 0.2.4



51
52
53
54
55
56
# File 'lib/raketeer/files_bumper.rb', line 51

def add_change(line,push: true)
  puts "+ #{line}"

  @changes += 1
  @lines << line if push
end

#bump_files(&block) ⇒ Object

Since:

  • 0.2.4



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
# File 'lib/raketeer/files_bumper.rb', line 58

def bump_files(&block)
  @files.each do |filename|
    puts "[#{filename}]:"

    if !File.exist?(filename)
      puts '! File does not exist'

      next
    end

    @changes = 0 # For possible future use
    @lines = []
    @sem_ver = nil

    @init_per_file&.call(self)

    File.foreach(filename) do |line|
      @line = line

      block.call(self) if !@line.strip.empty?

      @lines << @line
    end

    next if bump_ver_empty?

    print '= '

    if @changes > 0
      if dry_run?
        puts "Nothing written (dry run): #{@sem_ver}"
      else
        File.open(filename,'w') do |file|
          file.puts @lines
        end

        puts "#{@changes} change#{@changes == 1 ? '' : 's'} written"
      end
    else
      puts "Nothing written (up-to-date): #{@sem_ver}"
    end
  end
end

#bump_line!(add_change: true) ⇒ :no_ver, ...

Returns:

  • (:no_ver, :same_ver, :bumped_ver)

Since:

  • 0.2.4



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/raketeer/files_bumper.rb', line 103

def bump_line!(add_change: true)
  @sem_ver = SemVer.parse_line(@line,strict: @strict)

  return :no_ver if @sem_ver.nil?

  @version = @sem_ver if @version.nil?

  if bump_ver_empty?
    puts "= #{@sem_ver}"

    return :same_ver
  else
    orig_line = @line.dup
    orig_sem_ver_s = @sem_ver.to_s

    @bump_ver.bump_line!(@line,@sem_ver,strict: @strict)

    if @sem_ver.to_s != orig_sem_ver_s
      if !@version_bumped
        @version = @sem_ver
        @version_bumped = true
      end

      self.add_change(@line,push: false) if add_change

      return :bumped_ver
    else
      @line = orig_line

      return :same_ver
    end
  end
end