Skip to content

Commit bf6e814

Browse files
Add shards info command
1 parent cb6c7c2 commit bf6e814

File tree

9 files changed

+145
-37
lines changed

9 files changed

+145
-37
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ uninstall: phony
3636
test: test_unit test_integration
3737

3838
test_unit: phony
39-
$(CRYSTAL) spec ./spec/unit/*_spec.cr
39+
$(CRYSTAL) spec ./spec/unit/
4040

4141
test_integration: bin/shards phony
42-
$(CRYSTAL) spec ./spec/integration/*_spec.cr
42+
$(CRYSTAL) spec ./spec/integration/
4343

4444
phony:

man/shards.1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ dependencies are satisfied,
3939
dependencies aren't satisfied.
4040
.RE
4141
.PP
42+
\fBinfo [<command>]\fR
43+
.RS 4
44+
Displays information about a shard.
45+
.SS
46+
.RS 4
47+
Commands:
48+
.PP
49+
.TP 3
50+
\fB--name\fR
51+
Print the name of the shard.
52+
.TP 3
53+
\fB--version\fR
54+
Print the version in `spec.yml`.
55+
.TP 3
56+
\fB-h, --help\fR
57+
Print usage synopsis.
58+
.RE
59+
.PP
60+
.RS 4
61+
If no command is given, a summary including name and version is printed.
62+
.RE
63+
.PP
4264
\fBinit\fR
4365
.RS 4
4466
Initializes a default \fIshard.yml\fR in the current folder.

spec/integration/info_spec.cr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "./spec_helper"
2+
3+
describe "shards info" do
4+
it "reports name" do
5+
Dir.cd(application_path) do
6+
with_shard({name: "foo"}) do
7+
output = run "shards info --name"
8+
output.should eq "foo\n"
9+
end
10+
end
11+
end
12+
13+
it "reports version" do
14+
Dir.cd(application_path) do
15+
with_shard({version: "1.2.3"}) do
16+
output = run "shards info --version"
17+
output.should eq "1.2.3\n"
18+
end
19+
end
20+
end
21+
22+
it "reports info" do
23+
Dir.cd(application_path) do
24+
with_shard({name: "foo", version: "1.2.3"}) do
25+
output = run "shards info"
26+
output.should eq <<-OUT
27+
name: foo
28+
version: 1.2.3
29+
30+
OUT
31+
end
32+
end
33+
end
34+
end

spec/support/cli.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "./factories"
2+
13
Spec.before_each do
24
path = application_path
35

spec/support/factories.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class FailedCommand < Exception
33
getter stderr : String
44

55
def initialize(message, @stdout, @stderr)
6-
super message
6+
super "#{message}: #{stderr}"
77
end
88
end
99

spec/unit/commands/info_spec.cr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "../../../src/commands/info"
2+
require "../spec_helper"
3+
require "../../support/cli"
4+
5+
private def capture(command, *args)
6+
String.build do |io|
7+
command.run(args.to_a, stdout: io)
8+
end.chomp
9+
end
10+
11+
describe Shards::Commands::Info do
12+
it "reports name" do
13+
with_shard({name: "foo", version: "1.2.3"}) do
14+
info = Shards::Commands::Info.new(application_path)
15+
16+
capture(info, "--name").should eq "foo"
17+
capture(info, "--version").should eq "1.2.3"
18+
capture(info, "").should eq <<-OUT
19+
name: foo
20+
version: 1.2.3
21+
OUT
22+
end
23+
end
24+
end

src/cli.cr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Shards
99
Commands:
1010
build [<targets>] [<options>] - Build the specified <targets> in `bin` path.
1111
check - Verify all dependencies are installed.
12+
info [<options>...] - Show information about a shard. Pass `--help` for details.
1213
init - Initialize a `shard.yml` file.
1314
install - Install dependencies, creating or using the `shard.lock` file.
1415
list [--tree] - List installed dependencies.
@@ -32,7 +33,6 @@ module Shards
3233
opts.on("--no-color", "Disable colored output.") { self.colors = false }
3334
opts.on("--production", "Run in release mode. No development dependencies and strict sync between shard.yml and shard.lock.") { self.production = true }
3435
opts.on("--local", "Don't update remote repositories, use the local cache only.") { self.local = true }
35-
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help_and_exit(opts) }
3636
opts.on("-v", "--verbose", "Increase the log verbosity, printing all debug statements.") { self.set_debug_log_level }
3737
opts.on("-q", "--quiet", "Decrease the log verbosity, printing only warnings and errors.") { self.set_warning_log_level }
3838

@@ -42,6 +42,8 @@ module Shards
4242
build(path, args)
4343
when "check"
4444
Commands::Check.run(path)
45+
when "info"
46+
Commands::Info.run(path, args)
4547
when "init"
4648
Commands::Init.run(path)
4749
when "install"
@@ -65,7 +67,7 @@ module Shards
6567
args.reject(&.starts_with?("--"))
6668
)
6769
when "version"
68-
Commands::Version.run(args[1]? || path)
70+
Commands::Info.run(args.shift? || path, ["--version"])
6971
when "--version"
7072
puts self.version_string
7173
when "-h", "--help"

src/commands/info.cr

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require "./command"
2+
3+
module Shards
4+
module Commands
5+
class Info < Command
6+
def initialize(path)
7+
super lookup_path(path)
8+
end
9+
10+
def display_help
11+
puts <<-HELP
12+
shards info [<command>]
13+
14+
Displays information about a shard.
15+
16+
Commands:
17+
--name - Print the name of the shard.
18+
--version - Print the version in `spec.yml`.
19+
-h, --help - Print usage synopsis.
20+
21+
If no command is given, a summary including name and version is printed.
22+
HELP
23+
end
24+
25+
def run(args, *, stdout = STDOUT)
26+
case args.shift?
27+
when "--name"
28+
stdout.puts spec.name
29+
when "--version"
30+
stdout.puts spec.version
31+
when "--help", "-h"
32+
display_help
33+
else
34+
stdout.puts " name: #{spec.name}"
35+
stdout.puts "version: #{spec.version}"
36+
end
37+
end
38+
39+
# look up for `SPEC_FILENAME` in *path* or up
40+
private def lookup_path(path)
41+
previous = nil
42+
current = File.expand_path(path)
43+
44+
until !File.directory?(current) || current == previous
45+
shard_file = File.join(current, SPEC_FILENAME)
46+
break if File.exists?(shard_file)
47+
48+
previous = current
49+
current = File.dirname(current)
50+
end
51+
52+
current
53+
end
54+
end
55+
end
56+
end

src/commands/version.cr

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)