mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-26 21:19:56 +03:00
59 lines
1.9 KiB
D
Executable file
59 lines
1.9 KiB
D
Executable file
#!/bin/env rdmd
|
|
|
|
/// A helper script that adds a header to every file in a directory.
|
|
|
|
import std;
|
|
|
|
enum fileExt = ".d";
|
|
enum header = "// ---
|
|
// Copyright 2024 Alexandros F. G. Kapretsos
|
|
// SPDX-License-Identifier: MIT
|
|
// Email: alexandroskapretsos@gmail.com
|
|
// Project: https://github.com/Kapendev/popka
|
|
// Version: v0.0.16
|
|
// ---";
|
|
|
|
int main(string[] args) {
|
|
if (args.length == 1) {
|
|
writeln("Provide a directory containing `%s` files.".format(fileExt));
|
|
return 0;
|
|
}
|
|
|
|
// Basic error checking.
|
|
auto targetDir = args[1];
|
|
if (!targetDir.exists) {
|
|
writeln("Path `%s` does not exist.".format(targetDir));
|
|
return 1;
|
|
}
|
|
auto tempIndex = header.countUntil("\n");
|
|
if (tempIndex == -1) {
|
|
writeln("Header separator does not exist.");
|
|
writeln("The first line of the header is the header separator.");
|
|
return 1;
|
|
}
|
|
auto headerSep = header[0 .. tempIndex];
|
|
|
|
// Add the header to the files.
|
|
foreach (file; dirEntries(targetDir, SpanMode.breadth).parallel) {
|
|
if (!file.name.endsWith(fileExt)) continue;
|
|
|
|
auto text = readText(file.name);
|
|
if (text.startsWith(headerSep)) {
|
|
foreach (i, c; text) {
|
|
if (i <= headerSep.length) continue;
|
|
if (i == text.length - headerSep.length) {
|
|
writeln("File `%s` does not have a second header separator.".format(file.name));
|
|
writeln("A header separator looks like this: `%s`".format(headerSep));
|
|
break;
|
|
}
|
|
if (text[i .. i + headerSep.length] == headerSep) {
|
|
std.file.write(file.name, header ~ text[i + headerSep.length .. $]);
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
std.file.write(file.name, header ~ "\n\n" ~ text);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|