Class: Baltix::Space

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/baltix/space.rb

Defined Under Namespace

Modules: Spec Classes: InvalidSpaceFileError

Constant Summary collapse

TYPES =
{
   sources: Baltix::Source
}
STATES =
{
   invalid: ->(_, source, _) { !source.valid? },
   disabled: ->(space, source, _) { space.is_disabled?(source) },
   duplicated: ->(_, _, dup) { dup },
}
TYPE_CHARS =
{
   fake: '·',
   gem: '*',
   gemfile: '&',
   rakefile: '^',
}
STATUS_CHARS =
{
   invalid: 'X',
   disabled: '-',
   duplicated: '=',
   valid: 'V',
}
@@space =
{}
@@options =
{}

Constants included from Log

Log::DEFAULT_IO_NAMES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

default_prefix, io_name_parse, ios, level, #level_match, #log, prefix, prefix_for, setup, setup_kind

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/baltix/space.rb', line 357

def method_missing method, *args
   value =
      instance_variable_get(:"@#{method}") ||
      (spec.send(method) rescue nil) ||
      options&.[](method) ||
      spec&.options&.[](method.to_s) ||
      state[method]

   instance_variable_set(:"@#{method}", value || super)

   value
end

Instance Attribute Details

#optionsObject (readonly)

options property returns the hash of the loaded options if any

options #=> …



48
49
50
# File 'lib/baltix/space.rb', line 48

def options
  @options
end

#rootdirObject

rootdir returns the root dir for the space got from the options, defaulting to the current folder.

rootdir #=> /root/dir/for/the/space



81
82
83
# File 'lib/baltix/space.rb', line 81

def rootdir
   @rootdir ||= read_attribute(:rootdir) || Dir.pwd
end

#stateObject (readonly)

options property returns the hash of the loaded options if any

options #=> …



48
49
50
# File 'lib/baltix/space.rb', line 48

def state
  @state
end

Class Method Details

.load(string) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/baltix/space.rb', line 371

def load string
   if Gem::Version.new(Psych::VERSION) >= Gem::Version.new("4.0.0")
      YAML.load(string,
         aliases: true,
         permitted_classes: [
            Baltix::Source::Fake,
            Baltix::Source::Rakefile,
            Baltix::Source::Gemfile,
            Baltix::Source::Gem,
            Baltix::Spec::Rpm,
            Baltix::Spec::Rpm::Name,
            Baltix::Spec::Rpm::Secondary,
            Gem::Specification,
            Gem::Version,
            Gem::Dependency,
            Gem::Requirement,
            OpenStruct,
            Symbol,
            Time,
            Date
         ])
   else
      YAML.load(string)
   end
end

.load_from(state: Dir[".space"].first, options: {}) ⇒ Object



416
417
418
419
420
# File 'lib/baltix/space.rb', line 416

def load_from state: Dir[".space"].first, options: {}
   load_from!(state: state, options: options)
rescue InvalidSpaceFileError
   @@space[nil] = new(nil, options)
end

.load_from!(state: Dir[".space"].first, options: {}) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/baltix/space.rb', line 397

def load_from! state: Dir[".space"].first, options: {}
#         system_path_check # TODO required to generate spec rubocop

   state_tmp =
      case state
      when IO, StringIO
         load(state.readlines.join(""))
      when String
         raise InvalidSpaceFileError.new(state: state) if !File.file?(state)

         load(IO.read(state))
      when NilClass
      else
         raise InvalidSpaceFileError
      end.to_os

   @@space[state_tmp.name] = self.new(state_tmp, options)
end

Instance Method Details

#changesObject

changes returns a list of open-struct formatted changes in the space or spec defined if any, otherwise returns blank array.

space.changes # => []



110
111
112
# File 'lib/baltix/space.rb', line 110

def changes
   @changes ||= spec&.changes || main_source&.respond_to?(:changes) && main_source.changes || []
end

#compilablesObject



172
173
174
# File 'lib/baltix/space.rb', line 172

def compilables
   @compilables ||= valid_sources.map { |s| s.extensions rescue [] }.flatten.uniq
end

#dependenciesObject

dependencies returns all the valid source dependencies list as an array of Gem::Dependency objects, otherwise returning blank array.



143
144
145
146
147
148
149
150
151
152
# File 'lib/baltix/space.rb', line 143

def dependencies
   @dependencies ||= valid_sources.map do |source|
      source.respond_to?(:dependencies) && source.dependencies || []
   end.flatten.reject do |dep|
      match_platform?(dep) || sources.any? do |s|
         dep.name == s.name &&
         dep.requirement.satisfied_by?(Gem::Version.new(s.version))
      end
   end
end

#docsObject



168
169
170
# File 'lib/baltix/space.rb', line 168

def docs
   @docs ||= valid_sources.map { |s| s.docs rescue [] }.flatten.uniq
end

#executablesObject



164
165
166
# File 'lib/baltix/space.rb', line 164

def executables
   @executables ||= valid_sources.map { |s| s.executables rescue [] }.flatten.uniq
end

#filesObject



160
161
162
# File 'lib/baltix/space.rb', line 160

def files
   @files ||= valid_sources.map { |s| s.files rescue [] }.flatten.uniq
end

#ignored_namesObject

