Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crystal provider #138

Merged
merged 6 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/crystal/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
5 changes: 5 additions & 0 deletions examples/crystal/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf
27 changes: 27 additions & 0 deletions examples/crystal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# crystal

TODO: Write a description here

## Installation

TODO: Write installation instructions here

## Usage

TODO: Write usage instructions here

## Development

TODO: Write development instructions here

## Contributing

1. Fork it (<https://github.com/your-github-user/crystal/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Jake Runzer](https://github.com/your-github-user) - creator and maintainer
2 changes: 2 additions & 0 deletions examples/crystal/shard.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 2.0
shards: {}
13 changes: 13 additions & 0 deletions examples/crystal/shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: crystal
version: 0.1.0

authors:
- Jake Runzer <jakerunzer@gmail.com>

targets:
crystal:
main: src/crystal.cr

crystal: 1.4.1

license: MIT
9 changes: 9 additions & 0 deletions examples/crystal/spec/crystal_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "./spec_helper"

describe Crystal do
# TODO: Write tests

it "works" do
false.should eq(true)
end
end
2 changes: 2 additions & 0 deletions examples/crystal/spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/crystal"
4 changes: 4 additions & 0 deletions examples/crystal/src/crystal.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TODO: Write documentation for `Crystal`
module Crystal
puts "Hello from Crystal!"
end
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::{
AppBuilderOptions,
},
providers::{
deno::DenoProvider, go::GolangProvider, haskell::HaskellStackProvider, node::NodeProvider,
python::PythonProvider, rust::RustProvider,
crystal::CrystalProvider, deno::DenoProvider, go::GolangProvider,
haskell::HaskellStackProvider, node::NodeProvider, python::PythonProvider,
rust::RustProvider,
},
};
use anyhow::{bail, Result};
Expand All @@ -25,6 +26,7 @@ pub fn get_providers() -> Vec<&'static dyn Provider> {
&RustProvider {},
&PythonProvider {},
&HaskellStackProvider {},
&CrystalProvider {},
]
}

Expand Down
65 changes: 65 additions & 0 deletions src/providers/crystal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::collections::HashMap;

use super::Provider;
use crate::nixpacks::{
app::App,
environment::Environment,
nix::Pkg,
phase::{BuildPhase, InstallPhase, SetupPhase, StartPhase},
};
use anyhow::{Context, Result};
use serde::Deserialize;

// https://github.com/crystal-lang/shards/blob/master/docs/shard.yml.adoc
#[derive(Deserialize, Debug)]
pub struct ShardYaml {
pub name: String,
pub targets: HashMap<String, HashMap<String, String>>,
}

pub struct CrystalProvider {}

impl Provider for CrystalProvider {
fn name(&self) -> &str {
"crystal"
}

fn detect(&self, app: &App, _env: &Environment) -> Result<bool> {
Ok(app.includes_file("shard.yml"))
}

fn setup(&self, _app: &App, _env: &Environment) -> Result<Option<SetupPhase>> {
Ok(Some(SetupPhase::new(vec![
Pkg::new("crystal"),
Pkg::new("shards"),
])))
}

fn install(&self, _app: &App, _env: &Environment) -> Result<Option<InstallPhase>> {
Ok(Some(InstallPhase::new("shards install".to_string())))
}

fn build(&self, _app: &App, _env: &Environment) -> Result<Option<BuildPhase>> {
Ok(Some(BuildPhase::new("shards build --release".to_string())))
}

fn start(&self, app: &App, _env: &Environment) -> Result<Option<StartPhase>> {
let config = CrystalProvider::get_config(app)?;
let target_names = config.targets.keys().cloned().collect::<Vec<_>>();
let start_phase = StartPhase::new(format!(
"./bin/{}",
target_names
.get(0)
.ok_or_else(|| anyhow::anyhow!("Unable to get executable name"))?
));

Ok(Some(start_phase))
}
}

impl CrystalProvider {
fn get_config(app: &App) -> Result<ShardYaml> {
app.read_yaml::<ShardYaml>("shard.yml")
.context("Reading shard.yml")
}
}
1 change: 1 addition & 0 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::nixpacks::{
};
use anyhow::Result;

pub mod crystal;
pub mod deno;
pub mod go;
pub mod haskell;
Expand Down
7 changes: 7 additions & 0 deletions tests/docker_run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ fn test_haskell_stack() {
assert!(output.contains("Hello from Haskell"));
}

#[test]
fn test_crystal() {
let name = simple_build("./examples/crystal");
let output = run_image(name);
assert!(output.contains("Hello from Crystal"));
}

#[test]
fn test_cowsay() {
let name = Uuid::new_v4().to_string();
Expand Down
22 changes: 22 additions & 0 deletions tests/generate_plan_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,28 @@ fn test_haskell_stack() -> Result<()> {
Ok(())
}

#[test]
fn test_crystal() -> Result<()> {
let plan = gen_plan(
"./examples/crystal",
Vec::new(),
None,
None,
Vec::new(),
false,
)?;
assert_eq!(
plan.install.unwrap().cmd,
Some("shards install".to_string())
);
assert_eq!(
plan.build.unwrap().cmd,
Some("shards build --release".to_string())
);
assert_eq!(plan.start.unwrap().cmd, Some("./bin/crystal".to_string()));
Ok(())
}

#[test]
fn test_overriding_environment_variables() -> Result<()> {
let plan = gen_plan(
Expand Down