aboutsummaryrefslogtreecommitdiff
path: root/scripts/paku
blob: b568a7df135fb1c65ee2553ae48313c1429da3de (about) (plain) (blame)
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
#!/bin/sh
set -eu

TARBALL_TEMPLATE='https://euandreh.xyz/@NAME@.git/snapshot/@NAME@-@VERSION@.tar.gz'

tarball_url() {
	NAME="$1"
	VERSION="$2"
	echo "$TARBALL_TEMPLATE" | \
		sed -e "s/@NAME@/$NAME/g" \
				-e "s/@VERSION@/$VERSION/g"
}

gen_guix() {
	GUIX_OUT="$(mktemp)"

	cat <<EOF >>"$GUIX_OUT"
(define-module (xyz euandreh packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix gexp)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix build-system gnu))
EOF

	for p in $(jq -r '.packages[] | @base64' < paku.json); do
		NAME="$(echo "$p" | base64 --decode | jq -r '.name')"
		SUFFIX="$(echo "$p" | base64 --decode | jq -r '.suffix')"
		VERSION="$(echo "$p" | base64 --decode | jq -r '.version')"
		TARBALL="$(tarball_url "$NAME" "$VERSION")"
		SHA256="$(guix download "$TARBALL" | tail -n 1)"
		JSON_CHECK_INPUTS="$(echo "$p" | base64 --decode | jq -r '.check_inputs')"
		if [ "$JSON_CHECK_INPUTS" = 'null' ]; then
			CHECK_INPUTS=''
		else
			INPUTS=''
			for check_input in $(echo "$JSON_CHECK_INPUTS" | jq -r '.[]'); do
				if [ "$INPUTS" = '' ]; then
					INPUTS="(\"$check_input\" ,(specification->package \"$check_input\"))"
				else
					INPUTS="$INPUTS
        (\"$check_input\" ,(specification->package \"$check_input\"))"
				fi
			done

			CHECK_INPUTS="
    (inputs
     \`($INPUTS))"
		fi

		cat <<EOF >>"$GUIX_OUT"

(define-public $NAME$SUFFIX
  (package
    (name "$NAME$SUFFIX")
    (version "$VERSION")
    (source (origin
              (method url-fetch)
              (uri (string-append
                    "https://euandreh.xyz/$NAME.git/snapshot/$NAME-"
                    version
                    ".tar.gz"))
              (sha256
               (base32
                "$SHA256"))))
    (build-system gnu-build-system)$CHECK_INPUTS
    (arguments \`(#:make-flags
                 (list (string-append "PREFIX=" %output))
                 #:phases
                 (modify-phases %standard-phases
                   (delete 'configure))))
    (synopsis (file-append source "/description"))
    (description (file-append source "/long-description"))
    (home-page "https://euandreh.xyz/$NAME/")
    (license license:agpl3+)))
EOF
	done
	
	printf '\n(list' >>"$GUIX_OUT"
	for p in $(jq -r '.packages[] | @base64' < paku.json); do
		NAME="$(echo "$p" | base64 --decode | jq -r '.name')"
		SUFFIX="$(echo "$p" | base64 --decode | jq -r '.suffix')"

		printf '\n %s%s' "$NAME" "$SUFFIX" >>"$GUIX_OUT"
	done
	printf ')\n' >>"$GUIX_OUT"

	mv "$GUIX_OUT" src/xyz/euandreh/packages.scm
}

gen_nix() {
	NIX_OUT="$(mktemp)"

	cat <<EOF >>"$NIX_OUT"
{ pkgs }:
self: super: {
  xyz-euandreh = {
EOF

	for p in $(jq -r '.packages[] | @base64' < paku.json); do
		NAME="$(echo "$p" | base64 --decode | jq -r '.name')"
		SUFFIX="$(echo "$p" | base64 --decode | jq -r '.suffix')"
		VERSION="$(echo "$p" | base64 --decode | jq -r '.version')"
		TARBALL="$(tarball_url "$NAME" "$VERSION")"
		SHA256="$(nix-prefetch-url --unpack "$TARBALL")"
		JSON_CHECK_INPUTS="$(echo "$p" | base64 --decode | jq -r '.check_inputs')"
		if [ "$JSON_CHECK_INPUTS" = 'null' ]; then
			CHECK_INPUTS=''
		else
			INPUTS='['
			for check_input in $(echo "$JSON_CHECK_INPUTS" | jq -r '.[]'); do
				INPUTS="$INPUTS $check_input"
			done
			INPUTS="$INPUTS ]"

			CHECK_INPUTS="
      checkInputs = with pkgs; $INPUTS;"
		fi

		cat <<EOF >>"$NIX_OUT"
    $NAME$SUFFIX = pkgs.stdenv.mkDerivation rec {
      name = "$NAME";
      version = "$VERSION";

      src = fetchTarball {
        url =
          "https://euandreh.xyz/\${name}.git/snapshot/\${name}-\${version}.tar.gz";
        sha256 = "$SHA256";
      };

      makeFlags = [ "PREFIX=\$(out)" ];
$CHECK_INPUTS
      doCheck = true;

      meta = with pkgs.lib; {
        description = readFile "\${src}/description";
        longDescription = readFile "\${src}/long-description";
        homepage = "https://euandreh.xyz/$NAME/";
        changelog = "https://euandreh.xyz/$NAME/CHANGELOG.html";
        downloadPage = "https://euandreh.xyz/$NAME/#releases";
        license = licenses.agpl3;
        platforms = platforms.unix;
      };
    };
EOF
	done

	cat <<EOF >>"$NIX_OUT"
  };
}
EOF

	mv "$NIX_OUT" default.nix
}

pascalize() {
	awk '{printf "%s", toupper(substr($1,1,1)) substr($1,2)}'
}

gen_homebrew() {
	for p in $(jq -r '.packages[] | @base64' < paku.json); do
		HOMEBREW_OUT="$(mktemp)"
		NAME="$(echo "$p" | base64 --decode | jq -r '.name')"
		NAME_UC="$(echo "$NAME" | pascalize)"
		SUFFIX="$(echo "$p" | base64 --decode | jq -r '.suffix')"
		SUFFIX_UC="$(echo "$SUFFIX" | cut -c2- | pascalize)"
		VERSION="$(echo "$p" | base64 --decode | jq -r '.version')"
		TYPE="$(echo "$p" | base64 --decode | jq -r '.type')"
		TARBALL_URL="$(tarball_url "$NAME" "$VERSION")"
		TARBALL="$(mktemp)"
		wget -O "$TARBALL" "$TARBALL_URL"
		SHA256="$(sha256sum "$TARBALL" | cut -d\  -f 1)"
		DESCRIPTION="$(tar xvf "$TARBALL" -O "$NAME-$VERSION/description")"
		JSON_CHECK_INPUTS="$(echo "$p" | base64 --decode | jq -r '.check_inputs')"
		if [ "$JSON_CHECK_INPUTS" = 'null' ]; then
			CHECK_INPUTS=''
		else
			CHECK_INPUTS=''
			for check_input in $(echo "$JSON_CHECK_INPUTS" | jq -r '.[]'); do
				CHECK_INPUTS="$CHECK_INPUTS
  depends_on '$check_input' => :build"
			done
			CHECK_INPUTS="$CHECK_INPUTS
"
		fi

		TEST_SECTION=''
		if [ "$TYPE" = 'bin' ]; then
			TEST_SECTION="

  test do
    system \"#{bin}/$NAME\", '-V'
  end"
		fi

		cat <<EOF >>"$HOMEBREW_OUT"
class ${NAME_UC}$SUFFIX_UC < Formula
  desc '$DESCRIPTION'
  homepage 'https://euandreh.xyz/$NAME/'
  url '$TARBALL_URL'
  sha256 '$SHA256'
  license 'AGPL-3.0-or-later'
$CHECK_INPUTS
  def install
    system 'make'
    system 'make', 'check'
    system 'make', 'install', "PREFIX=#{prefix}"
  end$TEST_SECTION
end
EOF
		mv "$HOMEBREW_OUT" "Formula/$NAME$SUFFIX.rb"
	done
}

refresh_packages() {
	COUNT="$(jq -r '.packages[].name' < paku.json | wc -l)"
	INDEXES="$(seq 0 "$((COUNT - 1))")"
	for N in $INDEXES; do
		NAME="$(jq -r ".packages[$N].name" < paku.json)"
		SUFFIX="$(jq -r ".packages[$N].suffix" < paku.json)"
		cd "../$NAME/"
		if [ "$SUFFIX" = '-latest' ]; then
			NEW_VERSION="$(git rev-parse HEAD)"
		else
			NEW_VERSION="$(git describe --tags --abbrev=0 | cut -c2-)"
		fi
		DESCRIPTION="$(cat description)"
		cd - >/dev/null
		jq -r ".packages[$N].version = \"$NEW_VERSION\"" paku.json | sponge paku.json
		jq -r ".packages[$N].description = \"$DESCRIPTION\"" paku.json | sponge paku.json
	done
}

report_index() {
	HTML_OUT='public/paku.html'
	cat <<EOF > "$HTML_OUT"
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=" />
		<link rel="icon" type="image/svg+html" href="favicon.svg" />
		<title>Package index</title>

		<style>
			ul {
				list-style: none;
			}
			li {
				margin-top: 30px;
			}
		</style>
	</head>
	<body>
		<h1>
			Package index
		</h1>
		<ul>
EOF

	for p in $(jq -r '.packages[] | @base64' < paku.json); do
		NAME="$(echo "$p" | base64 --decode | jq -r '.name')"
		DESCRIPTION="$(echo "$p" | base64 --decode | jq -r '.description')"
		SUFFIX="$(echo "$p" | base64 --decode | jq -r '.suffix')"
		if [ -z "$SUFFIX" ]; then
			VERSION="$(echo "$p" | base64 --decode | jq -r '.version')"
			ID='released'
		else
			VERSION='latest'
			ID='latest'
		fi

		cat <<EOF >> "$HTML_OUT"
			<li id="$NAME-$ID">
				<details>
					<summary><a href="#$NAME-$ID">$NAME</a> ($VERSION) - $DESCRIPTION</summary>
					<p>
						<a href="https://euandreh.xyz/$NAME/">Homepage</a>.
					</p>

					<section>
						<h2>Guix</h2>
						<p>
							After following the
							<a href="#guix-instructions">Guix instructions</a> to include this
							channel, now you can create an environment that includes this
							package:
						</p>
						<pre><code>$ guix environment --ad-hoc $NAME$SUFFIX</code></pre>
						<p>
							Alternatively, you can install it imperatively:
						</p>
						<pre><code>$ guix install $NAME$SUFFIX</code></pre>
					</section>
					<section>
						<h2>Nix</h2>
						<p>
							After following the
							<a href="#nix-instructions">Nix instructions</a> to include this
							repository as an overlay, now you can create a new Nix shell that
							includes this package:
						</p>
						<pre><code>$ nix-shell -p $NAME$SUFFIX</code></pre>
						<p>
							Alternatively, you can install it imperatively:
						</p>
						<pre><code>$ nix-env -i $NAME$SUFFIX</code></pre>
					</section>
				</details>
			</li>
EOF
	done

	cat <<EOF >> "$HTML_OUT"
		</ul>
		<article id="guix-instructions">
			<h2>Guix instructions</h2>
			<p>
				Add this channel to your <code>~/.config/guix/channels.scm</code>:
			</p>
			<pre><code>(cons*
 (channel
  (name 'xyz-euandreh)
  (url "git://euandreh.xyz/package-repository")
  (branch "main")
  (introduction
   (make-channel-introduction
    "d749e053e6db365069cb9b2ef47a78b06f9e7361"
    (openpgp-fingerprint
     "5BDA E9B8 B2F6 C6BC BB0D  6CE5 81F9 0EC3 CD35 6060"))))
 %default-channels)</code></pre>
		</article>

		<article id="nix-instructions">
			<h2>Nix instructions</h2>
			<p>
				Add the overlay to your <code>/etc/nixox/configuration.nix</code>:
			</p>
			<pre></code>nixpkgs = {
  overlays = [
    (import (fetchTarball {
      url = "https://euandreh.xyz/package-repository.git/snapshot/package-repository-main.tar.gz";
    }) { inherit pkgs; })
  ];
};</code></pre>
		</article>
	</body>
</html>
EOF
}

while getopts 'grl' flag; do
	case "$flag" in
		g)
			gen_guix
			gen_nix
			gen_homebrew
			;;
		r)
			refresh_packages
			;;
		l)
			report_index
			;;
		*)
			printf 'Unrecognized flag "%s".\n' "$flag" >&2
			;;
	esac
done