module Adminix::Helpers::Files

Public Class Methods

file_exists?(file) click to toggle source
# File lib/adminix/helpers/files.rb, line 64
def self.file_exists?(file)
  File.exist?(file)
end
mkdir_p(dirname) click to toggle source
# File lib/adminix/helpers/files.rb, line 56
def self.mkdir_p(dirname)
  `mkdir -p #{dirname}`
end
read_asset(file) click to toggle source
# File lib/adminix/helpers/files.rb, line 75
def self.read_asset(file)
  path = "#{Adminix.root}/app/assets#{file}"
  return unless File.exist?(path)
  IO.read(path)
end
read_erb_tpl(file, binding) click to toggle source
# File lib/adminix/helpers/files.rb, line 68
def self.read_erb_tpl(file, binding)
  path = "#{Adminix.root}/app/views/web/#{file}.erb"
  return unless File.exist?(path)
  template = ERB.new File.read(path), nil, '%'
  template.result(binding)
end
read_json_file(file) click to toggle source
# File lib/adminix/helpers/files.rb, line 8
def self.read_json_file(file)
  content = IO.read(file)
  JSON.parse(content)
rescue JSON::ParserError
  Helpers::Output.display_error_and_exit(
    "Error reading JSON inside file #{file}"
  )
  {}
rescue Errno::ENOENT
  Helpers::Output.display_error_and_exit("Error reading file #{file}")
  {}
end
read_plain_file(file) click to toggle source
# File lib/adminix/helpers/files.rb, line 34
def self.read_plain_file(file)
  IO.read(file)
rescue Errno::ENOENT
  Helpers::Output.display_error_and_exit("Error reading file #{file}")
end
read_yaml_file(file) click to toggle source
# File lib/adminix/helpers/files.rb, line 21
def self.read_yaml_file(file)
  content = IO.read(file)
  obj = YAML.safe_load(content)
  if obj.is_a? String
    Helpers::Output.display_error_and_exit("Error reading file #{file}")
    {}
  end
  obj
rescue Errno::ENOENT
  Helpers::Output.display_error_and_exit("Error reading file #{file}")
  {}
end
rm(file) click to toggle source
# File lib/adminix/helpers/files.rb, line 52
def self.rm(file)
  File.delete(file) if File.exist?(file)
end
touch(file_path) click to toggle source
# File lib/adminix/helpers/files.rb, line 60
def self.touch(file_path)
  `touch #{file_path}`
end
write_json_file(file, content) click to toggle source
# File lib/adminix/helpers/files.rb, line 40
def self.write_json_file(file, content)
  open(file, 'w') { |f| f.puts(content.to_json) }
end
write_plain_file(file, content) click to toggle source
# File lib/adminix/helpers/files.rb, line 48
def self.write_plain_file(file, content)
  open(file, 'w') { |f| f.puts(content) }
end
write_yaml_file(file, content) click to toggle source
# File lib/adminix/helpers/files.rb, line 44
def self.write_yaml_file(file, content)
  open(file, 'w') { |f| f.puts(content.to_yaml) }
end