# encoding: utf-8 require 'fedux_org_stdlib/require_files' require_library %w{ erubis forwardable active_support/core_ext/string/strip active_support/core_ext/object/blank } require 'fedux_org_stdlib/version_management/rubygem_version_file_parser' require 'fedux_org_stdlib/version_management/version_builder' require 'fedux_org_stdlib/version_management/library_builder' require 'fedux_org_stdlib/version_management/exceptions' module FeduxOrgStdlib module VersionManagement class RubygemVersionFile extend Forwardable def_delegators :@version, :bump def initialize(version, library) @version = version @library = library end def version @version.to_s end def version=(v) @version.update(v) end def write(path) File.write(path, to_s) end def self.read(path, parser = RubygemVersionFileParser.new, version_builder = VersionBuilder.new, library_builder = LibraryBuilder.new) parser.parse(path) version = version_builder.build_from(parser.version) library = library_builder.build_from(parser.modules) RubygemVersionFile.new(version, library) end def to_s klass = Class.new do attr_reader :version, :library def initialize(version, module_names) @version = version @module_names = module_names end def prefix(n) ' ' * 2 * n end end Erubis::Eruby.new(template).evaluate(klass.new(@version, @library.module_names)) end private def template <<-EOF.strip_heredoc # encoding: utf-8 # <%= @module_names.first %> <% @module_names.each_with_index do |m,i| -%> <%= prefix(i) %>module <%= m %> <% end -%> <%= prefix(@module_names.size) %>VERSION = '<%= @version %>' <% (@module_names.size - 1).downto(0) do |i| -%> <%= prefix(i) %>end <% end -%> EOF end end end end