From fe108563faf58be81a5efae3d89e8784c9a1dd32 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 30 Jun 2023 16:14:21 -0300 Subject: [PATCH] Add "server" rake task --- Rakefile.rb | 17 ++++++++++------- packages/ruby/server/lib/server.rb | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Rakefile.rb b/Rakefile.rb index 84b8c51..bb86b6f 100644 --- a/Rakefile.rb +++ b/Rakefile.rb @@ -2,6 +2,9 @@ require "bundler/setup" require "ryo" +require "yaml" + +build_dir = Ryo.from(YAML.load_file("./nanoc.yaml")).output_dir namespace :nanoc do desc "Compile the website" @@ -12,8 +15,6 @@ namespace :nanoc do desc "Delete the build directory" task :clean do - require "yaml" - build_dir = Ryo.from(YAML.load_file("./nanoc.yaml")).output_dir sh "rm -rf #{build_dir}" end end @@ -24,11 +25,13 @@ task build: "nanoc:compile" desc "Clean the build directory" task clean: "nanoc:clean" -desc "Start a Ruby web server on localhost" -task server: ["nanoc:compile"] do - Dir.chdir(File.join(Dir.getwd, "build", "al-quran")) do - sh "bundle exec adsf" - end +desc "Serve the website on localhost" +task :server do + require "server" + s = Server.for_dir(build_dir) + s.start(block: true) +rescue Interrupt + s.stop end namespace :watch do diff --git a/packages/ruby/server/lib/server.rb b/packages/ruby/server/lib/server.rb index d97bd64..812ad18 100644 --- a/packages/ruby/server/lib/server.rb +++ b/packages/ruby/server/lib/server.rb @@ -23,9 +23,10 @@ class Server @server = Puma::Server.new(@app, @events, @options) end - def start + def start(block: false) @server.binder.parse(@options[:binds]) - @server.run + thr = @server.run + block ? thr.join : thr end def stop