Skip to content

Commit 70d2204

Browse files
committed
Add CLI command for creating a new kata
1 parent 4e95d0f commit 70d2204

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

lib/cleo_katas/cli.rb

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@ class CLI < Thor
1111
attr_accessor :kata
1212

1313
source_root File.expand_path('templates', __dir__)
14+
15+
desc 'create', 'Create a new kata'
16+
def create(kata_name)
17+
self.kata = kata_name
18+
19+
kata_number = CleoKatas::KataFile.max_number.to_i + 1
20+
kata_name = kata_name.gsub(/[^a-z0-9]/i, '_').downcase
21+
kata_number_name = "#{kata_number}-#{kata_name}"
22+
23+
say "Creating kata #{kata_number_name}"
24+
25+
empty_directory(File.join(Dir.pwd, 'katas', "#{kata_number_name}"))
26+
empty_directory(File.join(Dir.pwd, 'katas', "#{kata_number_name}", 'source'))
27+
template('README.md.erb',
28+
File.join(Dir.pwd, 'katas', "#{kata_number_name}", 'README.md')
29+
)
30+
template(
31+
'main.rb.erb',
32+
File.join(Dir.pwd, 'katas', "#{kata_number_name}", 'source', 'main.rb')
33+
)
34+
template(
35+
'test.rb.erb',
36+
File.join(Dir.pwd, 'katas', "#{kata_number_name}", 'source', 'test.rb')
37+
)
38+
end
39+
1440
desc 'list', 'List all available katas'
1541
def list
1642
say 'Listing katas...'
@@ -21,19 +47,20 @@ def list
2147

2248
# rubocop:disable Metrics/MethodLength
2349
desc 'attempt DIRECTORY', 'Create a new kata attempt in your own directory'
24-
def attempt(kata)
25-
self.kata = kata
50+
def attempt(kata_name)
51+
self.kata = kata_name
2652

53+
begin
54+
kata_file.numbered_name
55+
rescue
56+
say "Kata #{kata_name} not found"
57+
return
58+
end
2759

2860
source_directory = File.join(Dir.pwd, 'katas', kata_file.numbered_name, 'source')
2961
target_directory = File.join(Dir.pwd, 'katas', kata_file.numbered_name, username)
3062
readme_target_file = File.join(target_directory, 'README.md')
3163

32-
puts <<~STRING
33-
source_directory: #{source_directory}
34-
target_directory: #{target_directory}
35-
readme_target_file: #{readme_target_file}
36-
STRING
3764
directory(source_directory,target_directory)
3865
copy_file(kata_file.path, readme_target_file)
3966
append_file(readme_target_file, <<~MARKDOWN)
@@ -52,6 +79,13 @@ def attempt(kata)
5279
end
5380
# rubocop:enable Metrics/MethodLength
5481

82+
protected
83+
84+
85+
def kata_class_name
86+
kata.to_s.split(/[^a-z0-9]+/i).map(&:capitalize).join
87+
end
88+
5589
private
5690

5791
def username

lib/cleo_katas/kata_file.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ class KataFile
1111

1212
def self.all
1313
Dir.glob(File.join(Dir.pwd, KATA_FILE_NAME_GLOB)).map do |kata_file|
14-
puts kata_file
1514
match_data = kata_file.match(KATA_FILE_NAME_REGEX)
1615
new(number: match_data[:kata_number],
1716
name: match_data[:kata_name])
1817
end
1918
end
2019

20+
def self.max_number
21+
all.map(&:number).max
22+
end
23+
2124
def self.find(kata_name)
2225
all.find { |kata_file| kata_file.name == kata_name }
2326
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# <%= kata.gsub(/\_\-/, ' ') %>
2+
3+
## Problem Description
4+
5+
## Requirements and Constraints
6+
7+
### Input Specifications
8+
9+
### Output Specifications
10+
11+
## Examples and Test Cases
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'minitest'
6+
end
7+
8+
class <%= kata_class_name %>
9+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require_relative '../main'
2+
require 'minitest/autorun'
3+
4+
class <%= kata_class_name %>Test < Minitest::Test
5+
def test_something
6+
skip 'Not implemented'
7+
end
8+
end

0 commit comments

Comments
 (0)