From 0415b7bef504b41b43672b3e153bbe260f2cc055 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 20 Sep 2018 21:54:06 +0200 Subject: Add c and zig template to sibs build, sibs init --- src/main.cpp | 222 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 181 insertions(+), 41 deletions(-) (limited to 'src') 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 <--exec|--static|--dynamic>\n\n"); + printf("Usage: sibs new <--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 fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include \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 fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include \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 fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include \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 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 fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include \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 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 fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include \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 fileOverwriteResult = fileWrite(mainFilePath.c_str(), "#include \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 fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include \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 fileOverwriteResult = fileWrite(testFilePath.c_str(), "#include \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 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); -- cgit v1.2.3