diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-31 09:13:32 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2017-12-31 09:29:38 +0100 |
commit | eda6a8e2c66380c773db32f720ef4b6a89f9b50a (patch) | |
tree | 6d8f5f9a6dd2d1923864f94d0df2db85d041d2d4 | |
parent | c849a30e7e9e2e7608376c9c538e1b45e623c805 (diff) |
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
-rw-r--r-- | include/Conf.hpp | 3 | ||||
-rw-r--r-- | src/Conf.cpp | 24 | ||||
-rw-r--r-- | src/main.cpp | 14 |
3 files changed, 39 insertions, 2 deletions
diff --git a/include/Conf.hpp b/include/Conf.hpp index d8ce3a8..f5fb382 100644 --- a/include/Conf.hpp +++ b/include/Conf.hpp @@ -141,6 +141,7 @@ namespace sibs const char* asString(Platform platform); const char* asString(OptimizationLevel optLevel); bool directoryToIgnore(const FileString &dir, const std::vector<std::string> &ignoreDirList); + bool isProjectNameValid(const std::string &projectName); class SibsConfig : public ConfigCallback { @@ -227,6 +228,8 @@ namespace sibs virtual void processObject(StringView name) override; virtual void processField(StringView name, const ConfigValue &value) override; virtual void finished() override; + private: + void validatePackageName() const; protected: StringView currentObject; Compiler compiler; 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; |