Class: Kata::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/kata/setup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kata_name = 'kata') ⇒ Setup

Returns a new instance of Setup.



9
10
11
12
# File 'lib/kata/setup.rb', line 9

def initialize kata_name = 'kata'
  self.kata_name = kata_name
  self.repo_name = kata_name
end

Instance Attribute Details

#kata_nameObject

Returns the value of attribute kata_name.



6
7
8
# File 'lib/kata/setup.rb', line 6

def kata_name
  @kata_name
end

#repo_nameObject

Returns the value of attribute repo_name.



7
8
9
# File 'lib/kata/setup.rb', line 7

def repo_name
  @repo_name
end

Instance Method Details

#build_treeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kata/setup.rb', line 61

def build_tree
  %W{#{repo_name}/lib #{repo_name}/spec/support/helpers #{repo_name}/spec/support/matchers}.each {|path| FileUtils.mkdir_p path}

  use_kata_name = kata_name.gsub(/( |-)\1?/, '_').downcase
  class_name = kata_name.split(/ |-|_/).map(&:capitalize).join

  # create the README file so github is happy
  File.open(File.join(repo_name, 'README'), 'w') {|f| f.write <<EOF}
Leveling up my ruby awesomeness!
EOF

  # create the base class file
  File.open(File.join(repo_name, 'lib', "#{use_kata_name}.rb"), 'w') {|f| f.write <<EOF}
class #{class_name}
end
EOF

  # create the spec_helper.rb file
  File.open(File.join(repo_name, 'spec', 'spec_helper.rb'), 'w') {|f| f.write <<EOF}
$: << '.' << File.join(File.dirname(__FILE__), '..', 'lib')

require 'rspec'

Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
EOF

  # create a working spec file for the kata
  File.open(File.join(repo_name, 'spec', "#{use_kata_name}_spec.rb"), 'w') {|f| f.write <<EOF}
require 'spec_helper'
require '#{use_kata_name}'

class #{class_name}
  describe "new" do
it "should instantiate" do
  lambda {
    #{class_name}.new
  }.should_not raise_exception
end
  end
end
EOF
  # stub out a custom matchers file
  File.open(File.join(repo_name, 'spec', 'support', 'matchers', "#{use_kata_name}.rb"), 'w') {|f| f.write <<EOF}
RSpec::Matchers.define :your_method do |expected|
  match do |your_match|
#your_match.method_on_object_to_execute == expected
  end
end
EOF
end

#create_repoObject

Raises:

  • (Exception)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kata/setup.rb', line 14

def create_repo
  # Setup from github configuration
  raise Exception, 'Git not installed' unless system 'which git > /dev/null'

  github = OpenStruct.new :url => 'http://github.com/api/v2/json/'

  github_user, shell_user = %x{git config --get github.user}.chomp, ENV['USER']

  github.user = github_user.empty? ? shell_user : github_user

  raise Exception, 'Unable to determine github user' if github.user.empty?

  github.token = %x{git config --get github.token}.chomp

  raise Exception, 'Unable to determine github api token' if github.token.empty?

  user_string = "-u '#{github.user}/token:#{github.token}'"
  repo_params = "-d 'name=#{repo_name}' -d 'description=code+kata+repo'"

  Create the repo on github
  raise SystemCallError, 'unable to use curl to create repo on github' unless system <<-EOF
    echo -n 'Creating github repo...';
    curl -s #{user_string} #{repo_params} #{github.url}repos/create 2> /dev/null;
    echo 'complete'
  EOF

  # publish to github
  raise SystemCallError, 'unable to publish repo to github' unless system <<-EOF
    echo -n 'Initializing repo...';
    cd #{repo_name};
    git init 2> /dev/null;
    git add README lib/ spec/ 2> /dev/null;
    git commit -m 'starting kata' 2> /dev/null;
    echo 'complete'
    echo -n 'adding origin...'
    git remote add origin [email protected]:#{github.user}/#{repo_name}.git 2> /dev/null;
    echo 'complete'
    echo -n 'pushing...'
    git push origin master 2> /dev/null
    echo 'complete'
  EOF
end