-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformUtil.hpp
More file actions
256 lines (214 loc) · 7.83 KB
/
Copy pathPlatformUtil.hpp
File metadata and controls
256 lines (214 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#ifndef RUNCPP2_PLATFORM_UTIL_HPP
#define RUNCPP2_PLATFORM_UTIL_HPP
#include "runcpp2/Data/ParseCommon.hpp"
#if !defined(NOMINMAX)
#define NOMINMAX 1
#endif
#include "System2.h"
#include "ssLogger/ssLog.hpp"
#include "ghc/filesystem.hpp"
#include <cctype>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
#include <chrono>
#include <iomanip>
#include <sstream>
#if !defined(INTERNAL_RUNCPP2_UNIT_TESTS) || !INTERNAL_RUNCPP2_UNIT_TESTS
#define CO_NO_OVERRIDE 1
#include "CppOverride.hpp"
#else
#include "CppOverride.hpp"
extern CO_DECLARE_INSTANCE(OverrideInstance);
#endif
namespace
{
char GetAltFileSystemSeparator()
{
#ifdef _WIN32
return '/';
#elif __unix__
return '\0';
#else
return '\0';
#endif
}
char GetFileSystemSeparator()
{
#ifdef _WIN32
return '\\';
#elif __unix__
return '/';
#else
return '/';
#endif
}
}
namespace runcpp2
{
inline std::string ProcessPath(const std::string& path)
{
std::string processedScriptPath;
char altSeparator = GetAltFileSystemSeparator();
//Convert alternative slashes to native slashes
if(altSeparator != '\0')
{
char separator = GetFileSystemSeparator();
processedScriptPath = path;
for(int i = 0; i < processedScriptPath.size(); ++i)
{
if(processedScriptPath[i] == altSeparator)
processedScriptPath[i] = separator;
}
return processedScriptPath;
}
else
return path;
}
inline std::vector<std::string> GetPlatformNames()
{
CO_INSERT_IMPL(OverrideInstance, std::vector<std::string>, ());
#ifdef _WIN32
return {"Windows", "DefaultPlatform"};
#elif __linux__
return {"Linux", "Unix", "DefaultPlatform"};
#elif __APPLE__
return {"MacOS", "Unix", "DefaultPlatform"};
#elif __unix__
return {"Unix", "DefaultPlatform"};
#else
return {"Unknown", "DefaultPlatform"};
#endif
}
inline bool RunCommand( const std::string& command,
const bool& captureOutput,
const std::string& runDirectory,
std::string& outOutput,
int& outReturnCode)
{
ssLOG_FUNC_DEBUG();
ssLOG_DEBUG("Running: " << command);
System2CommandInfo commandInfo = {};
if(!runDirectory.empty())
commandInfo.RunDirectory = runDirectory.c_str();
commandInfo.RedirectOutput = captureOutput;
SYSTEM2_RESULT sys2Result = System2Run(command.c_str(), &commandInfo);
if(sys2Result != SYSTEM2_RESULT_SUCCESS)
{
ssLOG_ERROR("System2Run failed with result: " << sys2Result);
return false;
}
outOutput.clear();
if(captureOutput)
{
do
{
uint32_t byteRead = 0;
outOutput.resize(outOutput.size() + 4096);
sys2Result =
System2ReadFromOutput( &commandInfo,
const_cast<char*>(outOutput.data()) + outOutput.size() - 4096,
4096,
&byteRead);
outOutput.resize(outOutput.size() + byteRead);
}
while(sys2Result == SYSTEM2_RESULT_READ_NOT_FINISHED);
}
sys2Result = System2GetCommandReturnValueSync(&commandInfo, &outReturnCode, false);
if(sys2Result != SYSTEM2_RESULT_SUCCESS)
{
ssLOG_ERROR("System2GetCommandReturnValueSync failed with result: " << sys2Result);
return false;
}
if(captureOutput)
ssLOG_DEBUG("outOutput: \n" << outOutput.c_str());
if(outReturnCode != 0)
{
ssLOG_DEBUG("Failed when running command with return code: " << outReturnCode);
return false;
}
return true;
}
#if defined(_WIN32)
inline std::string GetWindowsError()
{
//Get the error message ID, if any.
DWORD errorMessageID = ::GetLastError();
if(errorMessageID == 0) {
return std::string(); //No error message has been recorded
}
LPSTR messageBuffer = nullptr;
//Ask Win32 to give us the string version of that message ID.
//The parameters we pass in, tell Win32 to create the buffer that holds the message for us
//(because we don't yet know how long the message string will be).
size_t size = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorMessageID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&messageBuffer,
0,
NULL);
//Copy the error message into a std::string.
std::string message(messageBuffer, size);
//Free the Win32's string's buffer.
LocalFree(messageBuffer);
return message;
}
#endif
template <typename T>
inline bool HasValueFromPlatformMap(const std::unordered_map<PlatformName, T>& map)
{
std::vector<std::string> platformNames = runcpp2::GetPlatformNames();
for(int i = 0; i < platformNames.size(); ++i)
{
if(map.find(platformNames.at(i)) != map.end())
return true;
}
return false;
}
template <typename T>
inline const T* GetValueFromPlatformMap(const std::unordered_map<PlatformName, T>& map)
{
std::vector<std::string> platformNames = runcpp2::GetPlatformNames();
for(int i = 0; i < platformNames.size(); ++i)
{
if(map.find(platformNames.at(i)) != map.end())
return &map.at(platformNames[i]);
}
return nullptr;
}
inline std::string GetFileExtensionWithoutVersion(const ghc::filesystem::path& path)
{
std::string filename = path.filename().string();
bool inNumericPart = true;
int lastDotPos = filename.length();
for(int i = filename.length() - 1; i >= 0; --i)
{
if(filename[i] == '.')
{
if(!inNumericPart)
return filename.substr(i, lastDotPos - i);
inNumericPart = true;
lastDotPos = i;
}
else if(!std::isdigit(filename[i]))
inNumericPart = false;
}
return "";
}
//From https://stackoverflow.com/a/58523115
using time_point = std::chrono::system_clock::time_point;
std::string SerializeTimePoint(const time_point& time, const std::string& format = "%Y-%m-%d_%H:%M:%S")
{
std::time_t tt = std::chrono::system_clock::to_time_t(time);
std::tm tm = *std::gmtime(&tt); //GMT (UTC)
//std::tm tm = *std::localtime(&tt); //Locale time-zone, usually UTC by default.
std::stringstream ss;
ss << std::put_time( &tm, format.c_str() );
return ss.str();
}
}
#endif