class Snowglobe::Filesystem

Public Instance Methods

append_to_file(path, content, _options = {}) click to toggle source
# File lib/snowglobe/filesystem.rb, line 61
def append_to_file(path, content, _options = {})
  pathname = wrap_file(path)
  create_parents_of(pathname)
  pathname.open("a") { |f| f.puts(content + "\n") }
end
clean() click to toggle source
# File lib/snowglobe/filesystem.rb, line 5
def clean
  if root_directory.exist?
    root_directory.rmtree
  end
end
comment_lines_matching_in_file(path, pattern) click to toggle source
# File lib/snowglobe/filesystem.rb, line 31
def comment_lines_matching_in_file(path, pattern)
  transform_file(path) do |lines|
    lines.map do |line|
      if line && line =~ pattern
        "###{line}"
      else
        line
      end
    end
  end
end
create_project() click to toggle source
# File lib/snowglobe/filesystem.rb, line 15
def create_project
  project_directory.mkpath
end
find_in_project(path) click to toggle source
# File lib/snowglobe/filesystem.rb, line 23
def find_in_project(path)
  project_directory.join(path)
end
open_file(path, *args, &block) click to toggle source
# File lib/snowglobe/filesystem.rb, line 27
def open_file(path, *args, &block)
  wrap_file(path).open(*args, &block)
end
project_directory() click to toggle source
# File lib/snowglobe/filesystem.rb, line 11
def project_directory
  root_directory.join(Snowglobe.configuration.project_name)
end
read_file(path) click to toggle source
# File lib/snowglobe/filesystem.rb, line 50
def read_file(path)
  wrap_file(path).read
end
remove_from_file(path, pattern) click to toggle source
# File lib/snowglobe/filesystem.rb, line 67
def remove_from_file(path, pattern)
  unless pattern.is_a?(Regexp)
    pattern = Regexp.new("^" + Regexp.escape(pattern) + "$")
  end

  transform(path) do |lines|
    lines.reject { |line| line =~ pattern }
  end
end
transform_file(path) { |lines| ... } click to toggle source
# File lib/snowglobe/filesystem.rb, line 43
def transform_file(path)
  content = read_file(path)
  lines = content.split(/\n/)
  transformed_lines = yield lines
  write_file(path, transformed_lines.join("\n") + "\n")
end
within_project(&block) click to toggle source
# File lib/snowglobe/filesystem.rb, line 19
def within_project(&block)
  Dir.chdir(project_directory, &block)
end
write_file(path, content) click to toggle source
# File lib/snowglobe/filesystem.rb, line 54
def write_file(path, content)
  pathname = wrap_file(path)
  create_parents_of(pathname)
  pathname.open("w") { |f| f.write(content) }
  pathname
end

Private Instance Methods

create_parents_of(pathname) click to toggle source
# File lib/snowglobe/filesystem.rb, line 91
def create_parents_of(pathname)
  pathname.dirname.mkpath
end
root_directory() click to toggle source
# File lib/snowglobe/filesystem.rb, line 79
def root_directory
  Snowglobe.configuration.temporary_directory
end
wrap_file(path) click to toggle source
# File lib/snowglobe/filesystem.rb, line 83
def wrap_file(path)
  if path.is_a?(Pathname)
    path
  else
    find_in_project(path)
  end
end