Class: Bricolage::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/filesystem.rb

Direct Known Subclasses

ScopedFileSystem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, env) ⇒ FileSystem

Returns a new instance of FileSystem.



32
33
34
35
36
# File 'lib/bricolage/filesystem.rb', line 32

def initialize(path, env)
  @path = Pathname(path)
  @environment = env
  @subsystems = {}
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



43
44
45
# File 'lib/bricolage/filesystem.rb', line 43

def environment
  @environment
end

#pathObject (readonly)

Returns the value of attribute path.



42
43
44
# File 'lib/bricolage/filesystem.rb', line 42

def path
  @path
end

Class Method Details

.extract_home_dirs(job_path) ⇒ Object

job_path -> [home_path, subsys_id]



25
26
27
28
29
30
# File 'lib/bricolage/filesystem.rb', line 25

def FileSystem.extract_home_dirs(job_path)
  subsys_path = Pathname(job_path).realpath.parent
  return subsys_path.parent, subsys_path.basename
rescue SystemCallError => err
  raise ParameterError, "failed to access job file: #{err.message}"
end

.for_option_pathes(home_path, job_path, env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bricolage/filesystem.rb', line 8

def FileSystem.for_option_pathes(home_path, job_path, env)
  if job_path
    home, subsys_id = extract_home_dirs(job_path)
    if home_path and home_path.realpath.to_s != home.realpath.to_s
      raise OptionError, "--home option and job file is exclusive"
    end
    new(home, env).subsystem(subsys_id)
  elsif home_path
    new(home_path, env)
  elsif home = ENV['BRICOLAGE_HOME']
    new(home, env)
  else
    new(Pathname.getwd, env)
  end
end

Instance Method Details

#all_typed_pathes(type) ⇒ Object

/.TYPE



144
145
146
# File 'lib/bricolage/filesystem.rb', line 144

def all_typed_pathes(type)
  subsystems.map {|subsys| subsys.typed_pathes(type) }.flatten
end

#config_file_loaderObject



129
130
131
# File 'lib/bricolage/filesystem.rb', line 129

def config_file_loader
  ConfigLoader.new(home_path)
end

#config_pathes(name) ⇒ Object



125
126
127
# File 'lib/bricolage/filesystem.rb', line 125

def config_pathes(name)
  [ "config/#{name}", "config/#{@environment}/#{name}" ].map {|rel| root.relative(rel) }
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/bricolage/filesystem.rb', line 82

def exist?(name)
  relative(name).exist?
end

#file(name) ⇒ Object



86
87
88
# File 'lib/bricolage/filesystem.rb', line 86

def file(name)
  FileResource.new(relative(name))
end

#glob(pattern) ⇒ Object



139
140
141
# File 'lib/bricolage/filesystem.rb', line 139

def glob(pattern)
  Dir.glob("#{@path}/#{pattern}").map {|path| Pathname(path) }
end

#home_pathObject



74
75
76
# File 'lib/bricolage/filesystem.rb', line 74

def home_path
  root.path
end

#inspectObject



45
46
47
# File 'lib/bricolage/filesystem.rb', line 45

def inspect
  "\#<#{self.class} #{@path}>"
end

#job_dirObject



78
79
80
# File 'lib/bricolage/filesystem.rb', line 78

def job_dir
  scoped? ? @path : nil
end

#job_file(id) ⇒ Object



133
134
135
136
137
# File 'lib/bricolage/filesystem.rb', line 133

def job_file(id)
  path = typed_name(id, 'job')
  return path if path.exist?
  glob("#{id}.*.job").first or path
end

#parameter_file(name, type) ⇒ Object



117
118
119
# File 'lib/bricolage/filesystem.rb', line 117

def parameter_file(name, type)
  name.count('/') == 0 ? typed_file(name, type) : root.file(name)
end

#parameter_file_loaderObject



121
122
123
# File 'lib/bricolage/filesystem.rb', line 121

def parameter_file_loader
  ConfigLoader.new(home_path)
end

#relative_path(name) ⇒ Object Also known as: relative



96
97
98
99
100
101
102
103
# File 'lib/bricolage/filesystem.rb', line 96

def relative_path(name)
  path = Pathname(name)
  if path.absolute?
    path
  else
    @path + path
  end
end

#rootObject



49
50
51
# File 'lib/bricolage/filesystem.rb', line 49

def root
  self
end

#root_relative_path(rel) ⇒ Object Also known as: root_relative



90
91
92
# File 'lib/bricolage/filesystem.rb', line 90

def root_relative_path(rel)
  root.relative_path(rel)
end

#scoped?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/bricolage/filesystem.rb', line 38

def scoped?
  false
end

#subdirectoriesObject



70
71
72
# File 'lib/bricolage/filesystem.rb', line 70

def subdirectories
  @path.children(true).select {|path| path.directory? }
end

#subsystem(id) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/bricolage/filesystem.rb', line 53

def subsystem(id)
  @subsystems[id.to_s] ||= begin
    unless root.relative(id).directory?
      raise ParameterError, "no such subsystem: #{id}"
    end
    ScopedFileSystem.new(root, id)
  end
end

#subsystemsObject



62
63
64
65
66
67
68
# File 'lib/bricolage/filesystem.rb', line 62

def subsystems
  root.subdirectories
      .map {|path| path.basename.to_s }
      .select {|name| /\A\w+\z/ =~ name }
      .reject {|name| name == 'config' }
      .map {|name| subsystem(name) }
end

#typed_file(name, type) ⇒ Object

typed_file(“make_master”, “sql”) -> FileResource(“$prefix/make_master.sql”)



113
114
115
# File 'lib/bricolage/filesystem.rb', line 113

def typed_file(name, type)
  FileResource.new(typed_name(name, type))
end

#typed_name(name, type) ⇒ Object

typed_name(“make_master”, “sql”) -> Pathname(“$prefix/make_master.sql”)



108
109
110
# File 'lib/bricolage/filesystem.rb', line 108

def typed_name(name, type)
  relative(name.count('.') > 0 ? name : "#{name}.#{type}")
end

#typed_pathes(type) ⇒ Object



148
149
150
# File 'lib/bricolage/filesystem.rb', line 148

def typed_pathes(type)
  @path.children.select {|path| path.file? and path.extname.to_s == ".#{type}" }
end