jekyll: use git to fetch remote resources

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-10-26 02:05:56 +02:00
parent 1a0f070e2c
commit 06908b36f5
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
4 changed files with 88 additions and 84 deletions

View File

@ -47,6 +47,7 @@ ARG JEKYLL_ENV
ARG DOCS_URL ARG DOCS_URL
ENV TARGET=/out ENV TARGET=/out
RUN --mount=type=bind,target=.,rw \ RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/tmp/docker-docs-clone \
--mount=type=cache,target=/src/.jekyll-cache <<EOT --mount=type=cache,target=/src/.jekyll-cache <<EOT
set -eu set -eu
CONFIG_FILES=_config.yml$([ "$JEKYLL_ENV" = "production" ] && echo ",_config_production.yml" || true) CONFIG_FILES=_config.yml$([ "$JEKYLL_ENV" = "production" ] && echo ",_config_production.yml" || true)

View File

@ -16,8 +16,8 @@ end
# more info: https://github.com/docker/docs/issues/14788 # more info: https://github.com/docker/docs/issues/14788
gem 'rouge', '3.27.0' gem 'rouge', '3.27.0'
gem 'archive-zip', '0.12.0'
gem 'front_matter_parser', '1.0.1' gem 'front_matter_parser', '1.0.1'
gem 'git', '1.12.0'
gem 'html-proofer', '3.19.4' gem 'html-proofer', '3.19.4'
gem 'mdl', '0.11.0' gem 'mdl', '0.11.0'
gem 'octopress-hooks', '2.6.2' gem 'octopress-hooks', '2.6.2'

View File

@ -1,11 +1,9 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
addressable (2.8.0) addressable (2.8.1)
public_suffix (>= 2.0.2, < 5.0) public_suffix (>= 2.0.2, < 6.0)
archive-zip (0.12.0) chef-utils (18.0.161)
io-like (~> 0.3.0)
chef-utils (17.10.0)
concurrent-ruby concurrent-ruby
colorator (1.1.0) colorator (1.1.0)
concurrent-ruby (1.1.10) concurrent-ruby (1.1.10)
@ -18,6 +16,9 @@ GEM
ffi (1.15.5) ffi (1.15.5)
forwardable-extended (2.6.0) forwardable-extended (2.6.0)
front_matter_parser (1.0.1) front_matter_parser (1.0.1)
git (1.12.0)
addressable (~> 2.8)
rchardet (~> 1.8)
html-proofer (3.19.4) html-proofer (3.19.4)
addressable (~> 2.3) addressable (~> 2.3)
mercenary (~> 0.3) mercenary (~> 0.3)
@ -29,7 +30,6 @@ GEM
http_parser.rb (0.8.0) http_parser.rb (0.8.0)
i18n (1.12.0) i18n (1.12.0)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
io-like (0.3.1)
jekyll (4.2.2) jekyll (4.2.2)
addressable (~> 2.4) addressable (~> 2.4)
colorator (~> 1.0) colorator (~> 1.0)
@ -92,13 +92,14 @@ GEM
pathutil (0.16.2) pathutil (0.16.2)
forwardable-extended (~> 2.6) forwardable-extended (~> 2.6)
posix-spawn (0.3.15) posix-spawn (0.3.15)
public_suffix (4.0.7) public_suffix (5.0.0)
racc (1.6.0) racc (1.6.0)
rainbow (3.1.1) rainbow (3.1.1)
rake (13.0.6) rake (13.0.6)
rb-fsevent (0.11.1) rb-fsevent (0.11.2)
rb-inotify (0.10.1) rb-inotify (0.10.1)
ffi (~> 1.0) ffi (~> 1.0)
rchardet (1.8.0)
rexml (3.2.5) rexml (3.2.5)
rouge (3.27.0) rouge (3.27.0)
safe_yaml (1.0.5) safe_yaml (1.0.5)
@ -118,8 +119,8 @@ PLATFORMS
x86_64-linux x86_64-linux
DEPENDENCIES DEPENDENCIES
archive-zip (= 0.12.0)
front_matter_parser (= 1.0.1) front_matter_parser (= 1.0.1)
git (= 1.12.0)
html-proofer (= 3.19.4) html-proofer (= 3.19.4)
jekyll (= 4.2.2) jekyll (= 4.2.2)
jekyll-last-modified-at jekyll-last-modified-at

