Skip to content

Add shards info command #324

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ uninstall: phony
test: test_unit test_integration

test_unit: phony
$(CRYSTAL) spec ./spec/unit/*_spec.cr
$(CRYSTAL) spec ./spec/unit/

test_integration: bin/shards phony
$(CRYSTAL) spec ./spec/integration/*_spec.cr
$(CRYSTAL) spec ./spec/integration/

phony:
22 changes: 22 additions & 0 deletions man/shards.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ dependencies are satisfied,
dependencies aren't satisfied.
.RE
.PP
\fBinfo [<command>]\fR
.RS 4
Displays information about a shard.
.SS
.RS 4
Commands:
.PP
.TP 3
\fB--name\fR
Print the name of the shard.
.TP 3
\fB--version\fR
Print the version in `spec.yml`.
.TP 3
\fB-h, --help\fR
Print usage synopsis.
.RE
.PP
.RS 4
If no command is given, a summary including name and version is printed.
.RE
.PP
\fBinit\fR
.RS 4
Initializes a default \fIshard.yml\fR in the current folder.
Expand Down
34 changes: 34 additions & 0 deletions spec/integration/info_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "./spec_helper"

describe "shards info" do
it "reports name" do
Dir.cd(application_path) do
with_shard({name: "foo"}) do
output = run "shards info --name"
output.should eq "foo\n"
end
end
end

it "reports version" do
Dir.cd(application_path) do
with_shard({version: "1.2.3"}) do
output = run "shards info --version"
output.should eq "1.2.3\n"
end
end
end

it "reports info" do
Dir.cd(application_path) do
with_shard({name: "foo", version: "1.2.3"}) do
output = run "shards info"
output.should eq <<-OUT
name: foo
version: 1.2.3

OUT
end
end
end
end
2 changes: 2 additions & 0 deletions spec/support/cli.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "./factories"

Spec.before_each do
path = application_path

Expand Down
2 changes: 1 addition & 1 deletion spec/support/factories.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class FailedCommand < Exception
getter stderr : String

def initialize(message, @stdout, @stderr)
super message
super "#{message}: #{stderr}"
end
end

Expand Down
24 changes: 24 additions & 0 deletions spec/unit/commands/info_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "../../../src/commands/info"
require "../spec_helper"
require "../../support/cli"

private def capture(command, *args)
String.build do |io|
command.run(args.to_a, stdout: io)
end.chomp
end

describe Shards::Commands::Info do
it "reports name" do
with_shard({name: "foo", version: "1.2.3"}) do
info = Shards::Commands::Info.new(application_path)

capture(info, "--name").should eq "foo"
capture(info, "--version").should eq "1.2.3"
capture(info, "").should eq <<-OUT
name: foo
version: 1.2.3
OUT
end
end
end
27 changes: 17 additions & 10 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ require "option_parser"
require "./commands/*"

module Shards
def self.display_help_and_exit(opts)
def self.display_help(opts)
puts <<-HELP
shards [<options>...] [<command>]

Commands:
build [<targets>] [<options>] - Build the specified <targets> in `bin` path.
check - Verify all dependencies are installed.
info [<options>...] - Show information about a shard. Pass `--help` for details.
init - Initialize a `shard.yml` file.
install - Install dependencies, creating or using the `shard.lock` file.
list [--tree] - List installed dependencies.
Expand All @@ -17,31 +18,32 @@ module Shards
prune - Remove unused dependencies from `lib` folder.
update [<shards>] - Update dependencies and `shard.lock`.
version [<path>] - Print the current version of the shard.
--version - Print the `shards` version.
-h, --help - Print usage synopsis.

Options:
HELP
puts opts
exit
end

def self.run
OptionParser.parse(ARGV) do |opts|
path = Dir.current

opts.on("--no-color", "Disable colored output.") { self.colors = false }
opts.on("--version", "Print the `shards` version.") { puts self.version_string; exit }
opts.on("--production", "Run in release mode. No development dependencies and strict sync between shard.yml and shard.lock.") { self.production = true }
opts.on("--local", "Don't update remote repositories, use the local cache only.") { self.local = true }
opts.on("-v", "--verbose", "Increase the log verbosity, printing all debug statements.") { self.set_debug_log_level }
opts.on("-q", "--quiet", "Decrease the log verbosity, printing only warnings and errors.") { self.set_warning_log_level }
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help_and_exit(opts) }

opts.unknown_args do |args, options|
case args[0]? || DEFAULT_COMMAND
case args.shift? || DEFAULT_COMMAND
when "build"
build(path, args[1..-1])
build(path, args)
when "check"
Commands::Check.run(path)
when "info"
Commands::Info.run(path, args)
when "init"
Commands::Init.run(path)
when "install"
Expand All @@ -51,7 +53,7 @@ module Shards
when "lock"
Commands::Lock.run(
path,
args[1..-1].reject(&.starts_with?("--")),
args.reject(&.starts_with?("--")),
print: args.includes?("--print"),
update: args.includes?("--update")
)
Expand All @@ -62,12 +64,17 @@ module Shards
when "update"
Commands::Update.run(
path,
args[1..-1].reject(&.starts_with?("--"))
args.reject(&.starts_with?("--"))
)
when "version"
Commands::Version.run(args[1]? || path)
Commands::Info.run(args.shift? || path, ["--version"])
when "--version"
puts self.version_string
when "-h", "--help"
display_help(opts)
else
display_help_and_exit(opts)
display_help(opts)
exit 1
end

exit
Expand Down
56 changes: 56 additions & 0 deletions src/commands/info.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "./command"

module Shards
module Commands
class Info < Command
def initialize(path)
super lookup_path(path)
end

def display_help
puts <<-HELP
shards info [<command>]

Displays information about a shard.

Commands:
--name - Print the name of the shard.
--version - Print the version in `spec.yml`.
-h, --help - Print usage synopsis.

If no command is given, a summary including name and version is printed.
HELP
end

def run(args, *, stdout = STDOUT)
case args.shift?
when "--name"
stdout.puts spec.name
when "--version"
stdout.puts spec.version
when "--help", "-h"
display_help
else
stdout.puts " name: #{spec.name}"
stdout.puts "version: #{spec.version}"
end
end

# look up for `SPEC_FILENAME` in *path* or up
private def lookup_path(path)
previous = nil
current = File.expand_path(path)

until !File.directory?(current) || current == previous
shard_file = File.join(current, SPEC_FILENAME)
break if File.exists?(shard_file)

previous = current
current = File.dirname(current)
end

current
end
end
end
end
32 changes: 0 additions & 32 deletions src/commands/version.cr

This file was deleted.