Crystal: Change `crystal init app` template to exclude "put your code inside module"?

Created on 20 Jul 2018  路  4Comments  路  Source: crystal-lang/crystal

The default crystal init app foo template generates something like this:

require "./foo/*"

module Foo
  # TODO: put your code here
end

That's probably OK for a library, but for an app there's no need to put the main code inside the module. Well, you can do that, but it's not common, and then some people are confused by this (they might want to define methods inside the module and call them, but that doesn't work (you have to use def self.), or you might wonder why you have to put that code inside that module).

I propose we remove that module Foo declaration in that file for apps, and simply generate something like:

require "./foo/*"

# TODO: put your app code here
feature discussion tools

Most helpful comment

@wontruefree Yes, it can be this:

# TODO: Write documentation for `Foo`
module Foo
  VERSION = "0.1.0"
end

# TODO: Put your code here

Or simply:

# TODO: Put your code here

There's no need for a VERSION constant for an app... in fact, there's not even a need for a VERSION constant for a lib, not sure why we have that.

All 4 comments

I prefer to put app code in a module namespace as well, and only have a MyApp.run(ARGV) in the top level. This could also be provided by the template. But it doesn't really matter that much what the template produces, so I'm fine with this change.

The main project file now contains version after it was moved in Crystal#6317

This is what I get from a crystal init now

# TODO: Write documentation for `Foo`
module Foo
  VERSION = "0.1.0"

  # TODO: Put your code here
end

So the version constant has to go somewhere.

@wontruefree Yes, it can be this:

# TODO: Write documentation for `Foo`
module Foo
  VERSION = "0.1.0"
end

# TODO: Put your code here

Or simply:

# TODO: Put your code here

There's no need for a VERSION constant for an app... in fact, there's not even a need for a VERSION constant for a lib, not sure why we have that.

So I like what you are saying I would also say specs depend on the module sapce.

require "./spec_helper"

describe Foo do
  # TODO: Write tests

  it "works" do
    false.should eq(true)
  end
end

I like the idea of crystal init being very basic with flags to configure the setup. So the default generates shards, src, editorconfig, and gitignore. But you can build a project with flags for a test suite, shard dependencies, and CI.

crystal init app --test-suite=bacon --ci=travis
Was this page helpful?
0 / 5 - 0 ratings