From eda6a8e2c66380c773db32f720ef4b6a89f9b50a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 31 Dec 2017 09:13:32 +0100 Subject: Update new project template with user platform Validate package name only contains safe characters. TODO: Validate package name is allowed on windows, names such as NUL, AUX are not allowed --- src/Conf.cpp | 24 ++++++++++++++++++++++++ src/main.cpp | 14 ++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'src') 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 \n\nint main()\n{\n return 0;\n}\n"); + createProjectFile(projectPath + TINYDIR_STRING("/src/main.cpp"), "#include \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; -- cgit v1.2.3