require 'json' require 'yaml' require 'erb' module Adminix module Helpers module Files 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 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 def self.read_plain_file(file) IO.read(file) rescue Errno::ENOENT Helpers::Output.display_error_and_exit("Error reading file #{file}") end def self.write_json_file(file, content) open(file, 'w') { |f| f.puts(content.to_json) } end def self.write_yaml_file(file, content) open(file, 'w') { |f| f.puts(content.to_yaml) } end def self.write_plain_file(file, content) open(file, 'w') { |f| f.puts(content) } end def self.rm(file) File.delete(file) if File.exist?(file) end def self.mkdir_p(dirname) `mkdir -p #{dirname}` end def self.touch(file_path) `touch #{file_path}` end def self.file_exists?(file) File.exist?(file) end 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 def self.read_asset(file) path = "#{Adminix.root}/app/assets#{file}" return unless File.exist?(path) IO.read(path) end end end end