Chapter 4: Creating a new project
Up until now we’ve used the crystal command to only run our code.
Actually crystal command is pretty useful and does lot more than that. (check crystal --helpfor more)
For example we can use it to create a new Crystal project.
1
$ crystal init app sample
2
create sample/.gitignore
3
create sample/LICENSE
4
create sample/README.md
5
create sample/.travis.yml
6
create sample/shard.yml
7
create sample/src/sample.cr
8
create sample/src/sample/version.cr
9
create sample/spec/spec_helper.cr
10
create sample/spec/sample_spec.cr
11
Initialized empty Git repository in /Users/serdar/crystal_for_rubyists/code/04/sample/.git/
Copied!
Awesome. crystal helped us create a new project. Let’s see what it did for us.
    Created a new folder named sample
    Created a LICENSE
    Created .travis.yml to easily integrate Travis for continous integration.
    Created shard.yml for dependency management.
    Initialized an empty Git repository
    Created a README for our project
    Created src and spec folders to put our code and tests(ssh..we’ll talk about it soon) in it.
Let’s run it.
1
$ cd sample
2
$ crystal src/sample.cr
Copied!
Nothing! Yay :)
Now that we create our first project. Let’s use some external libraries.

Using Shards for dependency management

To manage dependencies of a project we use shards. shards is like bundler and shard.yml is like Gemfile.
Let’s open up shard.yml.
1
name: sample
2
version: 0.1.0
3
4
authors:
5
- sdogruyol <[email protected]>
6
7
license: MIT
Copied!
This is a default shard.yml and it contains the minimal necessary information about our project. Those are
    name specifies the name of the project
    version specifies the version of the project. Crystal itself uses semver for version management so it’s a good convention for you to follow.
    authors section specifies the authors of the project. By default this is taken from your global git configuration.
    license specifies the type of your project license. By default this is MIT.
Okay. That’s great but what can we do with this shard.yml? Well we can use this file to add external libraries(we call it dependency) and manage them without even worrying about any folders / paths e.g.. Sweet isn’t it?
Now that we know the true power of shards let’s add Kemal to our shard.yml and build a simple web application :)
Open up shard.yml. First we need to add Kemal as a dependency to our project. We do this by including
1
dependencies:
2
kemal:
3
github: sdogruyol/kemal
4
version: 0.14.1
Copied!
That’s great! Now we added Kemal to our project. First, we need to install it.
1
$ shards install
2
Updating https://github.com/sdogruyol/kemal.git
3
Updating https://github.com/luislavena/radix.git
4
Updating https://github.com/jeromegn/kilt.git
5
Installing kemal (0.14.1)
6
Installing radix (0.3.0)
7
Installing kilt (0.3.3)
Copied!
Okay now we are ready to use Kemal in our project. Open up src/sample.cr
1
require "./sample/*"
2
require "kemal"
3
4
module Sample
5
6
get "/" do
7
"Hello World!"
8
end
9
10
end
11
12
Kemal.run
Copied!
Look how we used require to access Kemal in our program.
Let’s run.
1
$ crystal src/sample.cr
2
[development] Kemal is ready to lead at http://0.0.0.0:3000
Copied!
Go to localhost:3000 and see it in action!
Now you know how to add dependencies and use others’ shards :)
Last modified 1yr ago