The Crystal Programming Language Forum

Set up shards

How do I set up a shard without a framework? I get that I’m supposed to have YAML files, but I have no idea what goes inside them.

If by setting up shard you mean installing it then you can just copy-paste required lines from sites like https://shardbox.org/

Let’s say you want to install Kemal. At kemal@1.1.0 on Shardbox you can click copy button on the right which would copy this to clipboard:

kemal:
  github: kemalcr/kemal
  version: ~> 1.1.0

And then you paste it into your shard.yml file. For full documentation about shard.yml format see shards/shard.yml.adoc at master · crystal-lang/shards · GitHub

If by setting up a shard you mean creating your own shard than Crystal has built-in command that would generate some files for your new shard:

crystal init lib my_shard

If you want to start building application with Crystal use init app instead of init lib command:

crystal init app my_app

There are not many differences in generated files.

The init lib command would add shard.lock to .gitignore file since you typically would lock shard versions in application that uses it and not in shard itself.

The init app command adds targets section to the shard.yml file so that you can use shard build command to build your app.

There are some difference in the generated README.md file as well. You can check the full difference between generated my_shard and my_app folders with:

git diff --no-index my_shard my_app
1 Like

OK, this makes a lot more sense

How do I call a shard from my program? do I just do require? And how do I open an app to test?

Yes. There is nothing special about installed shards. When you do shards install it just installs them inside lib directory in your project. You can put some code there manually and not use shards install - the effect would be the same.

Then you can just require "some_crystal_file".

You can read where and how Crystal searches for files to require in the documentation Requiring files - Crystal

Hey @BrettKnoss, I made this video on the topic a few months ago - it touches on depending on your own shards, too. I think you might find it useful

1 Like