-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflake.nix
182 lines (163 loc) · 6.4 KB
/
flake.nix
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
{
description = "Packages and dev Shell for the zitat-o-mat-org Website";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{ self
, nixpkgs
, flake-utils
, ...
}:
flake-utils.lib.eachDefaultSystem (
system:
with builtins;
let
pkgs = import nixpkgs { inherit system; };
package-json = fromJSON (readFile ./package.json);
lib = pkgs.lib;
parties = readFile ./resources/parties.json |> fromJSON;
parseProgramFile = path:
let
repeat = element: n: if n <= 0 then [] else [element] ++ (repeat element (n - 1));
withArticles = name: [
"Die ${name}"
"die ${name}"
"Das ${name}"
"das ${name}"
name
];
content = readFile path;
lines = lib.splitString "\n" content;
source = lines
|> head
|> lib.splitString " "
|> (e: elemAt e 1)
;
phrases = tail lines
|> map (l: lib.trim l)
|> filter (l: l != "" && ! lib.hasPrefix "# " l)
# Filter Party Names
|> map (replaceStrings (withArticles parties.${party}.short_name) (repeat "[Parteiname]" 5))
|> map (replaceStrings (withArticles parties.${party}.full_name) (repeat "[Parteiname]" 5))
|> map (replaceStrings (withArticles (lib.toUpper parties.${party}.full_name)) (repeat "[Parteiname]" 5))
|> map (replaceStrings (withArticles (lib.toUpper parties.${party}.short_name)) (repeat "[Parteiname]" 5))
|> (p: if hasAttr "alias" parties.${party} then foldl' (acc: curr:
map (replaceStrings (withArticles curr) (repeat "[Parteiname]" 5)) acc
) p parties.${party}.alias else p)
# Satzzeichen
|> map (lib.removeSuffix ".")
|> map (e: if lib.hasSuffix "!" e then e else e + ".")
;
fileName = (lib.path.splitRoot path).subpath
|> lib.path.subpath.components
|> lib.last
;
election = fileName
|> lib.splitString "."
|> head
;
type = if election == "mission_statement" || election == "party_platform" then
election else "election";
metadata = if type == "election" then
{inherit election type;} else
{inherit type;};
party = (lib.path.splitRoot path).subpath
|> lib.path.subpath.components
|> lib.init
|> lib.last
;
in
if lib.hasPrefix "none" content then
{party = parties.${party};} // metadata else
if lib.hasPrefix "todo" content then
lib.warn "${parties.${party}.full_name} hat vielleicht ein Wahlprogramm für die Wahl ${election}." ({party = parties.${party};} // metadata)
else if ! lib.hasPrefix "# https://" content then throw "das Wahlprogramm von ${parties.${party}.full_name} für die Wahl ${election} hat keine quelle" else
{party = parties.${party};} // { inherit phrases source; } // metadata;
data = lib.filesystem.listFilesRecursive ./resources/programs
|> map parseProgramFile
;
mission_statements = filter (e: e.type == "mission_statement") data;
party_platforms = filter (e: e.type == "party_platform") data;
byElection = election: data
|> filter (e: e.type == "election")
|> filter (e: e.election == election)
|> map (e:
let
fallback = [
(lib.findFirst (party_platform: e.party.full_name == party_platform.party.full_name) [] party_platforms)
(lib.findFirst (mission_statement: e.party.full_name == mission_statement.party.full_name) [] mission_statements)
];
in
if hasAttr "phrases" e then [e] else
if lib.flatten fallback == [] then
lib.warn "${e.party.full_name} tritt zur wahl ${election} an, es wurden aber keine programme gefunden" [] else
fallback
)
|> lib.flatten
|> filter (e: e != null)
|> toJSON
|> pkgs.writeText "${election}.json"
;
elections = data
|> filter (e: e.type == "election")
|> map (e: e.election)
|> lib.lists.unique
;
in
{
packages = rec {
default = web;
srcWithData = pkgs.stdenv.mkDerivation {
pname = "src";
inherit (package-json) version;
src = ./.;
installPhase = ''
mkdir -p $out/public
cp -r $src/* $out
cp -fr ${dataDir}/* $out/public/
'';
};
web = pkgs.buildNpmPackage rec {
pname = package-json.name;
inherit (package-json) version;
src = srcWithData;
npmDepsHash = "sha256-+9mVkwTRkw8WDAHV0EKhayUHoFTkuIAgq8Wa4UQG12k=";
installPhase = ''
mkdir -p $out
cp -r dist/* $out
'';
};
dataDir = pkgs.stdenv.mkDerivation
{
pname = "data-directory";
inherit (package-json) version;
src = ./.;
installPhase =
let
toElectionCopyString = election:
"cp ${byElection election} $out/election/${election}.json";
in ''
mkdir -p $out/election
${map toElectionCopyString elections |> foldl' (acc: curr: acc + "\n" + curr) ""}
cp ${elections |> toJSON |> pkgs.writeText "elections.json"} $out/elections.json
'';
};
};
devShell = pkgs.mkShell
{
nativeBuildInputs = with pkgs; [
nodejs
(vscode-with-extensions.override {
vscodeExtensions = with pkgs.vscode-extensions; [
jnoortheen.nix-ide
vue.volar
];
vscode = vscodium;
})
];
};
}
);
}