Class: GitParser

Inherits:
Object
  • Object
show all
Defined in:
lib/git_parser.rb

Constant Summary collapse

STATIC_PAGES_PATH =
['app/views/pages',
'app/views/layouts/cms_homepage.html.erb',
'client/js/www',
'client/stylesheets/www',
'client/js/common',
'app/controllers/pages_controller.rb']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GitParser

Returns a new instance of GitParser.



41
42
43
44
# File 'lib/git_parser.rb', line 41

def initialize(options = {})
  @options = options
  @options[:logger] ||= ActiveSupport::Logger.new(STDOUT)
end

Class Method Details

.get_repo_info(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/git_parser.rb', line 13

def self.get_repo_info(options = {})
  gp = new(options)
  git_info_results = gp.find_git_info(STATIC_PAGES_PATH)

  {
    page_repo_last_modified: git_info_results[:last_modified],
    etag_version_id: git_info_results[:commit_sha]
  }
end

.write_repo_info_flagsObject

Write files locally with repo information for static pages
Simulating the web_cache.rake capistrano formula



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/git_parser.rb', line 25

def self.write_repo_info_flags
  git_info_results = get_repo_info

  File.open(Rails.root.join('page_last_modified.txt'), 'w') do |f|
    f.write git_info_results[:page_repo_last_modified]
    f.flush
    f.fsync
  end

  File.open(Rails.root.join('etag_version_id.txt'), 'wb') do |file|
    file.write(git_info_results[:etag_version_id])
    file.flush
    file.fsync
  end
end

Instance Method Details

#find_git_info(*paths) ⇒ Object

load 'lib/git_parser.rb';GitParser.get_repo_info



59
60
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
# File 'lib/git_parser.rb', line 59

def find_git_info(*paths)
  git_info = {}
  results = paths.flatten.map do |path|
    res = get_git_log(path).presence
    # logger.info "[find_git_info] result: [#{res}] (#{res.class.name})"
    if res
      date_raw = begin
        res.match(/\bDate:(.+)\b/)[1].squish
      rescue StandardError
        nil
      end
      commit_sha = begin
        res.match(/\bcommit(.+)\b/)[1].squish
      rescue StandardError
        nil
      end
      timestamp = Time.current # Fallback default
      if date_raw
        # logger.debug "date_raw: #{date_raw} #{date_raw.inspect}, #{date_raw.class.name}"
        # logger.debug "Time: #{Time} #{Time.inspect}, #{Time.public_methods}"
        timestamp = Time.zone.parse(date_raw)
        # logger.info " ** Repo Timestamp detected as #{timestamp}"
      end
      if git_info[:last_modified].nil? or git_info[:last_modified] < timestamp
        git_info[:last_modified] = timestamp
        git_info[:commit_sha] = commit_sha
      end
    else
      logger.error " *GitParser could not find or execute #{path}"
    end
  end
  # return the last modification date and commit sha
  logger.info "Git Info for #{paths.inspect} -> #{git_info.inspect}"
  git_info
end

#get_git_log(path) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/git_parser.rb', line 50

def get_git_log(path)
  `git log -1 --follow #{path}`
rescue StandardError
  nil

  # logger.info "Last git commit on #{path}: #{res}"
end

#loggerObject



46
47
48
# File 'lib/git_parser.rb', line 46

def logger
  @options[:logger]
end