forked from uroesch/asciidoctor-readthedocs-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
128 lines (111 loc) · 3.34 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require 'yaml'
require 'erb'
require 'etc'
require 'ostruct'
# -----------------------------------------------------------------------------
# Global constants
# -----------------------------------------------------------------------------
BASE_DIR = File.dirname(__FILE__)
SRC_DIR = File.join(BASE_DIR, '_src')
FACTORY_DIR = File.join(BASE_DIR, 'factory')
STYLES_DIR = BASE_DIR
DOCKER_IMAGE = 'uroesch/docker-asciidoctor:latest'
# -----------------------------------------------------------------------------
# Methods
# -----------------------------------------------------------------------------
def inside_docker?
File.exist?('/.dockerenv')
end
def docker_asciidoctor(program = 'asciidoctor', **kwargs)
return program if inside_docker?
workdir = kwargs.fetch(:workdir, BASE_DIR)
docker_options = [
'--rm',
'--tty',
'--interactive',
"--mount type=bind,src=#{BASE_DIR},target=#{BASE_DIR}",
"--workdir=#{workdir}",
"--user #{Process.uid}:#{Process.gid}",
DOCKER_IMAGE,
program
]
"docker run " << docker_options.join(' ')
end
def asciidoctor_pdf
docker_asciidoctor('asciidoctor-pdf')
end
def asciidoctor
docker_asciidoctor
end
def compass
docker_asciidoctor('compass', workdir: FACTORY_DIR)
end
def to_filename(file)
file.pathmap('%f')
end
def to_basename(file)
file.pathmap('%n')
end
# -----------------------------------------------------------------------------
# Tasks
# -----------------------------------------------------------------------------
task :default => 'css:default'
task :clean => 'css:clean'
task :hooks => 'git_hooks:default'
task :test do
sh %(#{asciidoctor} ) +
%(-a stylesdir="#{BASE_DIR}" ) +
%(-a stylesheet=readthedocs.css ) +
%(-o samples/article.html ) +
%(test/article.adoc )
# create a standard stylesheet version for comparison.
sh %(#{asciidoctor} ) +
%(-o samples/standard-article.html ) +
%(test/article.adoc )
end
# -----------------------------------------------------------------------------
# Namespaces
# -----------------------------------------------------------------------------
namespace :css do
factory_url = 'https://github.com/asciidoctor/asciidoctor-stylesheet-factory.git'
factory_dir = to_basename(FACTORY_DIR)
sass_dir = File.join(factory_dir, 'sass')
desc "Clean css creation"
task :clean do
if File.exist?('.gitmodules') && File.size('.gitmodules') > 0
sh %(git submodule deinit --force #{factory_dir})
end
sh %(git rm -rf #{factory_dir}) if File.exist?(factory_dir)
sh %(git rm --cached .gitmodules 2>/dev/null) do |x,y| end
end
task :clone_factory do
next if File.exist?(factory_dir)
sh %(git submodule add --force #{factory_url} #{factory_dir})
end
task :clean_scss => :clone_factory do
rm_rf sass_dir
end
task :copy_scss => :clean_scss do
sh %(cp -va #{SRC_DIR}/sass #{factory_dir})
end
task :default => :copy_scss do
# cd into the directory
# when running inside docker
cd factory_dir do
sh %(#{compass} compile --css-dir #{STYLES_DIR} -s compressed)
end
end
end
namespace :git_hooks do
task :pre_commit do
pre_commit = <<~HOOK
#!/usr/bin/env bash
# !! managed by rake !!
rake css:clean
HOOK
File.open('.git/hooks/pre-commit', 'w') do |fh|
fh.write(pre_commit)
end
end
task :default => :pre_commit
end