Skip to content

Commit af30f05

Browse files
Adding tutorial
1 parent f7bdeb1 commit af30f05

File tree

5 files changed

+189
-24
lines changed

5 files changed

+189
-24
lines changed

Examples/InteractiveTutorial.cpp

Lines changed: 111 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Defines: ["ssLOG_USE_SOURCE=1"]
88
URL: "https://github.com/Neko-Box-Coder/ssLogger.git"
99
LibraryType: Header
1010
IncludePaths: ["Include"]
11+
Defines:
12+
- "_CRT_SECURE_NO_WARNINGS=1"
1113
1214
- Name: "System2.cpp"
1315
Platforms: [DefaultPlatform]
@@ -35,12 +37,28 @@ Defines: ["ssLOG_USE_SOURCE=1"]
3537
#include <thread>
3638
#include <chrono>
3739

38-
#define DELAYED_OUTPUT(str) \
39-
do { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); ssLOG_BASE(str); } while(0)
40+
std::string runcpp2ExecutablePath = "";
41+
bool integrationTest = false;
4042

43+
#define DELAYED_OUTPUT(str) \
44+
do \
45+
{ \
46+
if(!integrationTest) \
47+
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); \
48+
ssLOG_BASE(str); \
49+
} while(0)
4150

4251
std::string GetInput(bool allowEmpty = false)
4352
{
53+
if(integrationTest)
54+
{
55+
if(!allowEmpty)
56+
ssLOG_ERROR("GetInput() called with allowEmpty set to false for testing");
57+
58+
DELAYED_OUTPUT("");
59+
return "";
60+
}
61+
4462
std::string input;
4563
bool triggered = false;
4664
do
@@ -62,6 +80,12 @@ std::string GetInput(bool allowEmpty = false)
6280

6381
bool GetYN_WithDefault(bool defaultY)
6482
{
83+
if(integrationTest)
84+
{
85+
DELAYED_OUTPUT("y");
86+
return defaultY;
87+
}
88+
6589
while(true)
6690
{
6791
std::string input = GetInput(true);
@@ -90,7 +114,6 @@ bool GetYN()
90114
}
91115
}
92116

93-
std::string runcpp2ExecutablePath = "";
94117

95118
bool InitializeRuncpp2ExecutablePath()
96119
{
@@ -145,18 +168,22 @@ bool InitializeRuncpp2ExecutablePath()
145168
return true;
146169
}
147170

148-
bool RunCommand(const std::string& command, bool expectingZeroReturnValue = true)
171+
bool RunCommand(const std::string& command,
172+
bool expectingZeroReturnValue = true,
173+
bool convertSlashes = true)
149174
{
175+
(void)convertSlashes;
150176
std::string updatedCommand = command;
151177
#ifdef _WIN32
178+
if(convertSlashes)
152179
{
153180
std::string::size_type pos = 0;
154181
while((pos = updatedCommand.find('/', pos)) != std::string::npos)
155182
{
156183
updatedCommand.replace(pos, 1, "\\");
157184
pos++;
158185
}
159-
}
186+
}
160187
#endif
161188

162189
DELAYED_OUTPUT( "-----------------------\n"
@@ -166,20 +193,23 @@ bool RunCommand(const std::string& command, bool expectingZeroReturnValue = true
166193
SYSTEM2_RESULT result = System2CppRun(updatedCommand, commandInfo);
167194
if(result != SYSTEM2_RESULT_SUCCESS)
168195
{
169-
ssLOG_ERROR("Failed to run the command: " << result);
196+
ssLOG_ERROR("Failed to run the command: " << updatedCommand);
197+
ssLOG_ERROR("SYSTEM2_RESULT: " << result);
170198
return false;
171199
}
172200

173201
int returnCode = 0;
174202
result = System2CppGetCommandReturnValueSync(commandInfo, returnCode, false);
175203
if(result != SYSTEM2_RESULT_SUCCESS)
176204
{
205+
ssLOG_ERROR("Command failed: " << updatedCommand);
177206
ssLOG_ERROR("Failed to get the return value of the command: " << result);
178207
return false;
179208
}
180209

181210
if(expectingZeroReturnValue && returnCode != 0)
182211
{
212+
ssLOG_ERROR("Command failed: " << updatedCommand);
183213
ssLOG_ERROR("The command returned a non-zero return value: " << returnCode);
184214
return false;
185215
}
@@ -191,14 +221,16 @@ bool RunCommand(const std::string& command, bool expectingZeroReturnValue = true
191221
return true;
192222
}
193223

194-
bool RunCommandWithPrompt(const std::string& command, bool expectingZeroReturnValue = true)
224+
bool RunCommandWithPrompt( const std::string& command,
225+
bool expectingZeroReturnValue = true,
226+
bool convertSlashes = true)
195227
{
196228
//DELAYED_OUTPUT("> " << command << " # Press enter to continue...");
197229
std::cout << "> " << command << " # Press enter to continue...";
198230
GetInput(true);
199231
DELAYED_OUTPUT("");
200232

201-
return RunCommand(command, expectingZeroReturnValue);
233+
return RunCommand(command, expectingZeroReturnValue, convertSlashes);
202234
}
203235

204236
std::string ReadFile(const std::string& path)
@@ -286,15 +318,20 @@ int main(int, char**)
286318

287319
DELAYED_OUTPUT( "We can get the binary files of the script by "
288320
"using the `--build` (or `-b`) option.");
289-
DELAYED_OUTPUT("This will output the binary files in the directory specified.");
321+
DELAYED_OUTPUT( "And we can specify the output directory for the binary files with `--output` "
322+
"(or `-o`)");
290323
DELAYED_OUTPUT( "We also need to pass the `--executable` (or `-e`) option to "
291324
"explicitly get an executable file. \n"
292325
"See https://neko-box-coder.github.io/runcpp2/latest/guides/"
293326
"building_project_sources/ for more details.\n");
327+
328+
//TODO: Remove this when the cache bug is fixed
329+
DELAYED_OUTPUT("`--rebuild` is just resetting the build cache");
294330

295331
DELAYED_OUTPUT("Let's try it");
296332
if(!RunCommandWithPrompt( runcpp2ExecutablePath +
297-
" --build ./tutorial --executable tutorial/main.cpp"))
333+
" --build --output ./tutorial --executable --rebuild "
334+
"tutorial/main.cpp"))
298335
{
299336
return false;
300337
}
@@ -745,8 +782,8 @@ bool Chapter3_ExternalDependencies()
745782
#ifdef _WIN32
746783
if(!RunCommandWithPrompt( "powershell -Command \""
747784
"Invoke-WebRequest https://github.com/Neko-Box-Coder/runcpp2/raw/"
748-
"refs/heads/InteractiveTutorial/Examples/Logging.cpp"
749-
"-OutFile tutorial/Logging.cpp\""))
785+
"refs/heads/InteractiveTutorial/Examples/Logging.cpp "
786+
"-OutFile tutorial/Logging.cpp\"", true, false))
750787
{
751788
return false;
752789
}
@@ -782,7 +819,7 @@ bool Chapter3_ExternalDependencies()
782819
if(!RunCommandWithPrompt( "powershell -Command \""
783820
"Invoke-WebRequest https://github.com/Neko-Box-Coder/runcpp2/raw/"
784821
"refs/heads/InteractiveTutorial/Examples/SDLWindow.cpp "
785-
"-OutFile tutorial/SDLWindow.cpp\""))
822+
"-OutFile tutorial/SDLWindow.cpp\"", true, false))
786823
{
787824
return false;
788825
}
@@ -826,8 +863,17 @@ bool Chapter3_ExternalDependencies()
826863
"then link against the built binary and display a Window.");
827864
DELAYED_OUTPUT( "It might take a bit of time to build SDL2 but only happens when you run "
828865
"the script for the first time. Let's run it now.");
829-
if(!RunCommandWithPrompt("cd tutorial && " + runcpp2ExecutablePath + " SDLWindow.cpp"))
830-
return false;
866+
if(!integrationTest)
867+
{
868+
if(!RunCommandWithPrompt("cd tutorial && " + runcpp2ExecutablePath + " SDLWindow.cpp"))
869+
return false;
870+
}
871+
else
872+
{
873+
DELAYED_OUTPUT("running SDLWindow.cpp skipped for integration testing");
874+
if(!RunCommandWithPrompt("cd tutorial && " + runcpp2ExecutablePath + " --build SDLWindow.cpp"))
875+
return false;
876+
}
831877

