Изменение имени проекта
This commit is contained in:
parent
7714bc498e
commit
cc1a81261a
16 changed files with 70 additions and 70 deletions
107
source/snag/config/config.d
Normal file
107
source/snag/config/config.d
Normal file
|
@ -0,0 +1,107 @@
|
|||
module snag.config.config;
|
||||
|
||||
import std.json;
|
||||
import std.file;
|
||||
import std.path;
|
||||
import std.regex;
|
||||
import std.string;
|
||||
|
||||
import snag.config.exception;
|
||||
|
||||
class SnagConfig {
|
||||
private string _git;
|
||||
private string _project;
|
||||
private string _email;
|
||||
private string _user;
|
||||
|
||||
private bool isValidEmail(string email) {
|
||||
auto emailPattern = ctRegex!r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
|
||||
return !matchFirst(email, emailPattern).empty;
|
||||
}
|
||||
|
||||
this(string configFile) {
|
||||
string jsonText;
|
||||
JSONValue jsonData;
|
||||
|
||||
try {
|
||||
jsonText = readText(configFile);
|
||||
jsonData = parseJSON(jsonText);
|
||||
} catch (Exception e) {
|
||||
throw new SnagConfigException(
|
||||
"An error occurred while reading the configuration file:\n\t"
|
||||
~ e.msg
|
||||
);
|
||||
}
|
||||
|
||||
if ("git" !in jsonData)
|
||||
throw new SnagConfigException(
|
||||
"The configuration file is missing the \"git\" parameter"
|
||||
);
|
||||
|
||||
_git = jsonData["git"].str;
|
||||
|
||||
if (!_git.length)
|
||||
throw new SnagConfigException(
|
||||
"The \"git\" parameter must contain the path to the directory"
|
||||
);
|
||||
|
||||
if (!_git.isAbsolute)
|
||||
throw new SnagConfigException(
|
||||
"The \"git\" parameter must be an absolute path to the directory:\n\t"
|
||||
~ _git
|
||||
);
|
||||
|
||||
if ("project" !in jsonData)
|
||||
throw new SnagConfigException(
|
||||
"The configuration file is missing the \"project\" parameter"
|
||||
);
|
||||
|
||||
_project = jsonData["project"].str;
|
||||
|
||||
if (!_project.length)
|
||||
throw new SnagConfigException(
|
||||
"The \"project\" parameter must contain the path to the directory"
|
||||
);
|
||||
|
||||
if (!_project.isAbsolute)
|
||||
throw new SnagConfigException(
|
||||
"The \"project\" parameter must be an absolute path to the directory:\n\t"
|
||||
~ _project
|
||||
);
|
||||
|
||||
if ("email" !in jsonData)
|
||||
throw new SnagConfigException(
|
||||
"The configuration file is missing the \"email\" parameter"
|
||||
);
|
||||
|
||||
_email = jsonData["email"].str;
|
||||
|
||||
if (!_email.length)
|
||||
throw new SnagConfigException(
|
||||
"The \"email\" parameter must contain an email address"
|
||||
);
|
||||
|
||||
if (!isValidEmail(_email))
|
||||
throw new SnagConfigException(
|
||||
"Invalid email address provided in the \"email\" parameter:\n\t"
|
||||
~ _email
|
||||
);
|
||||
|
||||
if ("user" !in jsonData)
|
||||
throw new SnagConfigException(
|
||||
"The configuration file is missing the \"user\" parameter"
|
||||
);
|
||||
|
||||
_user = jsonData["user"].str;
|
||||
|
||||
if (!_user.length)
|
||||
throw new SnagConfigException(
|
||||
"The \"user\" parameter must contain an user name"
|
||||
);
|
||||
}
|
||||
|
||||
@property string git() const { return _git; }
|
||||
@property string project() const { return _project; }
|
||||
@property string email() const { return _email; }
|
||||
@property string user() const { return _user; }
|
||||
}
|
14
source/snag/config/exception.d
Normal file
14
source/snag/config/exception.d
Normal file
|
@ -0,0 +1,14 @@
|
|||
module snag.config.exception;
|
||||
|
||||
import std.exception;
|
||||
import std.stdio : writeln;
|
||||
|
||||
class SnagConfigException : Exception {
|
||||
this(string msg, string file = __FILE__, size_t line = __LINE__) {
|
||||
super(msg, file, line);
|
||||
}
|
||||
|
||||
void print() {
|
||||
writeln(msg);
|
||||
}
|
||||
}
|
4
source/snag/config/package.d
Normal file
4
source/snag/config/package.d
Normal file
|
@ -0,0 +1,4 @@
|
|||
module snag.config;
|
||||
|
||||
public import snag.config.exception;
|
||||
public import snag.config.config;
|
109
source/snag/core/core.d
Normal file
109
source/snag/core/core.d
Normal file
|
@ -0,0 +1,109 @@
|
|||
module snag.core.core;
|
||||
|
||||
import snag.config;
|
||||
import std.format;
|
||||
import std.stdio;
|
||||
import std.array;
|
||||
import std.exception;
|
||||
import std.process;
|
||||
import std.algorithm;
|
||||
import std.string;
|
||||
|
||||
class SnagException : Exception {
|
||||
this(string msg, string file = __FILE__, size_t line = __LINE__) {
|
||||
super(msg, file, line);
|
||||
}
|
||||
|
||||
void print() {
|
||||
writeln(msg);
|
||||
}
|
||||
}
|
||||
|
||||
class Snag {
|
||||
private string[] _baseCommand;
|
||||
private SnagConfig _config;
|
||||
|
||||
this(SnagConfig config) {
|
||||
_baseCommand = format(
|
||||
"git --git-dir=%s --work-tree=%s",
|
||||
config.git, config.project
|
||||
).split();
|
||||
_config = config;
|
||||
}
|
||||
|
||||
void initialize() {
|
||||
auto result = execute(_baseCommand ~ "init");
|
||||
if (result.status)
|
||||
throw new SnagException(
|
||||
"A Git repository initialization error occurred:\n"
|
||||
~ result.output
|
||||
);
|
||||
|
||||
result = execute(
|
||||
_baseCommand ~ ["config", "user.email", _config.email]
|
||||
);
|
||||
if (result.status)
|
||||
throw new SnagException(
|
||||
"A Git repository initialization error occurred:\n"
|
||||
~ result.output
|
||||
);
|
||||
|
||||
result = execute(
|
||||
_baseCommand ~ ["config", "user.name", _config.user]
|
||||
);
|
||||
if (result.status)
|
||||
throw new SnagException(
|
||||
"A Git repository initialization error occurred:\n"
|
||||
~ result.output
|
||||
);
|
||||
|
||||
writeln(
|
||||
"The Git repository has been initialized successfully: "
|
||||
~ _config.git
|
||||
);
|
||||
}
|
||||
|
||||
void status() {
|
||||
auto result = execute(
|
||||
_baseCommand ~ ["status", "--porcelain"]
|
||||
);
|
||||
if (result.status)
|
||||
throw new SnagException(
|
||||
"An error occurred while checking the file tracking status:\n"
|
||||
~ result.output
|
||||
);
|
||||
|
||||
writeln("The following list of files requires backup:");
|
||||
|
||||
/**
|
||||
Цепочка выполняет разбивку по переводу на новую строку,
|
||||
отсеивает пустые строки, перебирает в цикле имеющиеся строки,
|
||||
выводит только вторую часть строки (разделенную по пробелу)
|
||||
*/
|
||||
result.output.split('\n').filter!(e => !e.strip.empty).each!((e) {
|
||||
writefln("\t/%s", e.strip.split[1]);
|
||||
});
|
||||
}
|
||||
|
||||
void create() {
|
||||
auto result = execute(
|
||||
_baseCommand ~ ["add", "."]
|
||||
);
|
||||
if (result.status)
|
||||
throw new SnagException(
|
||||
"Failed to prepare files for archiving:\n"
|
||||
~ result.output
|
||||
);
|
||||
|
||||
result = execute(
|
||||
_baseCommand ~ ["commit", "-m", "test"]
|
||||
);
|
||||
if (result.status)
|
||||
throw new SnagException(
|
||||
"Failed to create a backup:\n"
|
||||
~ result.output
|
||||
);
|
||||
|
||||
writeln("Backup was created successfully");
|
||||
}
|
||||
}
|
3
source/snag/core/package.d
Normal file
3
source/snag/core/package.d
Normal file
|
@ -0,0 +1,3 @@
|
|||
module snag.core;
|
||||
|
||||
public import snag.core.core;
|
5
source/snag/package.d
Normal file
5
source/snag/package.d
Normal file
|
@ -0,0 +1,5 @@
|
|||
module snag;
|
||||
|
||||
public import snag.version_;
|
||||
public import snag.config;
|
||||
public import snag.core;
|
3
source/snag/version_.d
Normal file
3
source/snag/version_.d
Normal file
|
@ -0,0 +1,3 @@
|
|||
module snag.version_;
|
||||
|
||||
enum snagVersion = "0.0.4";
|
Loading…
Add table
Add a link
Reference in a new issue