Writing Shards
Simply put, a Shard is a package of Crystal code, made to be shared-with and used-by other projects.
See the Shards command for details.
Introduction
In this tutorial, we’ll be making a Crystal library called palindrome-example.
In order to release a Crystal Shard, and follow along with this tutorial, you will need the following:
- A working installation of the Crystal compiler
- A working installation of
- A GitHub or account
Creating the Project
Begin by using ‘s init lib
command to create a Crystal library with the standard directory structure.
In your terminal: crystal init lib <YOUR-SHARD-NAME>
e.g.
…and cd
into the directory:
e.g.
cd palindrome-example
The code you write is up to you, but how you write it impacts whether people want to use your library and/or help you maintain it.
Testing the Code
- Test your code. All of it. It’s the only way for anyone, including you, to know if it works.
- Crystal has . Use it!
Documentation
- Document your code with comments. All of it. Even the private methods.
- Crystal has . Use it!
Run crystal docs
to convert your code and comments into interlinking API documentation. Open the files in the directory with a web browser to see how your documentation is looking along the way.
See below for instructions on hosting your compiler-generated docs on GitHub/GitLab Pages.
Once your documentation is ready and available, you can add a documentation badge to your repository so users know that it exists. In GitLab this badge belongs to the project so we’ll cover it in the GitLab instructions below, for GitHub it is common to place it below the description in your README.md like so: (Be sure to replace <LINK-TO-YOUR-DOCUMENTATION>
accordingly)
[![Docs](https://img.shields.io/badge/docs-available-brightgreen.svg)](<LINK-TO-YOUR-DOCUMENTATION>)
Writing a README
A good README can make or break your project. is a nice curation of examples and resources on the topic.
Most importantly, your README should explain:
- What it does
- How to use it
This explanation should include a few examples along with subheadings.
Note
Be sure to replace all instances of [your-github-name]
in the Crystal-generated README template with your GitHub/GitLab username. If you’re using GitLab, you’ll also want to change all instances of github
with gitlab
.
Coding Style
- It’s fine to have your own style, but sticking to can help keep your code consistent, readable and usable for other developers.
- Utilize Crystal’s built-in code formatter to automatically format all
.cr
files in a directory.
e.g.
e.g.
This check is good to add as a step in .
The spec is your rulebook. Follow it.
Name
Your shard.yml
‘s name
property should be concise and descriptive.
- Search crystalshards.xyz to check if your name is already taken.
e.g.
Description
Add a description
to your shard.yml
.
A description
is a single line description used to search for and find your shard.
A description should be:
- Informative
- Discoverable
Optimizing
It’s hard for anyone to use your project if they can’t find it. There are several services for discovering shards, a list is available on the .
There are people looking for the exact functionality of our library and the general functionality of our library. e.g. Bob needs a palindrome library, but Felipe is just looking for libraries involving text and Susan is looking for libraries involving spelling.
A textual algorithm to tell if a word is spelled the same way forwards as it is backwards.
Hosting
From here the guide differs depending on whether you are hosting your repo on GitHub or GitLab. If you’re hosting somewhere else, please feel free to write up a guide and add it to this book!