Class: Autoversion::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/autoversion/dsl.rb

Defined Under Namespace

Classes: InvalidGitConfig, MissingReadBlock

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/autoversion/dsl.rb', line 14

def initialize
  @read_blk = nil
  @write_blk = nil
  @listeners = []
  @config = {
    :git => {
      :actions => [],
      :prefix => '',
      :stable_branch => 'master'
    }
  }
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/autoversion/dsl.rb', line 12

def config
  @config
end

#listenersObject

Returns the value of attribute listeners.



11
12
13
# File 'lib/autoversion/dsl.rb', line 11

def listeners
  @listeners
end

#read_blkObject

Returns the value of attribute read_blk.



9
10
11
# File 'lib/autoversion/dsl.rb', line 9

def read_blk
  @read_blk
end

#write_blkObject

Returns the value of attribute write_blk.



10
11
12
# File 'lib/autoversion/dsl.rb', line 10

def write_blk
  @write_blk
end

Class Method Details

.evaluate(script) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/autoversion/dsl.rb', line 124

def evaluate(script)
  obj = self.new
  obj.instance_eval(script)
  obj.validate!

  return obj.read_blk, obj.write_blk, obj.listeners, obj.config
end

Instance Method Details

#after(event, &blk) ⇒ Object

Register a block that will be executed after a certain event has fired.



105
106
107
108
109
110
111
# File 'lib/autoversion/dsl.rb', line 105

def after event, &blk
  @listeners.push({
    :type => :after,
    :event => event,
    :blk => blk
  })
end

#automate_git(*args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/autoversion/dsl.rb', line 79

def automate_git *args
  if args.length == 0
    @config[:git][:actions] = [:commit, :tag]
  else
    args[0].each do |arg| 
      if [:actions, :stable_branch, :prefix].include?(arg[0])
        @config[:git][arg[0]] = arg[1]
      end
    end
  end
end

#before(event, &blk) ⇒ Object

Register a block that will be executed before a certain event has fired.



115
116
117
118
119
120
121
# File 'lib/autoversion/dsl.rb', line 115

def before event, &blk
  @listeners.push({
    :type => :before,
    :event => event,
    :blk => blk
  })
end

#parse_file(path, matcher) ⇒ Object

Parse the specified file with the provided matcher.

The first returned match will be used as the version.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/autoversion/dsl.rb', line 30

def parse_file path, matcher
  File.open(path) do |f|
    f.each do |line|
      if m = matcher.call(line)
        return m
      end
    end
  end

  raise "#{path}: found no matching lines."
end

#read_version(&blk) ⇒ Object

Register a block that will be used to read the version number from the current project.



93
94
95
# File 'lib/autoversion/dsl.rb', line 93

def read_version &blk
  @read_blk = blk
end

#update_file(path, matcher, currentVersion, nextVersion) ⇒ Object

Update a file naively matching the specified matcher and replace any matching lines with the new version.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/autoversion/dsl.rb', line 44

def update_file path, matcher, currentVersion, nextVersion
  temp_path = "#{path}.autoversion"

  begin
    File.open(path) do |source|
      File.open(temp_path, 'w') do |target|
        source.each do |line|
          if matcher.call(line)
            target.write line.gsub currentVersion.to_s, nextVersion.to_s
          else
            target.write line
          end
        end
      end
    end

    File.rename temp_path, path
  ensure
    File.unlink temp_path if File.file? temp_path
  end
end

#update_files(paths, matcher, currentVersion, nextVersion) ⇒ Object

Convenience function for update_file to apply to multiple files.



67
68
69
70
71
# File 'lib/autoversion/dsl.rb', line 67

def update_files paths, matcher, currentVersion, nextVersion
  paths.each do |path|
    update_file path, matcher, currentVersion, nextVersion
  end
end

#validate!Object

Raises:



73
74
75
76
77
# File 'lib/autoversion/dsl.rb', line 73

def validate!
  # A read_version block is required
  raise MissingReadBlock unless @read_blk
  raise InvalidGitConfig if @config[:git][:actions].include?(:tag) && !@config[:git][:actions].include?(:commit)
end

#write_version(&blk) ⇒ Object

Register a block that will be used to write the version number to the current project.



99
100
101
# File 'lib/autoversion/dsl.rb', line 99

def write_version &blk
  @write_blk = blk
end