Class: Gravitext::DevTools::VersionWriter

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/gravitext-devtools/version_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVersionWriter

Returns a new instance of VersionWriter.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gravitext-devtools/version_writer.rb', line 32

def initialize

  @local_dep_prefix = nil

  @git_lister = GitFileLister.new
  @inclusions = []
  @exclusions = @git_lister.exclusions.dup
  @version = nil

  @patterns = {
    :history => [ /History\./,
                  /^=== ([0-9a-z.]+) \((TBD|[0-9\-]+)\)/ ],
    :pom     => [ /pom\.xml$/,
                  /<version>([0-9a-z.]+)<\/version>/ ],
    :rb      => [ /(version|base)\.rb$/,
                  /VERSION\s*=\s*['"]([0-9a-z.]+)['"]/ ],
    :init    => [ /init\//,
                  /^gem.+,\s*['"]=\s*([0-9a-z.]+)['"]/ ],
    :gemspec => [ /\.gemspec$/,
                  /RJack::TarPit\.specify/ ]
  }

  Hooker.apply( [ :gt, :version ], self )
end

Instance Attribute Details

#exclusionsObject

Returns the value of attribute exclusions.



28
29
30
# File 'lib/gravitext-devtools/version_writer.rb', line 28

def exclusions
  @exclusions
end

#inclusionsObject

Returns the value of attribute inclusions.



29
30
31
# File 'lib/gravitext-devtools/version_writer.rb', line 29

def inclusions
  @inclusions
end

#local_dep_prefixObject

Returns the value of attribute local_dep_prefix.



27
28
29
# File 'lib/gravitext-devtools/version_writer.rb', line 27

def local_dep_prefix
  @local_dep_prefix
end

#patternsObject

Returns the value of attribute patterns.



30
31
32
# File 'lib/gravitext-devtools/version_writer.rb', line 30

def patterns
  @patterns
end

Instance Method Details

#adjust_gemspec_local(lines, start) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gravitext-devtools/version_writer.rb', line 156

def adjust_gemspec_local( lines, start )
  parent = false
  local = false
  lines.each_index do |i|
    next unless i >= start
    line = lines[i]
    if line =~ /\.depend.+['"]#{@local_dep_prefix}/
      lines[i] = line.sub( /(\.depend.*,\s*['"])([^'"]+)(['"])/,
                           "\\1#{gem_version_range}\\3" )
    end
  end
end

#adjust_pom_local(lines, start) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gravitext-devtools/version_writer.rb', line 125

def adjust_pom_local( lines, start )
  parent = false
  local = false
  lines.each_index do |i|
    next unless i >= start
    line = lines[i]
    case line
    when /<parent>/
      local = true
      parent = true
    when /<\/parent>/
      local = false
      parent = false
    when /<artifactId>/
      local = ( line =~ /<artifactId>\s*#{@local_dep_prefix}/ )
    when /<version>([\[\]\(\)0-9a-z.,]+)(<\/version>)/
      if local
        if parent
          lines[i] = line.sub( /^(\s*<version>)([0-9a-z.]+)/,
                               "\\1#{@version}" )
        else
          lines[i] = line.sub( /^(\s*<version>)([\[\]\(\)0-9a-z.,]+)/,
                               "\\1#{maven_version_range}" )
        end
      end
    when /<\/dependencies>/
      break
    end
  end
end

#gem_version_rangeObject



182
183
184
# File 'lib/gravitext-devtools/version_writer.rb', line 182

def gem_version_range
  "~> #{@version}"
end

#maven_version_rangeObject



177
178
179
180
# File 'lib/gravitext-devtools/version_writer.rb', line 177

def maven_version_range
  minor_v = @version =~ /^(\d+\.\d+)/ && $1
  "[#{@version},#{minor_v}.999)"
end

#parse_options(args = ARGV) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gravitext-devtools/version_writer.rb', line 57

def parse_options( args = ARGV )

  @git_lister.parse_options( args ) do |opts|
    opts.banner = "Usage: gt-version [options] dir|files ..."
    opts.on( "-v", "--version V", "Specify new version", String ) do |v|
      @version = v
    end
    opts.on( "-d", "--depend-prefix P",
             "Also adjust local dependencies with prefix" ) do |p|
      @local_dep_prefix = p
    end
  end
end

#process(fname) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gravitext-devtools/version_writer.rb', line 82

def process( fname )
  type, (_,vline) = @patterns.find { |_,(fp,_)| fname =~ fp }
  return false unless type

  # With no version specified, only process history for release date
  return false unless @version || type == :history

  lines = IO.readlines( fname )
  first_match = true
  lines.each_index do |i|
    line = lines[i]
    if line =~ vline
      case type
      when :history
        if $2 == 'TBD'
          lines[i] = if @version.nil? || $1 == @version
                       "=== #{$1} (#{release_date})"
                     else
                       "=== #{@version} (TBD)"
                     end
        else
          lines.insert( i, [ "=== #{@version} (TBD)", '' ] )
        end
      when :pom
        lines[i] = line.sub( /^(\s*<version>)([0-9a-z.]+)(<\/version>)/,
                             "\\1#{@version}\\3" )
        adjust_pom_local( lines, i+1 ) if @local_dep_prefix
      when :rb
        lines[i] = line.sub( /^(\s*VERSION\s*=\s*['"])([0-9a-z.]+)/,
                             "\\1#{@version}" )
      when :init
        lines[i] = line.sub( /^(gem.+,\s*['"]=\s*)([0-9a-z.]+)/,
                             "\\1#{@version}" )
      when :gemspec
        adjust_gemspec_local( lines, i+1 ) if @local_dep_prefix
      end
      break
    end
  end
  rewrite_file( fname, lines )
  true
end

#release_dateObject



169
170
171
# File 'lib/gravitext-devtools/version_writer.rb', line 169

def release_date
  Time.now.strftime( "%Y-%-m-%-d" )
end

#rewrite_file(file, lines) ⇒ Object



173
174
175
# File 'lib/gravitext-devtools/version_writer.rb', line 173

def rewrite_file( file, lines )
  open( file, "w" ) { |fout| fout.puts( lines ) }
end

#run(args = ARGV) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/gravitext-devtools/version_writer.rb', line 71

def run( args = ARGV )
  parse_options( args )

  @git_lister.inclusions = @inclusions
  @git_lister.exclusions = @exclusions

  @git_lister.files.each do |fname|
    process( fname )
  end
end