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

Code reloading #335

Closed
veelenga opened this Issue Mar 17, 2017 · 11 comments

Comments

Projects
None yet
8 participants
@veelenga
Copy link
Contributor

veelenga commented Mar 17, 2017

Currently to see code changes we need to restart a web server. Are there plans/ideas regarding code reloading, so we would need just to reload a page, but do not restart a server ? Is it even possible to do for now ?

PS: thanks for a great work on Kemal!

@sdogruyol

This comment has been minimized.

Copy link
Member

sdogruyol commented Mar 17, 2017

@veelenga since ecr is compiled i don't think there's a way to update the view without restarting the process 😢

@sdogruyol sdogruyol added the question label Mar 17, 2017

@flagello

This comment has been minimized.

Copy link

flagello commented Mar 22, 2017

sentry by @samueleaton might be of help — it does restart the process, but it's something.

@faustinoaq

This comment has been minimized.

Copy link
Contributor

faustinoaq commented Mar 24, 2017

Hello!

I'm playing with Kemal, WebSockets and Sentry.
I can't do code reloading with ECR. I think maybe using as dynamic library.

Instead of ECR I'm using client side rendering (exist a lot of options EJS, Handlebears etc). EJS has similar syntax to ECR.

This could be good only for fast development environment, when turn on production the views can be converted to plain HTML and then compiled to ECR (if is a static site). If is a dynamic one then client rendering is the way.

Basically I'm using File.read and passing data as json because each template recompiling is so heavy even using Sentry.

see: A example using EJS

@faustinoaq

This comment has been minimized.

Copy link
Contributor

faustinoaq commented Mar 26, 2017

@veelenga Seems that Liquid.cr and Crustache are engines that support template render without recompile.

However, Kilt doesn't support Liquid yet and the Crustache engine inside Kilt use Mustache.embed instead of Crustache.render

@huacnlee

This comment has been minimized.

Copy link

huacnlee commented Mar 28, 2017

https://github.com/samueleaton/sentry

src/sentry.cr

require "sentry"

sentry = Sentry::ProcessRunner.new(
  process_name: "Kemal Dev",
  build_command: "crystal build ./src/my_app.cr -o ./bin/my_app",
  run_command: "./bin/my_app",
  files: ["./src/**/*.cr", "./src/**/*.ecr"]
)
sentry.run

after run dev by:

$ crystal run src/sentry.cr

🤖  Your SentryBot is vigilant. beep-boop...
🤖  watching file: ./src/my_app/topics.cr
🤖  watching file: ./src/my_app.cr
🤖  compiling my_app...
🤖  starting my_app...
[development] Kemal is ready to lead at http://0.0.0.0:3000
@faustinoaq

This comment has been minimized.

Copy link
Contributor

faustinoaq commented Mar 28, 2017

@huacnlee good 👍

I created a shard to use sentry embedded in my code without external sentry.cr:

require "kemal"
require "sentry-run"

get "/" do
  "Hello world"
end

Sentry.config(files: ["src/app.cr"])

Sentry.run do
  Kemal.run
end

https://github.com/faustinoaq/sentry-run

@samueleaton

This comment has been minimized.

Copy link
Contributor

samueleaton commented Mar 28, 2017

Sorry I was on hiatus for the last month or so.

I'm thinking of adding the ability to add a hot-reloading client side build (for webpack or Gulp or whatever) in Sentry. Something similar to what webpack-dev-server or the phoenix framework (Elixir) do. But the trick is to keep sentry light and simple to use without too much feature bloat.

If you think of anything you'd like to add in sentry or any shortcomings, feel free to create an issue.

EDIT: I made a suggestion (as you can see below in the referenced issue) for a way we can add our own tooling into Kemal. This would allow us to inject things into the html to achieve features like webpack-dev-server for hot reloading.

@huacnlee

This comment has been minimized.

Copy link

huacnlee commented Mar 29, 2017

@faustinoaq How about integrated that into Kemal by using Compile-time flags?

For example:

module Kemal
  # Overload of self.run with the default startup logging
  def self.run(port = nil)
    {% if flag?(:release) %}
    kemal_run(port)
    {% else %}
    Sentry.run do
      kemal_run(port)
    end
    {% end %}
  end

  def self.kemal_run(port = nil)
    # original Kemal run implement
  end
end
@faustinoaq

This comment has been minimized.

Copy link
Contributor

faustinoaq commented Mar 29, 2017

@huacnlee sound great!

In fact The fist version of sentry-run was named kemal-sentry 😄 but I changed because I think that Sentry could be used in many other crystal programs, not only Kemal.

@sdogruyol sdogruyol closed this May 8, 2017

@kazzkiq

This comment has been minimized.

Copy link

kazzkiq commented Aug 3, 2017

If you have Node.js installed (not uncommon even for backend devs nowadays) you can simply run the instructions bellow (inside your project root directory, of course):

  1. npm i -g nodemon
  2. nodemon --exec "crystal run" src/{YOUR_MAIN_FILE}.cr

Where {YOUR_MAIN_FILE} is the filename you use to run your app.

Now every time you make a change in any file inside your project, nodemon will automatically detect and re-build your project. Works like a charm with Kemal.

@karmakaze

This comment has been minimized.

Copy link

karmakaze commented Aug 7, 2018

Putting the following in your package.json let's you run npm start or npm build:

{
  ...
  "scripts": {
        "dev": "node_modules/.bin/nodemon --exec \"crystal run\" src/{YOUR_MAIN_FILE}.cr",
        "start": "npm run dev",
        "build": "crystal build src/{YOUR_MAIN_FILE}.cr"
  },
  "dependencies": {
        "nodemon": "^1.18.3"
  },
  "nodemonConfig": {
        "ignore": ["node_modules/*", "lib/*"]
  }
  ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.