Skip to content

assemble files in assets folder into executable binary use `backed_file_system` at compile time, then mount it to new file system folder at runtime.

License

Notifications You must be signed in to change notification settings

crystal-china/baked_file_system_mounter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

baked_file_system_mounter

assemble files inside current directories into executable binary use backed_file_system at compile time, then mount it on new file system at runtime.

Let us assume there are assets folders like this:

PROJECT_ROOT/
  src/assets/
  └── materialize
	  ├── css
	  │   └── materialize.min.css
	  └── js
		  └── materialize.min.js

What we want is:

  1. Assemble those assets file into binary when build.

  2. When copying binary into /public directory (on target host), running it will extract assets, like this:

my_app*
public/
└── materialize
    ├── css
    │   └── materialize.min.css
    └── js
        └── materialize.min.js
3 directories, 2 files

With following configuration.

require "baked_file_system_mounter"

BakedFileSystemMounter.assemble(
  {
    "src/assets" => "public",
  }
)

{% if flag?(:release) %}
  BakedFileSystemStorage.mount
{% end %}

Then, use can use those assets both in development(src/assets/materialize) and production(public/materialize), like this:

<html>
  <head>
    <link rel="stylesheet" href="/materialize/css/materialize.min.css" />
  </head>
  <body>
      <%= yield_content "footer" %>
  </body>
</html>

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      baked_file_system_mounter:
        github: crystal-china/baked_file_system_mounter
  2. Run shards install

Usage

You can passing a Hash as argument for mapping.

require "baked_file_system_mounter"

#
# we assemble all files in `src/assets`,`db` into executable binary when we build,
BakedFileSystemMounter.assemble(
  {
    "src/assets" => "public",
    "db" => "db"
  }
)

if APP_ENV == "production"
  # we assemble the db into db folder too
  # Then mount files in `src/assets` into `public` and files in `db` into `db`.
  # folder will be created it if not exists.
  BakedFileSystemStorage.mount
end

You can pass a Array as argument too.

BakedFileSystemMounter.assemble(["public", "db"])

# It's same as:

# BakedFileSystemMounter.assemble(
#   {
#     "public" => "public",
#     "db" => "db"
#   }
# )

if APP_ENV == "production" 
  BakedFileSystemStorage.mount
end

It can be used to mount assets outside current directory, e.g. /tmp

BakedFileSystemMounter.assemble(
    {
      "sounds" => "/tmp/sounds",
    }
)

if APP_ENV == "production" 
  BakedFileSystemStorage.mount
end

Development

TODO: Write development instructions here

Contributing

  1. Fork it (https://github.com/crystal-china/baked_file_system_mounter/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

About

assemble files in assets folder into executable binary use `backed_file_system` at compile time, then mount it to new file system folder at runtime.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published