# frozen_string_literal: true module Groundskeeper # The `version.rb` file in an application. class RailsVersion APPLICATION_CONFIG_FILE = "%sconfig/application.rb" MODULE_NAME_EXPRESSION = /module (\w+)/ VERSION_FILE = "%slib/%s/version.rb" VERSION_EXPRESSION = /(?<=VERSION = ")([^"]+)(?=")/ attr_reader :relative_path, :module_file, :version_file def initialize(relative_path = "") @relative_path = relative_path find_version_file end def current_version version_file.match(VERSION_EXPRESSION) || "" end def update_version!(value) version_file.gsub_file VERSION_EXPRESSION, value end def exists? version_file.exists? end private def find_module_file @module_file = Groundskeeper::Document.new(module_file_path) end def find_version_file find_module_file @version_file = Groundskeeper::Document.new(version_file_path) end def module_file_path format APPLICATION_CONFIG_FILE, relative_path end def version_file_path format VERSION_FILE, relative_path, project_namespace end def project_namespace underscore(module_file.match(MODULE_NAME_EXPRESSION)) end def underscore(word) return "" unless word.respond_to?(:gsub) word .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end end end