Git 'dashed' commands are deprecated and no longer available with new installs from...
[git-blog.git] / lib / git-blog.rb
blob12822c0914a975338a5b53ae7a48f6403a18ff26
1 require 'fileutils'
2 include FileUtils
4 require 'git-blog/core'
5 require 'haml'
7 desc 'Setup the blog repository'
8 task :initialize do
9   path = File.expand_path '.'
10   
11   # Will create the repo if it doesn't exist
12   until (blog = Git.open path rescue false)
13     Git.init path
14   end
15   
16   cd path
17   mkdir 'posts'
18   mkdir 'design'
19   cp GitBlog::Scope / :prepped / '.gitignore', '.'
20   %w(welcome_to_your_git_blog.markdown .gitignore).each do |file|
21     cp GitBlog::Scope / :prepped / :posts / file, 'posts'
22   end
23   %w(main.css post.haml index.haml).each do |file|
24     cp GitBlog::Scope / :prepped / :design / file, 'design'
25   end
26   
27   Dir['**/**'].each do |file|
28     if File.directory? file
29       chmod 0775, file
30     else
31       chmod 0664, file
32     end
33   end
34   chmod 0755, '.git'
35   chmod 0664, '.gitignore'
36   chmod 0664, :posts / '.gitignore'
37   
38   blog.add
39   blog.commit_all("A bird... no, a plane... no, a git-blog!")
40 end
42 desc 'Attach the blog to a GitHub repository - can also clone an existing git-blog repository here'
43 task :github, :user_name, :repo_name do |_, params|
44   path = File.expand_path '.'
45   user_name = params[:user_name].nil? ? %x(whoami).chomp : params[:user_name]
46   repo_name = params[:repo_name].nil? ? File.basename(path) : params[:repo_name]
47   
48   github_url = "[email protected]:#{user_name.downcase}/#{repo_name.downcase}.git"
49   
50   if File.directory? File.join(path, 'posts') and
51     (blog = Git.open path rescue false)
52     
53     github = blog.add_remote 'github', github_url
54     blog.push github
55   else
56     system "git init"
57     system "git remote add -f github #{github_url}"
58     system "git checkout -b master github/master"
59   end
60 end
62 desc 'Prepare the blog to be served (configures hooks)'
63 task :servable do
64   should_be_initialized File.expand_path('.')
65   
66   mv '.git' / :hooks / 'post-receive', '.git' / :hooks / 'post-receive.old'
67   cp GitBlog::Scope / :prepped / 'post-receive.hook', '.git' / :hooks / 'post-receive'
68   chmod 0775, '.git' / :hooks / 'post-receive'
69   
70   puts '** git-blog is prepared for serving (git post-recieve hook prepared)'
71 end
73 desc 'Create and open for editing a new post'
74 task :post do
75   @resume = false
76   temporary_post = :post_in_progress.markdown
77   
78   should_be_initialized File.expand_path('.')
79   
80   if File.file? temporary_post
81     puts '** You have an unfinished post from before,'
82     puts 'do you want to r)esume it or overwrite it with a n)ew one? '
83     case STDIN.gets
84     when /r/i
85       @resume = true
86     when /n/i
87       # do nothing, go on to overwriting it
88     else
89       raise 'Invalid entry, exiting out'
90     end
91   end
92   
93   unless @resume
94     File.open temporary_post, File::RDWR|File::TRUNC|File::CREAT, 0664 do |post|
95       post.puts  'Replace this text with your title!'
96       post.puts  '=================================='
97       post.print "\n"; post.close