Skip to content

Commit 2c8d30d

Browse files
Merge pull request #370 from Insti/No_x-common_warning
Generator: Add message if x-common doesnt exist.
2 parents 2e5ad5e + 88fa9bf commit 2c8d30d

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

lib/generator.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
require 'ostruct'
66

77
class Generator
8+
METADATA_REPOSITORY = 'x-common'.freeze
9+
810
attr_reader :name, :cases
911
def initialize(name, cases)
1012
@name = name
1113
@cases = cases
1214
end
1315

1416
def metadata_dir
15-
File.expand_path(File.join('..', '..', '..', 'x-common'), __FILE__)
17+
File.expand_path(File.join('..', '..', '..', METADATA_REPOSITORY), __FILE__)
1618
end
1719

1820
def data
@@ -35,13 +37,43 @@ def test_cases
3537
cases.call(data)
3638
end
3739

40+
def metadata_repository_missing_message
41+
<<-EOM.gsub(/^ {6}/, '')
42+
43+
'#{METADATA_REPOSITORY}' repository not found.
44+
Try running the command:
45+
git clone https://github.com/exercism/#{METADATA_REPOSITORY}.git "#{metadata_dir}"
46+
47+
EOM
48+
end
49+
3850
def generate
51+
check_metadata_repository_exists
52+
generate_test_file
53+
increment_version
54+
increment_version_in_example
55+
end
56+
57+
def check_metadata_repository_exists
58+
unless File.directory?(metadata_dir)
59+
STDERR.puts metadata_repository_missing_message
60+
fail Errno::ENOENT.new(metadata_dir)
61+
end
62+
end
63+
64+
def generate_test_file
3965
File.open(path_to("#{name.gsub(/[ -]/, '_')}_test.rb"), 'w') do |f|
4066
f.write ERB.new(File.read(path_to('example.tt'))).result binding
4167
end
68+
end
69+
70+
def increment_version
4271
File.open(path_to('.version'), 'w') do |f|
4372
f.write version + 1
4473
end
74+
end
75+
76+
def increment_version_in_example
4577
contents = File.read(path_to('example.rb'))
4678
File.open(path_to('example.rb'), 'w') do |f|
4779
f.write contents.gsub("VERSION = #{version}", "VERSION = #{version + 1}")

0 commit comments

Comments
 (0)