-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.lua
500 lines (474 loc) · 18.3 KB
/
build.lua
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
--[[
Configuration script for l3build from the scontents package
At the moment the possible options that can be passed on to
l3build are:
* tag : Update the version and date
* doc : Generate the documentation [-q]
* unpack : Unpacks the source files [-q]
* install : Install the package locally, you can use
it in conjunction with [--full] [--dry-run]
* uninstall : Uninstall the package locally
* clean : Clean the directory tree and repo
* ctan : Generate the compressed package (.zip)
* upload : Upload the package to ctan, you must add
-F ctan.ann in conjunction with [--debug]
* testpkg : Compile the tests included in the test-pkg/
* tagged : Check version and date in files
* examples : Compile the example files included in .dtx file
* release : It performs the checks before generating a public
release (on git and ctan).
--]]
-- General package identification
module = "scontents"
pkgversion = "2.1"
pkgdate = "2024-06-14"
-- Configuration of files for build and installation
maindir = "."
sourcefiledir = "./sources"
textfiledir = "./sources"
--textfiles = {textfiledir.."/CTANREADME.md"}
sourcefiles = {"**/*.dtx", "**/*.ins"}
installfiles = {"**/*.sty", "**/*.tex", "**/*.mkiv"}
tdslocations = {
"tex/generic/scontents/scontents.tex",
"tex/generic/scontents/scontents-code.tex",
"tex/latex/scontents/scontents.sty",
"tex/context/third/scontents/t-scontents.mkiv",
"doc/latex/scontents/scontents.pdf",
"doc/latex/scontents/README.md",
"source/latex/scontents/scontents.dtx",
"source/latex/scontents/scontents.ins"
}
-- Unpacking files from .ins file
unpackfiles = { "scontents.ins" }
unpackopts = "--interaction=batchmode"
unpackexe = "luatex"
-- Generating documentation
typesetfiles = {"scontents.dtx"}
-- Update package date and version
tagfiles = {"sources/scontents.ins", "sources/scontents.dtx", "sources/CTANREADME.md", "ctan.ann"}
local mydate = os.date("!%Y-%m-%d")
function update_tag(file,content,tagname,tagdate)
if not tagname and tagdate == mydate or "" then
tagname = pkgversion
tagdate = pkgdate
print("** "..file.." have been tagged with the version and date of build.lua")
else
local v_maj, v_min = string.match(tagname, "^v?(%d+)(%S*)$")
if v_maj == "" or not v_min then
print("Error!!: Invalid tag '"..tagname.."', none of the files have been tagged")
os.exit(0)
else
tagname = string.format("%i%s", v_maj, v_min)
tagdate = mydate
end
print("** "..file.." have been tagged with the version "..tagname.." and date "..mydate)
end
if string.match(file, "scontents.ins") then
content = string.gsub(content,
"date=%d%d%d%d%-%d%d%-%d%d",
"date="..tagdate)
content = string.gsub(content,
"version=%d%.%d%w?",
"version="..tagname)
end
if string.match(file, "scontents.dtx") then
content = string.gsub(content,
"\\ScontentsFileDate{(.-)}",
"\\ScontentsFileDate{"..tagdate.."}")
content = string.gsub(content,
"\\ScontentsCoreFileDate{(.-)}",
"\\ScontentsCoreFileDate{"..tagdate.."}")
content = string.gsub(content,
"\\ScontentsFileVersion{(.-)}",
"\\ScontentsFileVersion{"..tagname.."}")
end
if string.match(file, "CTANREADME.md") then
content = string.gsub(content,
"Release v%d+.%d+%a* \\%[%d%d%d%d%-%d%d%-%d%d\\%]",
"Release v"..tagname.." \\["..tagdate.."\\]")
end
if string.match(file,"ctan.ann") then
content = string.gsub(content,
"v%d%.%d%w? %d%d%d%d%-%d%d%-%d%d",
"v"..tagname..' '..tagdate)
end
return content
end
-- Configuration for ctan
ctanreadme = "CTANREADME.md"
ctanpkg = "scontents"
ctanzip = ctanpkg.."-"..pkgversion
packtdszip = false
-- Clean files
cleanfiles = {module..".pdf", ctanzip..".curlopt", ctanzip..".zip"}
-- Configuration for package distribution in ctan
uploadconfig = {
author = "Pablo González L",
uploader = "Pablo González L",
email = "pablgonz@yahoo.com",
pkg = ctanpkg,
version = pkgversion,
license = "lppl1.3c",
summary = "Stores LaTeX contents in memory or files",
description =[[This package stores valid LaTeX code in memory (sequences) using the l3seq module of expl3. The stored content (including verbatim) can be used as many times as desired in the document, additionally can be written to external files if desired.]],
topic = { "File management", "Experimental LaTeX3" },
ctanPath = "/macros/latex/contrib/" .. ctanpkg,
repository = "https://github.com/pablgonz/" .. module,
bugtracker = "https://github.com/pablgonz/" .. module .. "/issues",
support = "https://github.com/pablgonz/" .. module .. "/issues",
note = [[Uploaded automatically by l3build...]],
announcement_file="ctan.ann",
update = true
}
-- Line length in 80 characters
local function os_message(text)
local mymax = 77 - string.len(text) - string.len("done")
local msg = text.." "..string.rep(".", mymax).." done"
return print(msg)
end
-- Typesetting scontents documentation step by step :)
function docinit_hook()
errorlevel = (cp("*.tex", unpackdir, typesetdir) + cp("*.sty", unpackdir, typesetdir))
if errorlevel ~= 0 then
error("** Error!!: Can't copy .tex and .sty files from "..unpackdir.." to "..typesetdir)
return errorlevel
end
return 0
end
function typeset(file)
local file = jobname(sourcefiledir.."/scontents.dtx")
print("** Running: lualatex -draftmode -interaction=batchmode "..file..".dtx")
errorlevel = runcmd("lualatex -draftmode -interaction=batchmode "..file..".dtx >"..os_null, typesetdir, {"TEXINPUTS","LUAINPUTS"})
if errorlevel ~= 0 then
local f = assert(io.open(typesetdir.."/scontents.log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(file..".log", typesetdir, maindir)
cp(file..".dtx", typesetdir, maindir)
error("** Error!!: lualatex -draftmode -interaction=batchmode "..file..".dtx")
return errorlevel
end
print("** Running: lualatex -draftmode -interaction=batchmode "..file..".dtx")
errorlevel = runcmd("lualatex -draftmode -interaction=batchmode "..file..".dtx >"..os_null, typesetdir, {"TEXINPUTS","LUAINPUTS"})
if errorlevel ~= 0 then
error("** Error!!: lualatex -draftmode -interaction=batchmode "..file..".dtx")
return errorlevel
end
print("** Running: lualatex -interaction=batchmode "..file..".dtx")
errorlevel = runcmd("lualatex -interaction=batchmode "..file..".dtx >"..os_null, typesetdir, {"TEXINPUTS","LUAINPUTS"})
if errorlevel ~= 0 then
error("** Error!!: lualatex -interaction=batchmode "..file..".dtx")
return errorlevel
end
return 0
end
-- Create check_marked_tags() function
local function check_marked_tags()
local f = assert(io.open("sources/scontents.dtx", "r"))
marked_tags = f:read("*all")
f:close()
local m_pkgd = string.match(marked_tags, "\\ScontentsFileDate{(.-)}")
local m_pkgv = string.match(marked_tags, "\\ScontentsFileVersion{(.-)}")
if pkgversion == m_pkgv and pkgdate == m_pkgd then
os_message("Checking version and date in scontents.dtx")
else
print("** Warning: scontents.dtx is marked with version "..m_pkgv.." and date "..m_pkgd)
print("** Warning: build.lua is marked with version "..pkgversion.." and date "..pkgdate)
print("** Check version and date in build.lua then run l3build tag")
end
end
-- Create check_readme_tags() function
local function check_readme_tags()
local pkgversion = "v"..pkgversion
local f = assert(io.open("sources/CTANREADME.md", "r"))
readme_tags = f:read("*all")
f:close()
local m_readmev, m_readmed = string.match(readme_tags, "Release (v%d+.%d+%a*) \\%[(%d%d%d%d%-%d%d%-%d%d)\\%]")
if pkgversion == m_readmev and pkgdate == m_readmed then
os_message("Checking version and date in README.md")
else
print("** Warning: README.md is marked with version "..m_readmev.." and date "..m_readmed)
print("** Warning: build.lua is marked with version "..pkgversion.." and date "..pkgdate)
print("** Check version and date in build.lua then run l3build tag")
end
end
-- Config tag_hook
function tag_hook(tagname)
check_marked_tags()
check_readme_tags()
end
-- Add "tagged" target to l3build CLI
if options["target"] == "tagged" then
check_marked_tags()
check_readme_tags()
os.exit(0)
end
-- Create make_tmp_dir() function
local function make_tmp_dir()
-- Check version and date
check_marked_tags()
check_readme_tags()
-- Fix basename(path) in windows
local function basename(path)
return path:match("^.*[\\/]([^/\\]*)$")
end
local tmpname = os.tmpname()
tmpdir = basename(tmpname)
-- Create a tmp dir
errorlevel = mkdir(tmpdir)
if errorlevel ~= 0 then
error("** Error!!: The ./"..tmpdir.." directory could not be created")
return errorlevel
else
os_message("Creating the temporary directory ./"..tmpdir)
end
-- Copy files
errorlevel = (cp("*.dtx", sourcefiledir, tmpdir) + cp("*.ins", sourcefiledir, tmpdir))
if errorlevel ~= 0 then
error("** Error!!: Can't copy .dtx and .ins files from "..sourcefiledir.." to ./"..tmpdir)
return errorlevel
else
os_message("Copying scontents.dtx and scontents.ins from "..sourcefiledir.." to ./"..tmpdir)
end
-- Unpack files
print("Unpacks the source files into ./"..tmpdir)
local file = jobname(tmpdir.."/scontents.ins")
errorlevel = run(tmpdir, "luatex -interaction=batchmode "..file..".ins > "..os_null)
if errorlevel ~= 0 then
local f = assert(io.open(tmpdir.."/"..file..".log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(file..".log", tmpdir, maindir)
cp(file..".ins", tmpdir, maindir)
error("** Error!!: luatex -interaction=batchmode "..file..".ins")
return errorlevel
else
os_message("** Running: luatex -interaction=batchmode "..file..".ins")
rm(tmpdir, file..".log")
end
return 0
end
-- We added a new target "testpkg" to run the tests files in test-pkg/
if options["target"] == "testpkg" then
-- Create a tmp dir and unpack files
make_tmp_dir()
-- Copy test files
errorlevel = cp("*.*", "sources/test-pkg", tmpdir)
if errorlevel ~= 0 then
error("** Error!!: Can't copy files from sources/test-pkg to ./"..tmpdir)
return errorlevel
else
os_message("** Copying files from sources/test-pkg to ./"..tmpdir)
end
-- First, no check error level :(
local file = jobname(tmpdir.."/test-pkg-current.tex")
print("Running first test on the file: "..file..".tex using [pdflatex]")
os_message("** Running: pdflatex -interaction=batchmode "..file..".tex")
errorlevel = run(tmpdir, "pdflatex -no-file-line-error -interaction=nonstopmode "..file..".tex")
-- Second
local file = jobname(tmpdir.."/test-format.plain.tex")
print("Running second test on the file: "..file..".tex using [pdftex]")
errorlevel = run(tmpdir, "pdftex "..file..".tex > "..os_null)
if errorlevel ~= 0 then
local f = assert(io.open(tmpdir.."/"..file..".log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(file..".log", tmpdir, maindir)
error("** Error!!: pdftex "..file..".tex")
return errorlevel
else
os_message("** Running: pdftex "..file..".tex")
end
--Third
local file = jobname(tmpdir.."/test-format.latex.tex")
print("Running third test on the file: "..file..".tex using [latex>dvips>ps2pdf]")
errorlevel = run(tmpdir, "latex "..file..".tex > "..os_null)
if errorlevel ~= 0 then
local f = assert(io.open(tmpdir.."/"..file..".log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(file..".log", tmpdir, maindir)
error("** Error!!: latex "..file..".tex")
return errorlevel
else
os_message("** Running: latex "..file..".tex")
end
errorlevel = run(tmpdir, "dvips -q "..file..".dvi > "..os_null)
if errorlevel ~= 0 then
local f = assert(io.open(tmpdir.."/"..file..".log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(file..".log", tmpdir, maindir)
error("** Error!!: dvips "..file..".dvi")
return errorlevel
else
os_message("** Running: dvips "..file..".dvi")
end
errorlevel = run(tmpdir, "ps2pdf "..file..".ps > "..os_null)
if errorlevel ~= 0 then
local f = assert(io.open(tmpdir.."/"..file..".log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(file..".log", tmpdir, maindir)
error("** Error!!: ps2pdf "..file..".ps")
return errorlevel
else
os_message("** Running: ps2pdf "..file..".ps")
end
-- Fourth
local file = jobname(tmpdir.."/test-format.context.tex")
print("Running fourth test on the file: "..file..".tex using [context]")
errorlevel = run(tmpdir, "context --luatex "..file..".tex > "..os_null)
if errorlevel ~= 0 then
local f = assert(io.open(tmpdir.."/"..file..".log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(file..".log", tmpdir, maindir)
error("** Error!!: context --luatex "..file..".tex")
return errorlevel
else
os_message("** Running: context --luatex "..file..".tex")
end
-- Copy generated .pdf files to maindir
errorlevel = cp("*.pdf", tmpdir, maindir)
if errorlevel ~= 0 then
error("** Error!!: Can't copy generated pdf files to ./"..maindir)
return errorlevel
else
os_message("Copy generated .pdf files to ./"..maindir)
end
-- If are OK then remove ./temp dir
cleandir(tmpdir)
lfs.rmdir(tmpdir)
os_message("Remove temporary directory ./"..tmpdir)
os.exit(0)
end
-- We added a new target "examples" to run the examples files for scontents
if options["target"] == "examples" then
-- Create a tmp dir and unpack files
make_tmp_dir()
local file = jobname(tmpdir.."/scontents.dtx")
-- Unpack sample files
print("Unpack samples into ./"..tmpdir.." from file "..file..".dtx")
errorlevel = run(tmpdir, "lualatex -draftmode -interaction=batchmode "..file..".dtx > "..os_null)
if errorlevel ~= 0 then
error("** Error!!: lualatex -draftmode -interaction=batchmode "..file..".dtx")
return errorlevel
else
os_message("** Running: lualatex -draftmode -interaction=batchmode "..file..".dtx")
end
-- List of sample files
samples = {
"scexamp1",
"scexamp2",
"scexamp3",
"scexamp4",
"scexamp5",
"scexamp6",
"scexamp7",
"scexamp8",
"scexamp9",
"scexamp10",
}
-- Compiling sample files
print("Compiling sample files in ./"..tmpdir.." using [arara]")
for i, samples in ipairs(samples) do
errorlevel = run(tmpdir, "arara "..samples..".ltx > "..os_null)
if errorlevel ~= 0 then
local f = assert(io.open(tmpdir.."/"..samples..".log", "r"))
err_log_file = f:read("*all")
print(err_log_file)
cp(samples..".ltx", tmpdir, maindir)
cp(samples..".log", tmpdir, maindir)
error("** Error!!: arara "..samples..".ltx")
return errorlevel
else
os_message("** Running: arara "..samples..".ltx")
end
end
-- Copy generated .pdf files to maindir
errorlevel = cp("*.pdf", tmpdir, maindir)
if errorlevel ~= 0 then
error("** Error!!: Can't copy generated pdf files to ./"..maindir)
return errorlevel
else
os_message("Copy generated .pdf files to ./"..maindir)
end
-- If are OK then remove ./temp dir
cleandir(tmpdir)
lfs.rmdir(tmpdir)
os_message("Remove temporary directory ./"..tmpdir)
os.exit(0)
end
-- Clean repo
if options["target"] == "clean" then
print("Clean files in repo")
os.execute("git clean -xdfq")
os_message("** Running: git clean -xdfq")
end
-- Capture os cmd for git
local function os_capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
-- We added a new target "release" to do the final checks for git and ctan
if options["target"] == "release" then
-- os.execute("git clean -xdfq")
local gitbranch = os_capture("git symbolic-ref --short HEAD")
local gitstatus = os_capture("git status --porcelain")
local tagongit = os_capture('git for-each-ref refs/tags --sort=-taggerdate --format="%(refname:short)" --count=1')
local gitpush = os_capture("git log --branches --not --remotes")
if gitbranch == "master" then
os_message("** Checking git branch '"..gitbranch.."': OK")
else
error("** Error!!: You must be on the 'master' branch")
end
local file = jobname(sourcefiledir.."/scontents.ins")
errorlevel = run(sourcefiledir, "luatex --interaction=batchmode "..file..".ins > "..os_null)
if errorlevel ~= 0 then
error("** Error!!: luatex -interaction=batchmode "..file..".ins")
return errorlevel
else
os_message("** Running: luatex -interaction=batchmode "..file..".ins")
end
if gitstatus == "" then
os_message("** Checking status of the files: OK")
else
error("** Error!!: Files have been edited, please commit all changes")
end
if gitpush == "" then
os_message("** Checking pending commits: OK")
else
error("** Error!!: There are pending commits, please run git push")
end
check_marked_tags()
local pkgversion = "v"..pkgversion
os_message("** Checking last tag marked in GitHub "..tagongit..": OK")
errorlevel = os.execute("git tag -a "..pkgversion.." -m 'Release "..pkgversion.." "..pkgdate.."'")
if errorlevel ~= 0 then
error("** Error!!: run git tag -d "..pkgversion.." && git push --delete origin "..pkgversion)
return errorlevel
else
os_message("** Running: git tag -a "..pkgversion.." -m 'Release "..pkgversion.." "..pkgdate.."'")
end
os_message("** Running: git push --tags --quiet")
os.execute("git push --tags --quiet")
if fileexists(ctanzip..".zip") then
os_message("** Checking "..ctanzip..".zip file to send to CTAN: OK")
else
os_message("** Creating "..ctanzip..".zip file to send to CTAN")
os.execute("l3build ctan > "..os_null)
end
os_message("** Running: l3build upload -F ctan.ann --debug")
os.execute("l3build upload -F ctan.ann --debug >"..os_null)
print("** Now check "..ctanzip..".curlopt file and add changes to ctan.ann")
print("** If everything is OK run (manually): l3build upload")
os.exit(0)
end