@@ -14,11 +14,78 @@ namespace fs = std::filesystem;
1414constexpr 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 ( " \t Error: 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 ( " \t API 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 ( " \t Error: 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 ( " \t API 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 ( " \t Error: 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 ( " \t API CMakeLists generated\n " );
84+ return true ;
85+ }
86+
87+ // //////////////////////////////////////////////////////////////////////
88+
2289int main ( int argc, char ** argv )
2390{
2491 std::string versionStr = " \n Static 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}
0 commit comments