-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#131 - Add 'play' management command
- Loading branch information
Showing
5 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require "./spec_helper" | ||
|
||
describe Marten::CLI::Manage::Command::Play do | ||
describe "#run" do | ||
it "starts the Crystal playground as expected when no host/port are provided" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
command = Marten::CLI::Manage::Command::Play.new( | ||
options: ["--no-open"], | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
spawn { command.handle } | ||
|
||
sleep 1 | ||
|
||
command.playground_process.try(&.terminate) | ||
|
||
stdout.rewind.gets_to_end.includes?("Listening on http://127.0.0.1:8080").should be_true | ||
end | ||
|
||
it "starts the Crystal playground as expected when a host is provided" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
command = Marten::CLI::Manage::Command::Play.new( | ||
options: ["--no-open", "--bind", "localhost"], | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
spawn { command.handle } | ||
|
||
sleep 1 | ||
|
||
command.playground_process.try(&.terminate) | ||
|
||
stdout.rewind.gets_to_end.includes?("Listening on http://[::1]:8080").should be_true | ||
end | ||
|
||
it "starts the Crystal playground as expected when a port is provided" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
command = Marten::CLI::Manage::Command::Play.new( | ||
options: ["--no-open", "--port", "3000"], | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
spawn { command.handle } | ||
|
||
sleep 1 | ||
|
||
command.playground_process.try(&.terminate) | ||
|
||
stdout.rewind.gets_to_end.includes?("Listening on http://127.0.0.1:3000").should be_true | ||
end | ||
|
||
it "starts the Crystal playground as expected when a host and port are provided" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
command = Marten::CLI::Manage::Command::Play.new( | ||
options: ["--no-open", "--bind", "localhost", "--port", "3000"], | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
spawn { command.handle } | ||
|
||
sleep 1 | ||
|
||
command.playground_process.try(&.terminate) | ||
|
||
stdout.rewind.gets_to_end.includes?("Listening on http://[::1]:3000").should be_true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
module Marten | ||
module CLI | ||
class Manage | ||
module Command | ||
class Play < Base | ||
@host : String? | ||
@open : Bool = true | ||
@playground_process : Process? | ||
@port : Int32? | ||
|
||
# :nodoc: | ||
getter playground_process | ||
|
||
help "Start a Crystal playground server initialized for the current project." | ||
|
||
def setup | ||
on_option_with_arg( | ||
:b, | ||
:bind, | ||
arg: "host", | ||
description: "Binds the playground to the specified IP" | ||
) do |v| | ||
@host = v | ||
end | ||
|
||
on_option_with_arg( | ||
:p, | ||
:port, | ||
arg: "port", | ||
description: "Runs the playground on the specified port" | ||
) do |v| | ||
@port = v.to_i | ||
end | ||
|
||
on_option(:"no-open", description: "Do not open the playground in the default browser") do | ||
@open = false | ||
end | ||
end | ||
|
||
def run | ||
write_playground_source | ||
@playground_process = Process.new(play_command, shell: true, output: stdout, error: stderr) | ||
Process.run(open_command, shell: true) if open? | ||
@playground_process.try(&.wait) | ||
end | ||
|
||
private PLAYGROUND_SOURCE_CONTENT = <<-CRYSTAL | ||
require "./src/project" | ||
# Setup the project. | ||
Marten.setup | ||
# Write your code here. | ||
CRYSTAL | ||
|
||
private PLAYGROUND_SOURCE_PATH = "tmp/project_playground.cr" | ||
|
||
private getter host | ||
private getter port | ||
|
||
private getter? open | ||
|
||
private def open_command | ||
# Identify which 'open' command to use based on the OS based on flags | ||
url = "http://#{host || "localhost"}:#{port || 8080}" | ||
|
||
open_command = "" | ||
{% if flag?(:linux) %} | ||
open_command = "xdg-open #{url}" # Linux | ||
{% elsif flag?(:win32) || flag?(:win64) %} | ||
open_command = "start #{url}" # Windows | ||
{% else %} | ||
open_command = "open #{url}" # macOS | ||
{% end %} | ||
end | ||
|
||
private def play_command | ||
command = String.build do |s| | ||
s << "crystal play" | ||
s << " --binding #{host}" if host | ||
s << " --port #{port}" if port | ||
s << " #{PLAYGROUND_SOURCE_PATH}" | ||
end | ||
end | ||
|
||
private def write_playground_source | ||
File.write(PLAYGROUND_SOURCE_PATH, PLAYGROUND_SOURCE_CONTENT) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |