lib/kata/setup/base.rb in kata-1.5.0 vs lib/kata/setup/base.rb in kata-1.6.0
- old
+ new
@@ -26,89 +26,127 @@
def build_tree(type = 'ruby')
case type
when 'ruby'
Kata::Setup::Ruby.new(kata_name).build_tree
- when 'javascript'
- Kata::Setup::Javascript.new(kata_name).build_tree
+ when 'node'
+ Kata::Setup::Node.new(kata_name).build_tree
+ when 'php'
+ Kata::Setup::Php.new(kata_name).build_tree
+ else
+ raise(ArgumentError, "Invalid language type #{type}")
end
end
private
+ def tree(path)
+ FileUtils.mkdir_p(File.join(repo_name, path))
+ end
+
def use_kata_name
kata_name.gsub(/( |-)\1?/, '_').downcase
end
def class_name
kata_name.split(/ |-|_/).map(&:capitalize).join
end
+ def write_repo_file(use_file, use_contents, permissions = 0644)
+ File.open(File.join(repo_name, use_file), 'w') do |f|
+ f.write(use_contents)
+ f.chmod(permissions) rescue Exception
+ end
+ end
+
def readme
- File.open(File.join(repo_name, 'README'), 'w') { |f| f.write(<<EOF) }
+ write_repo_file('README',<<EOF)
Leveling up my coding awesomeness!
EOF
end
def github
# Setup from github configuration
raise Exception, 'Git not installed? Could not find git using which' unless system('which git > /dev/null')
@github ||=
begin
- tmp = OpenStruct.new
+ struct = OpenStruct.new
+ struct.token = %x{git config --get github.token}.chomp
+
github_user = %x{git config --get github.user}.chomp
shell_user = ENV['USER']
- tmp.user = github_user.empty? ? shell_user : github_user
+ struct.user = github_user.empty? ? shell_user : github_user
- raise Exception, 'Unable to determine github user' if tmp.user.empty?
+ struct
+ end
+ end
- print 'Github account password: '
- tmp.password = STDIN.noecho(&:gets).chomp
+ def client_factory
+ if github.token
+ # nothing to do
+ elsif github.user
+ get_password
+ get_token
+ else
+ raise Exception, 'Unable to determine github.token or github.user' if github.user.empty?
+ end
- tmp
- end
+ client.access_token = github.token
end
def client
- @client ||= Octokit::Client.new(:login => github.user, :password => github.password)
+ @client ||= Octokit::Client.new
end
- def create_session_token
- authorization = client.create_authorization({:scopes => ['public_repo'], :note => 'Ruby Kata'})
+ def get_password
+ print 'Github account password: '
+ github.password = STDIN.noecho(&:gets).chomp
+ end
+ def get_token
+ client.login = github.user
+ client.password = github.password
+
+ authorization = client.create_authorization({:scopes => ['public_repo'], :note => 'Code Kata'})
+
github.token = authorization.token
cmd = "git config --add github.token #{github.token}"
raise SystemCallError, 'Unable to cache github api token' unless system(cmd)
end
def create_remote_repo
- create_session_token
+ client_factory
- puts "Creating remote repo..."
- client.create_repo "#{repo_name}"
- puts "end"
+ begin
+ print "Creating remote repo..."
+ client.create_repo "#{repo_name}"
+ rescue Exception
+ puts "\nError: unable to create the git repo."
+ print 'Proceeding...'
+ ensure
+ puts "done"
+ 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 &&"
+ cmd << "git init &&"
+ #cmd << "git add README .rspec lib/ spec/ &&"
+ cmd << "git add . &&"
else
- cmd << "git add #{ENV['PWD']}/#{repo_name} >/dev/null 2>&1;"
+ cmd << "git add #{ENV['PWD']}/#{repo_name};"
end
- cmd << "git commit -m 'starting kata' > /dev/null 2>&1;"
+ cmd << "git commit -m 'starting kata';"
if new_repo
- cmd << "git remote add origin git@github.com:#{github.user}/#{repo_name}.git >/dev/null 2>&1 &&"
+ cmd << "git remote add origin git@github.com:#{github.user}/#{repo_name}.git &&"
end
cmd << 'git push origin master'
raise SystemCallError, 'unable to add files to github repo' unless system(cmd)