Class: ProjectInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/reap/projectinfo.rb

Overview

Project information, generally read from a file. Simply by calling ‘ProjectInfo.load’.

ProjectInfo is a Singleton. Access it via the ProjectInfo.instance method.

Defined Under Namespace

Classes: HashBuilder

Constant Summary collapse

INFO_FILES =
[
  'ProjectInfo',
  'ReapFile',
  'projectinfo',
  'reapfile'
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ProjectInfo

Returns a new instance of ProjectInfo.



87
88
89
90
91
92
93
94
# File 'lib/reap/projectinfo.rb', line 87

def initialize( &block )
  @info = {}
  @info_stream = nil

  define( &block ) if block_given?

  #$PROJECT_INFO = self
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



85
86
87
# File 'lib/reap/projectinfo.rb', line 85

def info
  @info
end

#info_dirObject (readonly)

Returns the value of attribute info_dir.



85
86
87
# File 'lib/reap/projectinfo.rb', line 85

def info_dir
  @info_dir
end

#info_fileObject (readonly)

Returns the value of attribute info_file.



85
86
87
# File 'lib/reap/projectinfo.rb', line 85

def info_file
  @info_file
end

#info_streamObject (readonly)

Returns the value of attribute info_stream.



85
86
87
# File 'lib/reap/projectinfo.rb', line 85

def info_stream
  @info_stream
end

Class Method Details

.findObject

Find project information file.



68
69
70
71
72
73
74
75
76
# File 'lib/reap/projectinfo.rb', line 68

def find
  info_dir, info_file = nil, nil
  Dir.ascend(Dir.pwd) do |info_dir|
    info_file = INFO_FILES.find{ |f| File.file?( File.join( info_dir, f ) ) }
    break if info_file
  end
  return nil unless info_file
  return File.join( info_dir, info_file )
end

.instance(*args, &block) ⇒ Object



48
49
50
# File 'lib/reap/projectinfo.rb', line 48

def instance( *args, &block )
  @instance ||= new( *args, &block )
end

.load(fpath = nil, report = false) ⇒ Object

Load the project information from a file. Generally no file needs to be specified; the file will be found by ascending up the current path until a default file name is found (eg. ProjectInfo or Reapfile).



57
58
59
60
61
62
63
64
# File 'lib/reap/projectinfo.rb', line 57

def load( fpath=nil, report=false )
  if fpath
    new.read( fpath, report )
  else
    fpath = find
    instance.read( fpath, report )
  end
end

Instance Method Details

#[](name) ⇒ Object

Information fetch.



201
202
203
# File 'lib/reap/projectinfo.rb', line 201

def [](name)
  info[name]
end

#[]=(name, x) ⇒ Object

Information store.



207
208
209
# File 'lib/reap/projectinfo.rb', line 207

def []=(name, x)
  info[name] = x
end

#defaultsObject

Project information defaults.



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/reap/projectinfo.rb', line 167

def defaults
  self['title']       ||= self['name'].capitalize
  self['series']      ||= '1'
  self['date']        ||= Time.now.strftime("%Y-%m-%d")
  self['author']      ||= "Anonymous"
  self['maintainer']  ||= self['author']
  self['arch']        ||= 'Any'
  self['license']     ||= 'Ruby/GPL'
  self['status']      ||= 'Beta'
  self['project']     ||= self['rubyforge'] ? self['rubyforge']['project'] : nil
  self['homepage']    ||= self['rubyforge'] ? self['rubyforge']['homepage'] : nil
end

#define(&block) ⇒ Object

Define project information programmatically.



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/reap/projectinfo.rb', line 98

def define( &block )
  return unless block

  @info = HashBuilder.new( &block  ).to_h
  @info_stream = @info.to_yaml
  @info_dir = Dir.pwd #?
  @info_file = nil

  #validate
  defaults

  self
end

#read(fpath, report = true) ⇒ Object

Load project information from YAML file.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/reap/projectinfo.rb', line 114

def read( fpath, report=true )
  return unless fpath

  @info_dir = File.dirname( fpath )
  @info_file = fpath #File.basename( fpath )
  @info_stream = File.read( fpath ).strip
  @info = YAML::load( info_stream ).traverse{ |k,v| [k.to_s.downcase, v] }

  Dir.chdir(@info_dir)
  if report
    puts "(in #{Dir.pwd})" #unless dir == Dir.pwd
  end

  #validate
  defaults

  self
end

#require_custom_tasksObject

Load custom tasks.



182
183
184
185
186
187
188
189
190
191
# File 'lib/reap/projectinfo.rb', line 182

def require_custom_tasks
  # Universal custom tasks for all projects.
  dir = File.join( Config::CONFIG['datadir'], 'reap/task' )
  require_all( File.join(dir, '*') ) if File.directory?(dir)
  # Personal tasks for all projects.
  dir = File.expand_path( '~/.share/reap/task' )
  require_all( File.join(dir, '*') ) if File.directory?(dir)
  # Project specific tasks.
  require_all('task/*') if File.directory?('task')
end

#to_cascading_open_objectObject

Convert to a CascadinOpenObject.



195
196
197
# File 'lib/reap/projectinfo.rb', line 195

def to_cascading_open_object
  CascadingOpenObject.new( @info )
end

#to_hObject

Information to hash.



213
214
215
# File 'lib/reap/projectinfo.rb', line 213

def to_h
  @info
end

#update(info = nil, &block) ⇒ Object

Update project information.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/reap/projectinfo.rb', line 135

def update( info=nil, &block )
  if info
    @info.update( info.traverse{ |k,v| [k.to_s.downcase, v] } )
  end
  if block_given?
    @info.update( HashBuilder.new( &block  ).to_h )
  end
  @info_stream = @info.to_yaml

  #validate
  defaults

  self
end

#validateObject

Validate project information.



158
159
160
161
162
163
# File 'lib/reap/projectinfo.rb', line 158

def validate #( *fields )
  val = true
  (puts "NAME is required in project information file."; val=false) unless info['name']
  (puts "VERSION is required in project information file."; val=false) unless info['version']
  exit -1 unless val
end