aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-09-20 21:54:06 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-06 07:39:33 +0200
commit0415b7bef504b41b43672b3e153bbe260f2cc055 (patch)
treeeb2e5657c0b9e4319885dce4bb09ee4c9ac0a110 /src
parent8e8dda19284c2e3767aa7b97fb29edad56344aae (diff)
Add c and zig template to sibs build, sibs init
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp222
1 files changed, 181 insertions, 41 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 74349cb..d8ce41c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -139,13 +139,14 @@ void usageBuild()
void usageNew()
{
- printf("Usage: sibs new <project_name> <--exec|--static|--dynamic>\n\n");
+ printf("Usage: sibs new <project_name> <--exec|--static|--dynamic> <--lang [c|c++|zig]>\n\n");
printf("Create a new sibs project\n\n");
printf("Options:\n");
printf(" project_name\t\tThe name of the project you want to create\n");
printf(" --exec\t\t\tProject compiles to an executable\n");
printf(" --static\t\t\tProject compiles to a static library\n");
printf(" --dynamic\t\t\tProject compiles to a dynamic library\n");
+ printf(" --lang\t\t\tProject template language - Optional (default: c++)\n");
printf("Examples:\n");
printf(" sibs new hello_world --exec\n");
exit(1);
@@ -166,13 +167,14 @@ void usageTest()
void usageInit()
{
- printf("Usage: sibs init [project_path] <--exec|--static|--dynamic>\n\n");
+ printf("Usage: sibs init [project_path] <--exec|--static|--dynamic> <--lang [c|c++|zig]>\n\n");
printf("Create sibs project structure in an existing directory\n\n");
printf("Options:\n");
printf(" project_path\t\tThe directory where you want to initialize sibs project - Optional (default: current directory)\n");
printf(" --exec\t\t\tProject compiles to an executable\n");
printf(" --static\t\t\tProject compiles to a static library\n");
printf(" --dynamic\t\t\tProject compiles to a dynamic library\n");
+ printf(" --lang\t\t\tProject template language - Optional (default: c++)\n");
printf("Examples:\n");
printf(" sibs init --exec\n");
printf(" sibs init dirA/dirB --dynamic");
@@ -534,11 +536,9 @@ void gitIgnoreAppendSibs(const FileString &gitIgnoreFilePath)
int initProject(int argc, const _tinydir_char_t **argv)
{
- if(argc > 2)
- usageInit();
-
FileString projectPath;
const _tinydir_char_t *projectType = nullptr;
+ const _tinydir_char_t *lang = nullptr;
for(int i = 0; i < argc; ++i)
{
@@ -570,6 +570,30 @@ int initProject(int argc, const _tinydir_char_t **argv)
}
projectType = arg;
}
+ else if(_tinydir_strcmp(arg, TINYDIR_STRING("--lang")) == 0)
+ {
+ if(i == argc - 1)
+ {
+ ferr << "Error: Expected language argument after --lang" << endl;
+ usageInit();
+ }
+
+ ++i;
+ arg = argv[i];
+
+ if(lang)
+ {
+ ferr << "Error: Project language was defined more than once. First as " << lang << " then as " << arg << endl;
+ usageInit();
+ }
+ lang = arg;
+
+ if(_tinydir_strcmp(lang, TINYDIR_STRING("c")) != 0 && _tinydir_strcmp(lang, TINYDIR_STRING("c++")) != 0 && _tinydir_strcmp(lang, TINYDIR_STRING("zig")) != 0)
+ {
+ ferr << "Expected project language to be either c, c++ or zig; was: " << lang << endl << endl;
+ usageInit();
+ }
+ }
else if(_tinydir_strncmp(arg, TINYDIR_STRING("--"), 2) == 0)
{
ferr << "Error: Invalid argument " << arg << endl;
@@ -604,10 +628,16 @@ int initProject(int argc, const _tinydir_char_t **argv)
ferr << "Expected project type to be either --exec, --static or --dynamic; was: " << projectType << endl << endl;
usageInit();
}
+
+ if(!lang)
+ lang = TINYDIR_STRING("c++");
// TODO: If projectPath is not defined and working directory does not contain project.conf, then search every parent directory until one is found
if(projectPath.empty())
- projectPath = TINYDIR_STRING(".");
+ {
+ ferr << "Error: Project path not defined" << endl;
+ usageInit();
+ }
validateDirectoryPath(projectPath.c_str());
if(projectPath.back() != '/')
@@ -663,19 +693,37 @@ int initProject(int argc, const _tinydir_char_t **argv)
exit(20);
}
createDirectoryRecursive(projectPath + TINYDIR_STRING("/src"));
- createDirectoryRecursive(projectPath + TINYDIR_STRING("/include"));
createDirectoryRecursive(projectPath + TINYDIR_STRING("/tests"));
- if(projectTypeConf == "executable")
+ if(_tinydir_strcmp(lang, TINYDIR_STRING("c")) == 0 || _tinydir_strcmp(lang, TINYDIR_STRING("c++")) == 0)
+ {
+ createDirectoryRecursive(projectPath + TINYDIR_STRING("/include"));
+
+ FileString mainFileName;
+ if(_tinydir_strcmp(lang, TINYDIR_STRING("c")) == 0)
+ mainFileName = TINYDIR_STRING("main.c");
+ else
+ mainFileName = TINYDIR_STRING("main.cpp");
+
+ if(projectTypeConf == "executable")
+ {
+ auto mainFilePath = projectPath + TINYDIR_STRING("/src/") + mainFileName;
+ Result<bool> fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
+ if(!fileOverwriteResult)
+ fout << "Warning: Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
+ }
+
+ auto testFilePath = projectPath + TINYDIR_STRING("/tests/") + mainFileName;
+ Result<bool> fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
+ if(!fileOverwriteResult)
+ fout << "Warning: Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
+ }
+ else if(_tinydir_strcmp(lang, TINYDIR_STRING("zig")) == 0 && projectTypeConf == "executable")
{
- auto mainFilePath = projectPath + TINYDIR_STRING("/src/main.cpp");
- Result<bool> fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include <cstdio>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
+ auto mainFilePath = projectPath + TINYDIR_STRING("/src/main.zig");
+ Result<bool> fileOverwriteResult = fileWrite(mainFilePath.c_str(), "const warn = @import(\"std\").debug.warn;\n\npub fn main() void\n{\n warn(\"Hello, world!\\n\");\n}\n");
if(!fileOverwriteResult)
fout << "Warning: Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
}
- auto testFilePath = projectPath + TINYDIR_STRING("/tests/main.cpp");
- Result<bool> fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include <cstdio>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
- if(!fileOverwriteResult)
- fout << "Warning: Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
auto gitProjDir = projectPath + TINYDIR_STRING("/.git");
if(getFileType(gitProjDir.c_str()) == FileType::FILE_NOT_FOUND)
gitInitProject(projectPath);
@@ -707,9 +755,96 @@ void checkFailCreateSubDir(Result<bool> createSubDirResult)
int newProject(int argc, const _tinydir_char_t **argv)
{
- if(argc != 2)
+ string projectName;
+ const _tinydir_char_t *projectType = nullptr;
+ const _tinydir_char_t *lang = nullptr;
+
+ for(int i = 0; i < argc; ++i)
{
- ferr << "Expected 'new' command to be followed by two arguments - project name and type of project (--exec, --static or --dynamic)" << endl << endl;
+ const _tinydir_char_t *arg = argv[i];
+ if(_tinydir_strcmp(arg, TINYDIR_STRING("--exec")) == 0)
+ {
+ if(projectType)
+ {
+ ferr << "Error: Project type was defined more than once. First as " << projectType << " then as " << arg << endl;
+ usageNew();
+ }
+ projectType = arg;
+ }
+ else if(_tinydir_strcmp(arg, TINYDIR_STRING("--static")) == 0)
+ {
+ if(projectType)
+ {
+ ferr << "Error: Project type was defined more than once. First as " << projectType << " then as " << arg << endl;
+ usageNew();
+ }
+ projectType = arg;
+ }
+ else if(_tinydir_strcmp(arg, TINYDIR_STRING("--dynamic")) == 0)
+ {
+ if(projectType)
+ {
+ ferr << "Error: Project type was defined more than once. First as " << projectType << " then as " << arg << endl;
+ usageNew();
+ }
+ projectType = arg;
+ }
+ else if(_tinydir_strcmp(arg, TINYDIR_STRING("--lang")) == 0)
+ {
+ if(i == argc - 1)
+ {
+ ferr << "Error: Expected language argument after --lang" << endl;
+ usageNew();
+ }
+
+ ++i;
+ arg = argv[i];
+
+ if(lang)
+ {
+ ferr << "Error: Project language was defined more than once. First as " << lang << " then as " << arg << endl;
+ usageNew();
+ }
+ lang = arg;
+
+ if(_tinydir_strcmp(lang, TINYDIR_STRING("c")) != 0 && _tinydir_strcmp(lang, TINYDIR_STRING("c++")) != 0 && _tinydir_strcmp(lang, TINYDIR_STRING("zig")) != 0)
+ {
+ ferr << "Expected project language to be either c, c++ or zig; was: " << lang << endl << endl;
+ usageNew();
+ }
+ }
+ else if(_tinydir_strncmp(arg, TINYDIR_STRING("--"), 2) == 0)
+ {
+ ferr << "Error: Invalid argument " << arg << endl;
+ usageNew();
+ }
+ else
+ {
+ if(!projectName.empty())
+ {
+ ferr << "Error: Project name was defined more than once. First defined as " << projectName << " then as " << arg << endl;
+ usageNew();
+ }
+ projectName = toUtf8(arg);
+ }
+ }
+
+ if(!projectType)
+ {
+ ferr << "Error: Project type not defined, expected to be either --exec, --static or --dynamic" << endl;
+ usageNew();
+ }
+
+ string projectTypeConf;
+ if(_tinydir_strcmp(projectType, TINYDIR_STRING("--exec")) == 0)
+ projectTypeConf = "executable";
+ else if(_tinydir_strcmp(projectType, TINYDIR_STRING("--static")) == 0)
+ projectTypeConf = "static";
+ else if(_tinydir_strcmp(projectType, TINYDIR_STRING("--dynamic")) == 0)
+ projectTypeConf = "dynamic";
+ else
+ {
+ ferr << "Expected project type to be either --exec, --static or --dynamic; was: " << projectType << endl << endl;
usageNew();
}
@@ -720,7 +855,6 @@ int newProject(int argc, const _tinydir_char_t **argv)
exit(20);
}
- string projectName = toUtf8(argv[0]);
if(!isProjectNameValid(projectName))
{
ferr << "Project name can only contain alphanumerical characters, dash (-) or underscore (_)" << endl;
@@ -736,21 +870,6 @@ int newProject(int argc, const _tinydir_char_t **argv)
ferr << "Unable to create a new project at path '" << projectPath << "'. A file or directory already exists in the same location" << endl;
exit(20);
}
-
- const _tinydir_char_t *projectType = argv[1];
-
- string projectTypeConf;
- if(_tinydir_strcmp(projectType, TINYDIR_STRING("--exec")) == 0)
- projectTypeConf = "executable";
- else if(_tinydir_strcmp(projectType, TINYDIR_STRING("--static")) == 0)
- projectTypeConf = "static";
- else if(_tinydir_strcmp(projectType, TINYDIR_STRING("--dynamic")) == 0)
- projectTypeConf = "dynamic";
- else
- {
- ferr << "Expected project type to be either --exec, --static or --dynamic; was: " << projectType << endl << endl;
- usageNew();
- }
newProjectCreateMainDir(projectPath);
auto createProjectConfResult = newProjectCreateConf(projectName, projectTypeConf, projectPath);
@@ -760,24 +879,45 @@ int newProject(int argc, const _tinydir_char_t **argv)
exit(20);
}
createDirectoryRecursive(projectPath + TINYDIR_STRING("/src"));
- createDirectoryRecursive(projectPath + TINYDIR_STRING("/include"));
createDirectoryRecursive(projectPath + TINYDIR_STRING("/tests"));
- if(projectTypeConf == "executable")
+ if(_tinydir_strcmp(lang, TINYDIR_STRING("c")) == 0 || _tinydir_strcmp(lang, TINYDIR_STRING("c++")) == 0)
{
- auto mainFilePath = projectPath + TINYDIR_STRING("/src/main.cpp");
- Result<bool> fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include <cstdio>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
+ createDirectoryRecursive(projectPath + TINYDIR_STRING("/include"));
+
+ FileString mainFileName;
+ if(_tinydir_strcmp(lang, TINYDIR_STRING("c")) == 0)
+ mainFileName = TINYDIR_STRING("main.c");
+ else
+ mainFileName = TINYDIR_STRING("main.cpp");
+
+ if(projectTypeConf == "executable")
+ {
+ auto mainFilePath = projectPath + TINYDIR_STRING("/src/") + mainFileName;
+ Result<bool> fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
+ if(!fileOverwriteResult)
+ {
+ ferr << "Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
+ exit(20);
+ }
+ }
+
+ auto testFilePath = projectPath + TINYDIR_STRING("/tests/") + mainFileName;
+ Result<bool> fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
if(!fileOverwriteResult)
{
ferr << "Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
exit(20);
}
}
- auto testFilePath = projectPath + TINYDIR_STRING("/tests/main.cpp");
- Result<bool> fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include <cstdio>\n\nint main(int argc, char **argv)\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n");
- if(!fileOverwriteResult)
+ else if(_tinydir_strcmp(lang, TINYDIR_STRING("zig")) == 0 && projectTypeConf == "executable")
{
- ferr << "Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
- exit(20);
+ auto mainFilePath = projectPath + TINYDIR_STRING("/src/main.zig");
+ Result<bool> fileOverwriteResult = fileWrite(mainFilePath.c_str(), "const warn = @import(\"std\").debug.warn;\n\npub fn main() void\n{\n warn(\"Hello, world!\\n\");\n}\n");
+ if(!fileOverwriteResult)
+ {
+ ferr << " Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl;
+ exit(20);
+ }
}
// We are ignoring git init result on purpose. If it fails, just ignore it; not important
gitInitProject(projectPath);