Skip to content

Commit

Permalink
Add ProjectInfo.find_default_name
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Apr 8, 2020
1 parent 7a4188c commit 56e80ed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
24 changes: 24 additions & 0 deletions spec/compiler/crystal/tools/doc_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,28 @@ describe Crystal::Doc::ProjectInfo do
end
end
end

it "find_default_name" do
with_tempfile("docs-shard-name") do |tempdir|
Dir.mkdir tempdir
Dir.cd(tempdir) do
Crystal::Doc::ProjectInfo.find_default_name.should be_nil

File.write("shard.yml", "foo: bar\n")
Crystal::Doc::ProjectInfo.find_default_name.should be_nil

File.write("shard.yml", "name: \n")
Crystal::Doc::ProjectInfo.find_default_name.should be_nil

File.write("shard.yml", " name: bar\n")
Crystal::Doc::ProjectInfo.find_default_name.should be_nil

File.write("shard.yml", "name: bar\n")
Crystal::Doc::ProjectInfo.find_default_name.should eq "bar"

File.write("shard.yml", "name: bar # comment\n")
Crystal::Doc::ProjectInfo.find_default_name.should eq "bar"
end
end
end
end
25 changes: 19 additions & 6 deletions src/compiler/crystal/command/docs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ class Crystal::Command
setup_compiler_warning_options(opts, compiler)
end

unless project_name
abort "missing --project-name"
end

project_version ||= Doc::ProjectInfo.find_default_project_version
project_info = Doc::ProjectInfo.new(project_name.not_nil!, project_version)
project_info = create_project_info(project_name, project_version)

if options.empty?
sources = [Compiler::Source.new("require", %(require "./src/**"))]
Expand All @@ -125,4 +120,22 @@ class Crystal::Command
report_warnings result
exit 1 if warnings_fail_on_exit?(result)
end

private def create_project_info(name, version)
name ||= Doc::ProjectInfo.find_default_name
unless name
STDERR.puts "Couldn't determine name from shard.yml, please provide --project-name option"
end

version ||= Doc::ProjectInfo.find_default_version
unless version
STDERR.puts "Couldn't determine version from git, please provide --project-version option"
end

unless name && version
abort
end

Doc::ProjectInfo.new(name, version)
end
end

0 comments on commit 56e80ed

Please sign in to comment.