mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-27 14:57:40 +00:00
update from github if necessary, and implement "sync" aka 'git push'
This commit is contained in:
parent
cec182882f
commit
4513df26f5
2 changed files with 49 additions and 19 deletions
|
|
@ -49,10 +49,12 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
|
|
||||||
def years
|
def years
|
||||||
|
update_if_needed
|
||||||
Dir[post_path('20*')].map { |x| File.basename(x) }.sort
|
Dir[post_path('20*')].map { |x| File.basename(x) }.sort
|
||||||
end
|
end
|
||||||
|
|
||||||
def months
|
def months
|
||||||
|
update_if_needed
|
||||||
years.map do |year|
|
years.map do |year|
|
||||||
# hack: month dirs (and only month dirs) are always 2 characters in length
|
# hack: month dirs (and only month dirs) are always 2 characters in length
|
||||||
Dir[post_path("#{year}/??")].map { |x| [year, File.basename(x)] }
|
Dir[post_path("#{year}/??")].map { |x| [year, File.basename(x)] }
|
||||||
|
|
@ -152,6 +154,11 @@ class HarpBlog
|
||||||
run("make #{target}")
|
run("make #{target}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sync
|
||||||
|
update_if_needed
|
||||||
|
git_push
|
||||||
|
end
|
||||||
|
|
||||||
def compile
|
def compile
|
||||||
run('make compile')
|
run('make compile')
|
||||||
@mutated = false
|
@mutated = false
|
||||||
|
|
@ -165,7 +172,9 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
############################################################################################
|
||||||
|
private ##################################################################################
|
||||||
|
############################################################################################
|
||||||
|
|
||||||
def find_title(url)
|
def find_title(url)
|
||||||
@title_finder.find_title(url)
|
@title_finder.find_title(url)
|
||||||
|
|
@ -182,6 +191,7 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_posts(post_dir, extra_fields = nil)
|
def read_posts(post_dir, extra_fields = nil)
|
||||||
|
update_if_needed
|
||||||
extra_fields ||= {}
|
extra_fields ||= {}
|
||||||
post_data = read_post_data(post_path(post_dir))
|
post_data = read_post_data(post_path(post_dir))
|
||||||
post_data.sort_by do |k, v|
|
post_data.sort_by do |k, v|
|
||||||
|
|
@ -198,6 +208,7 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_post(post_dir, id, extra_fields = nil)
|
def read_post(post_dir, id, extra_fields = nil)
|
||||||
|
update_if_needed
|
||||||
post_filename = post_path(post_dir, id)
|
post_filename = post_path(post_dir, id)
|
||||||
post_data = read_post_data(post_path(post_dir))
|
post_data = read_post_data(post_path(post_dir))
|
||||||
if File.exist?(post_filename) && fields = post_data[id]
|
if File.exist?(post_filename) && fields = post_data[id]
|
||||||
|
|
@ -222,13 +233,10 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_post(action, post)
|
def save_post(action, post)
|
||||||
git_fetch
|
update_if_needed
|
||||||
git_reset_hard('origin/master')
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
write_post(post)
|
write_post(post)
|
||||||
git_commit(action, post.title, post_path(post.dir))
|
git_commit(action, post.title, post_path(post.dir))
|
||||||
git_push
|
|
||||||
@mutated = true
|
@mutated = true
|
||||||
post
|
post
|
||||||
|
|
||||||
|
|
@ -240,6 +248,15 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def delete_post_from_dir(post_dir, id)
|
||||||
|
update_if_needed
|
||||||
|
post_dir = post_path(post_dir)
|
||||||
|
delete_post_body(post_dir, id)
|
||||||
|
delete_post_index(post_dir, id)
|
||||||
|
git_commit('delete', id, post_path(post_dir))
|
||||||
|
@mutated = true
|
||||||
|
end
|
||||||
|
|
||||||
def write_post(post)
|
def write_post(post)
|
||||||
post_dir = post_path(post.dir)
|
post_dir = post_path(post.dir)
|
||||||
unless post.draft?
|
unless post.draft?
|
||||||
|
|
@ -256,13 +273,6 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_post_from_dir(post_dir, id)
|
|
||||||
post_dir = post_path(post_dir)
|
|
||||||
delete_post_body(post_dir, id)
|
|
||||||
delete_post_index(post_dir, id)
|
|
||||||
@mutated = true
|
|
||||||
end
|
|
||||||
|
|
||||||
def write_post_body(dir, id, body)
|
def write_post_body(dir, id, body)
|
||||||
post_filename = File.join(dir, "#{id}.md")
|
post_filename = File.join(dir, "#{id}.md")
|
||||||
write_file(post_filename, body)
|
write_file(post_filename, body)
|
||||||
|
|
@ -370,6 +380,7 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
|
|
||||||
def git_sha
|
def git_sha
|
||||||
|
update_if_needed
|
||||||
run('git log -n1 | head -n1 | cut -d" " -f2', :nondestructive).strip
|
run('git log -n1 | head -n1 | cut -d" " -f2', :nondestructive).strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -379,18 +390,25 @@ class HarpBlog
|
||||||
run("git add -A #{quoted_files.join(' ')} && git commit -m \"#{message}\"")
|
run("git add -A #{quoted_files.join(' ')} && git commit -m \"#{message}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def git_fetch
|
|
||||||
run('git fetch')
|
|
||||||
end
|
|
||||||
|
|
||||||
def git_reset_hard(ref = nil)
|
def git_reset_hard(ref = nil)
|
||||||
args = ref ? "'#{ref}'" : ''
|
args = ref ? "'#{ref}'" : ''
|
||||||
run("git reset --hard #{args}")
|
run("git reset --hard #{args}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def git_push(force = false)
|
def git_push
|
||||||
args = force ? '-f' : ''
|
run("git push")
|
||||||
run("git push #{args}")
|
end
|
||||||
|
|
||||||
|
def git_update(remote = 'origin')
|
||||||
|
run("git update #{remote} && rm origin-updated")
|
||||||
|
end
|
||||||
|
|
||||||
|
def origin_updated?
|
||||||
|
File.exist?('origin-updated')
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_if_needed
|
||||||
|
git_update if origin_updated?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,18 @@ post '/publish' do
|
||||||
status 204
|
status 204
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# sync with github
|
||||||
|
post '/sync' do
|
||||||
|
unless authenticated?(request.env['HTTP_AUTH'])
|
||||||
|
status 403
|
||||||
|
return 'forbidden'
|
||||||
|
end
|
||||||
|
|
||||||
|
blog.sync
|
||||||
|
status 204
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
# list years
|
# list years
|
||||||
get '/years' do
|
get '/years' do
|
||||||
status 200
|
status 200
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue