@@ -4739,8 +4739,14 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4739
4739
};
4740
4740
var ok_count : usize = 0 ;
4741
4741
4742
+ // default replacements for templates
4743
+ const replacements = &[_ ]Replacement {
4744
+ .{ .variable = "$root" , .replacement = cwd_basename },
4745
+ .{ .variable = "$version" , .replacement = build_options .version },
4746
+ };
4747
+
4742
4748
for (template_paths ) | template_path | {
4743
- if (templates .write (arena , fs .cwd (), cwd_basename , template_path )) | _ | {
4749
+ if (templates .write (arena , fs .cwd (), template_path , replacements )) | _ | {
4744
4750
std .log .info ("created {s}" , .{template_path });
4745
4751
ok_count += 1 ;
4746
4752
} else | err | switch (err ) {
@@ -7427,7 +7433,12 @@ fn loadManifest(
7427
7433
var templates = findTemplates (gpa , arena );
7428
7434
defer templates .deinit ();
7429
7435
7430
- templates .write (arena , options .dir , options .root_name , Package .Manifest .basename ) catch | e | {
7436
+ const replacements = &[_ ]Replacement {
7437
+ .{ .variable = "$root" , .replacement = options .root_name },
7438
+ .{ .variable = "$version" , .replacement = build_options .version },
7439
+ };
7440
+
7441
+ templates .write (arena , options .dir , Package .Manifest .basename , replacements ) catch | e | {
7431
7442
fatal ("unable to write {s}: {s}" , .{
7432
7443
Package .Manifest .basename , @errorName (e ),
7433
7444
});
@@ -7467,6 +7478,19 @@ fn loadManifest(
7467
7478
return .{ manifest , ast };
7468
7479
}
7469
7480
7481
+ /// replace variables for Zig templates
7482
+ /// $root -> "project_name"
7483
+ const Replacement = struct {
7484
+ variable : []const u8 ,
7485
+ replacement : []const u8 ,
7486
+
7487
+ pub inline fn check_variable (self : * @This ()) ! void {
7488
+ if (self .variable .len < 2 ) {
7489
+ return error .InvalidVariable ;
7490
+ }
7491
+ }
7492
+ };
7493
+
7470
7494
const Templates = struct {
7471
7495
zig_lib_directory : Cache.Directory ,
7472
7496
dir : fs.Dir ,
@@ -7483,8 +7507,8 @@ const Templates = struct {
7483
7507
templates : * Templates ,
7484
7508
arena : Allocator ,
7485
7509
out_dir : fs.Dir ,
7486
- root_name : []const u8 ,
7487
7510
template_path : []const u8 ,
7511
+ replacements : ? []const Replacement ,
7488
7512
) ! void {
7489
7513
if (fs .path .dirname (template_path )) | dirname | {
7490
7514
out_dir .makePath (dirname ) catch | err | {
@@ -7500,24 +7524,47 @@ const Templates = struct {
7500
7524
templates .buffer .clearRetainingCapacity ();
7501
7525
try templates .buffer .ensureUnusedCapacity (contents .len );
7502
7526
7503
- if (mem .eql (u8 , template_path , Package .Manifest .basename )) {
7504
- for (contents ) | c | {
7505
- if (c == '$' ) {
7506
- try templates .buffer .appendSlice (root_name );
7507
- } else if (c == '*' ) {
7508
- try templates .buffer .appendSlice (build_options .version );
7509
- } else {
7510
- try templates .buffer .append (c );
7511
- }
7512
- }
7513
- } else {
7514
- for (contents ) | c | {
7515
- if (c == '$' ) {
7516
- try templates .buffer .appendSlice (root_name );
7527
+ var iterator = mem .splitScalar (u8 , contents , '\n ' );
7528
+
7529
+ // replace variables like $root and $version with the project name
7530
+ // and zig compiler version respectively
7531
+ while (iterator .next ()) | line | {
7532
+
7533
+ var i : usize = 0 ;
7534
+ while (i < line .len ) : (i += 1 ) {
7535
+ const c = line [i ];
7536
+
7537
+ if (replacements ) | replace_items | {
7538
+ if (c == '$' ) {
7539
+ var found : bool = false ;
7540
+
7541
+ for (replace_items ) | replacement | {
7542
+ try replacement .check_variable ();
7543
+
7544
+ if (line .len - i < replacement .variable .len ) {
7545
+ continue ;
7546
+ }
7547
+
7548
+ // found a match, break out
7549
+ if (mem .eql (u8 , replacement .variable , line [i .. i + replacement .variable .len ])) {
7550
+ try templates .buffer .appendSlice (replacement .replacement );
7551
+ i += replacement .variable .len - 1 ;
7552
+ found = true ;
7553
+ break ;
7554
+ }
7555
+ }
7556
+
7557
+ if (! found ) try templates .buffer .append (c );
7558
+ } else {
7559
+ // if we make it out here, no replacement was found, and we write out the literal '$'
7560
+ try templates .buffer .append (c );
7561
+ }
7517
7562
} else {
7518
7563
try templates .buffer .append (c );
7519
7564
}
7520
7565
}
7566
+
7567
+ try templates .buffer .append ('\n ' );
7521
7568
}
7522
7569
7523
7570
return out_dir .writeFile (.{
0 commit comments