Class: Groundskeeper::RailsVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/groundskeeper/rails_version.rb

Overview

The version.rb file in an application.

Constant Summary collapse

APPLICATION_CONFIG_FILE =
"%sconfig/application.rb"
INITIAL_VERSION =
"0.0.0"
MODULE_NAME_EXPRESSION =
/module (\w+)/.freeze
NAMESPACE_FILE =
"%slib/%s.rb"
MODULE_DIRECTORY =
"%slib/%s"
VERSION_FILE =
"#{MODULE_DIRECTORY}/version.rb"
VERSION_EXPRESSION =
/(?<=VERSION = ")([^"]+)(?=")/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relative_path = "", project_name: "", config_file: nil, namespace_file:, version_file: nil) ⇒ RailsVersion

rubocop:enable Metrics/MethodLength



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/groundskeeper/rails_version.rb', line 37

def initialize(
  relative_path = "",
  project_name: "",
  config_file: nil,
  namespace_file:,
  version_file: nil
)
  @relative_path = relative_path
  @project_name = project_name
  @config_file = config_file || find_config_file
  @namespace_file = namespace_file
  @version_file = version_file || find_version_file
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



14
15
16
# File 'lib/groundskeeper/rails_version.rb', line 14

def config_file
  @config_file
end

#namespace_fileObject (readonly)

Returns the value of attribute namespace_file.



14
15
16
# File 'lib/groundskeeper/rails_version.rb', line 14

def namespace_file
  @namespace_file
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



14
15
16
# File 'lib/groundskeeper/rails_version.rb', line 14

def project_name
  @project_name
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



14
15
16
# File 'lib/groundskeeper/rails_version.rb', line 14

def relative_path
  @relative_path
end

#version_fileObject (readonly)

Returns the value of attribute version_file.



14
15
16
# File 'lib/groundskeeper/rails_version.rb', line 14

def version_file
  @version_file
end

Class Method Details

.build(relative_path = "") ⇒ Object

rubocop:disable Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/groundskeeper/rails_version.rb', line 18

def self.build(relative_path = "")
  config_file_path = format(APPLICATION_CONFIG_FILE, relative_path)
  config_file = Groundskeeper::Document.new(config_file_path)
  project_name = config_file.match(MODULE_NAME_EXPRESSION)
  folder_namespace = StringUtils.underscore(project_name)
  namespace_file_path =
    format(NAMESPACE_FILE, relative_path, folder_namespace)
  version_file_path = format(VERSION_FILE, relative_path, folder_namespace)

  new(
    relative_path,
    project_name: project_name,
    config_file: config_file,
    namespace_file: Groundskeeper::Document.new(namespace_file_path),
    version_file: Groundskeeper::Document.new(version_file_path)
  )
end

Instance Method Details

#create_initial_version!Object

rubocop:disable Metrics/MethodLength



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/groundskeeper/rails_version.rb', line 68

def create_initial_version!
  version_file.make_parent_directory!
  version_file.write_file(
    "      # frozen_string_literal: true\n\n      # Version string.\n      module \#{project_name}\n        VERSION = \"\#{INITIAL_VERSION}\"\n      end\n    RB\n  )\n  namespace_file.write_file(\n    <<~RB\n      # frozen_string_literal: true\n\n      require_relative \"./\#{folder_namespace}/version\"\n\n      # nodoc\n      module \#{project_name}\n      end\n    RB\n  )\nend\n"

#current_versionObject



51
52
53
# File 'lib/groundskeeper/rails_version.rb', line 51

def current_version
  version_file.match(VERSION_EXPRESSION) || ""
end

#exists?Boolean



63
64
65
# File 'lib/groundskeeper/rails_version.rb', line 63

def exists?
  version_file.exists?
end

#rails?Boolean



59
60
61
# File 'lib/groundskeeper/rails_version.rb', line 59

def rails?
  config_file.exists?
end

#update_version!(value) ⇒ Object



55
56
57
# File 'lib/groundskeeper/rails_version.rb', line 55

def update_version!(value)
  version_file.gsub_file VERSION_EXPRESSION, value
end