Class: DevSystem::TextShell

Inherits:
FileShell show all
Defined in:
lib/dev_system/sub/shell/shells/text_shell.rb

Class Method Summary collapse

Methods inherited from FileShell

_raise_if_blank, _raise_if_not_exists, directory?, exist?, file?, gitkeep, size, touch

Methods inherited from Shell

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

Methods inherited from Liza::Controller

color, inherited, on_connected

Methods inherited from Liza::Unit

const_missing, division, part, system, #system, test_class

Class Method Details

.append_line_after_last_including(path:, newline:, after:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dev_system/sub/shell/shells/text_shell.rb', line 44

def self.append_line_after_last_including(path:, newline:, after:)
  log "Appending to #{path}"
  state = nil
  lines = File.readlines(path)
  File.open(path, "w") do |out|
    lines.each do |line|
      state = :entered_systems_region if state == nil && line =~ after
      state = :left_systems_region if state == :entered_systems_region && line !~ after
      
      if state == :left_systems_region
        out.write("#{newline}\n")
        log :low, newline.strip.green
        state = :done
      end
      
      out.write(line)
      log :low, line.strip.light_black
    end
  end
end

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



19
20
21
22
23
24
25
# File 'lib/dev_system/sub/shell/shells/text_shell.rb', line 19

def self.read path, log_level: self.log_level
  log log_level, "Reading #{path}"
  _raise_if_blank path
  _raise_if_not_exists path, log_level: log_level

  File.read path
end

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



27
28
29
30
31
32
# File 'lib/dev_system/sub/shell/shells/text_shell.rb', line 27

def self.read_lines path, log_level: self.log_level
  log log_level, "Reading lines from #{path}"
  return [] unless exist? path

  File.readlines path
end

.read_lines!(path, log_level: self.log_level) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/dev_system/sub/shell/shells/text_shell.rb', line 34

def self.read_lines! path, log_level: self.log_level
  log log_level, "Reading lines from #{path}"
  _raise_if_blank path
  _raise_if_not_exists path

  File.readlines path
end

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



7
8
9
10
11
12
13
14
15
# File 'lib/dev_system/sub/shell/shells/text_shell.rb', line 7

def self.write path, content, create_dir: nil, log_level: self.log_level
  log log_level, "Writing #{content.to_s.size} characters (#{content.encoding}) to #{path}"
  _raise_if_blank path

  create_dir = get :create_dir if create_dir.nil?
  DevSystem::DirShell.create File.dirname(path), log_level: log_level if create_dir

  File.write path, content
end