832878
DELAYED_OUTPUT("You can also have a dependency as a standalone YAML file which you can import.");
833879
DELAYED_OUTPUT("Let me move the SDL2 dependency to a standalone YAML file.");
@@ -877,15 +923,15 @@ bool Chapter3_ExternalDependencies()
877923
int expectedIndent = 0;
878924
std::string sdlContent;
879925
{
880-
for(int i = sdlStart - 1;
926+
for(int i = (int)sdlStart - 1;
881927
i >= 0 && (fileContent[i] == ' ' || fileContent[i] == '-');
882928
--i)
883929
{
884930
expectedIndent++;
885931
}
886932

887933
size_t sdlEnd = sdlStart;
888-
for(int i = sdlStart + 1; i < (int)commentBlock.length(); ++i)
934+
for(int i = (int)sdlStart + 1; i < (int)commentBlock.length(); ++i)
889935
{
890936
if(commentBlock[i] == '\n')
891937
{
@@ -905,6 +951,8 @@ bool Chapter3_ExternalDependencies()
905951
//Short empty line, remove it
906952
else if(commentBlock[lineStart + spaces] == '\n' && spaces < expectedIndent)
907953
commentBlock.erase(lineStart, spaces + 1);
954+
else if(commentBlock[lineStart + spaces] == '\r')
955+
continue;
908956
else
909957
break;
910958
}
@@ -936,8 +984,8 @@ bool Chapter3_ExternalDependencies()
936984
std::string indentSpaces(expectedIndent, ' ');
937985
indentSpaces = "\n" + indentSpaces;
938986

939-
int currentIndentPos = sdlContent.find(indentSpaces, firstNewline);
940-
while((size_t)currentIndentPos != std::string::npos)
987+
size_t currentIndentPos = sdlContent.find(indentSpaces, firstNewline);
988+
while(currentIndentPos != std::string::npos)
941989
{
942990
sdlContent.replace(currentIndentPos, indentSpaces.length(), "\n");
943991
currentIndentPos = sdlContent.find(indentSpaces, ++currentIndentPos);
@@ -972,8 +1020,17 @@ bool Chapter3_ExternalDependencies()
9721020
GetInput(true);
9731021

9741022
DELAYED_OUTPUT("Let's run it now.");
975-
if(!RunCommandWithPrompt("cd tutorial && " + runcpp2ExecutablePath + " SDLWindow.cpp"))
976-
return false;
1023+
if(!integrationTest)
1024+
{
1025+
if(!RunCommandWithPrompt("cd tutorial && " + runcpp2ExecutablePath + " SDLWindow.cpp"))
1026+
return false;
1027+
}
1028+
else
1029+
{
1030+
DELAYED_OUTPUT("running SDLWindow.cpp skipped for integration testing");
1031+
if(!RunCommandWithPrompt("cd tutorial && " + runcpp2ExecutablePath + " --build SDLWindow.cpp"))
1032+
return false;
1033+
}
9771034

9781035
DELAYED_OUTPUT("This concludes the 3rd chapter of the tutorial.");
9791036
DELAYED_OUTPUT("Press enter to continue...");
@@ -982,17 +1039,47 @@ bool Chapter3_ExternalDependencies()
9821039
return true;
9831040
}
9841041

985-
9861042
int main(int argc, char* argv[])
9871043
{
9881044
ssLOG_SET_CURRENT_THREAD_TARGET_LEVEL(ssLOG_LEVEL_WARNING);
9891045

1046+
//--test <path to runcpp2> <path to config file>
1047+
integrationTest = argc > 2 && std::string(argv[1]) == "--test";
1048+
9901049
DELAYED_OUTPUT( "===========================================\n"
9911050
"Runcpp2 Interactive Tutorial\n"
9921051
"===========================================\n");
9931052

994-
if(!InitializeRuncpp2ExecutablePath())
995-
return 1;
1053+
if(!integrationTest)
1054+
{
1055+
if(!InitializeRuncpp2ExecutablePath())
1056+
return 1;
1057+
}
1058+
else
1059+
{
1060+
if(argc != 4)
1061+
{
1062+
ssLOG_ERROR("Missing arguments for integrationTest");
1063+
return 1;
1064+
}
1065+
1066+
std::error_code ec;
1067+
auto exePath = ghc::filesystem::absolute(ghc::filesystem::path(argv[2]), ec);
1068+
if(ec)
1069+
{
1070+
ssLOG_ERROR("Failed to get absolute path for " << argv[2] << ". \n" << ec.message());
1071+
return 1;
1072+
}
1073+
1074+
auto configPath = ghc::filesystem::absolute(ghc::filesystem::path(argv[3]), ec);
1075+
if(ec)
1076+
{
1077+
ssLOG_ERROR("Failed to get absolute path for " << argv[3] << ". \n" << ec.message());
1078+
return 1;
1079+
}
1080+
1081+
runcpp2ExecutablePath = exePath.string() + " -l -c " + configPath.string();
1082+
}
9961083

9971084
DELAYED_OUTPUT("The whole tutorial is about 15 minutes long.");
9981085

Include/runcpp2/Data/CmdOptions.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace runcpp2
2323
CLEANUP,
2424
BUILD_SOURCE_ONLY,
2525
THREADS,
26+
TUTORIAL,
2627
COUNT
2728
};
2829
}

Include/runcpp2/runcpp2.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ namespace runcpp2
5555
int& returnStatus);
5656

5757
std::string PipelineResultToString(PipelineResult result);
58+
59+
bool DownloadTutorial(char* runcppPath);
5860
}
5961

6062
#endif

Src/runcpp2/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ int main(int argc, char* argv[])
305305
ssLOG_BASE(" -v, --[v]ersion Show the version of runcpp2");
306306
ssLOG_BASE(" -h, --[h]elp Show this help message");
307307
ssLOG_BASE(" --log-level <level> Sets the log level (Normal, Info, Debug) for runcpp2");
308+
ssLOG_BASE(" Others:");
309+
ssLOG_BASE(" --tutorial Start interactive tutorial");
308310
return 0;
309311
}
310312

@@ -346,6 +348,14 @@ int main(int argc, char* argv[])
346348
return 0;
347349
}
348350

351+
//Download tutorial
352+
if(currentOptions.count(runcpp2::CmdOptions::TUTORIAL))
353+
{
354+
if(!runcpp2::DownloadTutorial(argv[0]))
355+
return -1;
356+
return 0;
357+
}
358+
349359
//Resetting user config
350360
if(currentOptions.count(runcpp2::CmdOptions::RESET_USER_CONFIG))
351361
{

0 commit comments

Comments
 (0)