module Spark::File
Overview
Spark::File allows you to interact with the user's filesystem.
Extended Modules
Defined in:
spark/file.crspark/file/create_file.cr
Constant Summary
-
BEGINNING_OF_FILE_REGEX =
/\A/
-
END_OF_FILE_REGEX =
/\z/
Instance Method Summary
-
#append_to_file(relative_path : String, *content)
Append any number of strings to the end of a file.
-
#append_to_file(relative_path : String, & : -> String)
Append the provided block content to the end of a file.
-
#chmod_file(file_path : String, permissions : Int | ::File::Permissions)
Change the permissions of the specified file.
-
#copy_file(source_path : String, destination_path : String) : String
Copy a file from a provided source path to a provided destination.
-
#create_file(relative_path : String, *content) : String
Create a new file with the provided content.
-
#create_file(relative_path : String, & : -> String) : String
Create a new file with the provided block content.
-
#inject_into_file(relative_path : String, *content, after pattern : Regex | String)
Inject any number of strings after the provided pattern.
-
#inject_into_file(relative_path : String, *, after pattern : Regex | String, & : -> String)
Inject the provided block content after the provided pattern.
-
#inject_into_file(relative_path : String, *content, before pattern : Regex | String)
Inject any number of strings before the provided pattern.
-
#inject_into_file(relative_path : String, *, before pattern : Regex | String, & : -> String)
Inject the provided block content before the provided pattern.
-
#move_file(source_path : String, destination_path : String) : String
Move a file from the provided source path to the provided destination.
-
#prepend_to_file(relative_path : String, *content)
Prepend any number of strings to the beginning of a file.
-
#prepend_to_file(relative_path : String, & : -> String)
Prepend the provided block content to the beginning of a file.
-
#remove_file(relative_path : String)
Remove a file.
-
#replace_in_file(relative_path : String, pattern : Regex | String, replacement : String)
Replace a specific pattern with some replacement text throughout a given file.
Instance Method Detail
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.")
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
Change the permissions of the specified file.
Example:
Spark::File.chmod_file("README.md", File::Permissions::All)
File.info("README.md").permissions.value # => 0o777
Copy a file from a provided source path to a provided destination.
Example:
Spark::File.copy_file("README.md", "IDENTICAL_README.md")
Create a new file with the provided content.
Example:
Spark::File.create_file("README.md", "# Welcome\n\n", "This is my new file.")
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
Inject any number of strings after the provided pattern.
Example:
Spark::File.inject_into_file("README.md", "# New Section", after: "# Last Section\n")
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
Inject any number of strings before the provided pattern.
Example:
Spark::File.inject_into_file("README.md", "# New Section", before: "# First Section\n")
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
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")
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.")
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
Remove a file.
Removing a single file:
Spark::File.remove_file("README.md")
Removing a directory:
Spark::File.remove_file("src/")
Replace a specific pattern with some replacement text throughout a given file.
Example:
Spark::File.replace_in_file("shard.yml", pattern: "MIT", replacement: "Apache")