@@ -83,8 +83,8 @@ static llvm::cl::list<std::string> UserStylesheets(
83
83
84
84
static llvm::cl::opt<std::string>
85
85
UserAssetPath (" asset" ,
86
- llvm::cl::desc (" User supplied asset path for html output to "
87
- " override the default css and js files" ),
86
+ llvm::cl::desc (" User supplied asset path to "
87
+ " override the default css and js files for html output " ),
88
88
llvm::cl::cat(ClangDocCategory));
89
89
90
90
static llvm::cl::opt<std::string> SourceRoot (" source-root" , llvm::cl::desc(R"(
@@ -137,25 +137,27 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
137
137
return llvm::sys::fs::getMainExecutable (Argv0, MainAddr);
138
138
}
139
139
140
- void GetAssetFiles (clang::doc::ClangDocContext &CDCtx) {
140
+ llvm::Error GetAssetFiles (clang::doc::ClangDocContext &CDCtx) {
141
141
std::error_code Code;
142
- for (auto DirIt = llvm::sys::fs::directory_iterator (
143
- std::string (UserAssetPath), Code),
144
- dir_end = llvm::sys::fs::directory_iterator ();
145
- !Code && DirIt != dir_end; DirIt.increment (Code)) {
146
- llvm::SmallString<128 > filePath = llvm::SmallString<128 >(DirIt->path ());
147
- if (llvm::sys::fs::is_regular_file (filePath)) {
148
- if (filePath.ends_with (" .css" )) {
142
+ for (auto DirIt = llvm::sys::fs::directory_iterator (UserAssetPath, Code),
143
+ DirEnd = llvm::sys::fs::directory_iterator ();
144
+ !Code && DirIt != DirEnd; DirIt.increment (Code)) {
145
+ llvm::SmallString<128 > FilePath = llvm::SmallString<128 >(DirIt->path ());
146
+ if (!Code) {
147
+ return llvm::createFileError (FilePath, Code);
148
+ }
149
+ if (llvm::sys::fs::is_regular_file (FilePath)) {
150
+ if (llvm::sys::path::extension (FilePath) == " .css" ) {
149
151
CDCtx.UserStylesheets .insert (CDCtx.UserStylesheets .begin (),
150
- std::string (filePath ));
151
- } else if (filePath. ends_with ( " .js" ) ) {
152
- CDCtx.FilesToCopy .emplace_back (filePath .str ());
152
+ std::string (FilePath ));
153
+ } else if (llvm::sys::path::extension (FilePath) == " .js" ) {
154
+ CDCtx.FilesToCopy .emplace_back (FilePath .str ());
153
155
}
154
156
}
155
157
}
156
158
}
157
159
158
- void GetDefaultAssetFiles (const char *Argv0,
160
+ llvm::Error GetDefaultAssetFiles (const char *Argv0,
159
161
clang::doc::ClangDocContext &CDCtx) {
160
162
void *MainAddr = (void *)(intptr_t )GetExecutablePath;
161
163
std::string ClangDocPath = GetExecutablePath (Argv0, MainAddr);
@@ -172,12 +174,35 @@ void GetDefaultAssetFiles(const char *Argv0,
172
174
llvm::SmallString<128 > IndexJS;
173
175
llvm::sys::path::native (AssetsPath, IndexJS);
174
176
llvm::sys::path::append (IndexJS, " index.js" );
177
+
178
+ if (!llvm::sys::fs::is_regular_file (IndexJS)) {
179
+ return llvm::createStringError (llvm::inconvertibleErrorCode (),
180
+ " error default index.js file at " + IndexJS + " \n " );
181
+ }
182
+
183
+ if (!llvm::sys::fs::is_regular_file (DefaultStylesheet)) {
184
+ return llvm::createStringError (llvm::inconvertibleErrorCode (),
185
+ " error default clang-doc-default-stylesheet.css file at " + DefaultStylesheet + " \n " );
186
+ }
187
+
175
188
CDCtx.UserStylesheets .insert (CDCtx.UserStylesheets .begin (),
176
189
std::string (DefaultStylesheet));
177
190
CDCtx.FilesToCopy .emplace_back (IndexJS.str ());
178
191
179
- llvm::outs () << " No default asset path found using default asset path : "
192
+ llvm::outs () << " Using default asset: "
180
193
<< AssetsPath << " \n " ;
194
+
195
+ return llvm::Error::success ();
196
+ }
197
+
198
+ llvm::Error GetHTMLAssetFiles (const char *Argv0, clang::doc::ClangDocContext &CDCtx) {
199
+ if (!UserAssetPath.empty () && !llvm::sys::fs::is_directory (std::string (UserAssetPath))) {
200
+ llvm::outs () << " Asset path supply is not a directory: " << UserAssetPath << " falling back to default\n " ;
201
+ }
202
+ if (llvm::sys::fs::is_directory (std::string (UserAssetPath))) {
203
+ return GetAssetFiles (CDCtx);
204
+ }
205
+ return GetDefaultAssetFiles (Argv0, CDCtx);
181
206
}
182
207
183
208
int main (int argc, const char **argv) {
@@ -231,12 +256,11 @@ Example usage for a project using a compile commands database:
231
256
{" index.js" , " index_json.js" }};
232
257
233
258
if (Format == " html" ) {
234
- if (!UserAssetPath.empty () &&
235
- llvm::sys::fs::is_directory (std::string (UserAssetPath))) {
236
- GetAssetFiles (CDCtx);
237
- } else {
238
- GetDefaultAssetFiles (argv[0 ], CDCtx);
239
- }
259
+ auto Err = GetHTMLAssetFiles (argv[0 ], CDCtx);
260
+ if (Err) {
261
+ llvm::errs () << toString (std::move (Err)) << " \n " ;
262
+ return 1 ;
263
+ }
240
264
}
241
265
242
266
// Mapping phase
0 commit comments