-
Notifications
You must be signed in to change notification settings - Fork 8
/
tasks.py
253 lines (202 loc) · 6.22 KB
/
tasks.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from __future__ import unicode_literals
import datetime
import io
import json
import os
import shutil
import invoke
import six
from lektor.builder import Builder
from lektor.cli import Context
from lektor.db import F
from lektor.devserver import run_server
from lektor.project import Project
from lektor.reporter import CliReporter, reporter
if six.PY2:
input = raw_input # noqa
ROOT_DIR = os.path.dirname(__file__)
OUTPUT_DIR = os.path.join(ROOT_DIR, 'build')
def padright(s, unit):
pad = ' ' * (unit - len(s) % unit)
return '{}{}'.format(s, pad)
def get_static_redirect():
redirects_fn = os.path.join(ROOT_DIR, 'redirects.txt')
with io.open(redirects_fn, encoding='utf8') as f:
return f.read()
def generate_download_redirects(pad):
for page in pad.query('/download'):
yield '{}{}'.format(
padright('/download/v{}/'.format(page['version']), 24),
page['download_url'],
)
def get_latest_download_redirect(pad):
latest = (
pad
.query('/download')
.filter(F.channels.contains('stable'))
.order_by('-build_number')
.first()
)
if not latest:
return None
return '{}{}302'.format(
padright('/download/latest/', 24),
padright(latest['download_url'], 8),
)
def generate_blog_post_redirects(pad):
for post in pad.query('/blog'):
redirect_url = post.url_path
yield '{}{}'.format( # For Disqus.
padright('/blog/{}'.format(post['id']), 24),
redirect_url,
)
yield '{}{}'.format( # For Disqus.
padright('/blog/{}/*'.format(post['id']), 24),
redirect_url,
)
yield '{}{}'.format( # Backward compatibility to the Django site.
padright('/blog/post/{}'.format(post['id']), 24),
redirect_url,
)
yield '{}{}'.format( # Backward compatibility to the Django site.
padright('/blog/post/{}/*'.format(post['id']), 24),
redirect_url,
)
@invoke.task
def serve(ctx):
lektor_cli_ctx = Context()
lektor_cli_ctx.load_plugins()
env = lektor_cli_ctx.get_env()
run_server(
('127.0.0.1', 5000), env=env, output_path=OUTPUT_DIR, verbosity=0,
)
@invoke.task
def remove(ctx, relpath):
lektor_cli_ctx = Context()
lektor_cli_ctx.load_plugins()
env = lektor_cli_ctx.get_env()
path = os.path.join(OUTPUT_DIR, relpath)
with CliReporter(env, verbosity=0), reporter.build('prune', None):
if os.path.exists(path):
reporter.report_pruned_artifact(relpath)
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
@invoke.task
def build(ctx, bust=False):
lektor_cli_ctx = Context()
if bust:
project = lektor_cli_ctx.get_project()
shutil.rmtree(project.get_package_cache_path(), ignore_errors=True)
shutil.rmtree(project.get_output_path(), ignore_errors=True)
lektor_cli_ctx.load_plugins()
env = lektor_cli_ctx.get_env()
pad = env.new_pad()
# This is essentially `lektor build --output-path build`.
with CliReporter(env, verbosity=0):
builder = Builder(pad, OUTPUT_DIR)
failures = builder.build_all()
if failures:
raise invoke.Exit('Builder failed.')
# Generate redirect file.
redirect_filename = os.path.join(OUTPUT_DIR, '_redirects')
with io.open(redirect_filename, mode='w', encoding='utf8') as f:
static_redirect = get_static_redirect()
f.write(static_redirect)
if not static_redirect.endswith('\n'):
f.write('\n')
f.write('\n')
f.write('# Blog posts.\n')
f.write('\n'.join(generate_blog_post_redirects(pad)))
f.write('\n')
f.write('\n')
f.write('# Download redirects.\n')
f.write('\n'.join(generate_download_redirects(pad)))
f.write('\n')
latest_redirect = get_latest_download_redirect(pad)
if latest_redirect is not None:
f.write('\n')
f.write('# Latests version download links.\n')
f.write(latest_redirect)
f.write('\n')
blog_post_template = """id: {id}
---
title:
---
author: Tzu-ping Chung
---
pub_date: {today}
---
content:
"""
@invoke.task
def post(ctx, slug=None):
project = Project.discover()
env = project.make_env()
pad = env.new_pad()
blog = pad.get('/blog')
latest_post = blog.children.first()
if latest_post is None:
next_id = 1
else:
next_id = latest_post['id'] + 1
if slug is None:
slug = input('Enter slug: ') or '__new_post__'
new_dirname = os.path.join(
ROOT_DIR, os.path.dirname(blog.source_filename), slug,
)
os.mkdir(new_dirname)
contents_fn = os.path.join(new_dirname, 'contents.lr')
with io.open(contents_fn, mode='w', encoding='utf8') as f:
f.write(blog_post_template.format(
id=next_id,
today=datetime.date.today().strftime(r'%Y-%m-%d'),
))
release_template = """_model: release
---
channels:
stable
testing
---
build_number: {build_number}
---
version: {version}
---
dsa_signature: {dsa_signature}
---
length: {length}
---
min_sysver: {min_sysver}
---
download_url: {download_url}
---
pub_datetime: {pub_datetime}
---
note:
"""
@invoke.task
def release(ctx, path_to_info):
project = Project.discover()
env = project.make_env()
pad = env.new_pad()
later = datetime.datetime.now() + datetime.timedelta(hours=1)
with io.open(path_to_info, encoding='utf8') as f:
info = json.load(f)
info.setdefault('min_sysver', '10.8')
info.setdefault('pub_datetime', later.strftime(r'%Y-%m-%d %H:%M:%S'))
info.setdefault(
'download_url',
'https://github.com/MacDownApp/macdown/releases/download/'
'v{version}/MacDown.app.zip'.format(version=info['version']),
)
download = pad.get('/download')
new_dirname = os.path.join(
ROOT_DIR,
os.path.dirname(download.source_filename),
info['build_number'],
)
os.mkdir(new_dirname)
contents_fn = os.path.join(new_dirname, 'contents.lr')
with io.open(contents_fn, mode='w', encoding='utf8') as f:
f.write(release_template.format(**info))