#!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'optparse' require 'fileutils' require 'erb' require 'yaml' require 'pathname' include FileUtils def main() options = { "debug" => false, "username" => "", "email" => "" } option_parser = OptionParser.new do |opts| executable_name = File.basename($PROGRAM_NAME) opts.banner = "make jekyll blog which hosted in github Usage: #{executable_name} [options] " opts.on("-d","--debug","debug") do options["debug"] = true end opts.on("-u","--username name","your name in blog, like \"Larry Cai\"") do | name| options["username"] = name end opts.on("-e","--email email","email could be contacted in blog") do |name| options["email"] = name end opts.on("-g","--generate project","generate blog under project.github.com") do |name| unless name =~ /^[a-zA-Z0-9]+$/ raise ArgumentError,"name should be [a-zA-Z0-9]" end options["command"] = "generate" options["account"] = name end opts.on("-s","--show","show how to continue") do options["command"] = "show" end end option_parser.parse! puts options.inspect if options["debug"] case options["command"] when "generate" then generate_project(options["account"],options["username"],options["email"],options["debug"]) when "show" then showhelp() else puts "--help for help" end end def showhelp() puts "1. update your personal information in theme (TODO)" puts "2. create project in github.com and push this project" puts "3. follow http://pages.github.com/" end def guess() username = "Larry Cai" email = "larry.caiyu@gmail.com" # scan ~/.gitconfig gitconfig = ".gitconfig" gconf = File.join(Dir.home, gitconfig) if File.exists?(gconf) puts "..scan `#{gconf}`" File.open(gconf).each do |record| name,value = record.split("=") name.gsub!(/\t*\s*/,'') # puts "[#{name}]" + ":" case name when "name" then username = value.gsub(/\t*\s*\n*/,'') when "email" then email = value.gsub(/ /,'') end end end # puts username + ":" + email return username,email end # http://stackoverflow.com/questions/5074327/most-appropriate-way-to-generate-directory-of-files-from-directory-of-template-f def generate_project(account,username,email,debug) destination = account + ".github.com" #puts username, email gitusername,gitemail = guess() username = gitusername if username.empty? email = gitemail if email.empty? source = File.dirname(__FILE__)+"/../templates" #puts "generate project \"#{destination}\" from source \"#{source}\"" FileUtils.rmtree(destination) FileUtils.mkdir_p(destination) sourceroot=Pathname.new(source) sourcerealpath = sourceroot.cleanpath puts "generate project \"#{destination}\" from source \"#{sourcerealpath}\"" Dir.glob(File.join(source, '**/*')).each do |path| pathname = Pathname.new(path) relative = pathname.relative_path_from(sourceroot) #puts "parent:" , sourceroot #puts "relative:", relative if File.directory?(pathname) destdir = File.join(destination, relative.dirname) #puts "create #{destdir} " FileUtils.mkdir_p(destdir) else FileUtils.mkdir_p(File.join(destination, relative.dirname)) if pathname.extname == '.erb' #puts pathname.basename.sub(/\.erb$/, '') #puts destination #puts relative.dirname #puts File.join(destination,relative.dirname,pathname.basename.sub(/\.erb$/, '')) target = File.join(destination,relative.dirname,pathname.basename.sub(/\.erb$/, '')) if debug == true print " ", pathname.cleanpath, " => ", target,"\n" end File.open(target, 'w') do |file| file.puts(ERB.new(File.read(path)).result(binding)) end else if debug == true print " ", pathname.cleanpath, " => ", File.join(destination, relative.dirname),"\n" end FileUtils.cp(pathname, File.join(destination, relative.dirname)) end end end puts puts "The blog is now yours" puts "=> name:" + username puts "=> email:" + email puts "=> github: http://github.com/" + account puts "please run `cd " + account + ".github.com/" + " ; jekyll --server'" puts "Then and open browser to http://localhost:4000 see the magic" end main