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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
|
const options = @import("options");
const std = @import("std");
const assert = std.debug.assert;
const cli = @import("cli.zig");
const fs = @import("fs.zig");
const conf = @import("conf.zig");
const tmpl = @import("template.zig");
const tree = @import("tree.zig");
const port = @import("port.zig");
const git = @import("git.zig");
const Env = @import("env.zig");
const Time = @import("time.zig").Time;
const default_tree_name = "default";
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var main_cmd: Main_Command = undefined;
var args: cli.Args = try .init(allocator, "porteur", &main_cmd);
defer args.deinit(allocator);
try main_cmd.execute(allocator, &args);
}
const Main_Command = struct {
pub const help = cli.Command_Help{
.name = "porteur",
.synopsis = &.{
"porteur [--etc=⟨path⟩] tree ⟨command⟩ [options] [⟨args⟩]",
"porteur [--etc=⟨path⟩] tmpl ⟨command⟩ [options] [⟨args⟩]",
"porteur [--etc=⟨path⟩] port ⟨command⟩ [options] [⟨args⟩]",
"porteur help [⟨command⟩ [⟨subcommand⟩]]",
},
.short_description = "Build your own ports.",
.long_description = std.fmt.comptimePrint(
\\Porteur is a tool for managing and creating custom ports from a Git repository.
\\Each port is part of a ports tree and results in a FreeBSD port usable by
\\`make` or `poudriere`. It uses a template to generate the files that describe
\\how to build the application from source code.
\\
\\To add a new port some basic information (e.g. name, category) needs to be
\\provided including a Git repository where the source code can be found. Porteur
\\always uses the configured branch of the Git repository and operates only on
\\that version of the source code. When a port is updated, porteur checks the
\\latest commit on this branch and updates the port information accordingly.
\\
\\Each port lives in a ports tree which can be used by the FreeBSD ports system.
\\When operating on ports there must always be a tree specified. If no tree name
\\is provided explicitly the default tree `{[default_tree]s}` will be used.
, .{ .default_tree = default_tree_name }),
.extra_sections = &.{
.{
.heading = "TEMPLATES",
.body =
\\Each FreeBSD port consists of a collection of files that automate the
\\compilation of an application. Since each port in the FreeBSD ports
\\tree requires its own set of files, maintaining them can be tedious,
\\especially when large parts of the content is identical.
\\
\\To make the definition of such files reusable, porteur uses templates,
\\located in the `⟨etc⟩/porteur/tmpl/` directory (see FILES). Each port
\\must reference a template and defines the variables used within it.
\\
\\A template is a collection of files that will used to render a port
\\usable by FreeBSD. Each file can use variables to render port specific
\\data, where a variable `v` can be referenced with the placeholder
\\`{{v}}`. It is also possible to use variables in filenames. For more
\\information how to define variables, see the VARIABLES section.
\\
\\Normally, the processed template files are used to create the port
\\files, the distribution file is created, and a distinfo is generated.
\\To have more control over this process, a template can define a file
\\named `.config` in its root directory. For a documentation of all
\\configurable options, see the sample configuration file (see FILES).
,
},
.{
.heading = "VARIABLES",
.body =
\\Variables are used to substitute port specific values in templates.
\\Defining variables is file-based and has the following syntax:
\\
\\ key = value
\\
\\Keys define the variable name that can later be referenced (e.g. in
\\a template file) and the assigned value is the string that replaces
\\this key. Leading and trailing whitespace in a value is trimmed.
\\Normally, a key/value pair is defined on a single line, but multi-line
\\values are also supported:
\\
\\ key = value line one
\\ = value line two
\\ =
\\ = value line three
\\
\\Each continuation line starts with an empty key and is rendered as a
\\separate line in the template. This also allows empty lines to be
\\part of the value. Again, leading and trailing whitespace of each line
\\is trimmed.
\\
\\To avoid redefining common information already known (e.g., port name,
\\port version), porteur predefines certain variables. All predefined
\\variable names start with a dot (`.`), so it is recommended not to use
\\custom variables starting with a dot.
\\
\\List of predefined variables:
\\
\\ .portname - Name of the port
\\ .portversion - Current version of the port
\\ .category - Category of the port
\\ .distdir - Directory containing all distfiles
\\ .distname - Name of the archive file containing the distribution
,
},
.{
.heading = "DISTFILES",
.body =
\\To compile a port, a distribution is necessary that contains the source
\\code. Porteur creates a gzipped archive of the Git repository and puts
\\it in the configured `distfiles-dir`. The filename of this archive is
\\provided in the `.dist.name` variable and the directory where its put in
\\is provided in the `.dist.dir` variable (see VARIABLES). So the template
\\can reference the archive's location and tell FreeBSD where to find the
\\source code.
\\
\\Sometimes it is necessary to do extra steps before the distribution is
\\created (e.g. vendoring). These steps can be defined as a target in the
\\port Makefile of the template. When porteur executes this target it will
\\set ${WRKSRC} to the port's source tree. The target name can be defined
\\with `dist-prepare-target` in the template's configuration file (see
\\TEMPLATES).
,
},
.{
.heading = "FILES",
.body = std.fmt.comptimePrint(
\\Porteur uses two locations where data will be stored. The first location
\\is `{[prefix]s}/etc/` and contains all configuration files (ports and
\\templates). This location can be overwritten with the --etc flag. The
\\second location is `{[prefix]s}/porteur/` and contains a copy of the
\\sources and all generated port files that FreeBSD uses.
\\
\\The following directory structure is used:
\\
\\ {[prefix]s}/etc/porteur/porteur.conf
\\ Configuration file for porteur.
\\
\\ {[prefix]s}/etc/porteur/porteur.conf.sample
\\ Sample configuration file for porteur.
\\
\\ {[prefix]s}/etc/porteur/template.conf.sample
\\ Sample configuration file for a template.
\\
\\ {[prefix]s}/etc/porteur/ports/<tree>/
\\ Configuration for each port.
\\
\\ {[prefix]s}/etc/porteur/tmpl/⟨template⟩/
\\ Template definitions used by the ports.
\\
\\ {[prefix]s}/porteur/ports/⟨tree⟩/
\\ Generated ports tree used by FreeBSD.
\\
\\ {[prefix]s}/porteur/src/⟨tree⟩/
\\ Copies of the sources for each port.
\\
\\ {[prefix]s}/porteur/.wrk/
\\ Working directory for porteur.
, .{ .prefix = options.path_prefix }),
},
},
};
options: struct {
etc: cli.Option([]const u8, .{
.name = "etc",
.argument_name = "path",
.short_description = "The path to porteur's configuration directory.",
.long_description = std.fmt.comptimePrint(
\\The path of the directory porteur is storing its configuration.
\\Defaults to `{[prefix]s}/etc/`. See the FILES section for more
\\information.
, .{ .prefix = options.path_prefix }),
.default_value = options.path_prefix ++ "/etc/",
}),
},
pub const Subcommands = union(enum) {
help: Help_Command,
tmpl: Tmpl_Command,
tree: Tree_Command,
port: Port_Command,
};
pub fn execute(self: *Main_Command, allocator: std.mem.Allocator, args: *cli.Args) !void {
const subcmd = args.next_subcommand(Main_Command) orelse {
cli.print_usage(cli.stdout, Main_Command);
return;
};
const etc_path = self.options.etc.value_or_default();
const env: Env = .init(allocator, etc_path, args);
switch (subcmd) {
.help => |cmd| cmd.execute(env),
.tmpl => |cmd| cmd.execute(env),
.tree => |cmd| cmd.execute(env),
.port => |cmd| cmd.execute(env),
}
}
};
const Help_Command = struct {
pub const help = cli.Command_Help{
.name = "porteur help",
.synopsis = &.{
"porteur help [⟨command⟩ [⟨subcommand⟩]]",
},
.short_description = "Display help information.",
.long_description =
\\Without ⟨command⟩ and ⟨subcommand⟩ given, the synopsis of the porteur command
\\will be printed. This is equivalent to using `porteur` without any arguments.
\\
\\When ⟨command⟩ is specified without ⟨subcommand⟩, a detailed description of this
\\command is printed. An additional ⟨subcommand⟩ prints the detailed description of
\\the subcommand.
\\
\\The `porteur help ...` and `porteur ... --help` commands are identical.
\\
\\This help text can be displayed with `porteur help help` or `porteur help --help`.
,
};
pub const Subcommands = Help_Subcommands(Main_Command.Subcommands);
fn execute(self: Help_Command, env: Env) void {
_ = self;
const subcmd = env.args.next_subcommand(Help_Command) orelse {
cli.print_usage(cli.stdout, Main_Command);
return;
};
switch (subcmd) {
inline else => |cmd| execute_subcmd(env.args, @TypeOf(cmd)),
}
}
fn execute_subcmd(args: *cli.Args, comptime Command: type) void {
if (@hasDecl(Command, "Subcommands")) {
if (args.next_subcommand(Command)) |subcmd| {
switch (subcmd) {
inline else => |cmd| {
execute_subcmd(args, @TypeOf(cmd));
return;
},
}
}
}
cli.print_help(Command);
}
fn Help_Subcommands(comptime Subcmds: type) type {
assert(@typeInfo(Subcmds) == .@"union");
const union_info = @typeInfo(Subcmds).@"union";
var fields: [union_info.fields.len]std.builtin.Type.UnionField = undefined;
for (union_info.fields, 0..) |field, i| {
fields[i] = .{
.name = field.name,
.alignment = field.alignment,
.type = struct {
pub const help = field.type.help;
},
};
}
return @Type(.{ .@"union" = .{
.layout = union_info.layout,
.tag_type = union_info.tag_type,
.fields = &fields,
.decls = &.{},
} });
}
};
const Tmpl_Command = struct {
pub const help = cli.Command_Help{
.name = "porteur tmpl",
.synopsis = &.{
"porteur tmpl ⟨command⟩ [options] [⟨args⟩]",
},
.short_description = "Provide management of port templates.",
.long_description =
\\This command manages port templates. A port template defines the files
\\a port uses to automate the compilation of its source code. To use a
\\template with multiple ports variables can be used which are defined
\\for each port and later replaced when the port will be updated.
\\
\\A variable definition within a template file has the form `{{key}}`,
\\where the value of `key` can be defined for each port using this
\\template. If no such value is defined for a port, it will simply be
\\replaced by an empty string.
\\
\\For more information about defining and using templates, see the
\\TEMPLATES section in `porteur --help`.
,
};
pub const Subcommands = union(enum) {
list: Tmpl_Ls,
conf: Tmpl_Conf,
};
pub fn execute(self: Tmpl_Command, env: Env) void {
_ = self;
const subcmd = env.args.next_subcommand(Tmpl_Command) orelse {
cli.fatal_with_usage(Tmpl_Command, "missing subcommand", .{});
};
switch (subcmd) {
.list => |cmd| cmd.execute(env) catch |err| fatal_err(err, "failed to list templates"),
.conf => |cmd| cmd.execute(env) catch |err| fatal_err(err, "failed to update template configuration"),
}
}
const Tmpl_Ls = struct {
pub const help = cli.Command_Help{
.name = "porteur tmpl ls",
.synopsis = &.{
"porteur tmpl [options] ls",
},
.short_description = "List all port templates.",
.long_description =
\\This command lists the names of all port templates that are known by
\\porteur.
,
};
options: struct {
long: cli.Option(bool, .{
.name = "long",
.short_name = 'l',
.short_description = "List templates in the long format.",
.long_description =
\\List the templates in a longer format that includes more information
\\about each template. In the long format a template is listed with its
\\path.
,
.default_value = false,
}),
},
fn execute(self: Tmpl_Ls, env: Env) !void {
const long = self.options.long.value_or_default();
const print_opts: cli.Print_Options = .{ .hanging_indent = 4 };
var iter = try tmpl.iterate(env);
defer iter.deinit();
if (long) {
while (try iter.next()) |template| {
cli.stdout.printf_line("{s} {s}", .{ template.name, template.path.name() }, print_opts);
}
} else {
while (try iter.next()) |template| {
cli.stdout.print_line(template.name, print_opts);
}
}
cli.stdout.flush();
}
};
const Tmpl_Conf = struct {
pub const help = cli.Command_Help{
.name = "porteur tmpl conf",
.synopsis = &.{
"porteur tmpl [options] conf ⟨template⟩",
"porteur tmpl [options] conf (--edit | -e) ⟨template⟩",
"porteur tmpl [options] conf --import=⟨path⟩ ⟨template⟩",
},
.short_description = "Manage the configuration for a template.",
.long_description =
\\Manage the configuration for a template. Without a flag specified it lists
\\the current configuration values for this template. To edit the configuration
\\either use the --edit or the --import option. With the --edit option set,
\\the existing configuration can be modified interactively using an editor.
\\The --import flag imports an external configuration from a file and overwrites
\\the existing one. The configuration file consists of a list of variables (see
\\the VARIABLES section in `porteur --help`). A documentation of possible
\\configuration values can be found in `⟨etc⟩/porteur/template.conf.sample`.
,
};
options: struct {
edit: cli.Option(bool, .{
.name = "edit",
.short_name = 'e',
.short_description = "Edit the template configuration interactively.",
.long_description =
\\Edit the existing configuration for the template interactively. An
\\editor will be invoked where the existing configuration can be updated.
\\After exiting the editor the updates need to be confirmed. This
\\flag is mutual exclusive with --import.
,
}),
import: cli.Option([]const u8, .{
.name = "import",
.argument_name = "path",
.short_description = "Import new configuration from the given file.",
.long_description =
\\Import the new configuration for the template from the specified file.
\\This file must contain valid variable definitions and will overwrite
\\the existing configuration for the template. This flag is mutual
\\exclusive with --edit.
,
}),
},
fn execute(self: Tmpl_Conf, env: Env) !void {
const template_name = env.args.next_positional() orelse {
cli.fatal_with_usage(@This(), "missing template name", .{});
};
const edit = self.options.edit.value;
const import = self.options.import.value;
if (edit != null and import != null) {
cli.fatal("can either edit or import configuration", .{});
}
if (edit) |_| {
try tmpl.edit_config(env, template_name);
} else if (import) |filename| {
try tmpl.import_config(env, template_name, .init(filename));
} else {
const template = try tmpl.must_get(env, template_name);
const config = try tmpl.read_config(env, template);
defer config.deinit();
var iter = config.raw.iterate();
while (iter.next()) |c| {
cli.stdout.write_line("{f}", .{c});
}
cli.stdout.flush();
}
}
};
};
const Tree_Command = struct {
pub const help = cli.Command_Help{
.name = "porteur tree",
.synopsis = &.{
"porteur tree ⟨command⟩ [options] [⟨args⟩]",
},
.short_description = "Provide management of ports trees.",
.long_description =
\\This command manages ports trees. A ports tree is a collection of your own
\\ports which can be used with the FreeBSD ports system. A port is a collection
\\of files that automate the compilation of an application from its source code.
,
};
pub const Subcommands = union(enum) {
list: Tree_Ls,
rm: Tree_Rm,
};
pub fn execute(self: Tree_Command, env: Env) void {
_ = self;
const subcmd = env.args.next_subcommand(Tree_Command) orelse {
cli.fatal_with_usage(Tree_Command, "missing subcommand", .{});
};
switch (subcmd) {
.list => |cmd| cmd.execute(env) catch |err| fatal_err(err, "failed to iterate ports trees"),
.rm => |cmd| cmd.execute(env) catch |err| fatal_err(err, "failed to remove ports trees"),
}
}
const Tree_Ls = struct {
pub const help = cli.Command_Help{
.name = "porteur tree ls",
.synopsis = &.{
"porteur tree ls",
},
.short_description = "List all existing ports trees.",
.long_description =
\\This command lists the names of all ports trees that are initialized with
\\porteur. The name is used to specify a specific tree when dealing with
\\ports.
\\
\\It simply prints all tree names line by line. For more information about
\\a specific tree use the `porteur tree info` command.
,
};
fn execute(self: Tree_Ls, env: Env) !void {
_ = self;
var iter = try tree.iterate(env);
defer iter.deinit();
while (try iter.next()) |t| {
cli.stdout.print_line(t.name, .{});
}
cli.stdout.flush();
}
};
const Tree_Rm = struct {
pub const help = cli.Command_Help{
.name = "porteur tree rm",
.synopsis = &.{
"porteur tree rm ⟨tree⟩",
},
.short_description = "Remove an existing ports tree.",
.long_description =
\\This command removes an existing ports tree and deletes all its ports.
\\It asks for confirmation before deleting a tree.
,
};
options: struct {
force: cli.Option(bool, .{
.name = "force",
.short_name = 'f',
.short_description = "Do not prompt for confirmation.",
.long_description =
\\Attempt to remove a ports tree without prompting for confirmation.
\\If the tree does not exist, do not report an error.
,
.default_value = false,
}),
},
fn execute(self: Tree_Rm, env: Env) !void {
const force = self.options.force.value_or_default();
const name = env.args.next_positional() orelse {
if (force) std.process.exit(0);
cli.fatal_with_usage(@This(), "missing tree name", .{});
};
const ports_tree = try tree.get(env, name) orelse {
if (force) return;
cli.fatal("tree not found", .{});
};
if (!force) {
const confirmed = cli.ask_confirmation("Remove tree {s}?", .{ports_tree.name}, .no);
if (!confirmed) return;
}
try port.remove_all(.{
.env = env,
.tree = ports_tree,
});
tree.remove(env, ports_tree) catch {};
}
};
};
const Port_Command = struct {
pub const help = cli.Command_Help{
.name = "porteur port",
.synopsis = &.{
"porteur port [--tree=⟨tree⟩ | -t ⟨tree⟩] ⟨command⟩ [options] [⟨args⟩]",
},
.short_description = "Provide management of ports in a ports tree.",
.long_description =
\\This command manages the ports of a ports tree. A port is a collection of
\\files that automate the compilation of an application from its source code.
\\It references a Git repository that contains the source code and a template
\\which is used to generate the port files from.
,
};
options: struct {
tree: cli.Option([]const u8, .{
.name = "tree",
.short_name = 't',
.argument_name = "tree",
.short_description = "The name of the ports tree.",
.long_description = std.fmt.comptimePrint(
\\The name of the ports tree. If this option is not specified the default
\\tree `{[default_tree]s}` will be used.
, .{ .default_tree = default_tree_name }),
.default_value = default_tree_name,
}),
},
pub const Subcommands = union(enum) {
list: Port_Ls,
info: Port_Info,
add: Port_Add,
vars: Port_Vars,
update: Port_Update,
bump_version: Port_Bump_Version,
history: Port_History,
rollback: Port_Rollback,
rm: Port_Rm,
};
pub fn execute(self: Port_Command, env: Env) void {
const subcmd = env.args.next_subcommand(Port_Command) orelse {
cli.fatal_with_usage(Port_Command, "missing subcommand", .{});
};
const tree_name = self.options.tree.value_or_default();
const ports_tree = tree.get(env, tree_name) catch |err| fatal_err(err, "failed to get tree");
if (ports_tree == null) cli.fatal("ports tree not found: {s}", .{tree_name});
const ctx = port.Ctx{
.env = env,
.tree = ports_tree.?,
};
switch (subcmd) {
.list => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to list ports"),
.info => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to fetch port"),
.add => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to write port"),
.vars => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to update port variables"),
.update => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to update port"),
.bump_version => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to bump the port version"),
.history => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to list distfile history"),
.rollback => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to roll back the port version"),
.rm => |cmd| cmd.execute(ctx) catch |err| fatal_err(err, "failed to delete port"),
}
}
const Port_Ls = struct {
pub const help = cli.Command_Help{
.name = "porteur port ls",
.synopsis = &.{
"porteur port [options] ls",
},
.short_description = "List all ports of a ports tree.",
.long_description =
\\This command lists all ports the ports tree contains. It simply prints
\\all port names along with the port versions line by line. For more
\\information about a specific port see the `porteur port info` command.
,
};
options: struct {
long: cli.Option(bool, .{
.name = "long",
.short_name = 'l',
.short_description = "List ports in the long format.",
.long_description =
\\List the ports in a longer format that includes more information about
\\each port. In the long format a port is listed with its current version
\\and its category.
,
.default_value = false,
}),
},
fn execute(self: Port_Ls, ctx: port.Ctx) !void {
const long = self.options.long.value_or_default();
const print_opts: cli.Print_Options = .{ .hanging_indent = 4 };
var iter = try port.iterate(ctx);
defer iter.deinit();
if (long) {
while (try iter.next()) |p| {
const version = try port.read_version(ctx, p);
cli.stdout.printf_line("{s}@{f} {s}", .{ p.name, version, p.category }, print_opts);
}
} else {
while (try iter.next()) |p| {
cli.stdout.print_line(p.name, print_opts);
}
}
cli.stdout.flush();
}
};
const Port_Info = struct {
pub const help = cli.Command_Help{
.name = "porteur port info",
.synopsis = &.{
"porteur port [options] info ⟨port⟩",
},
.short_description = "Show information about a port.",
.long_description =
\\This command prints information about the specified port of a ports
\\tree. Variables for this port are not listed here. To show them use
\\the `porteur port vars` command. If the port cannot be found in the
\\ports tree an error will be reported.
,
};
fn execute(self: Port_Info, ctx: port.Ctx) !void {
_ = self;
const port_name = ctx.env.args.next_positional() orelse {
cli.fatal_with_usage(@This(), "missing port name", .{});
};
const p = try port.must_get(ctx, port_name);
defer p.deinit();
const version = try port.read_version(ctx, p);
const source = try port.read_source(ctx, p);
defer source.deinit();
const distdir = ctx.env.ports_dist_dir(ctx.tree.name, p.name);
cli.stdout.printf_line("Name: {s}", .{p.name}, .{});
cli.stdout.printf_line("Category: {s}", .{p.category}, .{});
cli.stdout.printf_line("Template: {s}", .{p.template}, .{});
cli.stdout.printf_line("Version: {f}", .{version}, .{});
cli.stdout.printf_line("Git Repo: {s}", .{source.repo.url}, .{});
cli.stdout.printf_line("Git Branch: {s}", .{source.repo.branch}, .{});
if (source.commit.hash.len > 0) {
cli.stdout.printf_line("Commit Hash: {s}", .{source.commit.hash}, .{});
cli.stdout.printf_line("Commit Time: {f}", .{Time.from_timestamp(source.commit.timestamp)}, .{});
} else {
cli.stdout.printf_line("Commit Hash: -", .{}, .{});
cli.stdout.printf_line("Commit Time: -", .{}, .{});
}
cli.stdout.printf_line("Distdir: {s}", .{distdir.name()}, .{});
cli.stdout.flush();
}
};
const Port_Add = struct {
pub const help = cli.Command_Help{
.name = "porteur port add",
.synopsis = &.{
"porteur port [options] add",
},
.short_description = "Add a new port to the ports tree.",
.long_description =
\\This command adds a new port to the ports tree. A port needs to reference a
\\Git repository that holds the source code for distribution and compilation.
\\Porteur creates a local clone of this repository.
\\
\\When adding a new port there will be no variables defined and no FreeBSD
\\ports tree files generated. To add variables for the new port, use the
\\`porteur port vars` command. If everything is set up correctly, execute
\\a `porteur port update` to initially render the port.
\\
\\This command is interactive.
,
};
fn execute(self: Port_Add, ctx: port.Ctx) !void {
_ = self;
if (!try tmpl.exists_any(ctx.env)) {
const tmpl_dir = tmpl.etc_dir(ctx.env);
cli.stderr.print_line("No templates found.", .{});
cli.fatal("Please define a template in `{s}`.", .{tmpl_dir.name()});
}
const namebuf = try ctx.env.allocator.alloc(u8, 64);
defer ctx.env.allocator.free(namebuf);
const name = while (true) {
const input = cli.ask("Name: ", .{}, namebuf) orelse {
cli.stderr.print_line("ERROR: The port name is required.", .{});
cli.stderr.flush();
continue;
};
if (std.mem.indexOfAny(u8, input, "/\\\t ")) |_| {
cli.stderr.print_line("ERROR: The port name must not contain slash, backslash, or space.", .{});
cli.stderr.flush();
continue;
}
break input;
};
var catbuf: [128]u8 = undefined;
var default_cat_buf: [128]u8 = undefined;
const default_cat = if (ctx.env.default_category) |cat|
try std.fmt.bufPrint(&default_cat_buf, " [{s}]", .{cat})
else
"";
const category = while (true) {
const input = cli.ask("Category{s}: ", .{default_cat}, &catbuf) orelse default_cat: {
if (ctx.env.default_category) |c| break :default_cat c;
cli.stderr.print_line("ERROR: The port category is required.", .{});
cli.stderr.flush();
continue;
};
if (std.mem.indexOfAny(u8, input, "/\\\t ")) |_| {
cli.stderr.print_line("ERROR: The port category must not contain slash, backslash, or space.", .{});
cli.stderr.flush();
continue;
}
break input;
};
var verbuf: [64]u8 = undefined;
const version = while (true) {
const input = cli.ask("Initial Version [0.1.0]: ", .{}, &verbuf) orelse "0.1.0";
const ver = port.Version.parse_semver(input) catch {
cli.stderr.print_line("ERROR: The port version must be in the format `⟨major⟩[[.⟨minor⟩[.⟨patch⟩]`.", .{});
cli.stderr.flush();
continue;
};
break ver;
};
var repourlbuf: [std.fs.max_path_bytes]u8 = undefined;
const repo_url = while (true) {
const input = cli.ask("Git Repository: ", .{}, &repourlbuf) orelse {
cli.stderr.print_line("ERROR: The Git repository is required.", .{});
cli.stderr.flush();
continue;
};
break input;
};
var repobranchbuf: [64]u8 = undefined;
const repo_branch = while (true) {
const default_branch = ctx.env.default_repo_branch;
const input = cli.ask("Git Branch [{s}]: ", .{default_branch}, &repobranchbuf) orelse default_branch;
if (!git.is_valid_branch_name(input)) {
cli.stderr.print_line("ERROR: Invalid Git branch name.", .{});
cli.stderr.flush();
continue;
}
break input;
};
var templatebuf: [64]u8 = undefined;
const template = while (true) {
const input = cli.ask("Template: ", .{}, &templatebuf) orelse {
cli.stderr.print_line("ERROR: The template is required.", .{});
cli.stderr.flush();
continue;
};
if (!try tmpl.exists(ctx.env, input)) {
cli.stderr.print_line("ERROR: Template does not exist.", .{});
cli.stderr.flush();
continue;
}
break input;
};
cli.stderr.print_line("", .{});
cli.stderr.printf_line("Name: {s}", .{name}, .{});
cli.stderr.printf_line("Category: {s}", .{category}, .{});
cli.stderr.printf_line("Initial Version: {f}", .{version}, .{});
cli.stderr.printf_line("Git Repository: {s}", .{repo_url}, .{});
cli.stderr.printf_line("Git Branch: {s}", .{repo_branch}, .{});
cli.stderr.printf_line("Template: {s}", .{template}, .{});
cli.stderr.flush();
const ok = cli.ask_confirmation("OK?", .{}, .yes);
if (ok) {
const new_port: port.Info = .init(ctx.tree, name, category, template);
try port.create(ctx, new_port, .{
.repo_url = repo_url,
.repo_branch = repo_branch,
.initial_version = version,
});
cli.stderr.print_line("", .{});
cli.stderr.printf_line("port created: {s}", .{new_port.name}, .{});
cli.stderr.print_line("", .{});
cli.stderr.print_line("Next steps:", .{});
cli.stderr.print_line("(1) use `porteur port vars` to define variables", .{ .margin = 2 });
cli.stderr.print_line("(2) use `porteur port update` to initially render the port", .{ .margin = 2 });
cli.stderr.flush();
}
}
};
const Port_Vars = struct {
pub const help = cli.Command_Help{
.name = "porteur port vars",
.synopsis = &.{
"porteur port [options] vars ⟨port⟩",
"porteur port [options] vars (--edit | -e) ⟨port⟩",
"porteur port [options] vars --import=⟨path⟩ ⟨port⟩",
},
.short_description = "Manage variables for a port.",
.long_description =
\\Manage the variables for a port. Without a flag specified it lists all
\\existing variables for this port. To edit the variables either use the
\\--edit or the --import option. With the --edit option set, all existing
\\variables can be modified interactively using an editor. The --import
\\flag imports the variables from a file and overwrites all existing ones.
\\For more information on how to format the variables see the VARIABLES
\\section in `porteur --help`.
,
};
options: struct {
edit: cli.Option(bool, .{
.name = "edit",
.short_name = 'e',
.short_description = "Edit the port variables interactively.",
.long_description =
\\Edit the existing variables for the port interactively. An editor
\\will be invoked where the existing variables can be updated. After
\\exiting the editor the updates need to be confirmed. This flag is
\\mutual exclusive with --import.
,
}),
import: cli.Option([]const u8, .{
.name = "import",
.argument_name = "path",
.short_description = "Import new port variables from the given file.",
.long_description =
\\Import the new variables for the port from the specified file. This
\\file must contain valid variable definitions. It will overwrite all
\\existing port variables. This flag is mutual exclusive with --edit.
,
}),
},
fn execute(self: Port_Vars, ctx: port.Ctx) !void {
const port_name = ctx.env.args.next_positional() orelse {
cli.fatal_with_usage(@This(), "missing port name", .{});
};
const edit = self.options.edit.value;
const import = self.options.import.value;
if (edit != null and import != null) {
cli.fatal("can either edit or import variables", .{});
}
if (edit) |_| {
try port.edit_vars(ctx, port_name);
} else if (import) |filename| {
try port.import_vars(ctx, port_name, .init(filename));
} else {
const p = try port.must_get(ctx, port_name);
defer p.deinit();
const vars = try port.read_vars(ctx, p);
defer vars.deinit();
var iter = vars.iterate();
while (iter.next()) |v| {
cli.stdout.write_line("{f}", .{v});
}
cli.stdout.flush();
}
}
};
const Port_Update = struct {
pub const help = cli.Command_Help{
.name = "porteur port update",
.synopsis = &.{
"porteur port [options] update [⟨port⟩ ...]",
},
.short_description = "Update ports in a ports tree.",
.long_description =
\\This command updates one or more ports in the ports tree. For each port, it
\\checks if there is a new commit for the configured Git branch. If so, the
\\files in the ports tree will be generated from the template and the new
\\distribution files will be created. A Git commit is considered new if it has
\\a newer date than the last commit from which the port files were generated
\\or if the date is the same, but the commit hash is different.
\\
\\If no port name is provided it updates all existing ports in the ports tree.
,
};
options: struct {
force: cli.Option(bool, .{
.name = "force",
.short_name = 'f',
.short_description = "Force the port update.",
.long_description =
\\Attempt to do the port update although the current commit for the branch
\\is not newer than the last one. This option forces a rerender for the
\\port based on the branch's latest commit.
,
.default_value = false,
}),
},
fn execute(self: Port_Update, ctx: port.Ctx) !void {
const opts: port.Update_Options = .{
.force = self.options.force.value_or_default(),
};
var count: usize = 0;
while (ctx.env.args.next_positional()) |port_name| {
count += 1;
try update_single_port(ctx, port_name, opts);
}
if (count == 0) {
var iter = try port.iterate(ctx);
defer iter.deinit();
while (try iter.next()) |p| {
try update_single_port(ctx, p.name, opts);
}
}
}
fn update_single_port(ctx: port.Ctx, port_name: []const u8, opts: port.Update_Options) !void {
if (try port.update_version(ctx, port_name, opts)) |new_version| {
cli.stderr.printf_line("updated {s} to {f}", .{ port_name, new_version }, .{});
} else {
cli.stderr.printf_line("already up to date: {s}", .{port_name}, .{});
}
cli.stderr.flush();
}
};
const Port_Bump_Version = struct {
pub const help = cli.Command_Help{
.name = "porteur port bump-version",
.synopsis = &.{
"porteur port [options] bump-version [--major | --minor | --patch] ⟨port⟩",
},
.short_description = "Increment the version of a port.",
.long_description =
\\This command increments the version of a port without updating its sources.
\\It is possible to increment the major version, the minor version, or the
\\patch version. By default, the patch version will be incremented.
,
};
options: struct {
major: cli.Option(bool, .{
.name = "major",
.short_description = "Increment the major version.",
.long_description =
\\Increment the major version of the port by one and reset the minor and patch
\\version to zero. This flag in mutual exclusive with --minor and --patch.
,
.default_value = false,
}),
minor: cli.Option(bool, .{
.name = "minor",
.short_description = "Increment the minor version.",
.long_description =
\\Increment the minor version of the port by one and reset the patch version
\\to zero. The major version stays untouched. This flag in mutual exclusive
\\with --major and --patch.
,
.default_value = false,
}),
patch: cli.Option(bool, .{
.name = "patch",
.short_description = "Increment the patch version.",
.long_description =
\\Increment the patch version of the port by one. The major and minior version
\\stays untouched. This flag in mutual exclusive with --major and --minor.
,
.default_value = false,
}),
},
fn execute(self: Port_Bump_Version, ctx: port.Ctx) !void {
const bump_major: u8 = @intFromBool(self.options.major.value_or_default());
const bump_minor: u8 = @intFromBool(self.options.minor.value_or_default());
const bump_patch: u8 = @intFromBool(self.options.patch.value_or_default());
switch (bump_major + bump_minor + bump_patch) {
0, 1 => {},
else => cli.fatal("can either bump major, minor, or patch version", .{}),
}
const port_name = ctx.env.args.next_positional() orelse {
cli.fatal_with_usage(@This(), "missing port name", .{});
};
const new_version = try port.bump_version(ctx, port_name, .{
.which = if (bump_major != 0) .major else if (bump_minor != 0) .minor else .patch,
});
cli.stderr.printf_line("port version bumped to {f}", .{new_version}, .{});
cli.stderr.flush();
}
};
const Port_History = struct {
pub const help = cli.Command_Help{
.name = "porteur port history",
.synopsis = &.{
"porteur port [options] history ⟨port⟩",
},
.short_description = "List all existing distfile version of a port.",
.long_description =
\\This command lists all existing distfile versions for the given port.
\\The number of distfiles kept can be configured in `porteur.conf`.
,
};
options: struct {
long: cli.Option(bool, .{
.name = "long",
.short_name = 'l',
.short_description = "List distfile version in the long format.",
.long_description =
\\List the distfile version in a longer format that also includes the
\\distfile locations.
,
.default_value = false,
}),
},
fn execute(self: Port_History, ctx: port.Ctx) !void {
const long = self.options.long.value_or_default();
const port_name = ctx.env.args.next_positional() orelse {
cli.fatal_with_usage(@This(), "missing port name", .{});
};
var distfiles = try port.list_distfiles(ctx, port_name);
defer distfiles.deinit(ctx.env.allocator);
if (long) {
const version_strings = try ctx.env.allocator.alloc(port.Version_String, distfiles.items.len);
defer ctx.env.allocator.free(version_strings);
var max_version_len: usize = 0;
for (distfiles.items, 0..) |distfile, i| {
version_strings[i] = distfile.version.to_string();
max_version_len = @max(max_version_len, version_strings[i].len);
}
const spaces = [_]u8{' '} ** 64;
for (distfiles.items, version_strings) |distfile, version| {
const fill = spaces[0 .. max_version_len - version.len];
cli.stdout.write_line("{s}{s} {s}", .{ version.slice(), fill, distfile.path.name() });
}
} else {
for (distfiles.items) |d| {
cli.stdout.write_line("{f}", .{d.version});
}
}
cli.stdout.flush();
}
};
const Port_Rollback = struct {
pub const help = cli.Command_Help{
.name = "porteur port rollback",
.synopsis = &.{
"porteur port [options] rollback ⟨port⟩ ⟨version⟩",
},
.short_description = "Roll back a port to a specific version.",
.long_description =
\\This command rolls back an existing ports to the given version.
\\The distfile of this version must still be in the port's history.
\\It asks for confirmation before rolling back a port.
,
};
fn execute(self: Port_Rollback, ctx: port.Ctx) !void {
_ = self;
const port_name = ctx.env.args.next_positional() orelse {
cli.fatal_with_usage(@This(), "missing port name", .{});
};
const version_str = ctx.env.args.next_positional() orelse {
cli.fatal_with_usage(@This(), "missing version", .{});
};
const version = port.Version.parse(version_str) catch {
cli.fatal("incorrect version: {s}", .{version_str});
};
try port.rollback(ctx, port_name, version);
}
};
const Port_Rm = struct {
pub const help = cli.Command_Help{
.name = "porteur port rm",
.synopsis = &.{
"porteur port [options] rm ⟨port⟩ [⟨port⟩ ...]",
},
.short_description = "Remove ports from a ports tree.",
.long_description =
\\This command removes one or more existing ports from the ports tree.
\\It asks for confirmation before deleting a port.
,
};
options: struct {
force: cli.Option(bool, .{
.name = "force",
.short_name = 'f',
.short_description = "Do not prompt for confirmation.",
.long_description =
\\Attempt to remove ports without prompting for confirmation. If the port
\\does not exist, do not report an error.
,
.default_value = false,
}),
},
fn execute(self: Port_Rm, ctx: port.Ctx) !void {
const force = self.options.force.value_or_default();
var count: usize = 0;
var exit_code: u8 = 0;
while (ctx.env.args.next_positional()) |port_name| {
count += 1;
if (!force) {
const confirmed = cli.ask_confirmation("Remove port {s}?", .{port_name}, .no);
if (!confirmed) continue;
}
const deleted = try port.remove(ctx, port_name);
if (!deleted and !force) {
cli.stderr.printf_line("port not found: {s}", .{port_name}, .{});
cli.stderr.flush();
exit_code = 1;
}
}
if (count == 0 and !force) {
cli.fatal_with_usage(Port_Rm, "missing port name", .{});
} else if (exit_code != 0) {
std.process.exit(exit_code);
}
}
};
};
fn fatal_err(err: anyerror, comptime errmsg: []const u8) noreturn {
cli.fatal(errmsg ++ ": {s}", .{@errorName(err)});
}
test {
std.testing.refAllDecls(@This());
}
|