Class: Kata::Setup::Base

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

Direct Known Subclasses

Ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kata_name = 'kata') ⇒ Base

Returns a new instance of Base.



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

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.



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

def kata_name
  @kata_name
end

#repo_nameObject

Returns the value of attribute repo_name.



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

def repo_name
  @repo_name
end

Instance Method Details

#build_tree(type = 'ruby') ⇒ Object



68
69
70
71
72
73
# File 'lib/kata/setup/base.rb', line 68

def build_tree(type = 'ruby')
  case type
  when 'ruby'
    Kata::Setup::Ruby.new(kata_name).build_tree
  end
end

#create_repo(options) ⇒ Object

Raises:

  • (Exception)


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
56
57
58
59
60
61
62
# File 'lib/kata/setup/base.rb', line 15

def create_repo options
  # 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
  if options.repo
    print 'Creating github repo...'
    raise SystemCallError, 'unable to use curl to create repo on github' unless system <<-EOF
      curl -s #{user_string} #{repo_params} #{github.url}repos/create 2>&1 > /dev/null;
    EOF
    puts 'complete'
  end

  # publish to github

  print 'creating files for repo and initializing...'

  cmd = "cd #{repo_name};"
  if options.repo
    cmd << "git init 2>&1 > /dev/null;"
    cmd << "git add README .rspec lib/ spec/ 2>&1 > /dev/null;"
  else
    cmd << "git add #{ENV['PWD']}/#{repo_name};"
  end
  cmd << "git commit -m 'starting kata' 2>&1 > /dev/null;"
  cmd << "git remote add origin [email protected]:#{github.user}/#{repo_name}.git 2>&1 > /dev/null;" if options.repo
  cmd << 'git push origin master 2> /dev/null'

  raise SystemCallError, 'unable to add files to github repo' unless system(cmd)

  puts 'done'
  puts "You can now change directories to #{repo_name} and take your kata"
end