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 7861c98 commit 637191b
Show file tree
Hide file tree
Showing 3 changed files with 55 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
12 changes: 12 additions & 0 deletions src/compiler/crystal/tools/doc/project_info.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 637191b

Please sign in to comment.