diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Conf.cpp | 24 | ||||
-rw-r--r-- | src/main.cpp | 14 |
2 files changed, 36 insertions, 2 deletions
diff --git a/src/Conf.cpp b/src/Conf.cpp index 02e92fe..c487e28 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -394,6 +394,17 @@ namespace sibs return false; } + bool isProjectNameValid(const string &projectName) + { + for(int i = 0; i < projectName.size(); ++i) + { + char c = projectName[i]; + if(!isalpha(c) && !isdigit(c) && c != '-' && c != '_') + return false; + } + return true; + } + bool SibsConfig::isDefined(const std::string &name) const { return defines.find(name) != defines.end(); @@ -480,6 +491,8 @@ namespace sibs packageName = string(value.asSingle().data, value.asSingle().size); else throw ParserException("Expected package.name to be a single value, was a list"); + + validatePackageName(); } else if(name.equals("type")) { @@ -710,6 +723,17 @@ namespace sibs if(platforms.empty()) throw ParserException("Missing required config package.platforms. If the package supports all platforms, add:\nplatforms = [\"any\"]\nto project.conf under [package]"); } + + void SibsConfig::validatePackageName() const + { + if(!isProjectNameValid(packageName)) + { + string errMsg = "Invalid package name: "; + errMsg += packageName; + errMsg += ". Package name can only contain alphanumerical characters, dash (-) or underscore (_)"; + throw ParserException(errMsg); + } + } void SibsTestConfig::processObject(StringView name) { diff --git a/src/main.cpp b/src/main.cpp index 88a837f..fc413d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -314,7 +314,11 @@ void newProjectCreateConf(const string &projectName, const string &projectType, string projectConfStr = "[package]\n"; projectConfStr += "name = \"" + projectName + "\"\n"; projectConfStr += "type = \"" + projectType + "\"\n"; - projectConfStr += "version = \"0.1.0\"\n\n"; + projectConfStr += "version = \"0.1.0\"\n"; + projectConfStr += "platforms = [\""; + projectConfStr += asString(SYSTEM_PLATFORM); + projectConfStr += "\"]\n"; + projectConfStr += "\n"; projectConfStr += "[dependencies]\n"; FileString projectConfPath = projectPath; @@ -353,6 +357,12 @@ int newProject(int argc, const _tinydir_char_t **argv) } string projectName = toUtf8(argv[0]); + if(!isProjectNameValid(projectName)) + { + ferr << "Project name can only contain alphanumerical characters, dash (-) or underscore (_)" << endl; + exit(20); + } + FileString projectPath = cwdResult.unwrap(); projectPath += TINYDIR_STRING("/"); projectPath += toFileString(projectName); @@ -382,7 +392,7 @@ int newProject(int argc, const _tinydir_char_t **argv) newProjectCreateConf(projectName, projectTypeConf, projectPath); createProjectSubDir(projectPath + TINYDIR_STRING("/src")); createProjectSubDir(projectPath + TINYDIR_STRING("/include")); - createProjectFile(projectPath + TINYDIR_STRING("/src/main.cpp"), "#include <cstdio>\n\nint main()\n{\n return 0;\n}\n"); + createProjectFile(projectPath + TINYDIR_STRING("/src/main.cpp"), "#include <cstdio>\n\nint main()\n{\n printf(\"hello, world!\\n\");\n return 0;\n}\n"); // We are ignoring git init result on purpose. If it fails, just ignore it; not important gitInitProject(projectPath); return 0; |