bump-version-in-source 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env ruby
  2. require 'json'
  3. def puts_c(color, str)
  4. puts "\x1b[#{color}m#{str}\x1b[0m"
  5. end
  6. class Source
  7. attr_accessor :path, :v_regex
  8. def initialize(path, v_regex)
  9. @path = path
  10. @v_regex = v_regex
  11. end
  12. end
  13. class Bumper
  14. attr_accessor :sources, :bumped, :target_v
  15. def initialize(sources)
  16. @sources = sources
  17. end
  18. def start
  19. # get package.json version
  20. package = JSON.parse File.read 'package.json'
  21. @target_v = package['version']
  22. @bumped = false
  23. @sources.each {|source| bump_source(source)}
  24. # if bumped, do extra stuff and notify the user
  25. if @bumped
  26. `git add mustache.js`
  27. `git commit --amend --no-edit`
  28. puts_c 32, "successfully bumped version to #{@target_v}!"
  29. puts_c 33, "don't forget to `npm publish`!"
  30. end
  31. exit 0
  32. end
  33. def bump_source(source)
  34. file_buffer = File.read source.path
  35. if match = file_buffer.match(source.v_regex)
  36. file_v = match.captures[0]
  37. if @target_v != file_v
  38. did_bump
  39. puts "> bumping version in file '#{source.path}': #{file_v} -> #{@target_v}..."
  40. file_buffer[source.v_regex, 1] = @target_v
  41. File.open(source.path, 'w') { |f| f.write file_buffer }
  42. end
  43. else
  44. puts_c 31, "ERROR: Can't find version in '#{source.path}'"
  45. exit 1
  46. end
  47. end
  48. def did_bump
  49. if !@bumped
  50. puts 'bump detected!'
  51. end
  52. @bumped = true
  53. end
  54. end
  55. bumper = Bumper.new([
  56. Source.new('mustache.js', /version: '([^']+)'/),
  57. ])
  58. bumper.start