TDME2  1.9.200
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
makefilegenerator-main.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <vector>
3 
4 #include <tdme/tdme.h>
6 #include <tdme/engine/Version.h>
10 #include <tdme/utilities/Console.h>
13 
14 using std::string;
15 using std::to_string;
16 using std::vector;
17 
26 
27 void scanDir(const string& folder, vector<string>& sourceFiles, vector<string>& mainSourceFiles) {
28  class SourceFilesFilter : public virtual FileNameFilter {
29  public:
30  virtual ~SourceFilesFilter() {}
31 
32  bool accept(const string& pathName, const string& fileName) override {
33  if (fileName == ".") return false;
34  if (fileName == "..") return false;
35  if (FileSystem::getInstance()->isPath(pathName + "/" + fileName) == true) return true;
36  // skip on CPP files that gets #include ed
37  if (StringTools::endsWith(StringTools::toLowerCase(fileName), ".incl.cpp") == true) return false;
38  if (StringTools::endsWith(StringTools::toLowerCase(fileName), ".include.cpp") == true) return false;
39  // CPP hit, yes
40  if (StringTools::endsWith(StringTools::toLowerCase(fileName), ".cpp") == true) return true;
41  return false;
42  }
43  };
44 
45  SourceFilesFilter sourceFilesFilter;
46  vector<string> files;
47 
48  FileSystem::getInstance()->list(folder, files, &sourceFilesFilter);
49 
50  for (const auto& fileName: files) {
51  if (StringTools::endsWith(fileName, "-main.cpp") == true) {
52  mainSourceFiles.push_back(folder + "/" + fileName);
53  } else
54  if (StringTools::endsWith(fileName, ".cpp") == true) {
55  sourceFiles.push_back(folder + "/" + fileName);
56  } else {
57  scanDir(folder + "/" + fileName, sourceFiles, mainSourceFiles);
58  }
59  }
60 }
61 
62 int main(int argc, char** argv)
63 {
64  Console::println(string("makefilegenerator ") + Version::getVersion());
65  Console::println(Version::getCopyright());
66  Console::println();
67 
68  //
69  if (argc != 2) {
70  Console::println("Usage: makefilegenerator path_to_source");
71  Application::exit(Application::EXITCODE_FAILURE);
72  }
73 
74  //
75  auto pathToSource = string(argv[1]);
76 
77  //
78  try {
79  Console::println("Scanning source files");
80  vector<string> sourceFiles;
81  vector<string> mainSourceFiles;
82  scanDir(pathToSource, sourceFiles, mainSourceFiles);
83 
84  string sourceFilesVariable = "\\\n";
85  for (const auto& file: sourceFiles) sourceFilesVariable+= "\t" + file + "\\\n";
86  sourceFilesVariable+= "\n";
87 
88  string mainSourceFilesVariable = "\\\n";
89  for (const auto& file: mainSourceFiles) mainSourceFilesVariable+= "\t" + file + "\\\n";
90  mainSourceFilesVariable+= "\n";
91 
92  Console::println("Generating Makefile");
93 
94  auto executableFolder = StringTools::replace(argv[0], "\\", "/");
95  auto tdme2Folder = StringTools::substring(executableFolder, 0, StringTools::toLowerCase(executableFolder).rfind("/tdme2/") + string("/tdme2/").length());
96 
97  auto makefileSource = FileSystem::getInstance()->getContentAsString(tdme2Folder + "/resources/engine/templates/makefiles", "Makefile");
98  makefileSource = StringTools::replace(makefileSource, "{$source-files}", sourceFilesVariable);
99  makefileSource = StringTools::replace(makefileSource, "{$main-source-files}", mainSourceFilesVariable);
100  FileSystem::getInstance()->setContentFromString(".", "Makefile", makefileSource);
101  } catch (Exception& exception) {
102  Console::println("An error occurred: " + string(exception.what()));
103  }
104 
105  //
106  Application::exit(Application::EXITCODE_SUCCESS);
107 }
Application base class, please make sure to allocate application on heap to have correct application ...
Definition: Application.h:41
File system singleton class.
Definition: FileSystem.h:17
Console class.
Definition: Console.h:29
String tools class.
Definition: StringTools.h:22
int main(int argc, char **argv)
void scanDir(const string &folder, vector< string > &sourceFiles, vector< string > &mainSourceFiles)
std::exception Exception
Exception base class.
Definition: Exception.h:18
File system file name filter interface.
virtual bool accept(const string &path, const string &file)=0
Accept a file.