-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdefault.nix
More file actions
251 lines (231 loc) · 7.22 KB
/
default.nix
File metadata and controls
251 lines (231 loc) · 7.22 KB
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
{ pkgs ? import <nixpkgs> { }
, stdenv ? pkgs.stdenv
, lib ? pkgs.lib
, ghostscript ? pkgs.ghostscript
, ref ? null
, doPdfGeneration ? true
# Attempts to render every page individually to make sure the dependencies
# are set correctly.
, doCheck ? true
# Checks whether the generated output matches the checked-in SSS32.ps.
# When doing development you may want to shut this off to obtain the
# output file that you need to check in.
#
# Has no effect if doChecks is not set.
, doOutputDiff ? true
}:
let
src =
if isNull ref
then lib.sourceFilesBySuffices ./. [ ".ps" ".inc" ]
else
fetchGit {
url = ./.;
inherit ref;
};
shortId = lib.optionalString (! isNull ref) ("-" + builtins.substring 0 8 src.rev);
setup = rec {
# Will be broken into multiple sub-files in a later PR.
fullSetup = {
content = builtins.readFile "${src}/include/setup.ps.inc";
dependencies = [ ];
};
};
# Dependencies that every page has
standardDependencies = [ setup.fullSetup ];
allPages = {
title = {
sourceHeader = "Title Page";
content = builtins.readFile "${src}/include/title.ps.inc";
dependencies = [ ];
skipPageNumber = true;
};
license = {
sourceHeader = "License Information";
content = builtins.readFile "${src}/include/license.ps.inc";
dependencies = [ ];
skipPageNumber = true;
};
reference = {
sourceHeader = "Reference Sheet";
content = builtins.readFile "${src}/include/reference.ps.inc";
dependencies = [ ];
drawFooter = true;
};
principalTables = {
sourceHeader = "Arithmetic Tables";
content = builtins.readFile "${src}/include/principal-tables.ps.inc";
dependencies = [ ];
};
additionBottom = {
content = "{xor} (Addition) code dup perm drawBottomWheelPage\n";
dependencies = [ ];
};
additionTop = {
content = "showTopWheelPage\n";
dependencies = [ ];
};
recovery = {
content = builtins.readFile "${src}/include/volvelle-recovery.ps.inc";
dependencies = [ ];
};
fusionInner = {
content = builtins.readFile "${src}/include/volvelle-fusion-1.ps.inc";
dependencies = [ ];
};
fusionOuter = {
content = builtins.readFile "${src}/include/volvelle-fusion-2.ps.inc";
dependencies = [ ];
};
generationInstructions = {
content = builtins.readFile "${src}/include/page7.ps.inc";
dependencies = [ ];
};
checksumTable1 = {
content = builtins.readFile "${src}/include/checksum-table-1.ps.inc";
isLandscape = true;
dependencies = [ ];
drawFooter = true;
};
checksumTable2 = {
content = builtins.readFile "${src}/include/checksum-table-2.ps.inc";
dependencies = [ ];
isLandscape = true;
drawFooter = true;
};
checksumWorksheet = {
content = builtins.readFile "${src}/include/checksum-worksheet.ps.inc";
dependencies = [ ];
isLandscape = true;
drawFooter = true;
};
shareTable = a: b: c: d: {
content = "${toString a} ${toString b} ${toString c} ${toString d} showShareTablePage\n";
dependencies = [ ];
drawFooter = true;
};
};
fullBooklet = {
name = "SSS32.ps";
pages = with allPages; [
title
license
reference
principalTables
additionBottom
additionTop
generationInstructions
(shareTable 29 24 13 25)
(shareTable 9 8 23 18)
(shareTable 22 31 27 19)
(shareTable 1 0 3 16)
(shareTable 11 28 12 14)
(shareTable 6 4 2 15)
(shareTable 10 17 21 20)
(shareTable 26 30 7 5)
recovery
fusionInner
fusionOuter
checksumTable1
checksumTable2
checksumWorksheet
];
};
dependencyContentRecur = content: builtins.concatMap
(item: (dependencyContentRecur item.dependencies) ++ [ item.content ])
content;
dependencyContent = pages: lib.lists.unique (
(map (dep: dep.content) standardDependencies) ++
(builtins.concatMap (page: dependencyContentRecur page.dependencies) pages)
);
renderBooklet = booklet:
let
addPage = content: pageData: {
content = content.content + lib.optionalString (pageData ? sourceHeader) ''
%****************************************************************
%*
%* ${pageData.sourceHeader}
%*
%****************************************************************
'' + ''
%%Page: ${toString content.nextPgIdx} ${toString content.nextPgIdx}
${lib.optionalString (pageData ? isLandscape) "%%PageOrientation: Landscape\n"}
%%BeginPageSetup
/pgsave save def
%%EndPageSetup
${if pageData ? isLandscape then "landscapePage" else "portraitPage"} begin
${lib.optionalString (pageData ? drawFooter) "${toString content.nextFooterIdx} drawFooter"}
${pageData.content}
end
pgsave restore
showpage
'';
nextFooterIdx = content.nextFooterIdx + (if pageData ? drawFooter then 1 else 0);
nextPgIdx = content.nextPgIdx + 1;
};
initialContent = {
content = ''
%!PS-Adobe-3.0
%%Orientation: Portrait
%%Pages: ${toString (builtins.length booklet.pages)}
%%EndComments
%%BeginSetup
${toString (dependencyContent (booklet.pages))}%%EndSetup
%************************************************************************
%************************************************************************
%*
%* Section Three: Page Rendering
%*
%************************************************************************
%************************************************************************
'';
nextPgIdx = 1;
nextFooterIdx = 1;
};
finalContent = builtins.foldl' addPage initialContent booklet.pages;
in
pkgs.writeTextFile {
name = booklet.name;
text = finalContent.content + ''
%%EOF
'';
};
checkSinglePage = page: renderBooklet {
name = "test-single-page.ps";
pages = [ page ];
};
in
stdenv.mkDerivation {
name = "codex32${shortId}";
nativeBuildInputs = if doPdfGeneration then [ ghostscript ] else [ ];
phases = [ "buildPhase" ] ++ lib.optionals doCheck [ "checkPhase" ];
buildPhase = ''
set -e
FULL_BW="${renderBooklet fullBooklet}"
# Copy output Postscript into place
mkdir "$out"
cd "$out"
cp "$FULL_BW" SSS32.ps
# Patch to include version
sed -i 's/(revision \(.*\))/(revision \1${shortId})/' ./SSS32.ps
# Produce PDF, if requested.
${lib.optionalString doPdfGeneration "ps2pdf -dPDFSETTINGS=/prepress SSS32.ps"}
'';
checkPhase = toString
(map
(page: "ghostscriptTest ${checkSinglePage page}")
fullBooklet.pages
) + ''
ghostscriptTest() {
echo "Ghostscript testing $1"
local output;
if ! output="$(gs -dNOPAUSE -dNODISPLAY "$1" < /dev/null 2>&1)"; then
echo "Failed to run ghostscript on $1"
echo "$output"
exit 1
fi
}
ghostscriptTest "$FULL_BW"
${lib.optionalString doOutputDiff "diff -C 5 ${src}/SSS32.ps SSS32.ps"}
'';
}