As of version 0.6.0, the method BakedFileSystemStorage.mount
has been renamed
to BakedFileSystemMounter::Storage.mount
.
The baked_file_system_mounter allows you to assemble files in the a directory (current working directory by default) into binary executable at compile time (using baked_file_system) and mount the files back into a new file system at runtime.
Imagine you have an assets folder structured like this:
PROJECT_ROOT/
src/assets/
└── materialize
├── css
│ └── materialize.min.css
└── js
└── materialize.min.js
If you want to:
- Compile the files in the
src/assets
folder into a binary during the build process. - At runtime, extract the assets from the binary and make them available in the
directory
/foo
on the target host.
After running, your directory structure will look like this:
/foo/
public/
└── materialize
├── css
│ └── materialize.min.css
└── js
└── materialize.min.js
3 directories, 2 files
This can be achieved with the following configuration:
require "baked_file_system_mounter"
BakedFileSystemMounter.assemble(
{
"src/assets" => "public",
}
)
{% if flag?(:release) %}
BakedFileSystemMounter::Storage.mount
{% end %}
Now you can access the assets in both 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>
-
Add the dependency to your
shard.yml
:dependencies: baked_file_system_mounter: github: crystal-china/baked_file_system_mounter
-
Run
shards install
require "baked_file_system_mounter"
#
# Assemble files from `src/assets` and `db` into the executable binary during build
BakedFileSystemMounter.assemble(
{
"src/assets" => "public",
"db" => "db"
}
)
if APP_ENV == "production"
# Mount the contents during runtime
# Files from `src/assets` will appear in `public`
# Files from `db` will appear in `db`
BakedFileSystemMounter::Storage.mount
end
You can also pass an Array as an argument for default mappings. In this case, the current working directory (PWD) is used as the base directory.
BakedFileSystemMounter.assemble(["public", "db"])
# It's same as:
# BakedFileSystemMounter.assemble(
# {
# "public" => "public",
# "db" => "db"
# }
# )
if APP_ENV == "production"
BakedFileSystemMounter::Storage.mount
end
You can even mount assets outside the current working directory.
For example, /tmp:
BakedFileSystemMounter.assemble(
{
"sounds" => "/tmp/sounds",
}
)
if APP_ENV == "production"
BakedFileSystemMounter::Storage.mount
end
TODO: Write development instructions here
- Fork it (https://github.com/crystal-china/baked_file_system_mounter/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Billy.Zheng - creator and maintainer