lib/gitdocs/runner.rb in gitdocs-0.3.6 vs lib/gitdocs/runner.rb in gitdocs-0.4.0

- old
+ new

@@ -16,35 +16,48 @@ out, status = sh_with_code "which growlnotify" @use_growl = @share.notification && status.success? @current_remote = @share.remote_name @current_branch = @share.branch_name @current_revision = sh_string("git rev-parse HEAD") + mutex = Mutex.new info("Running gitdocs!", "Running gitdocs in `#{@root}'") - mutex = Mutex.new # Pull changes from remote repository - Thread.new do - loop do + syncer = proc do + EM.defer(proc do mutex.synchronize { sync_changes } - sleep @polling_interval - end - end.abort_on_exception = true - - # Listen for changes in local repository - @listener = FSEvent.new - @listener.watch(@root) do |directories| - directories.uniq! - directories.delete_if {|d| d =~ /\/\.git/} - unless directories.empty? - mutex.synchronize { push_changes } - end + end, proc do + EM.add_timer(@polling_interval) { + syncer.call + } + end) end - at_exit { @listener.stop } - @listener.run + syncer.call + # Listen for changes in local repository + + EM.defer(proc{ + listener = Guard::Listener.select_and_init(@root, :watch_all_modifications => true) + listener.on_change { |directories| + directories.uniq! + directories.delete_if {|d| d =~ /\/\.git/} + unless directories.empty? + EM.next_tick do + EM.defer(proc { + mutex.synchronize { push_changes } + }, proc {} ) + end + end + } + listener.start + }, proc{EM.stop_reactor}) end + def clear_state + @state = nil + end + def sync_changes out, status = sh_with_code("git fetch --all && git merge #{@current_remote}/#{@current_branch}") if status.success? changes = get_latest_changes unless changes.empty? @@ -111,9 +124,31 @@ lines end else [] end + end + + IGNORED_FILES = ['.gitignore'] + # dir_files("some/dir") => [<Docfile>, <Docfile>] + def dir_files(dir) + dir_path = File.expand_path(dir, @root) + files = {} + ls_files = sh_string("git ls-files").split("\n").map { |f| Docfile.new(f) } + ls_files.select { |f| f.within?(dir, @root) }.each do |f| + path = File.expand_path(f.parent, root) + files[path] ||= Docdir.new(path) + files[path].files << f unless IGNORED_FILES.include?(f.name) + end + files.keys.each { |f| files[f].parent = files[File.dirname(f)] } + files[dir_path] + end + + def file_meta(file) + file = file.gsub(%r{^/}, '') + author, modified = sh_string("git log --format='%aN|%ai' -n1 #{ShellTools.escape(file)}").split("|") + modified = Time.parse(modified.sub(' ', 'T')).utc.iso8601 + { :author => author, :modified => modified } end def valid? out, status = sh_with_code "git status" status.success?