Skip to content

Commit

Permalink
feat: file permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwagner committed Jul 15, 2023
1 parent 54d55c7 commit 35db457
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 17 deletions.
55 changes: 55 additions & 0 deletions spec/lucky_template_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,61 @@ describe LuckyTemplate do
end
end
end

context "with permissions" do
it "adds executable file" do
filename = "hello.bash"
file_perms = 0o755_i16
LuckyTemplate.write!(Path["."]) do |folder|
folder.add_file(filename, <<-BASH, file_perms)
#!/usr/bin/env bash
echo Hello World
BASH
end
File.read(Path[filename]).should eq(<<-BASH)
#!/usr/bin/env bash
echo Hello World
BASH
File.info(Path[filename]).permissions.value.should eq(file_perms)
end

it "adds executable file with block" do
filename = "hello.bash"
file_perms = 0o755_i16
LuckyTemplate.write!(Path["."]) do |folder|
folder.add_file(filename, file_perms) do |io|
io << "#!/usr/bin/env bash"
io << '\n'
io << "echo Hello World"
end
end
File.read(Path[filename]).should eq(<<-BASH)
#!/usr/bin/env bash
echo Hello World
BASH
File.info(Path[filename]).permissions.value.should eq(file_perms)
end

it "adds empty executable file" do
filename = "hello.bash"
file_perms = 0o755_i16
LuckyTemplate.write!(Path["."]) do |folder|
folder.add_file(filename, file_perms)
end
File.size(Path[filename]).should eq(0)
File.info(Path[filename]).permissions.value.should eq(file_perms)
end

it "adds executable file from Int16#to_s" do
filename = "hello.bash"
file_perms = 0o755_i16
LuckyTemplate.write!(Path["."]) do |folder|
folder.add_file(filename, file_perms.to_s, file_perms)
end
File.read(Path[filename]).should eq(file_perms.to_s)
File.info(Path[filename]).permissions.value.should eq(file_perms)
end
end
end

describe "#add_folder" do
Expand Down
3 changes: 3 additions & 0 deletions src/lucky_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module LuckyTemplate
::File.open(path, "w") do |io|
file.to_s(io)
end
if perms = file.perms
::File.chmod(path, perms)
end
in Folder
Dir.mkdir_p(path)
write_folder!(path, file)
Expand Down
4 changes: 3 additions & 1 deletion src/lucky_template/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ module LuckyTemplate

# :nodoc:
class File
protected def initialize(@file : FileType)
property perms : Int16?

protected def initialize(@file : FileType, @perms)
end

# Appends file contents to `IO`
Expand Down
72 changes: 56 additions & 16 deletions src/lucky_template/folder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module LuckyTemplate

# Adds a new file to the folder with _content_
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _name_ contains invalid path(s)
#
# Examples(s):
Expand All @@ -24,13 +26,17 @@ module LuckyTemplate
# add_file("hello.txt", <<-TEXT)
# hello world
# TEXT
#
# add_file("hello.txt", "hello world", 0o644)
# ```
def add_file(name : String, content : String) : self
add_file(Path[name], content)
def add_file(name : String, content : String, perms : Int16? = nil) : self
add_file(Path[name], content, perms)
end

# Adds a new file to the folder with _content_
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _path_ contains invalid path(s)
#
# Examples(s):
Expand All @@ -40,13 +46,17 @@ module LuckyTemplate
# add_file(Path["./hello.txt"], <<-TEXT)
# hello world
# TEXT
#
# add_file(Path["./hello.txt"], "hello world", 0o644)
# ```
def add_file(path : Path, content : String) : self
add_file(path, File.new(content))
def add_file(path : Path, content : String, perms : Int16? = nil) : self
add_file(path, File.new(content, perms))
end

# Adds a new file to the folder with _klass_ implementing `Fileable` interface
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _name_ contains invalid path(s)
#
# Example(s):
Expand All @@ -60,13 +70,17 @@ module LuckyTemplate
# end
#
# add_file("hello.txt", Hello.new)
#
# add_file("hello.txt", Hello.new, 0o644)
# ```
def add_file(name : String, klass : Fileable) : self
add_file(Path[name], klass)
def add_file(name : String, klass : Fileable, perms : Int16? = nil) : self
add_file(Path[name], klass, perms)
end

# Adds a new file to the folder with _klass_ implementing `Fileable` interface
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _path_ contains invalid path(s)
#
# Example(s):
Expand All @@ -80,13 +94,17 @@ module LuckyTemplate
# end
#
# add_file(Path["./hello.txt"], Hello.new)
#
# add_file(Path["./hello.txt"], Hello.new, 0o644)
# ```
def add_file(path : Path, klass : Fileable) : self
add_file(path, File.new(klass))
def add_file(path : Path, klass : Fileable, perms : Int16? = nil) : self
add_file(path, File.new(klass, perms))
end

# Adds a new file to the folder yielding an `IO`
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _name_ contains invalid path(s)
#
# Example(s):
Expand All @@ -95,15 +113,23 @@ module LuckyTemplate
# ECR.embed("hello.ecr", io)
# end
#
# add_file("hello.txt", 0o644) do |io|
# ECR.embed("hello.ecr", io)
# end
#
# proc = LuckyTemplate::FileIO.new { |io| ECR.embed("hello.ecr", io) }
# add_file("hello.txt", &proc)
#
# add_file("hello.txt", 0o644, &proc)
# ```
def add_file(name : String, &block : FileIO) : self
add_file(Path[name], &block)
def add_file(name : String, perms : Int16? = nil, &block : FileIO) : self
add_file(Path[name], perms, &block)
end

# Adds a new file to the folder yielding an `IO`
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _path_ contains invalid path(s)
#
# Example(s):
Expand All @@ -112,35 +138,49 @@ module LuckyTemplate
# ECR.embed("hello.ecr", io)
# end
#
# add_file(Path["./hello.txt"], 0o644) do |io|
# ECR.embed("hello.ecr", io)
# end
#
# proc = LuckyTemplate::FileIO.new { |io| ECR.embed("hello.ecr", io) }
# add_file(Path["./hello.txt"], &proc)
#
# add_file(Path["./hello.txt"], 0o644, &proc)
# ```
def add_file(path : Path, &block : FileIO) : self
add_file(path, File.new(block))
def add_file(path : Path, perms : Int16? = nil, &block : FileIO) : self
add_file(path, File.new(block, perms))
end

# Adds a new empty file to the folder
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _name_ contains invalid path(s)
#
# Example(s):
# ```
# add_file("hello.txt")
#
# add_file("hello.txt", 0o644)
# ```
def add_file(name : String) : self
add_file(Path[name])
def add_file(name : String, perms : Int16? = nil) : self
add_file(Path[name], perms: perms)
end

# Adds a new empty file to the folder
#
# Optionally, provide _perms_ to specify file permissions
#
# Raises `Error` if _path_ contains invalid path(s)
#
# Example:
# ```
# add_file(Path["./hello.txt"])
#
# add_file(Path["./hello.txt"], 0o644)
# ```
def add_file(path : Path) : self
add_file(path, File.new(nil))
def add_file(path : Path, perms : Int16? = nil) : self
add_file(path, File.new(nil, perms))
end

private def add_file(path : Path, file : File) : self
Expand Down

0 comments on commit 35db457

Please sign in to comment.