Class: Version::File

Inherits:
Object
  • Object
show all
Defined in:
lib/versus/file.rb,
lib/versus/file/plain_format.rb,
lib/versus/file/jeweler_format.rb

Overview

Version::File class

Defined Under Namespace

Modules: JewelerFormat, PlainFormat

Constant Summary collapse

NAMES =

Possible names for a version file to look for in automatic look-up.

%w{
  VERSION
  VERSION.yml
  VERSION.yaml
  var/version
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

New Version::File instance.



96
97
98
# File 'lib/versus/file.rb', line 96

def initialize(path)
  @path = path
end

Class Method Details

.current(path = nil) ⇒ Version::Number

Get current version by look-up of version file.

If path is nil, then caller is used to automatically set the look-up path. This allows for some very cool code-fu for those who keep their project version is a project file:

module MyApp
  VERSION = Version::File.current
end

Returns:



39
40
41
42
# File 'lib/versus/file.rb', line 39

def self.current(path=nil)
  vfile = lookup(path || File.dirname(caller.first))
  vfile.version if vfile
end

.lookup(path = nil) ⇒ Version::File

Look-up and return version file.

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/versus/file.rb', line 49

def self.lookup(path=nil)
  # if path is nil, detect automatically; if path is a directory, detect
  # automatically in the directory; if path is a filename, use it directly
  file = if path
           if ::File.file?(path)
             ::File.expand_path(path)
           else
             version_file(path)
           end
         else
           version_file(Dir.pwd)
         end

  return nil unless file && ::File.file?(file)

  File.new(file)
end

.supported_formatsObject

Supported version file parse formats.



22
23
24
# File 'lib/versus/file.rb', line 22

def self.supported_formats
  [JewelerFormat, PlainFormat]
end

.version_file(path) ⇒ Object

Attempts to detect the version file for the passed filename. Looks up the directory hierarchy for a file named VERSION or VERSION.yml. Returns a Pathname for the file if found, otherwise nil.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/versus/file.rb', line 72

def self.version_file(path)
  path = File.expand_path(path)
  path = File.dirname(path) unless File.directory?(path)

  return nil unless File.directory?(path)

  home = File.expand_path('~')
  done = nil

  until path == '/' or path == home
    NAMES.each do |name|
      full = File.join(dir, name)
      break(done = full) if File.file?(full)
    end
    break done if done
    path = File.dirname(path)
  end

  done
end

Instance Method Details

#change(number, file = nil) ⇒ Object

Change the number in the the file.



125
126
127
128
# File 'lib/versus/file.rb', line 125

def change(number, file=nil)
  @number = Number.parse(number)
  save
end

#formatObject

Get the verison file format.



103
104
105
106
107
108
109
110
111
112
# File 'lib/versus/file.rb', line 103

def format
  @format ||= (
    if read
      fmt = self.class.supported_formats.find{ |fm| fm.match?(path, read) }
      raise IOError, "Version file matches no known format."
    else
      PlainFormat
    end
  )
end

#numberObject Also known as: version

Get a Version::Number instance from parsed file.



117
118
119
# File 'lib/versus/file.rb', line 117

def number
  @number ||= parse(read)
end

#parse(read) ⇒ Version::Number

Parse file constents.

Returns:



151
152
153
# File 'lib/versus/file.rb', line 151

def parse(read)
  format.parse(read)
end

#readObject

Read the version file.



133
134
135
# File 'lib/versus/file.rb', line 133

def read
  @read ||= File.read(path)
end

#save(file = nil) ⇒ Object

Save the version file.



140
141
142
143
144
# File 'lib/versus/file.rb', line 140

def save(file=nil)
  file = file || path 
  text = format.render(number)
  ::File.open(file, 'w'){ |f| f << text }
end