Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'pathname'
- require 'tmpdir'
- require 'git'
- require 'github_api'
- require 'json'
- require 'yaml'
- YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
- def fileitems(pattern = 'docs/**/README.md')
- Pathname.glob(pattern).select do |item|
- item if item.read =~ YAML_FRONT_MATTER_REGEXP
- end
- end
- def mktmpdir(prefix, &block)
- pwd = Dir.pwd
- Dir.mktmpdir(prefix) { |dir| Dir.chdir(dir, &block) }
- ensure
- Dir.chdir(pwd)
- end
- def jsonify(keys, args)
- JSON.pretty_generate(Hash[keys.zip(args)])
- end
- def meta(data)
- front_matter = data.scan(YAML_FRONT_MATTER_REGEXP).flatten.join
- YAML.safe_load(front_matter)
- end
- def last_commit(git_dir)
- base = Git::Base.open(git_dir)
- last_commit = Git::Lib.new(base).full_log_commits.first
- klass = Struct.new(*last_commit.keys.map(&:to_sym))
- klass.new(*last_commit.values)
- end
- def remote_vars(git_dir)
- git = Git.open(git_dir)
- r = git.remote
- [r.name, r.url]
- end
- if __FILE__ == $PROGRAM_NAME
- metas = []
- commit = last_commit('.')
- items = fileitems.map(&:dirname).map(&:basename)
- fileitems.each do |file|
- m = meta(file.read)
- m['last_commit'] = commit.sha
- m['tree'] = commit.tree
- m['remote'] = remote_vars('.')
- metas << m
- end
- puts jsonify(items, metas)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement