Module: Appium::Thor::Helpers

Included in:
Default
Defined in:
lib/appium_thor/helpers.rb

Instance Method Summary collapse

Instance Method Details

#_build_gemObject

Sets the permissions on the gem credentials Runs gem build gemspec



6
7
8
9
# File 'lib/appium_thor/helpers.rb', line 6

def _build_gem
  `chmod 0600 ~/.gem/credentials`
  sh "gem build #{gem_name}.gemspec"
end

#_bump(value) ⇒ Object

Updates the date and version in version_file. Prints the new version and date to the console. The date is not printed if it hasn’t changed.

x.y.z

Parameters:

  • value (symbol)

    value is either :x, :y, or :z. Default is z.



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
77
78
79
80
81
82
# File 'lib/appium_thor/helpers.rb', line 46

def _bump(value)
  data = File.read version_file

  v_line = data.match version_rgx
  d_line = data.match /\s*DATE\s*=\s*'([^']+)'/m

  old_v = v_line[0]
  old_d = d_line[0]

  old_num     = v_line[1]
  new_num     = old_num.split('.')
  new_num[-1] = new_num[-1].to_i + 1

  if value == :y
    new_num[-1] = 0 # x.y.Z -> x.y.0
    new_num[-2] = new_num[-2].to_i + 1 # x.Y -> x.Y+1
  elsif value == :x
    new_num[-1] = 0 # x.y.Z -> x.y.0
    new_num[-2] = 0 # x.Y.z -> x.0.z
    new_num[-3] = new_num[-3].to_i + 1
  end

  new_num = new_num.join '.'

  new_v = old_v.sub old_num, new_num
  puts "#{old_num} -> #{new_num}"

  old_date = d_line[1]
  new_date = Date.today.to_s
  new_d    = old_d.sub old_date, new_date
  puts "#{old_date} -> #{new_date}" unless old_date == new_date

  data.sub! old_v, new_v
  data.sub! old_d, new_d

  File.write version_file, data
end

#_installObject

Installs the local gem. It’s fast due to the flags

–no-rdoc = skip rdoc –no-ri = skip ri –local = only install from the local disk



150
151
152
153
154
# File 'lib/appium_thor/helpers.rb', line 150

def _install
  _build_gem
  _uninstall
  sh "gem install --no-rdoc --no-ri --local #{gem_name}-#{version}.gem"
end

#_publishObject

Publishes the master branch to github Creates a version tag Updates release notes and documentation Builds the gem Publishes the gem to rubygems



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/appium_thor/helpers.rb', line 168

def _publish
  unless `git branch`.include? "* #{branch}"
    puts "'#{branch}' branch required to release."
    exit!
  end

  # ensure gems are installed
  `bundle update`

  # Commit then pull before pushing.
  tag_name = "v#{version}"
  raise 'Tag already exists!' if tag_exists tag_name

  # Commit then pull before pushing.
  sh "git commit --allow-empty -am 'Release #{version}'"
  sh 'git pull'
  sh "git tag #{tag_name}"

  notes_failed = false

  # update notes now that there's a new tag
  notes rescue notes_failed = true
  docs
  sh "git commit --allow-empty -am 'Update release notes'" unless notes_failed
  sh "git push origin #{branch}"
  sh "git push origin #{tag_name}"
  _build_gem
  puts "Please run 'gem push #{gem_name}-#{version}.gem'"
end

#_uninstallObject

Uninstalls all versions of the gem



157
158
159
160
161
# File 'lib/appium_thor/helpers.rb', line 157

def _uninstall
  cmd = "gem uninstall -aIx #{gem_name}"
  # rescue from errors. avoids gem not installed error.
  sh "#{cmd}" rescue nil
end

#remove_non_ascii_from_cwdObject

Remove non-ascii bytes from all rb files in the current working directory. Used to purge byte order marks that mess up YARD



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/appium_thor/helpers.rb', line 200