View File

@ -1,9 +1,8 @@
require 'archive/zip'
require 'front_matter_parser' require 'front_matter_parser'
require 'git'
require 'jekyll' require 'jekyll'
require 'json' require 'json'
require 'octopress-hooks' require 'octopress-hooks'
require 'open-uri'
require 'rake' require 'rake'
require_relative 'util.rb' require_relative 'util.rb'
@ -12,14 +11,6 @@ module Jekyll
class FetchRemote < Octopress::Hooks::Site class FetchRemote < Octopress::Hooks::Site
priority :highest priority :highest
def self.download(url, dest)
uri = URI.parse(url)
result = File.join(dest, File.basename(uri.path))
puts " Downloading #{url}"
IO.copy_stream(URI.open(url), result)
return result
end
def self.copy(src, dest) def self.copy(src, dest)
if (tmp = Array.try_convert(src)) if (tmp = Array.try_convert(src))
tmp.each do |s| tmp.each do |s|
@ -50,16 +41,29 @@ module Jekyll
beginning_time = Time.now beginning_time = Time.now
puts "Starting plugin fetch_remote.rb..." puts "Starting plugin fetch_remote.rb..."
site.config['fetch-remote'].each do |entry| site.config['fetch-remote'].each do |entry|
puts " Repo #{entry['repo']} (#{entry['ref']})" puts " Repo #{entry['repo']}"
Dir.mktmpdir do |tmpdir|
tmpfile = FetchRemote.download("#{entry['repo']}/archive/#{entry['ref']}.zip", tmpdir) gituri = Git::URL.parse(entry['repo'])
Dir.mktmpdir do |ztmpdir| clonedir = "#{Dir.tmpdir}/docker-docs-clone#{gituri.path}"
puts " Extracting #{tmpfile}" if Dir.exist?(clonedir)
Archive::Zip.extract( puts " Opening #{clonedir}"
tmpfile, begin
ztmpdir, git = Git.open(clonedir)
:create => true git.chdir do
) puts " Fetching #{entry['ref']}"
git.checkout(entry['ref'])
git.fetch
end
rescue => e
FileUtils.rm_rf(clonedir)
puts " Cloning repository into #{clonedir}"
Git.clone("#{entry['repo']}.git", Pathname.new(clonedir), branch: entry['ref'])
end
else
puts " Cloning repository into #{clonedir}"
Git.clone("#{entry['repo']}.git", Pathname.new(clonedir), branch: entry['ref'])
end
entry['paths'].each do |path| entry['paths'].each do |path|
if File.extname(path['dest']) != "" if File.extname(path['dest']) != ""
if path['src'].size > 1 if path['src'].size > 1
@ -76,9 +80,9 @@ module Jekyll
files = FileList[] files = FileList[]
path['src'].each do |src| path['src'].each do |src|
if "#{src}".start_with?("!") if "#{src}".start_with?("!")
files.exclude(File.join(ztmpdir, "*/"+"#{src}".delete_prefix("!"))) files.exclude(File.join(clonedir, "/"+"#{src}".delete_prefix("!")))
else else
files.include(File.join(ztmpdir, "*/#{src}")) files.include(File.join(clonedir, "/#{src}"))
end end
end end
@ -87,7 +91,7 @@ module Jekyll
s = File.realpath(s) s = File.realpath(s)
# traverse source directory # traverse source directory
FileUtils::Entry_.new(s, nil, false).wrap_traverse(proc do |ent| FileUtils::Entry_.new(s, nil, false).wrap_traverse(proc do |ent|
file_clean = ent.path.delete_prefix(ztmpdir).split("/").drop(2).join("/") file_clean = ent.path.delete_prefix(clonedir).split("/").drop(1).join("/")
destent = FileUtils::Entry_.new(d, ent.rel, false) destent = FileUtils::Entry_.new(d, ent.rel, false)
puts " #{file_clean} => #{destent.path}" puts " #{file_clean} => #{destent.path}"
@ -121,8 +125,6 @@ module Jekyll
end end
end end
end end
end
end
end_time = Time.now end_time = Time.now
puts "done in #{(end_time - beginning_time)} seconds" puts "done in #{(end_time - beginning_time)} seconds"