MinitScript  0.9.31 PRE-BETA
minitscriptbuild-main.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <memory>
3 #include <string>
4 
5 // Windows MSC: required for OpenSSL to work when having OpenSSL embedded in a DLL which is used here
6 #if defined(_MSC_VER)
7  #include <openssl/applink.c>
8 #endif
9 
19 
20 using std::exit;
21 using std::make_unique;
22 using std::string;
23 using std::unique_ptr;
24 
33 
34 int main(int argc, char** argv)
35 {
36  // TODO: --application, --library, --native-only
37  //
38  auto printInformation = [&]() -> void {
39  Console::printLine(string("minitscriptbuild ") + Version::getVersion());
40  Console::printLine(Version::getCopyright());
41  Console::printLine();
42  Console::printLine("Usage: minitscriptbuild [--help] [--application|--library] [--native-only] main-script.tscript [additional-script1.tscript] [additional-script2.tscript] [additional-scriptN.tscript]");
43  };
44 
45  // no arguments, no build :DDD
46  if (argc == 1) {
47  printInformation();
48  return EXIT_SUCCESS;
49  }
50 
51  //
52  vector<string> scriptURIs;
53 
54  //
55  for (auto i = 1; i < argc; i++) {
56  string argument = argv[i];
57  if (argument == "--help") {
58  printInformation();
59  return EXIT_SUCCESS;
60  }
61  }
62 
63  //
64  MinitScript::initialize();
65  // run script
66  auto context = make_unique<Context>();
67  auto script = make_unique<MinitScript>();
68  //
69  {
70  // create main argument values
71  vector<string> argumentValues;
72  argumentValues.push_back(argv[0]);
73  for (auto i = 1; i < argc; i++) {
74  string argumentValue(argv[i]);
75  //
76  if (argumentValue == "--help") continue;
77  if (argumentValue == "--application") {
78  argumentValues.push_back(argumentValue);
79  continue;
80  }
81  if (argumentValue == "--library") {
82  argumentValues.push_back(argumentValue);
83  continue;
84  }
85  if (argumentValue == "--native-only") {
86  argumentValues.push_back(argumentValue);
87  continue;
88  }
89  /* we like / more than */
90  argumentValue = StringTools::replace(argumentValue, "\\", "/");
91  // we do not want to have .. or . in path names
92  argumentValue = FileSystem::getCanonicalURI(FileSystem::getPathName(argumentValue), FileSystem::getFileName(argumentValue));
93  //
94  if (FileSystem::exists(argumentValue) == false) {
95  _Console::printLine("Script URI invalid: File not found: " + argumentValue);
96  return EXIT_FAILURE;
97  }
98  //
99  argumentValues.push_back(argumentValue);
100  }
101  //
102  context->setArgumentValues(argumentValues);
103  }
104  auto pathToScript = MINITSCRIPT_DATA + "/resources/minitscript/scripts/build.tscript";
105  script = make_unique<MinitScript>();
106  script->setContext(context.get());
107  script->parseScript(
108  FileSystem::getPathName(pathToScript),
109  FileSystem::getFileName(pathToScript)
110  );
111  //
112  if (script->isValid() == false) {
113  Console::printLine(pathToScript + ": Build script not valid. Exiting!");
114  return EXIT_SUCCESS;
115  } else {
116  // TODO: we need a MinitScript startup routine
117  Network::initialize();
118  // Windows MSC: required for OpenSSL to work when having OpenSSL embedded in a DLL which is used here
119  #if defined(_MSC_VER)
120  OPENSSL_Applink();
121  #endif
122  // add script to context
123  auto scriptPtr = script.get();
124  context->push(scriptPtr);
125  context->addScript("application", script.release());
126  //
127  while (scriptPtr->isRunning() == true) {
128  scriptPtr->execute();
129  }
130  context->pop();
131  }
132 
133  //
134  return context->getExitCode();
135 }
static void printLine()
Print newline to console.
Definition: Console.cpp:66
#define MINITSCRIPT_DATA
Definition: minitscript.h:22
int main(int argc, char **argv)
std::exception Exception
Exception base class.
Definition: Exception.h:18