Module: Beet::FileSystem

Included in:
Executor
Defined in:
lib/beet/file_system.rb

Instance Method Summary collapse

Instance Method Details

#add_after(filename, matching_text, data = nil, &block) ⇒ Object

Add text after matching line

Example

add_after 'config/environment.rb', '# config.gem "aws-s3", :lib => "aws/s3"'


118
119
120
121
122
# File 'lib/beet/file_system.rb', line 118

def add_after(filename, matching_text, data=nil, &block)
  gsub_file filename, /(\s*#{Regexp.escape(matching_text)})/mi do |match|
    "#{match}\n#{data || block.call}"
  end
end

#add_before(filename, matching_text, data = nil, &block) ⇒ Object

Add text before matching line

Example

add_before 'config/environment.rb', '# config.gem "aws-s3", :lib => "aws/s3"'


130
131
132
133
134
# File 'lib/beet/file_system.rb', line 130

def add_before(filename, matching_text, data=nil, &block)
  gsub_file filename, /^(\s*#{Regexp.escape(matching_text)})/mi do |match|
    "#{data || block.call}#{match}"
  end
end

#append_file(relative_destination, data) ⇒ Object

Append text to a file

Example

append_file 'config/environments/test.rb', 'config.gem "rspec"'


107
108
109
110
# File 'lib/beet/file_system.rb', line 107

def append_file(relative_destination, data)
  path = destination_path(relative_destination)
  File.open(path, 'ab') { |file| file.write(data) }
end

#file(filename, data = nil, log_action = true, &block) ⇒ Object

Create a new file in the project folder. Specify the relative path from the project’s root. Data is the return value of a block or a data string.

Examples

file("lib/fun_party.rb") do
  hostname = ask("What is the virtual hostname I should use?")
  "vhost.name = #{hostname}"
end

file("config/apach.conf", "your apache config")


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/beet/file_system.rb', line 17

def file(filename, data = nil, log_action = true, &block)
  log 'file', filename if log_action
  dir, file = [File.dirname(filename), File.basename(filename)]

  inside(dir) do
    File.open(file, "w") do |f|
      if block_given?
        f.write(block.call)
      else
        f.write(data)
      end
    end
  end
end

#gsub_file(relative_destination, regexp, *args, &block) ⇒ Object

Run a regular expression replacement on a file

Example

gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'


90
91
92
93
94
95
96
97
98
99
# File 'lib/beet/file_system.rb', line 90

def gsub_file(relative_destination, regexp, *args, &block)
  #path = destination_path(relative_destination)
  path = relative_destination
  content = File.read(path)
  check_for = args.first || yield('')
  regex = Regexp.new(regexp.source + Regexp.escape(check_for))
  return if content =~ regex # if we can match the text and its leadin regex, don't add again
  content = content.gsub(regexp, *args, &block)
  File.open(path, 'wb') { |file| file.write(content) }
end

#in_rootObject



80
81
82
# File 'lib/beet/file_system.rb', line 80

def in_root
  FileUtils.cd(root) { yield }
end

#inside(dir = '', &block) ⇒ Object

Do something in the root of the project or a provided subfolder; the full path is yielded to the block you provide. The path is set back to the previous path when the method exits.



74
75
76
77
78
# File 'lib/beet/file_system.rb', line 74

def inside(dir = '', &block)
  folder = File.join(root, dir)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)
  FileUtils.cd(folder) { block.arity == 1 ? yield(folder) : yield }
end

#lib(filename, data = nil, &block) ⇒ Object

Create a new file in the lib/ directory. Code can be specified in a block or a data string can be given.

Examples

lib("crypto.rb") do
  "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'"
end

lib("foreign.rb", "# Foreign code is fun")


43
44
45
46
# File 'lib/beet/file_system.rb', line 43

def lib(filename, data = nil, &block)
  log 'lib', filename
  file("lib/#{filename}", data, false, &block)
end

#rakefile(filename, data = nil, &block) ⇒ Object

Create a new Rakefile with the provided code (either in a block or a string).

Examples

rakefile("bootstrap.rake") do
  project = ask("What is the UNIX name of your project?")

  <<-TASK
    namespace :#{project} do
      task :bootstrap do
        puts "i like boots!"
      end
    end
  TASK
end

rakefile("seed.rake", "puts 'im plantin ur seedz'")


66
67
68
69
# File 'lib/beet/file_system.rb', line 66

def rakefile(filename, data = nil, &block)
  log 'rakefile', filename
  file("lib/tasks/#{filename}", data, false, &block)
end