module Spark::File

Overview

Spark::File allows you to interact with the user's filesystem.

Extended Modules

Defined in:

spark/file.cr
spark/file/create_file.cr

Constant Summary

BEGINNING_OF_FILE_REGEX = /\A/
END_OF_FILE_REGEX = /\z/

Instance Method Summary

Instance Method Detail

def append_to_file(relative_path : String, *content) #

Append any number of strings to the end of a file.

Example:

Spark::File.append_to_file("README.md", "# Goodbye!", "You're at the bottom of the README.")

[View source]
def append_to_file(relative_path : String, & : -> String) #

Append the provided block content to the end of a file.

Example:

Spark::File.append_to_file("README.md") do
  <<-CONTENT
  # Goodbye

  You're at the bottom of the README.
  CONTENT
end

[View source]
def chmod_file(file_path : String, permissions : Int | ::File::Permissions) #

Change the permissions of the specified file.

Example:

Spark::File.chmod_file("README.md", File::Permissions::All)
File.info("README.md").permissions.value # => 0o777

[View source]
def copy_file(source_path : String, destination_path : String) : String #

Copy a file from a provided source path to a provided destination.

Example:

Spark::File.copy_file("README.md", "IDENTICAL_README.md")

[View source]
def create_file(relative_path : String, *content) : String #

Create a new file with the provided content.

Example:

Spark::File.create_file("README.md", "# Welcome\n\n", "This is my new file.")

[View source]
def create_file(relative_path : String, & : -> String) : String #

Create a new file with the provided block content.

Example:

Spark::File.create_file("README.md") do
  <<-CONTENT
  # Welcome

  This is my new file.
  CONTENT
end

[View source]
def inject_into_file(relative_path : String, *content, after pattern : Regex | String) #

Inject any number of strings after the provided pattern.

Example:

Spark::File.inject_into_file("README.md", "# New Section", after: "# Last Section\n")

[View source]
def inject_into_file(relative_path : String, *, after pattern : Regex | String, & : -> String) #

Inject the provided block content after the provided pattern.

Example:

Spark::File.inject_into_file("README.md", after: "# Last Section\n") do
  <<-CONTENT
  This is some new file content.
  It's going to be great!\n
  CONTENT
end

[View source]
def inject_into_file(relative_path : String, *content, before pattern : Regex | String) #

Inject any number of strings before the provided pattern.

Example:

Spark::File.inject_into_file("README.md", "# New Section", before: "# First Section\n")

[View source]
def inject_into_file(relative_path : String, *, before pattern : Regex | String, & : -> String) #

Inject the provided block content before the provided pattern.

Example:

Spark::File.inject_into_file("README.md", before: "# First Section\n") do
  <<-CONTENT
  This is some new file content.
  It's going to be great!\n
  CONTENT
end

[View source]
def move_file(source_path : String, destination_path : String) : String #

Move a file from the provided source path to the provided destination.

Note that the source file will no longer exist after this action. If you wish to preserve the source file, use Spark::File.copy_file.

Example:

Spark::File.move_file("README.md", "NEW_README.md")

[View source]
def prepend_to_file(relative_path : String, *content) #

Prepend any number of strings to the beginning of a file.

Example:

Spark::File.prepend_to_file("README.md", "# Welcome!", "You're at the top of the README.")

[View source]
def prepend_to_file(relative_path : String, & : -> String) #

Prepend the provided block content to the beginning of a file.

Example:

Spark::File.prepend_to_file("README.md") do
  <<-CONTENT
  # Welcome

  You're at the top of the README.
  CONTENT
end

[View source]
def remove_file(relative_path : String) #

Remove a file.

Removing a single file:

Spark::File.remove_file("README.md")

Removing a directory:

Spark::File.remove_file("src/")

[View source]
def replace_in_file(relative_path : String, pattern : Regex | String, replacement : String) #

Replace a specific pattern with some replacement text throughout a given file.

Example:

Spark::File.replace_in_file("shard.yml", pattern: "MIT", replacement: "Apache")

[View source]