Skip to content

Commit d55613c

Browse files
committed
project generation works
1 parent 473203b commit d55613c

File tree

10 files changed

+253
-50
lines changed

10 files changed

+253
-50
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,5 @@ build/
337337

338338
large_dir_output_api.h
339339
static_resource_assert_api.h
340-
.vscode
340+
.vscode
341+
sra_output

StaticResourceAssert/ApiGenerator.cpp

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#include <fstream>
77
#include <iomanip>
88

9-
std::string GenerateAPIHeaderString( const std::string_view& directoryCataloged, const std::vector<std::string>& arrayItems )
9+
std::string GenerateAPI_HeaderString( const std::string_view& directoryCataloged, const std::vector<DirEntryInfo>& arrayItems )
1010
{
1111
if (arrayItems.empty())
1212
return std::string();
1313

14-
std::ifstream inputFile("StaticResourceAssert/templates/static_resource_assert_api.ht");
14+
std::ifstream inputFile("StaticResourceAssert/templates/static_resource_assert_api.h.template");
1515
if(!inputFile) {
1616
return std::string();
1717
}
@@ -23,12 +23,12 @@ std::string GenerateAPIHeaderString( const std::string_view& directoryCataloged,
2323

2424
std::string arrayElements;
2525

26-
const uint64_t numOfItems = arrayItems.size();
26+
const size_t numOfItems = arrayItems.size();
2727
uint64_t counter = 0;
2828

2929
for( size_t c = 0; c < numOfItems; c++ )
3030
{
31-
const std::string str = arrayItems[c];
31+
const std::string str = arrayItems[c].path;
3232

3333
// build the element
3434
arrayElements += "u8\"";
@@ -45,27 +45,77 @@ std::string GenerateAPIHeaderString( const std::string_view& directoryCataloged,
4545
}
4646
}
4747
// generate information about the generation
48-
auto now = std::chrono::system_clock::now();
49-
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
50-
std::tm local_tm = *std::localtime(&now_time);
48+
const auto now = std::chrono::system_clock::now();
49+
const std::time_t now_time = std::chrono::system_clock::to_time_t(now);
50+
const std::tm local_tm = *std::localtime(&now_time);
5151

5252
std::ostringstream oss;
5353
oss << "SRA version: " << SRA_PROGRAM_VERSION << std::endl;
5454
oss << "Generation date: " << std::put_time(&local_tm, "%Y-%m-%d %H:%M:%S") << std::endl;
5555
oss << "Directory cataloged: " << directoryCataloged << std::endl;
5656
oss << "Number of items found: " << numOfItems << std::endl;
5757

58-
replace_all(headerString, "${ResourceFiles}", arrayElements);
59-
replace_all(headerString, "${ArraySize}", std::to_string(numOfItems));
60-
replace_all(headerString, "${GenerationComment}", oss.str());
58+
ReplaceAll(headerString, "${ResourceFiles}", arrayElements);
59+
ReplaceAll(headerString, "${ArraySize}", std::to_string(numOfItems));
60+
ReplaceAll(headerString, "${GenerationComment}", oss.str());
6161

6262
// Get the duration since the Unix epoch in milliseconds
6363
const auto duration = now.time_since_epoch();
6464

6565
// Convert duration to milliseconds
6666
const auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
6767

68-
replace_all(headerString, "${ArrayID}", std::to_string(milliseconds));
68+
ReplaceAll(headerString, "${ArrayID}", std::to_string(milliseconds));
6969

7070
return headerString;
71-
}
71+
}
72+
73+
std::string GenerateAPI_HeaderString(const std::string_view& outputHeaderName) {
74+
if (outputHeaderName.empty())
75+
return std::string();
76+
77+
std::ifstream inputFile("StaticResourceAssert/templates/static_resource_assert_api.cpp.template");
78+
if(!inputFile) {
79+
return std::string();
80+
}
81+
82+
std::stringstream buffer;
83+
buffer << inputFile.rdbuf();
84+
std::string cppString = buffer.str();
85+
inputFile.close();
86+
87+
ReplaceAll(cppString, "${IncludePath}", outputHeaderName);
88+
89+
return cppString;
90+
}
91+
92+
std::string GenerateAPI_CMakeListsString( const std::string_view& projectName, const std::string_view& libraryName, const std::string_view& outputHeaderName)
93+
{
94+
if (projectName.empty() || libraryName.empty() || outputHeaderName.empty())
95+
return std::string();
96+
97+
std::ifstream inputFile("StaticResourceAssert/templates/CMakeLists.txt.template");
98+
if(!inputFile) {
99+
return std::string();
100+
}
101+
102+
std::stringstream buffer;
103+
buffer << inputFile.rdbuf();
104+
std::string cmakeListsString = buffer.str();
105+
inputFile.close();
106+
107+
ReplaceAll(cmakeListsString, "${CMakeProjectName}", projectName);
108+
ReplaceAll(cmakeListsString, "${CMakeLibraryName}", libraryName);
109+
110+
std::string outputHeaderNameStr = outputHeaderName.data();
111+
const std::string_view filename = RemoveExt(outputHeaderName.data(), ".h");
112+
if (!filename.empty()) {
113+
outputHeaderNameStr = filename;
114+
}
115+
outputHeaderNameStr += ".cpp";
116+
ReplaceAll(cmakeListsString, "${CMakeLibrarySources}", outputHeaderNameStr);
117+
118+
return cmakeListsString;
119+
}
120+
121+

