aboutsummaryrefslogtreecommitdiff
path: root/lib/App/Rakumod/Bin.rakumod
blob: 98133e9be6b8969033559d83455f3e6be95393b0 (plain)
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
#! /usr/bin/env false

use v6.d;

use File::Which;
use IO::Path::XDG;
use JSON::Fast;
use Log;
use Template::Mustache;

use App::Rakumod::Commands::Add;
use App::Rakumod::Commands::Depend;
use App::Rakumod::Commands::New;
use App::Rakumod::Exception;
use App::Rakumod::Util;
use App::Rakumod;
use X::App::Rakumod::Clobber;
use X::App::Rakumod::DuplicateDependency;
use X::App::Rakumod::DuplicateProvide;
use X::App::Rakumod::MissingBinDep;
use X::App::Rakumod::MissingDistFile;
use X::App::Rakumod::NoDistribution;

unit module App::Rakumod::Bin;

#| Add a new file.
multi sub MAIN (
	'add',

	#| The name of the new unit.
	Str $provide is copy = Str,

	#| The kind of provide to create. See App::Rakumod::Commands::Add for
	#| more information.
	Str :$kind = Str,
) is export {
	CATCH {
		when App::Rakumod::Exception {
			note $_.message;
			exit $_.code;
		}
	}

	rakumod-cli-init;

	my $meta-file = rakumod-scour('META6.json', $*CWD);

	if (!$meta-file) {
		X::App::Rakumod::NoDistribution.new(
			path => $*CWD,
		).throw
	}

	my %meta = from-json($meta-file.slurp);

	if (!$provide) { $provide = rakumod-prompt('Unit name') }

	if (%meta<provides>.keys$provide) {
		X::App::Rakumod::DuplicateProvide.new(
			:$provide,
		).throw
	}

	my $contents = Str;
	my %variables =
		:%meta,
		this => {
			:$provide,
		},
		date => {
			year => DateTime.now.year,
		}
	;

	with (rakumod-template('provide.raku')) -> $template {
		my $body;

		with (rakumod-template("licenses/{%meta<license>.fc}-short.txt")) {
			my $license = rakumod-template-render(
				$_.slurp,
				%variables,
			).trim;

			if ($App::Rakumod::config.get('add.prepend-license-lines')) -> $prepend {
				$license = $license
					.lines
					.map(sub ($line) {
						return $prepend.trim if $line eq '';
						$prepend ~ $line;
					})
					.join("\n")
					;
			}

			%variables<license> = $license;
		} else {
			note $_.exception.message if $App::Rakumod::config.get('main.template-warnings');
		}

		if ($kind) {
			with (rakumod-template("kinds/$kind.raku")) {
				$body = rakumod-template-render(
					$_.slurp,
					%variables,
				);
			} else {
				note $_.exception.message if $App::Rakumod::config.get('main.template-warnings');
			}
		}

		$contents = rakumod-template-render($template.slurp, %(
			|%variables,
			body => $body.?trim // '# TODO',
		));
	} else {
		note $_.exception.message if $App::Rakumod::config.get('main.template-warnings');
	}

	my $file = rakumod-add(
		$provide,
		$meta-file,
		:$contents,
		:!sanity-checks
	);

	if (!$App::Rakumod::config.get('main.quiet')) {
		say "Added $file to provide $provide for %meta<name>";
	}
}

#| Add a module to depend on.
multi sub MAIN (
	'depend',

	#| The name of the module.
	Str $module is copy = Str,
) is export {
	CATCH {
		when App::Rakumod::Exception {
			note $_.message;
			exit $_.code;
		}
	}

	rakumod-cli-init;

	my $meta-file = rakumod-scour('META6.json', $*CWD);

	if (!$meta-file) {
		X::App::Rakumod::NoDistribution.new(
			path => $*CWD,
		).throw;
	}

	my %meta = from-json($meta-file.slurp);

	if (!$module) { $module = rakumod-prompt('Dependency name'); }

	if (%meta<depends>$module) {
		X::App::Rakumod::DuplicateDependency.new(
			:$module
		).throw
	}

	rakumod-depend(
		$module,
		$meta-file,
		:!sanity-checks,
	);

	if (!$App::Rakumod::config.get('main.quiet')) {
		say "Added $module as dependency for %meta<name>";
	}

	if ($App::Rakumod::config.get('depend.zef-install')) {
		run << zef install "$module" >>;
	}
}

