LuckyTemplate

CI

LuckyTemplate is a simple, yet versatile, library for creating file and folder structures as code templates. It has a lot in common with Teeplate, but it offers more capabilities and more ways to generate content.

Features

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  lucky_template:
    github: luckyframework/lucky_template
    version: ~> 0.2.0
  1. Run shards install

Quick Start

Here's a basic example of how to use LuckyTemplate:

require "lucky_template"

LuckyTemplate.write!(Path["."]) do |folder|
  folder.add_file(".keep")
end

This will create a folder at the current directory with an empty .keep file. See the sections below for more complex examples.

Examples

Simple Template

In this example, we create a simple README file using an ECR template, a welcome file, and a license file. We also create a new file and folder dynamically in a subfolder.

require "lucky_template"

class Readme
  include LuckyTemplate::Fileable

  def initialize(@name : String)
  end

  def to_file(io : IO) : Nil
    to_s(io)
  end

  ECR.def_to_s "README.md.ecr"
end

name = "John"
folder = LuckyTemplate.write!(Path["."]) do |dir|
  dir.add_file("README.md", Readme.new(name))

  dir.add_file("Welcome.md") do |io|
    io << "# Welcome " << name << "!\n"
  end

  dir.add_file("LICENSE", <<-LICENSE)
  The MIT License (MIT)
  ...
  LICENSE

  dir.add_folder("src") do |src|
    src.add_file(".keep")

    src.add_folder(name.downcase) do |name_dir|
      name_dir.add_file("#{name.downcase}.cr", <<-CR)
      class #{name}
      end

      pp! #{name}.new
      CR
    end
  end
end

Creating Multiple Folders

You can also create multiple folders at once or nest them within other folders:

folder1 = LuckyTemplate.create_folder do |dir|
  dir.add_file("folder_one.txt")
end
folder2 = LuckyTemplate.create_folder do |dir|
  dir.add_file("folder_two.txt")
end
folder3 = LuckyTemplate.create_folder do |dir|
  dir.add_file("folder_three.txt")
  dir.insert_folder("folder1", folder1)
  dir.insert_folder("folder2", folder2)
end
LuckyTemplate.write!(Path["."], folder3)

Snapshot Folders

Another great feature is that you can take snapshots of folder structures:

snapshot = LuckyTemplate.snapshot(folder3)

snapshot.each do |path, type|
  case type
  in .file?
    puts "File: #{path}"
  in .folder?
    puts "Folder: #{path}"
  end
end

Using Non-Crystal Templates

If you prefer to use another template engine besides Crystal's, you can do so by creating a running process, as shown in the following example with gettext envsubst:

class ExternalTemplate
  include LuckyTemplate::Fileable

  def initialize(@name : String)
  end

  def to_file(io : IO) : Nil
    input = IO::Memory.new("Hello $NAME!")
    Process.run(
      command: "envsubst",
      input: input,
      output: io,
      env: {
        "NAME" => @name,
      }
    )
  end
end

LuckyTemplate.write!(Path["."]) do |dir|
  dir.add_file("external_file", ExternalTemplate.new("John"))
end

FAQ

Contributing

  1. Fork it (https://github.com/luckyframework/lucky_template/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors