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+)/
NAMESPACE_FILE =
"%slib/%s.rb"
MODULE_DIRECTORY =
"%slib/%s"
VERSION_FILE =
"#{MODULE_DIRECTORY}/version.rb".freeze
VERSION_EXPRESSION =
/(?<=VERSION = ")([^"]+)(?=")/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

rubocop:enable Metrics/MethodLength



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

def initialize(
  relative_path = "",
  namespace_file:, project_name: "",
  config_file: nil,
  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



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

def create_initial_version!
  version_file.make_parent_directory!
  version_file.write_file(
    <<~RB
      # frozen_string_literal: true

      # Version string.
      module #{project_name}
        VERSION = "#{INITIAL_VERSION}"
      end
    RB
  )
  namespace_file.write_file(
    <<~RB
      # frozen_string_literal: true

      require_relative "./#{folder_namespace}/version"

      # nodoc
      module #{project_name}
      end
    RB
  )
end

#current_versionObject



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

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  version_file.exists?
end

#rails?Boolean

Returns:

  • (Boolean)


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

def rails?
  config_file.exists?
end

#update_version!(value) ⇒ Object



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

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