#| Create a distributable archive of a module.
multi sub MAIN (
	'dist',
) is export {
	CATCH {
		when App::Rakumod::Exception {
			note $_.message;
			exit $_.code;
		}
	}

	rakumod-cli-init;

	.debug('Checking for tar') with $Log::instance;

	my $tar = which('tar');

	X::App::Rakumod::MissingBinDep.new(dep => 'tar').throw unless $tar;

	my $meta-file = rakumod-scour('META6.json', $*CWD);
	my %meta = from-json($meta-file.slurp);
	my $distname = %meta<name>.subst('::', '-', :g) ~ '-' ~ %meta<version>;
	my $output = $App::Rakumod::config.get('dist.directory').add("$distname.tar.gz");
	my @files = (
		|%meta<provides>.values,
		|%meta<resources>.map({ "resources/$_" }),
		'META6.json',
		'LICENSE.txt',
		'README.rakudoc',
	);

	# Ensure all files actually exist
	for @files -> $file {
		.notice("Including $file in $distname") with $Log::instance;

		next if $file.IO.f;

		X::App::Rakumod::MissingDistFile.new(file => $file.IO).throw;
	}

	# Ensure the output directory exists
	if (!$output.parent.d) {
		.notice("Creating dist directory at {$output.parent}") with $Log::instance;
		$output.parent.mkdir;
	}

	my @cmd = «
		"$tar"
		-c
		-f "$output"
		--transform "s/^./$distname/"
		--owner=0
		--group=0
		--numeric-owner
		{@files.map({ "./$_" }).sort}
	»;

	.debug('> ' ~ @cmd.join(' ')) with $Log::instance;

	run(@cmd);

	say "Dist created of {%meta<name>}:ver<{%meta<version>}> at {$output.absolute}";
}

#| Create a new module skeleton.
multi sub MAIN (
	'new',

	#| The name of the module.
	Str $name is copy = Str,

	#| The path where the module directory will be created at.
	Str $path is copy = Str,
) is export {
	CATCH {
		when App::Rakumod::Exception {
			note $_.message;
			exit $_.code;
		}
	}

	rakumod-cli-init;

	if (!$name) { $name = rakumod-prompt('Name'); }
	if (!$path) { $path = rakumod-prompt('Path', $App::Rakumod::config.get('new.path')); }

	my $destination = $path.IO.add($name);

	if ($destination.e) {
		X::App::Rakumod::Clobber.new(
			path => $destination.absolute,
		).throw;
	}

	my $description = rakumod-prompt('Description', :empty);
	my $license = rakumod-prompt('License', 'AGPL-3.0-or-later');

	my ($, %meta) = rakumod-new(
		$name,
		$description,
		$license,
		[$App::Rakumod::config.get('new.author') // ''],
		$path.IO,
		:!sanity-checks,
	);

	if (!$App::Rakumod::config.get('main.quiet')) {
		say "Created skeleton for $name at $destination";
	}

	# Add readme
	with (rakumod-template('readme.rakudoc')) {
		$destination
			.add('README.rakudoc')
			.spurt(rakumod-template-render($_.slurp, {
				:%meta
			}))
			.trim
	} else {
		note $_.exception.message;
	}

	# Add license
	with (rakumod-template("licenses/{%meta<license>.fc}-full.txt")) {
		$destination
			.add('LICENSE.txt')
			.spurt(rakumod-template-render($_.slurp, {
				:%meta,
				date => {
					year => DateTime.now.year,
				}
			}))
			.trim
	} else {
		note $_.exception.message;
	}
}

=begin pod

=NAME    App::Rakumod::Bin
=AUTHOR  Patrick Spek <p.spek@tyil.work>
=VERSION 0.0.0

=head1 Synopsis

=head1 Description

=head1 Examples

=head1 See also

=end pod

# vim: ft=perl6 noet