Class: GitTagger::Version

Inherits:
Object
  • Object
show all
Defined in:
app/models/version.rb

Overview

Represents a version file within a project.

Instance Method Summary collapse

Constructor Details

#initialize(project_type) ⇒ Version

Sets the project name and filepath based on a project type



7
8
9
10
11
# File 'app/models/version.rb', line 7

def initialize(project_type)
  @project_name = find_project_name project_type
  @version_file_path = version_file_location project_type, @project_name
  @project_type = project_type
end

Instance Method Details

#create_version_file(path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'app/models/version.rb', line 70

def create_version_file(path)
  if !File.exist?(path)
    dirname = File.dirname(path)
    unless File.directory?(dirname)
      FileUtils.mkdir_p(dirname)
    end
    File.open(path, "w") {}
    `git add "#{ path }"`
  end
end

#find_project_name(project_type) ⇒ Object

locates and returns a project’s name based on the project type



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/version.rb', line 14

def find_project_name(project_type)
  case project_type
  when :rails_application
    Rails.application.class.parent_name.underscore
  when :rails_gem
    engine_path = ""
    Find.find(File.expand_path(Rails.root.join("../../lib"))) do |path|
      engine_path = path if path =~ /.*\/engine\.rb$/
    end
    engine_text = File.read(engine_path)
    (engine_text.match(/^module \w*$/))[0].split(" ")[1].underscore
  when :non_rails_gem
    # TODO: Add logic to find gem name and apply to version_file path
    "git_tagger"
  else
    puts "FATAL: Unknown project type, unable to determine project name!"
    abort("aborting tagging process")
  end
end

#update_version_file(semantic_version) ⇒ Object

Update version file to match updated tag



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/version.rb', line 35

def update_version_file(semantic_version)
  create_version_file(@version_file_path)

  version_text = File.read(@version_file_path)
  version_contents = version_text
                     .gsub(/  VERSION = "[0-9]+\.[0-9]+\.[0-9]+"/,
                           "  VERSION = \"#{ semantic_version }\"")
  File.open(@version_file_path, "w") { |file| file.puts version_contents }

  `git add "#{ @version_file_path }"`

  if :rails_gem == @project_type
    `(cd #{ File.expand_path(Rails.root) }; bundle install)`
    `git add "#{ File.expand_path(Rails.root.join("../../Gemfile.lock")) }"`
  end
end

#version_file_location(project_type, project_name) ⇒ Object

Locates the version file and returns its path



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/version.rb', line 53

def version_file_location(project_type, project_name)
  case project_type
  when :rails_application
    File.expand_path(Rails.root.join "lib/" \
                   "#{ project_name }/version.rb")
  when :rails_gem
    File.expand_path(Rails.root.join "../../lib/" \
                   "#{ project_name }/version.rb")
  when :non_rails_gem
    # TODO: add logic to find version path for non rails gems
    File.expand_path("../../../lib/git_tagger/version.rb", __FILE__)
  else
    puts "FATAL: Unknown project type, unable to update gem version!"
    abort("aborting tagging process")
  end
end