def is_regarded? source

   regarded_names.any? {|i| i === source.name } ||
      !ignored_names.any? {|i| i === source.name } &&
      !ignored_path_tokens.any? {|t| /\/#{t}\// =~ source.source_file }
end


204
205
206
# File 'lib/baltix/space.rb', line 204

def ignored_names
   @ignored_names ||= (read_attribute(:ignored_names) || [])
end

#ignored_path_tokensObject



212
213
214
# File 'lib/baltix/space.rb', line 212

def ignored_path_tokens
   @ignored_path_tokens ||= (read_attribute(:ignored_path_tokens) || [])
end

#is_disabled?(source) ⇒ Boolean

Returns:

  • (Boolean)


245
246
247
248
249
# File 'lib/baltix/space.rb', line 245

def is_disabled? source
   options.ignored_path_tokens.any? { |t| /\/#{t}\// =~ source.source_file } ||
      options.regarded_names.all? { |i| !i.match?(source.name) } &&
      options.ignored_names.any? { |i| i === source.name }
end

#licensesObject

licenses returns license list defined in all the valid sources found in the space.

space.licenses => # [“MIT”]



133
134
135
136
137
138
139
# File 'lib/baltix/space.rb', line 133

def licenses
   return @licenses if @licenses

   licenses = valid_sources.map { |source| source.licenses rescue [] }.flatten.uniq

   @licenses = !licenses.blank? && licenses || spec&.licenses || []
end

#main_sourceObject

main_source selects main source from the list of sources when source root dir is the space’s one and source’s name contains minimum name size among the matching sources



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/baltix/space.rb', line 88

def main_source
   return @main_source if @main_source

   reals = valid_sources.select { |source| !source.is_a?(Baltix::Source::Fake) }
   if spec && !spec.state.blank?
      specen_source = reals.find { |real| spec.state["name"] === real.name }
   end

   root_source ||= valid_sources.sort {|x, y| x.name.size <=> y.name.size }.find { |source| source.rootdir == rootdir }
   @main_source =
      specen_source || root_source.is_a?(Baltix::Source::Fake) && reals.size == 1 && reals.first || root_source
end

#match_platform?(dep) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
# File 'lib/baltix/space.rb', line 154

def match_platform? dep
   if dep.respond_to?(:platforms)
      (dep.platforms & options.skip_platforms).any?
   end
end

#nameObject

name returns a default name of the space with a prefix if any. It returns name of a source when its root is the same as the space’s root, or returns name defined in the spec if any. If no spec defined returns name of last folder in rootdir or “root” as a default main source name.

space.name # => space-name



56
57
58
59
60
# File 'lib/baltix/space.rb', line 56

def name
   return @name if @name

   @name = spec&.name || main_source&.name
end

#options_for(type) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/baltix/space.rb', line 224

def options_for type
   @@options[type] = type::OPTIONS.map do |option|
      value = self.options[option] || self.respond_to?(option) && self.send(option) || nil

      [ option, value ]
   end.compact.to_os
end

#read_attribute(attr) ⇒ Object



220
221
222
# File 'lib/baltix/space.rb', line 220

def read_attribute attr
   options.send(attr) || state.send(attr)
end

#regarded_namesObject



208
209
210
# File 'lib/baltix/space.rb', line 208

def regarded_names
   @regarded_names ||= read_attribute(:regarded_names) || []
end

#sourcesObject

sources returns all the sources in the space. It will load from the space sources, or by default will search sources in the provided folder or the current one.

space.sources => # [#<Baltix::Source:…>, #<…>]



181
182
183
# File 'lib/baltix/space.rb', line 181

def sources
   @sources ||= stat_sources.map { |x| x.first }
end

#specObject

spec property returns the hash of the loaded spec if any, it can be freely reassigned.

spec #=> …



237
238
239
# File 'lib/baltix/space.rb', line 237

def spec
   @spec ||= gen_spec
end

#spec=(value) ⇒ Object



241
242
243
# File 'lib/baltix/space.rb', line 241

def spec= value
   gen_spec(value)
end

#spec_typeObject



216
217
218
# File 'lib/baltix/space.rb', line 216

def spec_type
   @spec_type ||= read_attribute(:spec_type) || spec && spec.class.to_s.split("::").last.downcase
end

#summariesObject

summaries returns an open-struct formatted summaries with locales as keys in the space or spec defined if any, otherwise returns blank open struct.

space.summaries # => #<OpenStruct en_US.UTF-8: …>



119
120
121
122
123
124
125
126
127
# File 'lib/baltix/space.rb', line 119

def summaries
   return @summaries if @summaries

   if summaries = spec&.summaries || state.summaries
      summaries
   elsif summary = main_source&.summary
      { Baltix::I18n.default_locale => summary }.to_os
   end
end

#time_stampObject



101
102
103
# File 'lib/baltix/space.rb', line 101

def time_stamp
   Time.now.strftime("%Y%m%d")
end

#valid_sourcesObject

def valid_sources

   @valid_sources ||= sources.select do |source|
      source.valid? && is_regarded?(source)
   end
end


194
195
196
# File 'lib/baltix/space.rb', line 194

def valid_sources
   @valid_sources = stat_sources.map {|(source, status)| status == :valid && source || nil }.compact
end

#versionObject

version returns a default version for the space. Returns version of a source when its root is the same as the space’s root, or returns version defined in the spec if any, or returns default one.

space.version # => 2.1.1



68
69
70
71
72
# File 'lib/baltix/space.rb', line 68

def version
   return @version if @version

   @version ||= main_source&.version || spec&.version
end