StaticResourceAssert/Common.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
2323
NOTE: 0.0.YYYYMMDD should only be in the main branch. Its a non-release version number
2424
*/
25-
const char* SRA_PROGRAM_VERSION = "0.0.20250219";
25+
const char* SRA_PROGRAM_VERSION = "0.0.20250516";
2626

2727
////////////////////////////////////////////////////////////////////////////////////////////
2828

@@ -50,7 +50,7 @@ void __pstream(const std::ostream &t, bool bNewline)
5050
P( ss.str().c_str(), bNewline );
5151
}
5252

53-
std::size_t replace_all(std::string& inout, std::string_view what, std::string_view with)
53+
std::size_t ReplaceAll(std::string& inout, std::string_view what, std::string_view with)
5454
{
5555
std::size_t count{};
5656
for (std::string::size_type pos{};
@@ -61,6 +61,14 @@ std::size_t replace_all(std::string& inout, std::string_view what, std::string_v
6161
return count;
6262
}
6363

64+
std::string_view RemoveExt(const std::string_view& in, const std::string_view& ext) {
65+
const size_t pos = in.rfind(".h");
66+
if ( std::string_view::npos != pos && pos == in.length() - 2) {
67+
return in.substr(0, in.length() - 2);
68+
}
69+
return "";
70+
}
71+
6472
void TrimStartWhitespace(std::string& inout) {
6573
if(inout.empty()) {
6674
return;

StaticResourceAssert/Main.cpp

Lines changed: 116 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,78 @@ namespace fs = std::filesystem;
1414
constexpr const char* helpFile =
1515
"-d : Required. The directory to recursively search through and catalog entries\n"
1616
"-o : Name of the output api header. Always include the .h extention. Default is: static_resource_assert_api.h\n"
17+
"-odir : Output directory. Always add a '/' to the end of the directory path. Default is: sra_output/"
18+
"--c : Read file contents\n"
1719
"-h : Print version and command line options\n"
1820
;
1921

2022
////////////////////////////////////////////////////////////////////////
2123

24+
bool OutputAPI_HeaderString(const std::string& outputDir, const std::string& outputHeaderName, const std::string& directoryStr, const std::vector<DirEntryInfo>& arrayItems) {
25+
PSTREAM_NL( "Generating " << outputHeaderName << " ..." );
26+
27+
const std::string apiHeader = GenerateAPI_HeaderString(directoryStr, arrayItems);
28+
if(apiHeader.empty()) {
29+
PSTREAM_NL( "\tError: Failed to generate API header");
30+
return false;
31+
}
32+
33+
std::fstream file(outputDir + outputHeaderName, std::ios::trunc | std::ios::out);
34+
if( file.is_open() )
35+
{
36+
file.write( apiHeader.c_str(), apiHeader.size() );
37+
file.close();
38+
}
39+
40+
PSTREAM_NL( "\tAPI header generated\n" );
41+
return true;
42+
}
43+
44+
bool OutputAPI_CppString(const std::string& outputDir, const std::string& outputHeaderName) {
45+
const std::string_view filenameStrView = RemoveExt(outputHeaderName.data(), ".h");
46+
std::string filename(filenameStrView);
47+
filename += ".cpp";
48+
PSTREAM_NL( "Generating " << filename << " ..." );
49+
50+
const std::string apiCpp = GenerateAPI_HeaderString(outputHeaderName);
51+
if(apiCpp.empty()) {
52+
PSTREAM_NL( "\tError: Failed to generate API cpp");
53+
return false;
54+
}
55+
56+
std::fstream file(outputDir + filename, std::ios::trunc | std::ios::out);
57+
if( file.is_open() )
58+
{
59+
file.write( apiCpp.c_str(), apiCpp.size() );
60+
file.close();
61+
}
62+
63+
PSTREAM_NL( "\tAPI cpp generated\n" );
64+
return true;
65+
}
66+
67+
bool OutputAPI_CMakeListsString(const std::string& outputDir, const std::string& outputHeaderName, const std::string& projectName, const std::string& libraryName) {
68+
PSTREAM_NL( "Generating CMakeLists.txt ..." );
69+
70+
const std::string apiCMakeLists = GenerateAPI_CMakeListsString(projectName, libraryName, outputHeaderName);
71+
if(apiCMakeLists.empty()) {
72+
PSTREAM_NL( "\tError: Failed to generate API CMakeLists");
73+
return false;
74+
}
75+
76+
std::fstream file(outputDir + "CMakeLists.txt", std::ios::trunc | std::ios::out);
77+
if( file.is_open() )
78+
{
79+
file.write( apiCMakeLists.c_str(), apiCMakeLists.size() );
80+
file.close();
81+
}
82+
83+
PSTREAM_NL( "\tAPI CMakeLists generated\n" );
84+
return true;
85+
}
86+
87+
////////////////////////////////////////////////////////////////////////
88+
2289
int main( int argc, char** argv )
2390
{
2491
std::string versionStr = "\nStatic Resource Assert V: ";
@@ -40,22 +107,32 @@ int main( int argc, char** argv )
40107
}
41108

42109
std::string directoryStr = GetArgData( "-d", argc, argv );
43-
replace_all( directoryStr, "\\", "/" );
44-
replace_all( directoryStr, "\\", "" );
45-
replace_all( directoryStr, "<", "" );
46-
replace_all( directoryStr, ">", "" );
47-
replace_all( directoryStr, "\"", "" );
48-
replace_all( directoryStr, "|", "" );
49-
replace_all( directoryStr, "?", "" );
50-
replace_all( directoryStr, "*", "" );
110+
ReplaceAll( directoryStr, "\\", "/" );
111+
ReplaceAll( directoryStr, "\\", "" );
112+
ReplaceAll( directoryStr, "<", "" );
113+
ReplaceAll( directoryStr, ">", "" );
114+
ReplaceAll( directoryStr, "\"", "" );
115+
ReplaceAll( directoryStr, "|", "" );
116+
ReplaceAll( directoryStr, "?", "" );
117+
ReplaceAll( directoryStr, "*", "" );
51118

52119
TrimStartWhitespace(directoryStr);
53120
TrimEndWhitespace(directoryStr);
54121

55-
std::string outputHeaderStr = "static_resource_assert_api.h";
56-
if( HasArg( "-o", argc, argv ) )
57-
{
58-
outputHeaderStr = GetArgData( "-o", argc, argv );
122+
std::string outputHeaderName = "static_resource_assert_api.h";
123+
if( HasArg( "-o", argc, argv ) ) {
124+
outputHeaderName = GetArgData( "-o", argc, argv );
125+
}
126+
127+
std::string outputDir = "sra_output/";
128+
if( HasArg( "-odir", argc, argv ) ) {
129+
outputDir = GetArgData( "-odir", argc, argv );
130+
}
131+
fs::create_directories(outputDir);
132+
133+
bool readContents = false;
134+
if( HasArg( "--c", argc, argv ) ) {
135+
readContents = true;
59136
}
60137

61138
const fs::path dirPath { directoryStr };
@@ -69,37 +146,48 @@ int main( int argc, char** argv )
69146
fs::directory_entry resourceDir(dirPath);
70147

71148
std::error_code ec;
72-
std::vector<std::string> arrayItems;
149+
std::vector<DirEntryInfo> arrayItems;
73150
for( const fs::directory_entry& dirEntry : fs::recursive_directory_iterator{resourceDir, fs::directory_options::skip_permission_denied, ec} )
74151
{
75152
std::string pathPathStr = dirEntry.path().u8string();
76153

77-
replace_all( pathPathStr, "\\", "/" );
154+
ReplaceAll( pathPathStr, "\\", "/" );
155+
156+
ReplaceAll( pathPathStr, resourceDir.path().string() + "/", "" );
157+
158+
DirEntryInfo dirEntryInfo;
159+
dirEntryInfo.path = pathPathStr;
78160

79-
replace_all( pathPathStr, resourceDir.path().string() + "/", "" );
80-
arrayItems.push_back( pathPathStr );
161+
if(readContents) {
162+
std::ifstream istrm(dirEntryInfo.path, std::ios::binary);
163+
istrm >> dirEntryInfo.fileContents;
164+
istrm.close();
165+
}
166+
167+
arrayItems.push_back( dirEntryInfo );
81168
}
82169

83-
std::sort(arrayItems.begin(), arrayItems.end());
170+
std::sort(arrayItems.begin(), arrayItems.end()
171+
, [](const DirEntryInfo& a, const DirEntryInfo& b) -> bool {
172+
return a.path < b.path;
173+
});
84174

85175
PSTREAM_NL( "Directory elements found: " << arrayItems.size() );
86176

87-
PSTREAM_NL( "Generating " << outputHeaderStr << " ..." );
88-
89-
const std::string apiHeader = GenerateAPIHeaderString(directoryStr, arrayItems);
90-
if(apiHeader.empty()) {
91-
PSTREAM_NL( "Error: Failed to generate API");
177+
if(!OutputAPI_HeaderString(outputDir, outputHeaderName, directoryStr, arrayItems)) {
178+
PSTREAM_NL( "API header failed to generate." );
92179
return -3;
93180
}
94181

95-
std::fstream file(outputHeaderStr, std::ios::trunc | std::ios::out);
96-
if( file.is_open() )
97-
{
98-
file.write( apiHeader.c_str(), apiHeader.size() );
99-
file.close();
182+
if(!OutputAPI_CppString(outputDir, outputHeaderName)) {
183+
PSTREAM_NL( "API cpp failed to generate." );
184+
return -4;
100185
}
101186

102-
PSTREAM_NL( "API Header generated\n" );
187+
if(!OutputAPI_CMakeListsString(outputDir, outputHeaderName, "TestProjectName", "TestLibraryName")) {
188+
PSTREAM_NL( "API CMakeLists.txt failed to generate." );
189+
return -5;
190+
}
103191

104192
return 0;
105193
}

StaticResourceAssert/include/ApiGenerator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
////////////////////////////////////////////////////////////////////////
88

9-
std::string GenerateAPIHeaderString( const std::string_view& directoryCataloged, const std::vector<std::string>& arrayItems );
9+
std::string GenerateAPI_HeaderString( const std::string_view& directoryCataloged, const std::vector<DirEntryInfo>& arrayItems );
10+
11+
std::string GenerateAPI_HeaderString(const std::string_view& outputHeaderName);
12+
13+
std::string GenerateAPI_CMakeListsString( const std::string_view& projectName, const std::string_view& libraryName, const std::string_view& outputHeaderName);
1014

1115
#endif

StaticResourceAssert/include/Common.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@
44

55
#include <iostream>
66
#include <sstream>
7-
7+
#include <string_view>
88

99
extern const char* SRA_PROGRAM_VERSION;
1010

1111
////////////////////////////////////////////////////////////////////////////////////////////
1212

13+
struct DirEntryInfo {
14+
std::string path;
15+
std::string fileContents;
16+
};
1317

18+
////////////////////////////////////////////////////////////////////////
1419

1520
void __pstream(const std::ostream &t, bool bNewline = true );
1621

1722
#define PSTREAM( s ) __pstream( std::stringstream()<< s, false )
1823

1924
#define PSTREAM_NL( s ) __pstream( std::stringstream()<< s, true )
2025

21-
std::size_t replace_all(std::string& inout, std::string_view what, std::string_view with);
26+
std::size_t ReplaceAll(std::string& inout, std::string_view what, std::string_view with);
27+
28+
std::string_view RemoveExt(const std::string_view& in, const std::string_view& ext);
2229

2330
void TrimStartWhitespace(std::string& inout);
2431

0 commit comments

Comments
 (0)