Class: GitBlog::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/git-blog/workflow.rb

Instance Method Summary collapse

Instance Method Details

#need_setup?Boolean

Test if config is setup

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/git-blog/workflow.rb', line 54

def need_setup?
	!File.file?(GitBlog::Storage.config_path) ||
	!File.file?(GitBlog::Storage.prepare_commit_msg_template_path) ||
	!File.file?(GitBlog::Storage.commit_msg_script_path)
end

#setupObject

Setup basic configuration



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/git-blog/workflow.rb', line 28

def setup
	conf = GitBlog::Configuration.new
	print "Path to blog: "
	$stdin.flush
	conf.blog_path = File.expand_path $stdin.gets.chomp
	conf.write

	# Also, create the hook files
	current_path = File.expand_path(File.dirname(__FILE__))
	hook_path = File.join current_path, "hooks"
	source_template_path = File.join hook_path, "prepare-commit-msg"
	target_template_path = GitBlog::Storage.prepare_commit_msg_template_path
	if !File.file? target_template_path
		FileUtils.cp source_template_path, target_template_path
		FileUtils.chmod 0755, target_template_path
	end

	source_commit_script_path = File.join hook_path, "commit-msg"
	target_commit_script_path = GitBlog::Storage.commit_msg_script_path
	if !File.file? target_commit_script_path
		FileUtils.cp source_commit_script_path, target_commit_script_path
		FileUtils.chmod 0755, target_commit_script_path
	end
end

#start_blogging(cli_params) ⇒ Object

Do the job



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/git-blog/workflow.rb', line 61

def start_blogging cli_params
	current_path = Dir.pwd

	if !current_path.repo?
		puts "Error: please run git-blog inside a git repo."
		return
	end

	if need_setup?
		puts "Haven't setup git-repo, setting up now"
		setup
	end

	git_root_path = %x(git rev-parse --show-toplevel).strip
	hooks_path = File.join git_root_path, ".git", "hooks"
	source_template_path = GitBlog::Storage.prepare_commit_msg_template_path

	script = <<EOF
#!/usr/bin/env ruby

$commit_file_path = ARGV[0]

content = <<EOC
#{File.read(source_template_path)}
EOC

File.write($commit_file_path, content)
EOF

	target_template_path = File.join hooks_path, "prepare-commit-msg"
	File.open(target_template_path, "w") { |file| file.write script }
	FileUtils.chmod 0755, target_template_path

	source_commit_script_path = GitBlog::Storage.commit_msg_script_path
	target_commit_script_path = File.join hooks_path, "commit-msg"
	FileUtils.cp source_commit_script_path, target_commit_script_path
	FileUtils.chmod 0755, target_commit_script_path

	# Pass to git commit
	system("git commit #{cli_params.join(" ")}")

	# Clean up
	FileUtils.rm target_template_path
	FileUtils.rm target_commit_script_path
end