mirror of
https://github.com/jimeh/build-emacs-for-macos.git
synced 2026-02-19 13:06:38 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
8d165b627b
|
|||
|
9c977a75b5
|
52
bin/commit-info
Executable file
52
bin/commit-info
Executable file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'optparse'
|
||||||
|
|
||||||
|
require_relative '../lib/commit_info'
|
||||||
|
require_relative '../lib/errors'
|
||||||
|
require_relative '../lib/log'
|
||||||
|
|
||||||
|
options = {
|
||||||
|
repo: 'emacs-mirror/emacs',
|
||||||
|
output: File.expand_path('../tarballs', __dir__),
|
||||||
|
log_level: :info
|
||||||
|
}
|
||||||
|
|
||||||
|
OptionParser.new do |opts|
|
||||||
|
opts.banner = <<~TXT
|
||||||
|
Usage: ./commit-info [options] <branch/tag/sha>
|
||||||
|
|
||||||
|
Fetch commit info of given GitHub repository branch, tag, or SHA.
|
||||||
|
|
||||||
|
Options:
|
||||||
|
TXT
|
||||||
|
|
||||||
|
opts.on('-r', '--repo STRING',
|
||||||
|
"GitHub repository (default: #{options[:repo]})") do |v|
|
||||||
|
options[:repo] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.on('-o', '--output DIR', 'Directory to save tarball in ' \
|
||||||
|
"(default: #{options[:output]})") do |v|
|
||||||
|
options[:output] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.on('-l', '--log-level LEVEL', 'Log level ' \
|
||||||
|
"(default: #{options[:log_level]})") do |v|
|
||||||
|
options[:log_level] = v.to_sym
|
||||||
|
end
|
||||||
|
end.parse!
|
||||||
|
|
||||||
|
begin
|
||||||
|
logger = Log.new('commit-info', options[:log_level])
|
||||||
|
commit = CommitInfo.new(
|
||||||
|
ref: ARGV[0],
|
||||||
|
repo: options[:repo],
|
||||||
|
logger: logger
|
||||||
|
).perform
|
||||||
|
|
||||||
|
puts commit.to_yaml
|
||||||
|
rescue Error => e
|
||||||
|
handle_error(e)
|
||||||
|
end
|
||||||
53
bin/download-tarball
Executable file
53
bin/download-tarball
Executable file
@@ -0,0 +1,53 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'optparse'
|
||||||
|
|
||||||
|
require_relative '../lib/download_tarball'
|
||||||
|
require_relative '../lib/errors'
|
||||||
|
require_relative '../lib/log'
|
||||||
|
|
||||||
|
options = {
|
||||||
|
repo: 'emacs-mirror/emacs',
|
||||||
|
output: File.expand_path('../tarballs', __dir__),
|
||||||
|
log_level: :info
|
||||||
|
}
|
||||||
|
|
||||||
|
OptionParser.new do |opts|
|
||||||
|
opts.banner = <<~TXT
|
||||||
|
Usage: ./download-tarball [options] <branch/tag/sha>
|
||||||
|
|
||||||
|
Download a tarball of given GitHub repository branch, tag, or SHA.
|
||||||
|
|
||||||
|
Options:
|
||||||
|
TXT
|
||||||
|
|
||||||
|
opts.on('-r', '--repo STRING',
|
||||||
|
"GitHub repository (default: #{options[:repo]})") do |v|
|
||||||
|
options[:repo] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.on('-o', '--output DIR', 'Directory to save tarball in ' \
|
||||||
|
"(default: #{options[:output]})") do |v|
|
||||||
|
options[:output] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.on('-l', '--log-level LEVEL', 'Log level ' \
|
||||||
|
"(default: #{options[:log_level]})") do |v|
|
||||||
|
options[:log_level] = v.to_sym
|
||||||
|
end
|
||||||
|
end.parse!
|
||||||
|
|
||||||
|
begin
|
||||||
|
logger = Log.new('download-tarball', options[:log_level])
|
||||||
|
tarball = DownloadTarball.new(
|
||||||
|
ref: ARGV[0],
|
||||||
|
repo: options[:repo],
|
||||||
|
output: options[:output],
|
||||||
|
logger: logger
|
||||||
|
).perform
|
||||||
|
|
||||||
|
puts tarball.to_yaml
|
||||||
|
rescue Error => e
|
||||||
|
handle_error(e)
|
||||||
|
end
|
||||||
9
lib/base_action.rb
Normal file
9
lib/base_action.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative './common'
|
||||||
|
require_relative './output'
|
||||||
|
|
||||||
|
class BaseAction
|
||||||
|
include Common
|
||||||
|
include Output
|
||||||
|
end
|
||||||
46
lib/commit.rb
Normal file
46
lib/commit.rb
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'json'
|
||||||
|
require 'time'
|
||||||
|
require 'yaml'
|
||||||
|
|
||||||
|
require_relative './errors'
|
||||||
|
require_relative './common'
|
||||||
|
|
||||||
|
class Commit
|
||||||
|
include Common
|
||||||
|
|
||||||
|
attr_reader :repo
|
||||||
|
attr_reader :ref
|
||||||
|
attr_reader :message
|
||||||
|
attr_reader :sha
|
||||||
|
attr_reader :time
|
||||||
|
|
||||||
|
def initialize(sha:, time:, repo: nil, ref: nil, message: nil)
|
||||||
|
@sha = sha
|
||||||
|
@time = time
|
||||||
|
@repo = repo
|
||||||
|
@ref = ref
|
||||||
|
@message = message
|
||||||
|
end
|
||||||
|
|
||||||
|
def sha_short
|
||||||
|
sha[0..6]
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
{
|
||||||
|
'repo' => repo,
|
||||||
|
'ref' => ref,
|
||||||
|
'sha' => sha,
|
||||||
|
'sha_short' => sha_short,
|
||||||
|
'time' => time.utc,
|
||||||
|
'timestamp' => time.utc.to_i,
|
||||||
|
'message' => message
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_yaml
|
||||||
|
to_hash.to_yaml
|
||||||
|
end
|
||||||
|
end
|
||||||
43
lib/commit_info.rb
Normal file
43
lib/commit_info.rb
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative './base_action'
|
||||||
|
require_relative './commit'
|
||||||
|
|
||||||
|
class CommitInfo < BaseAction
|
||||||
|
COMMIT_URL = 'https://api.github.com/repos/%s/commits/%s'
|
||||||
|
|
||||||
|
attr_reader :ref
|
||||||
|
attr_reader :repo
|
||||||
|
attr_reader :logger
|
||||||
|
|
||||||
|
def initialize(ref:, repo:, logger:)
|
||||||
|
@ref = ref
|
||||||
|
@repo = repo
|
||||||
|
@logger = logger
|
||||||
|
|
||||||
|
err 'branch/tag/sha argument cannot be empty' if ref.nil? || ref.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def perform
|
||||||
|
info "Fetching info for git ref: #{ref}"
|
||||||
|
|
||||||
|
url = format(COMMIT_URL, repo, ref)
|
||||||
|
commit_json = http_get(url)
|
||||||
|
|
||||||
|
err "Failed to get commit info about: #{ref}" if commit_json.nil?
|
||||||
|
|
||||||
|
parsed = JSON.parse(commit_json)
|
||||||
|
commit = Commit.new(
|
||||||
|
repo: repo,
|
||||||
|
ref: ref,
|
||||||
|
sha: parsed&.dig('sha'),
|
||||||
|
message: parsed&.dig('commit', 'message'),
|
||||||
|
time: Time.parse(parsed&.dig('commit', 'committer', 'date')).utc
|
||||||
|
)
|
||||||
|
|
||||||
|
err 'Failed to get commit SHA' if commit.sha.nil? || commit.sha.empty?
|
||||||
|
err 'Failed to get commit time' if commit.time.nil?
|
||||||
|
|
||||||
|
commit
|
||||||
|
end
|
||||||
|
end
|
||||||
23
lib/common.rb
Normal file
23
lib/common.rb
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'net/http'
|
||||||
|
|
||||||
|
module Common
|
||||||
|
private
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
def run_cmd(*args)
|
||||||
|
info "executing: #{args.join(' ')}"
|
||||||
|
system(*args) || err("Exit code: #{$CHILD_STATUS.exitstatus}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def http_get(url)
|
||||||
|
response = Net::HTTP.get_response(URI.parse(url))
|
||||||
|
return unless response.code == '200'
|
||||||
|
|
||||||
|
response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
61
lib/download_tarball.rb
Normal file
61
lib/download_tarball.rb
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'fileutils'
|
||||||
|
require 'json'
|
||||||
|
require 'time'
|
||||||
|
|
||||||
|
require_relative './base_action'
|
||||||
|
|
||||||
|
require_relative './commit_info'
|
||||||
|
require_relative './tarball'
|
||||||
|
|
||||||
|
class DownloadTarball < BaseAction
|
||||||
|
TARBALL_URL = 'https://github.com/%s/tarball/%s'
|
||||||
|
|
||||||
|
attr_reader :ref
|
||||||
|
attr_reader :repo
|
||||||
|
attr_reader :output
|
||||||
|
attr_reader :logger
|
||||||
|
|
||||||
|
def initialize(ref:, repo:, output:, logger:)
|
||||||
|
@ref = ref
|
||||||
|
@repo = repo
|
||||||
|
@output = output
|
||||||
|
@logger = logger
|
||||||
|
|
||||||
|
err 'branch/tag/sha argument cannot be empty' if ref.nil? || ref.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def perform
|
||||||
|
FileUtils.mkdir_p(output)
|
||||||
|
tarball = Tarball.new(file: target, commit: commit)
|
||||||
|
|
||||||
|
if File.exist?(target)
|
||||||
|
info "#{filename} already exists locally, attempting to use."
|
||||||
|
return tarball
|
||||||
|
end
|
||||||
|
|
||||||
|
info 'Downloading tarball from GitHub. This could take a while, ' \
|
||||||
|
'please be patient.'
|
||||||
|
result = run_cmd('curl', '-L', url, '-o', target)
|
||||||
|
err 'Download failed.' if !result || !File.exist?(target)
|
||||||
|
|
||||||
|
tarball
|
||||||
|
end
|
||||||
|
|
||||||
|
def url
|
||||||
|
@url ||= format(TARBALL_URL, repo, commit.sha)
|
||||||
|
end
|
||||||
|
|
||||||
|
def filename
|
||||||
|
@filename ||= "#{repo.gsub(/[^\w]/, '-')}-#{commit.sha_short}.tgz"
|
||||||
|
end
|
||||||
|
|
||||||
|
def target
|
||||||
|
@target ||= File.join(output, filename)
|
||||||
|
end
|
||||||
|
|
||||||
|
def commit
|
||||||
|
@commit ||= CommitInfo.new(ref: ref, repo: repo, logger: logger).perform
|
||||||
|
end
|
||||||
|
end
|
||||||
12
lib/errors.rb
Normal file
12
lib/errors.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
def handle_error(err)
|
||||||
|
warn "ERROR: #{err.message}"
|
||||||
|
Process.exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
class Error < StandardError; end
|
||||||
|
|
||||||
|
class CommitNotFound < Error; end
|
||||||
|
class NoCommitSHA < Error; end
|
||||||
|
class NoCommitTime < Error; end
|
||||||
31
lib/log.rb
Normal file
31
lib/log.rb
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Log
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
attr_reader :name
|
||||||
|
attr_reader :level
|
||||||
|
|
||||||
|
def initialize(name, level = :info)
|
||||||
|
@name = name
|
||||||
|
@level = level
|
||||||
|
end
|
||||||
|
|
||||||
|
def_delegators :logger, :debug, :info, :warn, :error, :fatal, :unkonwn
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def logger
|
||||||
|
@logger ||= Logger.new($stderr).tap do |l|
|
||||||
|
l.progname = name
|
||||||
|
l.level = level
|
||||||
|
l.formatter = formatter
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def formatter
|
||||||
|
proc do |severity, _datetime, progname, msg|
|
||||||
|
"==> [#{progname}] #{severity}: #{msg}\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
28
lib/output.rb
Normal file
28
lib/output.rb
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'forwardable'
|
||||||
|
require 'logger'
|
||||||
|
|
||||||
|
require_relative './errors'
|
||||||
|
|
||||||
|
module Output
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend(ClassMethods)
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def logger_name(name = nil)
|
||||||
|
return @logger_name if name.nil?
|
||||||
|
|
||||||
|
@logger_name = name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def_delegators :logger, :debug, :info, :warn, :error, :fatal, :unkonwn
|
||||||
|
|
||||||
|
def err(msg = nil)
|
||||||
|
raise Error, msg
|
||||||
|
end
|
||||||
|
end
|
||||||
24
lib/tarball.rb
Normal file
24
lib/tarball.rb
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'yaml'
|
||||||
|
|
||||||
|
class Tarball
|
||||||
|
attr_reader :file
|
||||||
|
attr_reader :commit
|
||||||
|
|
||||||
|
def initialize(file:, commit:)
|
||||||
|
@file = file
|
||||||
|
@commit = commit
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
{
|
||||||
|
'file' => file,
|
||||||
|
'commit' => commit.to_hash
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_yaml
|
||||||
|
to_hash.to_yaml
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user