Class: DevSystem::FileShell

Inherits:
Shell show all
Defined in:
lib/dev_system/subsystems/shell/shells/file_shell.rb

Direct Known Subclasses

BinShell, DirShell, TextFileShell, TextShell

Instance Attribute Summary

Attributes inherited from Liza::Controller

#menv

Class Method Summary collapse

Methods inherited from Shell

all, cruby?, engine, jruby?, linux?, mac?, os, ruby_version, unix?, windows?

Methods inherited from Liza::Controller

#`, `, attr_accessor, attr_reader, attr_writer, #attrs, box, #box, call, color, division, division!, division?, inherited, menv_accessor, menv_reader, menv_writer, on_connected, panel, #panel, plural, require, requirements, sh, #sh, singular, subsystem, subsystem!, subsystem?, subsystem_token, token

Methods inherited from Liza::Unit

_erbs_for, #add, add, cl, #cl, class_methods_defined, const_added, const_missing, constants_defined, define_error, descendants_select, division, erbs_available, erbs_defined, erbs_for, errors, #fetch, fetch, get, #get, instance_methods_defined, log, #log, log?, #log?, #log_array, log_array, log_hash, #log_hash, #log_level, log_level, #log_level?, log_level?, log_levels, #log_levels, #log_render_convert, #log_render_format, #log_render_in, #log_render_out, method_added, methods_defined, namespace, part, raise_error, #raise_error, reload!, #reload!, #render, #render!, #render_stack, renderable_formats_for, renderable_names, section, sections, #set, set, #settings, settings, singleton_method_added, sleep, #sleep, stick, #stick, sticks, #sticks, subclasses_select, subunits, system, #system, system?, test_class, time_diff, #time_diff

Class Method Details

._raise_if_blank(path) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 3

def self._raise_if_blank path
  raise ArgumentError, "Path is required" if path.nil? || path.to_s.empty?
end

._raise_if_not_exists(path, log_level: self.log_level) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 7

def self._raise_if_not_exists path, log_level: self.log_level
  raise ArgumentError, "File does not exist at '#{path}'" unless exist?(path, log_level: log_level)
end

.category_for(path, log_level: self.log_level) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 50

def self.category_for path, log_level: self.log_level
  log log_level, "Getting category for '#{path}'"
  _raise_if_blank path

  return :directory     if directory?     path, log_level: :highest
  return :file          if file?          path, log_level: :highest
  return :symbolic_link if symbolic_link? path, log_level: :highest
end

.copy(source, destination, log_level: self.log_level) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 101

def self.copy(source, destination, log_level: self.log_level)
  log log_level, "Copying '#{source}' to '#{destination}'"
  source = Pathname(source)
  destination = Pathname(destination)
  _raise_if_blank source
  _raise_if_blank destination

  if source.directory?
    destination.mkpath
    source.each_child do |child|
      copy(child, destination.join(child.basename))
    end
  else
    destination.dirname.mkpath # Ensure the destination directory exists
    File.open(destination, "wb") do |dest_file|
      File.open(source, "rb") do |src_file|
        dest_file.write(src_file.read)
      end
    end
  end
end

.directory?(path, log_level: self.log_level) ⇒ Boolean

category

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 29

def self.directory? path, log_level: self.log_level
  log log_level, "Checking if '#{path}' is a directory"
  _raise_if_blank path

  File.directory? path
end

.exist?(path, log_level: self.log_level) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 13

def self.exist? path, log_level: self.log_level
  log log_level, "Checking if file exists at '#{path}'"
  _raise_if_blank path

  File.exist? path
end

.file?(path, log_level: self.log_level) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 36

def self.file? path, log_level: self.log_level
  log log_level, "Checking if '#{path}' is a file"
  _raise_if_blank path

  File.file? path
end

.gitkeep(path) ⇒ Object



73
74
75
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 73

def self.gitkeep path
  touch "#{path}/.gitkeep"
end

.read_binary(path, log_level: self.log_level) ⇒ Object

read



79
80
81
82
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 79

def self.read_binary(path, log_level: self.log_level)
  # TODO: move all code from BinShell to this file
  BinShell.read path
end

.read_text(path, log_level: self.log_level) ⇒ Object



84
85
86
87
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 84

def self.read_text(path, log_level: self.log_level)
  # TODO: move all code from TextShell to this file
  TextShell.read path, log_level:
end

.remove(path, log_level: self.log_level) ⇒ Object



123
124
125
126
127
128
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 123

def self.remove(path, log_level: self.log_level)
  log log_level, "Removing '#{path}'"
  _raise_if_blank path

  File.delete path
end

.size(path, log_level: self.log_level) ⇒ Object



20
21
22
23
24
25
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 20

def self.size path, log_level: self.log_level
  log log_level, "Getting size of file at '#{path}'"
  _raise_if_not_exists path

  File.size path
end

.symbolic_link?(path, log_level: self.log_level) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 43

def self.symbolic_link? path, log_level: self.log_level
  log log_level, "Checking if '#{path}' is a symbolic link"
  _raise_if_blank path

  File.symlink? path
end

.touch(path, log_level: self.log_level) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 61

def self.touch path, log_level: self.log_level
  log log_level, "Touching '#{path}'"
  _raise_if_blank path

  dir = File.dirname(path)
  DevSystem::DirShell.create dir

  File.open(path, "w").close

  true
end

.write_binary(path, content, create_dir: nil, log_level: self.log_level) ⇒ Object

write



91
92
93
94
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 91

def self.write_binary(path, content, create_dir: nil, log_level: self.log_level)
  # TODO: move all code from BinShell to this file
  BinShell.write path, content, create_dir:
end

.write_text(path, content, create_dir: nil, log_level: self.log_level) ⇒ Object



96
97
98
99
# File 'lib/dev_system/subsystems/shell/shells/file_shell.rb', line 96

def self.write_text(path, content, create_dir: nil, log_level: self.log_level)
  # TODO: move all code from TextShell to this file
  TextShell.write path, content, create_dir:, log_level:
end