aboutsummaryrefslogtreecommitdiff
path: root/lib/Dist/Maker/Bin.rakumod
blob: bef08b6c65b57e8439a389273b0776367851c569 (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
#! /usr/bin/env false

use v6.d;

use IO::Path::XDG;
use JSON::Fast;
use Template::Mustache;

use Dist::Maker::Commands::Add;
use Dist::Maker::Commands::Depend;
use Dist::Maker::Commands::New;
use Dist::Maker::Exception;
use Dist::Maker::Util;
use Dist::Maker;
use X::Dist::Maker::Clobber;
use X::Dist::Maker::DuplicateDependency;
use X::Dist::Maker::DuplicateProvide;
use X::Dist::Maker::NoDistribution;

unit module Dist::Maker::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 Dist::Maker::Commands::Add for
	#| more information.
	Str :$kind = Str,
) is export {
	CATCH {
		when Dist::Maker::Exception {
			note $_.message;
			exit $_.code;
		}
	}

	dm-config-load;

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

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

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

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

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

	my $contents = Str;
	my $template = xdg-config-home.add('rdm/templates/provide.raku');

	if ($template.f) {
		my $body;

		if ($kind) {
			my $kind-template = $template
				.parent
				.add("kinds/$kind.raku");

			if ($kind-template.f) {
				$body = Template::Mustache.render(
					$kind-template.slurp,
					{
						:%meta,
						this => {
							:$provide,
						},
					},
				);
			} else {
				note "No template found at $kind-template.absolute()";
			}
		}

		$contents = Template::Mustache.render($template.slurp, {
			:%meta,
			body => $body.?trim // '# TODO',
			this => {
				:$provide,
			}
		});
	}

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

	if (!dm-config('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 Dist::Maker::Exception {
			note $_.message;
			exit $_.code;
		}
	}

	dm-config-load;

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

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

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

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

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

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

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

	if (dm-config('depend.zef-install')) {
		run << zef install "$module" >>;
	}
}

#| 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 Dist::Maker::Exception {
			note $_.message;
			exit $_.code;
		}
	}

	dm-config-load;

	if (!$name) { $name = dm-prompt('Name'); }
	if (!$path) { $path = dm-prompt('Path', dm-config('new.path')); }

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

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

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

	dist-maker-new(
		$name,
		$description,
		$license,
		$path.IO,
		:!sanity-checks,
	);

	if (!dm-config('main.quiet')) {
		say "Created skeleton for $name at $destination";
	}
}

=begin pod

=NAME    Dist::Maker::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