Writing Shards

How to write and release Crystal Shards.

What’s a Shard?

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.

For those who don’t know, a palindrome is a word which is spelled the same way forwards as it is backwards. e.g. racecar, mom, dad, kayak, madam

Requirements

In order to release a Crystal Shard, and follow along with this tutorial, you will need the following:

Creating the Project

Begin by using the Crystal compiler‘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.

  1. $ crystal init lib palindrome-example
  2. create palindrome-example/.gitignore
  3. create palindrome-example/.editorconfig
  4. create palindrome-example/LICENSE
  5. create palindrome-example/README.md
  6. create palindrome-example/shard.yml
  7. create palindrome-example/src/palindrome-example.cr
  8. create palindrome-example/spec/spec_helper.cr
  9. create palindrome-example/spec/palindrome-example_spec.cr
  10. Initialized empty Git repository in /<YOUR-DIRECTORY>/.../palindrome-example/.git/

…and cd into the directory:

e.g.

  1. cd palindrome-example

Then add & commit to start tracking the files with Git:

  1. $ git add -A
  2. $ git commit -am "First Commit"
  3. [master (root-commit) 77bad84] First Commit
  4. 8 files changed, 104 insertions(+)
  5. create mode 100644 .editorconfig
  6. create mode 100644 .gitignore
  7. create mode 100644 LICENSE
  8. create mode 100644 README.md
  9. create mode 100644 shard.yml
  10. create mode 100644 spec/palindrome-example_spec.cr
  11. create mode 100644 spec/spec_helper.cr
  12. create mode 100644 src/palindrome-example.cr

Writing the Code

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 a built-in testing library. Use it!

Documentation

Run crystal docs to convert your code and comments into interlinking API documentation. Open the files in the /docs/ 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)

  1. [![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. Awesome README is a nice curation of examples and resources on the topic.

Most importantly, your README should explain:

  1. What your library is
  2. What it does
  3. 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

e.g.

  1. crystal tool format

To check if your code is formatted correctly, or to check if using the formatter wouldn’t produce any changes, simply add --check to the end of this command.

e.g.

  1. crystal tool format --check

This check is good to add as a step in continuous integration.

Writing a shard.yml

The spec is your rulebook. Follow it.

Name

Your shard.yml‘s name property should be concise and descriptive.

  • Search any of the available shard databases to check if your name is already taken.

e.g.

  1. name: palindrome-example

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:

  1. Informative
  2. 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 Crystal Community page.

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.

Our name is already descriptive enough for Bob’s search of “palindrome”. We don’t need to repeat the palindrome keyword. Instead, we’ll catch Susan’s search for “spelling” and Felipe’s search for “text”.

  1. description: |
  2. 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!