Make file paths work cross platform

This commit is contained in:
Robert Long 2018-03-29 18:25:33 -07:00
parent de09acb789
commit 15d0516fd3
7 changed files with 456 additions and 298 deletions

View File

@ -38,12 +38,11 @@ function convert(srcFile, destFile, opts = []) {
throw new Error(`Unsupported file extension: ${destFile}`); throw new Error(`Unsupported file extension: ${destFile}`);
} }
let srcPath = fs.realpathSync(srcFile); let destDir = path.dirname(destFile);
let destDir = fs.realpathSync(path.dirname(destFile));
let destPath = path.join(destDir, path.basename(destFile, destExt)); let destPath = path.join(destDir, path.basename(destFile, destExt));
let args = opts.slice(0); let args = opts.slice(0);
args.push('--input', srcPath, '--output', destPath); args.push('--input', srcFile, '--output', destPath);
let child = childProcess.spawn(tool, args); let child = childProcess.spawn(tool, args);
let output = ''; let output = '';
@ -52,7 +51,7 @@ function convert(srcFile, destFile, opts = []) {
child.on('error', reject); child.on('error', reject);
child.on('close', code => { child.on('close', code => {
// the FBX SDK may create an .fbm dir during conversion; delete! // the FBX SDK may create an .fbm dir during conversion; delete!
let fbmCruft = srcPath.replace(/.fbx$/i, '.fbm'); let fbmCruft = srcFile.replace(/.fbx$/i, '.fbm');
// don't stick a fork in things if this fails, just log a warning // don't stick a fork in things if this fails, just log a warning
const onError = error => const onError = error =>
error && console.warn(`Failed to delete ${fbmCruft}: ${error}`); error && console.warn(`Failed to delete ${fbmCruft}: ${error}`);
@ -67,6 +66,11 @@ function convert(srcFile, destFile, opts = []) {
reject(new Error(`Converter output:\n` + reject(new Error(`Converter output:\n` +
(output.length ? output : "<none>"))); (output.length ? output : "<none>")));
} else { } else {
if (destExt === ".gltf") {
destPath = path.join(destPath + "_out", path.basename(destFile, destExt));
}
resolve(destPath + destExt); resolve(destPath + destExt);
} }
}); });

File diff suppressed because it is too large Load Diff

View File

@ -197,7 +197,7 @@ Copyright (c) 2016-2017 Oculus VR, LLC.
if (options.count("output") == 0) { if (options.count("output") == 0) {
// if -o is not given, default to the basename of the .fbx // if -o is not given, default to the basename of the .fbx
outputPath = "./" + StringUtils::GetFileBaseString(inputPath); outputPath = "." + (const char)StringUtils::GetPathSeparator() + StringUtils::GetFileBaseString(inputPath);
} }
std::string outputFolder; // the output folder in .gltf mode, not used for .glb std::string outputFolder; // the output folder in .gltf mode, not used for .glb
std::string modelPath; // the path of the actual .glb or .gltf file std::string modelPath; // the path of the actual .glb or .gltf file
@ -207,7 +207,7 @@ Copyright (c) 2016-2017 Oculus VR, LLC.
} else { } else {
// in gltf mode, we create a folder and write into that // in gltf mode, we create a folder and write into that
outputFolder = outputPath + "_out/"; outputFolder = outputPath + "_out" + (const char)StringUtils::GetPathSeparator();
modelPath = outputFolder + StringUtils::GetFileNameString(outputPath) + ".gltf"; modelPath = outputFolder + StringUtils::GetFileNameString(outputPath) + ".gltf";
} }
if (!FileUtils::CreatePath(modelPath.c_str())) { if (!FileUtils::CreatePath(modelPath.c_str())) {

View File

@ -182,7 +182,7 @@ namespace FileUtils {
return true; return true;
} }
bool CopyFile(const std::string &srcFilename, const std::string &dstFilename) { bool CopyFile(const std::string &srcFilename, const std::string &dstFilename, bool createPath = false) {
std::ifstream srcFile(srcFilename, std::ios::binary); std::ifstream srcFile(srcFilename, std::ios::binary);
if (!srcFile) { if (!srcFile) {
fmt::printf("Warning: Couldn't open file %s for reading.\n", srcFilename); fmt::printf("Warning: Couldn't open file %s for reading.\n", srcFilename);
@ -193,9 +193,14 @@ namespace FileUtils {
std::streamsize srcSize = srcFile.tellg(); std::streamsize srcSize = srcFile.tellg();
srcFile.seekg(0, std::ios::beg); srcFile.seekg(0, std::ios::beg);
if (createPath && !CreatePath(dstFilename.c_str())) {
fmt::printf("Warning: Couldn't create directory %s.\n", dstFilename);
return false;
}
std::ofstream dstFile(dstFilename, std::ios::binary | std::ios::trunc); std::ofstream dstFile(dstFilename, std::ios::binary | std::ios::trunc);
if (!dstFile) { if (!dstFile) {
fmt::printf("Warning: Couldn't open file %s for writing.\n", srcFilename); fmt::printf("Warning: Couldn't open file %s for writing.\n", dstFilename);
return false; return false;
} }
dstFile << srcFile.rdbuf(); dstFile << srcFile.rdbuf();

View File

@ -21,7 +21,7 @@ namespace FileUtils {
bool CreatePath(const char *path); bool CreatePath(const char *path);
bool CopyFile(const std::string &srcFilename, const std::string &dstFilename); bool CopyFile(const std::string &srcFilename, const std::string &dstFilename, bool createPath = false);
} }
#endif // !__FILE_UTILS_H__ #endif // !__FILE_UTILS_H__

View File

@ -16,4 +16,32 @@ namespace StringUtils {
return (s == PATH_WIN) ? PATH_UNIX : PATH_WIN; return (s == PATH_WIN) ? PATH_UNIX : PATH_WIN;
} }
PathSeparator GetPathSeparator() {
#if defined( __unix__ ) || defined( __APPLE__ )
return PATH_UNIX;
#else
return PATH_WIN;
#endif
}
const std::string NormalizePath(const std::string &path)
{
PathSeparator separator = GetPathSeparator();
char replace;
if (separator == PATH_WIN) {
replace = PATH_UNIX;
}
else {
replace = PATH_WIN;
}
std::string normalizedPath = path;
for (size_t s = normalizedPath.find(replace, 0); s != std::string::npos; s = normalizedPath.find(replace, s)) {
normalizedPath[s] = separator;
}
return normalizedPath;
}
} }

View File

@ -32,6 +32,10 @@ namespace StringUtils {
PathSeparator operator!(const PathSeparator &s); PathSeparator operator!(const PathSeparator &s);
PathSeparator GetPathSeparator();
const std::string NormalizePath(const std::string &path);
inline const std::string GetCleanPathString(const std::string &path, const PathSeparator separator = PATH_WIN) inline const std::string GetCleanPathString(const std::string &path, const PathSeparator separator = PATH_WIN)
{ {
std::string cleanPath = path; std::string cleanPath = path;