Class: GitParser

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

Overview

Shells out to git in the working tree to gather repo info (latest
commit on the static-pages paths, file last-changed timestamps,
etc.) so the public website can show "last updated" timestamps and
the CRM can surface deployment metadata. Restricted to the paths
listed in STATIC_PAGES_PATH.

Constant Summary collapse

STATIC_PAGES_PATH =

Filesystem/URL path for static pages.

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GitParser

Returns a new instance of GitParser.



48
49
50
51
# File 'lib/git_parser.rb', line 48

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

Class Method Details

.get_repo_info(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/git_parser.rb', line 20

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/git_parser.rb', line 32

def self.write_repo_info_flags
  git_info_results = get_repo_info

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

  Rails.root.join('etag_version_id.txt').open('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



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

def find_git_info(*paths)
  git_info = {}
  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? || (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



57
58
59
60
61
62
63
# File 'lib/git_parser.rb', line 57

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

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

#loggerObject



53
54
55
# File 'lib/git_parser.rb', line 53

def logger
  @options[:logger]
end