lib/kata/setup/base.rb in kata-1.3.2 vs lib/kata/setup/base.rb in kata-1.3.3

- old
+ new

@@ -1,7 +1,9 @@ require 'fileutils' require 'ostruct' +require 'octokit' +require 'io/console' module Kata module Setup class Base attr_accessor :kata_name @@ -11,66 +13,94 @@ self.kata_name = kata_name self.repo_name = kata_name end def create_repo options + create_remote_repo if options.repo + + push_local_repo(options.repo) + end + + def repo_name=(kata_name) + @repo_name = "#{kata_name.gsub(/( |-)\1?/, '_')}-#{Time.now.strftime('%Y-%m-%d-%H%M%S')}".downcase + end + + def build_tree(type = 'ruby') + case type + when 'ruby' + Kata::Setup::Ruby.new(kata_name).build_tree + end + end + + private + + def github # Setup from github configuration - raise Exception, 'Git not installed' unless system 'which git > /dev/null' + raise Exception, 'Git not installed? Could not find git using which' unless system('which git > /dev/null') - github = OpenStruct.new :url => 'http://github.com/api/v2/json/' + @github ||= + begin + tmp = OpenStruct.new - github_user, shell_user = %x{git config --get github.user}.chomp, ENV['USER'] + github_user = %x{git config --get github.user}.chomp + shell_user = ENV['USER'] - github.user = github_user.empty? ? shell_user : github_user + tmp.user = github_user.empty? ? shell_user : github_user - raise Exception, 'Unable to determine github user' if github.user.empty? + raise Exception, 'Unable to determine github user' if tmp.user.empty? - github.token = %x{git config --get github.token}.chomp + print 'Github account password: ' + tmp.password = STDIN.noecho(&:gets).chomp - raise Exception, 'Unable to determine github api token' if github.token.empty? + tmp + end + end - user_string = "-u '#{github.user}/token:#{github.token}'" - repo_params = "-d 'name=#{repo_name}' -d 'description=code+kata+repo'" + def client + @client ||= Octokit::Client.new(:login => github.user, :password => github.password) + end - # 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 + def create_session_token + authorization = client.create_authorization({:scopes => ['public_repo'], :note => 'Ruby Kata'}) - # publish to github + github.token = authorization.token - print 'creating files for repo and initializing...' + cmd = "git config --add github.token #{github.token}" + raise SystemCallError, 'Unable to cache github api token' unless system(cmd) + end - 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;" + def create_remote_repo + create_session_token + + puts "Creating remote repo..." + client.create_repo "#{repo_name}" + puts "end" + end + + def push_local_repo(new_repo) + print "creating files for repo and initializing..." + + cmd = "cd #{repo_name} &&" + + if new_repo + cmd << "git init >/dev/null 2>&1 &&" + cmd << "git add README .rspec lib/ spec/ >/dev/null 2>&1 &&" else - cmd << "git add #{ENV['PWD']}/#{repo_name};" + cmd << "git add #{ENV['PWD']}/#{repo_name} >/dev/null 2>&1;" end - cmd << "git commit -m 'starting kata' 2>&1 > /dev/null;" - cmd << "git remote add origin git@github.com:#{github.user}/#{repo_name}.git 2>&1 > /dev/null;" if options.repo - cmd << 'git push origin master 2> /dev/null' + cmd << "git commit -m 'starting kata' > /dev/null 2>&1;" + + if new_repo + cmd << "git remote add origin git@github.com:#{github.user}/#{repo_name}.git >/dev/null 2>&1 &&" + end + + cmd << 'git push origin master' + 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 - - def repo_name=(kata_name) - @repo_name = "#{kata_name.gsub(/( |-)\1?/, '_')}-#{Time.now.strftime('%Y-%m-%d-%H%M%S')}".downcase - end - - def build_tree(type = 'ruby') - case type - when 'ruby' - Kata::Setup::Ruby.new(kata_name).build_tree - end end end end end