def remove_non_ascii_from_cwd
  glob = File.expand_path File.join(Dir.pwd, '**', '*.rb')
  Dir.glob(glob) do |path|
    path = File.expand_path path
    next if File.directory? path

    data = File.read path
    data = data.encode('US-ASCII', invalid: :replace, undef: :replace, replace: '')
    data = data.encode('UTF-8')
    File.open(path, 'w') { |f| f.write data }
  end
end

#sh(command) ⇒ Object

Runs command. Raises an exception if the command doesn’t execute successfully.



18
19
20
21
22
23
24
25
26
27
# File 'lib/appium_thor/helpers.rb', line 18

def sh(command)
  process = POSIX::Spawn::Child.new command

  unless process.success?
    raise "Command #{command} failed. out: #{process.out}\nerr: #{process.err}"
  end

  out = process.out
  out ? out.strip : out
end

#tag_exists(tag_name) ⇒ Object

Returns true if the tag exists on the master branch.



12
13
14
15
# File 'lib/appium_thor/helpers.rb', line 12

def tag_exists(tag_name)
  cmd = %Q(git branch -a --contains "#{tag_name}")
  POSIX::Spawn::Child.new(cmd).out.include? "* #{branch}"
end

#update_release_notesObject

Creates release_notes.md based on changes between tags. Note that the first tag won’t contain notes.



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/appium_thor/helpers.rb', line 86

def update_release_notes
  tag_sort = ->(tag1, tag2) do
    tag1_numbers = tag1.match(/\.?v(\d+\.\d+\.\d+)$/)
    # avoid indexing into a nil match. nil[1]
    tag1_numbers = tag1_numbers[1].split('.').map! { |n| n.to_i } if tag1_numbers
    tag2_numbers = tag2.match(/\.?v(\d+\.\d+\.\d+)$/)
    tag2_numbers = tag2_numbers[1].split('.').map! { |n| n.to_i } if tag2_numbers
    tag1_numbers <=> tag2_numbers
  end

  tags = `git tag`.split "\n"
  begin
    tags.sort! &tag_sort
  rescue
    message = 'Skipping release notes (unable to sort)'
    $stderr.puts message
    fail message
  end
  pairs = []
  tags.each_index { |a| pairs.push tags[a] + '...' + tags[a+1] unless tags[a+1].nil? }

  notes = ''

  dates = `git log --tags --simplify-by-decoration --pretty="format:%d %ad" --date=short`.split "\n"

  pairs.sort! &tag_sort
  pairs.reverse! # pairs are in reverse order.

  tag_date = []
  pairs.each do |pair|
    tag = pair.split('...').last
    dates.each do |line|
      # regular tag, or tag on master.
      if line.include?(tag + ')') || line.include?(tag + ',')
        tag_date.push tag + ' ' + line.match(/\d{4}-\d{2}-\d{2}/)[0]
        break
      end
    end
  end

  pairs.each_index do |a|
    data     =`git log --pretty=oneline #{pairs[a]}`
    new_data = ''
    data.split("\n").each do |line|
      hex     = line.match(/[a-zA-Z0-9]+/)[0]
      # use first 7 chars to match GitHub
      comment = line.gsub(hex, '').strip
      next if comment == 'Update release notes'
      new_data.concat("- [#{hex[0...7]}](https://github.com/#{github_owner}/#{github_name}/commit/#{hex}) #{comment}\n")
    end
    data  = "#{new_data}\n"

    # last pair is the released version.
    notes.concat("#### #{tag_date[a]}\n\n#{data}\n")
  end

  File.open('release_notes.md', 'w') { |f| f.write notes.to_s.strip }
end

#versionObject

Returns the version number from version_file as a string



35
36
37
# File 'lib/appium_thor/helpers.rb', line 35

def version
  @version ||= File.read(version_file).match(version_rgx)[1]
end

#version_rgxObject

Used to parse the version number from version_file



30
31
32
# File 'lib/appium_thor/helpers.rb', line 30

def version_rgx
  /\s*VERSION\s*=\s*'([^']+)'/m
end