Module: RailsBase::FileManipulation

Extended by:
CliActions
Included in:
RailsBase::Features::Chat, RailsBase::Features::Facebook, RailsBase::Features::Twilio, RailsActions
Defined in:
lib/rs-rails-base/file_manipulation.rb

Overview

This module provides help to file manipulation

Instance Method Summary collapse

Methods included from CliActions

ask_for_something, say_something

Instance Method Details

#append_to_file(file_name, from) ⇒ Object



10
11
12
13
# File 'lib/rs-rails-base/file_manipulation.rb', line 10

def append_to_file(file_name, from)
  from_file = File.expand_path("#{__dir__}/#{from}")
  TTY::File.append_to_file(file_name, read_all_content(from_file))
end

#create_file(file_name, from) ⇒ Object



15
16
17
18
# File 'lib/rs-rails-base/file_manipulation.rb', line 15

def create_file(file_name, from)
  from_file = File.expand_path("#{__dir__}/#{from}")
  TTY::File.create_file(file_name, read_all_content(from_file))
end

#inject_into_file(file_name, after, from, extra_line_flag = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rs-rails-base/file_manipulation.rb', line 20

def inject_into_file(file_name, after, from, extra_line_flag = false)
  from_file = File.expand_path("#{__dir__}/#{from}")
  content = read_all_content(from_file)
  content += "\n" if extra_line_flag
  begin
    TTY::File.inject_into_file(file_name, content, after: "#{after}\n")
  rescue StandardError => ex
    say_something('Make sure you are on the root of your project please')
    puts ex
  end
end

#install_gem(name, version) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rs-rails-base/file_manipulation.rb', line 43

def install_gem(name, version)
  if File.exist?('Gemfile')
    unless read_all_content('Gemfile').include? name
      gem_line = "gem '#{name}', '~> #{version}' \n"
      TTY::File.append_to_file('Gemfile', gem_line)
      system('bundle install')
    end
  else
    say_something('Please go to your root folder :)')
  end
end

#read_all_content(file_name) ⇒ Object



36
37
38
39
40
41
# File 'lib/rs-rails-base/file_manipulation.rb', line 36

def read_all_content(file_name)
  file = File.open(file_name, 'rb')
  content = file.read
  file.close
  content
end

#replace_in_file(file_name, old_content, new_content) ⇒ Object



32
33
34
# File 'lib/rs-rails-base/file_manipulation.rb', line 32

def replace_in_file(file_name, old_content, new_content)
  TTY::File.replace_in_file(file_name, old_content, new_content)
end