mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-25 20:49:57 +03:00
More script stuff.
This commit is contained in:
parent
ee265d440a
commit
62046365b0
5 changed files with 486 additions and 82 deletions
|
@ -5,4 +5,4 @@
|
|||
"description": "A minimal D application.",
|
||||
"license": "proprietary",
|
||||
"name": "android"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,8 @@ version (Windows) {
|
|||
}
|
||||
|
||||
int main(string[] args) {
|
||||
logw("Script is not done yet.");
|
||||
logw("Script is not done!");
|
||||
logi("Base on: https://github.com/raysan5/raylib/wiki/Working-for-Android");
|
||||
foreach (path; buildDirs) mkdir(path);
|
||||
if (readYesNo("Would you like to install sdk packages?", args.length > 1 ? args[1] : "?").isYes) {
|
||||
if (cmd(sdkmanagerName, "--sdk_root=./android/sdk", "--update")) {
|
||||
|
@ -46,9 +47,10 @@ int main(string[] args) {
|
|||
|
||||
// [Noby Library]
|
||||
|
||||
enum cloneExt = "._cl";
|
||||
|
||||
Level minLogLevel = Level.info;
|
||||
bool isCmdLineHidden = false;
|
||||
|
||||
enum cloneExt = "._cl";
|
||||
|
||||
alias Sz = size_t; /// The result of sizeof, ...
|
||||
alias Str = char[]; /// A string slice of chars.
|
||||
|
@ -86,6 +88,16 @@ void echon(A...)(A args) {
|
|||
write(args);
|
||||
}
|
||||
|
||||
void echof(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writefln(text, args);
|
||||
}
|
||||
|
||||
void echofn(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writef(text, args);
|
||||
}
|
||||
|
||||
void cp(IStr source, IStr target) {
|
||||
import std.file;
|
||||
copy(source, target);
|
||||
|
@ -106,12 +118,17 @@ void mkdir(IStr path, bool isRecursive = false) {
|
|||
|
||||
void rmdir(IStr path, bool isRecursive = false) {
|
||||
import std.file;
|
||||
if (!path.isX) {
|
||||
if (path.isX) {
|
||||
if (isRecursive) rmdirRecurse(path);
|
||||
else std.file.rmdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
IStr pwd() {
|
||||
import std.file;
|
||||
return getcwd();
|
||||
}
|
||||
|
||||
IStr cat(IStr path) {
|
||||
import std.file;
|
||||
return path.isX ? readText(path) : "";
|
||||
|
@ -152,6 +169,10 @@ IStr readYesNo(IStr text, IStr firstValue = "?") {
|
|||
return result;
|
||||
}
|
||||
|
||||
IStr fmt(A...)(IStr text, A args...) {
|
||||
import std.format;
|
||||
return format(text, args);
|
||||
}
|
||||
|
||||
IStr join(IStr[] args...) {
|
||||
import std.path;
|
||||
|
@ -196,7 +217,6 @@ int findEnd(IStr str, IStr item) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
IStr trimStart(IStr str) {
|
||||
IStr result = str;
|
||||
while (result.length > 0) {
|
||||
|
@ -237,7 +257,7 @@ void paste(IStr path, IStr content, bool isOnlyMaking = false) {
|
|||
}
|
||||
|
||||
void clone(IStr path) {
|
||||
if (path.isX) paste(path ~ cloneExt, cat(path));
|
||||
if (path.isX) cp(path, path ~ cloneExt);
|
||||
}
|
||||
|
||||
void restore(IStr path, bool isOnlyRemoving = false) {
|
||||
|
@ -258,15 +278,25 @@ void log(Level level, IStr text) {
|
|||
}
|
||||
}
|
||||
|
||||
void logi(IStr text) {
|
||||
log(Level.info, text);
|
||||
}
|
||||
|
||||
void logw(IStr text) {
|
||||
log(Level.warning, text);
|
||||
}
|
||||
|
||||
void loge(IStr text) {
|
||||
log(Level.error, text);
|
||||
}
|
||||
|
||||
void logf(A...)(Level level, IStr text, A args) {
|
||||
import std.format;
|
||||
log(level, text.format(args));
|
||||
log(level, text.fmt(args));
|
||||
}
|
||||
|
||||
int cmd(IStr[] args...) {
|
||||
import std.stdio;
|
||||
import std.process;
|
||||
writeln("[CMD] ", args);
|
||||
if (!isCmdLineHidden) echo("[CMD] ", args);
|
||||
try {
|
||||
return spawnProcess(args).wait();
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -175,9 +175,10 @@ int main(string[] args) {
|
|||
|
||||
// [Noby Library]
|
||||
|
||||
enum cloneExt = "._cl";
|
||||
|
||||
Level minLogLevel = Level.info;
|
||||
bool isCmdLineHidden = false;
|
||||
|
||||
enum cloneExt = "._cl";
|
||||
|
||||
alias Sz = size_t; /// The result of sizeof, ...
|
||||
alias Str = char[]; /// A string slice of chars.
|
||||
|
@ -215,6 +216,16 @@ void echon(A...)(A args) {
|
|||
write(args);
|
||||
}
|
||||
|
||||
void echof(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writefln(text, args);
|
||||
}
|
||||
|
||||
void echofn(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writef(text, args);
|
||||
}
|
||||
|
||||
void cp(IStr source, IStr target) {
|
||||
import std.file;
|
||||
copy(source, target);
|
||||
|
@ -235,12 +246,17 @@ void mkdir(IStr path, bool isRecursive = false) {
|
|||
|
||||
void rmdir(IStr path, bool isRecursive = false) {
|
||||
import std.file;
|
||||
if (!path.isX) {
|
||||
if (path.isX) {
|
||||
if (isRecursive) rmdirRecurse(path);
|
||||
else std.file.rmdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
IStr pwd() {
|
||||
import std.file;
|
||||
return getcwd();
|
||||
}
|
||||
|
||||
IStr cat(IStr path) {
|
||||
import std.file;
|
||||
return path.isX ? readText(path) : "";
|
||||
|
@ -248,7 +264,7 @@ IStr cat(IStr path) {
|
|||
|
||||
IStr[] ls(IStr path = ".", bool isRecursive = false) {
|
||||
import std.file;
|
||||
IStr[] result;
|
||||
IStr[] result = [];
|
||||
foreach (dir; dirEntries(cast(string) path, isRecursive ? SpanMode.breadth : SpanMode.shallow)) {
|
||||
result ~= dir.name;
|
||||
}
|
||||
|
@ -267,8 +283,7 @@ IStr realpath(IStr path) {
|
|||
|
||||
IStr read() {
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
return readln().strip();
|
||||
return readln().trim();
|
||||
}
|
||||
|
||||
IStr readYesNo(IStr text, IStr firstValue = "?") {
|
||||
|
@ -282,6 +297,10 @@ IStr readYesNo(IStr text, IStr firstValue = "?") {
|
|||
return result;
|
||||
}
|
||||
|
||||
IStr fmt(A...)(IStr text, A args...) {
|
||||
import std.format;
|
||||
return format(text, args);
|
||||
}
|
||||
|
||||
IStr join(IStr[] args...) {
|
||||
import std.path;
|
||||
|
@ -310,6 +329,52 @@ bool endsWith(IStr str, IStr end) {
|
|||
return str[$ - end.length .. $] == end;
|
||||
}
|
||||
|
||||
int findStart(IStr str, IStr item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int findEnd(IStr str, IStr item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach_reverse (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
IStr trimStart(IStr str) {
|
||||
IStr result = str;
|
||||
while (result.length > 0) {
|
||||
auto isSpace = (result[0] >= '\t' && result[0] <= '\r') || (result[0] == ' ');
|
||||
if (isSpace) result = result[1 .. $];
|
||||
else break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr trimEnd(IStr str) {
|
||||
IStr result = str;
|
||||
while (result.length > 0) {
|
||||
auto isSpace = (result[$ - 1] >= '\t' && result[$ - 1] <= '\r') || (result[$ - 1] == ' ');
|
||||
if (isSpace) result = result[0 .. $ - 1];
|
||||
else break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr trim(IStr str) {
|
||||
return str.trimStart().trimEnd();
|
||||
}
|
||||
|
||||
void clear(IStr path = ".", IStr ext = "") {
|
||||
foreach (file; ls(path)) {
|
||||
if (file.endsWith(ext)) rm(file);
|
||||
}
|
||||
}
|
||||
|
||||
void paste(IStr path, IStr content, bool isOnlyMaking = false) {
|
||||
import std.file;
|
||||
if (isOnlyMaking) {
|
||||
|
@ -320,7 +385,7 @@ void paste(IStr path, IStr content, bool isOnlyMaking = false) {
|
|||
}
|
||||
|
||||
void clone(IStr path) {
|
||||
if (path.isX) paste(path ~ cloneExt, cat(path));
|
||||
if (path.isX) cp(path, path ~ cloneExt);
|
||||
}
|
||||
|
||||
void restore(IStr path, bool isOnlyRemoving = false) {
|
||||
|
@ -341,15 +406,25 @@ void log(Level level, IStr text) {
|
|||
}
|
||||
}
|
||||
|
||||
void logi(IStr text) {
|
||||
log(Level.info, text);
|
||||
}
|
||||
|
||||
void logw(IStr text) {
|
||||
log(Level.warning, text);
|
||||
}
|
||||
|
||||
void loge(IStr text) {
|
||||
log(Level.error, text);
|
||||
}
|
||||
|
||||
void logf(A...)(Level level, IStr text, A args) {
|
||||
import std.format;
|
||||
log(level, text.format(args));
|
||||
log(level, text.fmt(args));
|
||||
}
|
||||
|
||||
int cmd(IStr[] args...) {
|
||||
import std.stdio;
|
||||
import std.process;
|
||||
writeln("[CMD] ", args);
|
||||
if (!isCmdLineHidden) echo("[CMD] ", args);
|
||||
try {
|
||||
return spawnProcess(args).wait();
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -105,9 +105,9 @@ enum shellFileContent = `
|
|||
int main() {
|
||||
auto isSimpProject = !dubFile.isX;
|
||||
// Check if the files that are needed exist.
|
||||
if (!sourceDir.isX) { echo("Folder `", sourceDir, "` doesn't exist. Create one."); return 1; }
|
||||
if (!assetsDir.isX) { echo("Folder `", assetsDir, "` doesn't exist. Create one."); return 1; }
|
||||
if (!libFile.isX) { echo("File `" , libFile , "` doesn't exist. Download it from raylib releases."); return 1; }
|
||||
if (!sourceDir.isX) { echof("Folder `%s` doesn't exist. Create one.", sourceDir); return 1; }
|
||||
if (!assetsDir.isX) { echof("Folder `%s` doesn't exist. Create one.", assetsDir); return 1; }
|
||||
if (!libFile.isX) { echof("File `%s` doesn't exist. Download it from raylib releases.", libFile); return 1; }
|
||||
clear(".", ".o");
|
||||
// Compile the game.
|
||||
if (isSimpProject) {
|
||||
|
@ -151,9 +151,10 @@ int main() {
|
|||
|
||||
// [Noby Library]
|
||||
|
||||
enum cloneExt = "._cl";
|
||||
|
||||
Level minLogLevel = Level.info;
|
||||
bool isCmdLineHidden = false;
|
||||
|
||||
enum cloneExt = "._cl";
|
||||
|
||||
alias Sz = size_t; /// The result of sizeof, ...
|
||||
alias Str = char[]; /// A string slice of chars.
|
||||
|
@ -191,6 +192,16 @@ void echon(A...)(A args) {
|
|||
write(args);
|
||||
}
|
||||
|
||||
void echof(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writefln(text, args);
|
||||
}
|
||||
|
||||
void echofn(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writef(text, args);
|
||||
}
|
||||
|
||||
void cp(IStr source, IStr target) {
|
||||
import std.file;
|
||||
copy(source, target);
|
||||
|
@ -211,12 +222,17 @@ void mkdir(IStr path, bool isRecursive = false) {
|
|||
|
||||
void rmdir(IStr path, bool isRecursive = false) {
|
||||
import std.file;
|
||||
if (!path.isX) {
|
||||
if (path.isX) {
|
||||
if (isRecursive) rmdirRecurse(path);
|
||||
else std.file.rmdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
IStr pwd() {
|
||||
import std.file;
|
||||
return getcwd();
|
||||
}
|
||||
|
||||
IStr cat(IStr path) {
|
||||
import std.file;
|
||||
return path.isX ? readText(path) : "";
|
||||
|
@ -243,8 +259,7 @@ IStr realpath(IStr path) {
|
|||
|
||||
IStr read() {
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
return readln().strip();
|
||||
return readln().trim();
|
||||
}
|
||||
|
||||
IStr readYesNo(IStr text, IStr firstValue = "?") {
|
||||
|
@ -258,6 +273,10 @@ IStr readYesNo(IStr text, IStr firstValue = "?") {
|
|||
return result;
|
||||
}
|
||||
|
||||
IStr fmt(A...)(IStr text, A args...) {
|
||||
import std.format;
|
||||
return format(text, args);
|
||||
}
|
||||
|
||||
IStr join(IStr[] args...) {
|
||||
import std.path;
|
||||
|
@ -287,8 +306,43 @@ bool endsWith(IStr str, IStr end) {
|
|||
}
|
||||
|
||||
int findStart(IStr str, IStr item) {
|
||||
import std.string;
|
||||
return cast(int) str.indexOf(item);
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int findEnd(IStr str, IStr item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach_reverse (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
IStr trimStart(IStr str) {
|
||||
IStr result = str;
|
||||
while (result.length > 0) {
|
||||
auto isSpace = (result[0] >= '\t' && result[0] <= '\r') || (result[0] == ' ');
|
||||
if (isSpace) result = result[1 .. $];
|
||||
else break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr trimEnd(IStr str) {
|
||||
IStr result = str;
|
||||
while (result.length > 0) {
|
||||
auto isSpace = (result[$ - 1] >= '\t' && result[$ - 1] <= '\r') || (result[$ - 1] == ' ');
|
||||
if (isSpace) result = result[0 .. $ - 1];
|
||||
else break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr trim(IStr str) {
|
||||
return str.trimStart().trimEnd();
|
||||
}
|
||||
|
||||
void clear(IStr path = ".", IStr ext = "") {
|
||||
|
@ -307,7 +361,7 @@ void paste(IStr path, IStr content, bool isOnlyMaking = false) {
|
|||
}
|
||||
|
||||
void clone(IStr path) {
|
||||
if (path.isX) paste(path ~ cloneExt, cat(path));
|
||||
if (path.isX) cp(path, path ~ cloneExt);
|
||||
}
|
||||
|
||||
void restore(IStr path, bool isOnlyRemoving = false) {
|
||||
|
@ -328,15 +382,25 @@ void log(Level level, IStr text) {
|
|||
}
|
||||
}
|
||||
|
||||
void logi(IStr text) {
|
||||
log(Level.info, text);
|
||||
}
|
||||
|
||||
void logw(IStr text) {
|
||||
log(Level.warning, text);
|
||||
}
|
||||
|
||||
void loge(IStr text) {
|
||||
log(Level.error, text);
|
||||
}
|
||||
|
||||
void logf(A...)(Level level, IStr text, A args) {
|
||||
import std.format;
|
||||
log(level, text.format(args));
|
||||
log(level, text.fmt(args));
|
||||
}
|
||||
|
||||
int cmd(IStr[] args...) {
|
||||
import std.stdio;
|
||||
import std.process;
|
||||
writeln("[CMD] ", args);
|
||||
if (!isCmdLineHidden) echo("[CMD] ", args);
|
||||
try {
|
||||
return spawnProcess(args).wait();
|
||||
} catch (Exception e) {
|
||||
|
|
325
scripts/header.d
325
scripts/header.d
|
@ -1,63 +1,298 @@
|
|||
#!/bin/env -S dmd -i -run
|
||||
#!/bin/env -S dmd -run
|
||||
|
||||
/// A helper script that adds a header to every file in a directory.
|
||||
|
||||
import std.file;
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import std.algorithm;
|
||||
import std.parallelism;
|
||||
// [Noby Script]
|
||||
|
||||
enum fileExt = ".d";
|
||||
enum header = "// ---
|
||||
enum headerSep = "---";
|
||||
enum header = `
|
||||
// ---
|
||||
// Copyright 2024 Alexandros F. G. Kapretsos
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Email: alexandroskapretsos@gmail.com
|
||||
// Project: https://github.com/Kapendev/parin
|
||||
// Version: v0.0.41
|
||||
// ---";
|
||||
// ---
|
||||
`[1 .. $ - 1];
|
||||
|
||||
enum headerStart = header.length >= 3 + headerSep.length ? header[3 .. 3 + headerSep.length] : "1";
|
||||
enum headerEnd = header.length >= headerSep.length ? header[$ - headerSep.length .. $] : "2";
|
||||
static assert(headerStart == headerEnd, "The header should start and end with the header separator.");
|
||||
|
||||
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));
|
||||
if (args.length == 1 || (!args[1].isX || !args[1].isD)) {
|
||||
echof("Provide a folder containing `%s` files.", fileExt);
|
||||
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);
|
||||
foreach (path; ls(args[1], true)) {
|
||||
if (!path.endsWith(fileExt)) continue;
|
||||
auto text = cat(path);
|
||||
auto content = text;
|
||||
if (text.length >= header.length && text[3 .. $].startsWith(headerSep)) {
|
||||
content = text[header.length .. $].trimStart();
|
||||
}
|
||||
paste(path, header ~ "\n\n" ~ content);
|
||||
}
|
||||
echo("Done!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// [Noby Library]
|
||||
|
||||
Level minLogLevel = Level.info;
|
||||
bool isCmdLineHidden = false;
|
||||
|
||||
enum cloneExt = "._cl";
|
||||
|
||||
alias Sz = size_t; /// The result of sizeof, ...
|
||||
alias Str = char[]; /// A string slice of chars.
|
||||
alias IStr = const(char)[]; /// A string slice of constant chars.
|
||||
|
||||
enum Level : ubyte {
|
||||
none,
|
||||
info,
|
||||
warning,
|
||||
error,
|
||||
}
|
||||
|
||||
bool isX(IStr path) {
|
||||
import std.file;
|
||||
return path.exists;
|
||||
}
|
||||
|
||||
bool isF(IStr path) {
|
||||
import std.file;
|
||||
return path.exists;
|
||||
}
|
||||
|
||||
bool isD(IStr path) {
|
||||
import std.file;
|
||||
return path.isDir;
|
||||
}
|
||||
|
||||
void echo(A...)(A args) {
|
||||
import std.stdio;
|
||||
writeln(args);
|
||||
}
|
||||
|
||||
void echon(A...)(A args) {
|
||||
import std.stdio;
|
||||
write(args);
|
||||
}
|
||||
|
||||
void echof(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writefln(text, args);
|
||||
}
|
||||
|
||||
void echofn(A...)(IStr text, A args) {
|
||||
import std.stdio;
|
||||
writef(text, args);
|
||||
}
|
||||
|
||||
void cp(IStr source, IStr target) {
|
||||
import std.file;
|
||||
copy(source, target);
|
||||
}
|
||||
|
||||
void rm(IStr path) {
|
||||
import std.file;
|
||||
if (path.isX) remove(path);
|
||||
}
|
||||
|
||||
void mkdir(IStr path, bool isRecursive = false) {
|
||||
import std.file;
|
||||
if (!path.isX) {
|
||||
if (isRecursive) mkdirRecurse(path);
|
||||
else std.file.mkdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
void rmdir(IStr path, bool isRecursive = false) {
|
||||
import std.file;
|
||||
if (path.isX) {
|
||||
if (isRecursive) rmdirRecurse(path);
|
||||
else std.file.rmdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
IStr pwd() {
|
||||
import std.file;
|
||||
return getcwd();
|
||||
}
|
||||
|
||||
IStr cat(IStr path) {
|
||||
import std.file;
|
||||
return path.isX ? readText(path) : "";
|
||||
}
|
||||
|
||||
IStr[] ls(IStr path = ".", bool isRecursive = false) {
|
||||
import std.file;
|
||||
IStr[] result = [];
|
||||
foreach (dir; dirEntries(cast(string) path, isRecursive ? SpanMode.breadth : SpanMode.shallow)) {
|
||||
result ~= dir.name;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr basename(IStr path) {
|
||||
import std.path;
|
||||
return baseName(path);
|
||||
}
|
||||
|
||||
IStr realpath(IStr path) {
|
||||
import std.path;
|
||||
return absolutePath(cast(string) path);
|
||||
}
|
||||
|
||||
IStr read() {
|
||||
import std.stdio;
|
||||
return readln().trim();
|
||||
}
|
||||
|
||||
IStr readYesNo(IStr text, IStr firstValue = "?") {
|
||||
auto result = firstValue;
|
||||
while (true) {
|
||||
if (result.length == 0) result = "Y";
|
||||
if (result.isYesOrNo) break;
|
||||
echon(text, " [Y/n] ");
|
||||
result = read();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr fmt(A...)(IStr text, A args...) {
|
||||
import std.format;
|
||||
return format(text, args);
|
||||
}
|
||||
|
||||
IStr join(IStr[] args...) {
|
||||
import std.path;
|
||||
return buildPath(args);
|
||||
}
|
||||
|
||||
bool isYes(IStr arg) {
|
||||
return (arg.length == 1 && (arg[0] == 'Y' || arg[0] == 'y'));
|
||||
}
|
||||
|
||||
bool isNo(IStr arg) {
|
||||
return (arg.length == 1 && (arg[0] == 'N' || arg[0] == 'n'));
|
||||
}
|
||||
|
||||
bool isYesOrNo(IStr arg) {
|
||||
return arg.isYes || arg.isNo;
|
||||
}
|
||||
|
||||
bool startsWith(IStr str, IStr start) {
|
||||
if (str.length < start.length) return false;
|
||||
return str[0 .. start.length] == start;
|
||||
}
|
||||
|
||||
bool endsWith(IStr str, IStr end) {
|
||||
if (str.length < end.length) return false;
|
||||
return str[$ - end.length .. $] == end;
|
||||
}
|
||||
|
||||
int findStart(IStr str, IStr item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int findEnd(IStr str, IStr item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach_reverse (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
IStr trimStart(IStr str) {
|
||||
IStr result = str;
|
||||
while (result.length > 0) {
|
||||
auto isSpace = (result[0] >= '\t' && result[0] <= '\r') || (result[0] == ' ');
|
||||
if (isSpace) result = result[1 .. $];
|
||||
else break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr trimEnd(IStr str) {
|
||||
IStr result = str;
|
||||
while (result.length > 0) {
|
||||
auto isSpace = (result[$ - 1] >= '\t' && result[$ - 1] <= '\r') || (result[$ - 1] == ' ');
|
||||
if (isSpace) result = result[0 .. $ - 1];
|
||||
else break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
IStr trim(IStr str) {
|
||||
return str.trimStart().trimEnd();
|
||||
}
|
||||
|
||||
void clear(IStr path = ".", IStr ext = "") {
|
||||
foreach (file; ls(path)) {
|
||||
if (file.endsWith(ext)) rm(file);
|
||||
}
|
||||
}
|
||||
|
||||
void paste(IStr path, IStr content, bool isOnlyMaking = false) {
|
||||
import std.file;
|
||||
if (isOnlyMaking) {
|
||||
if (!path.isX) write(path, content);
|
||||
} else {
|
||||
write(path, content);
|
||||
}
|
||||
}
|
||||
|
||||
void clone(IStr path) {
|
||||
if (path.isX) cp(path, path ~ cloneExt);
|
||||
}
|
||||
|
||||
void restore(IStr path, bool isOnlyRemoving = false) {
|
||||
auto clonePath = path ~ cloneExt;
|
||||
if (clonePath.isX) {
|
||||
if (!isOnlyRemoving) paste(path, cat(clonePath));
|
||||
rm(clonePath);
|
||||
}
|
||||
}
|
||||
|
||||
void log(Level level, IStr text) {
|
||||
if (minLogLevel == 0 || minLogLevel > level) return;
|
||||
with (Level) final switch (level) {
|
||||
case info: echo("[INFO] ", text); break;
|
||||
case warning: echo("[WARNING] ", text); break;
|
||||
case error: echo("[ERROR] ", text); break;
|
||||
case none: break;
|
||||
}
|
||||
}
|
||||
|
||||
void logi(IStr text) {
|
||||
log(Level.info, text);
|
||||
}
|
||||
|
||||
void logw(IStr text) {
|
||||
log(Level.warning, text);
|
||||
}
|
||||
|
||||
void loge(IStr text) {
|
||||
log(Level.error, text);
|
||||
}
|
||||
|
||||
void logf(A...)(Level level, IStr text, A args) {
|
||||
log(level, text.fmt(args));
|
||||
}
|
||||
|
||||
int cmd(IStr[] args...) {
|
||||
import std.process;
|
||||
if (!isCmdLineHidden) echo("[CMD] ", args);
|
||||
try {
|
||||
return spawnProcess(args).wait();
|
||||
} catch (Exception e) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue