From 637191beee910d7a5c65091a2638a30d93543550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Wed, 8 Apr 2020 23:40:04 +0200 Subject: [PATCH] Add ProjectInfo.find_default_name --- spec/compiler/crystal/tools/doc_spec.cr | 24 ++++++++++++++++++ src/compiler/crystal/command/docs.cr | 25 ++++++++++++++----- .../crystal/tools/doc/project_info.cr | 12 +++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/spec/compiler/crystal/tools/doc_spec.cr b/spec/compiler/crystal/tools/doc_spec.cr index 38edd287961f..96ea75f772d6 100644 --- a/spec/compiler/crystal/tools/doc_spec.cr +++ b/spec/compiler/crystal/tools/doc_spec.cr @@ -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 diff --git a/src/compiler/crystal/command/docs.cr b/src/compiler/crystal/command/docs.cr index f19b7d489ab1..9292d8c63a18 100644 --- a/src/compiler/crystal/command/docs.cr +++ b/src/compiler/crystal/command/docs.cr @@ -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/**"))] @@ -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 diff --git a/src/compiler/crystal/tools/doc/project_info.cr b/src/compiler/crystal/tools/doc/project_info.cr index 433cfcecce0b..f1db22d4795b 100644 --- a/src/compiler/crystal/tools/doc/project_info.cr +++ b/src/compiler/crystal/tools/doc/project_info.cr @@ -19,5 +19,17 @@ module Crystal::Doc tags.first end end + + def self.find_default_name + return unless File.readable?("shard.yml") + + # Poor man's YAML reader + File.each_line("shard.yml") do |line| + if line.starts_with?("name:") + end_pos = line.byte_index("#") || line.bytesize + return line.byte_slice(5, end_pos - 5).strip.presence + end + end + end end end