Skip to content

Commit

Permalink
Ensure export directory paths are sane, absolute paths all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
ximion committed Feb 2, 2021
1 parent 4a2818d commit a4a60ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/app.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2017 Matthias Klumpp <[email protected]>
* Copyright (C) 2016-2021 Matthias Klumpp <[email protected]>
*
* Licensed under the GNU Lesser General Public License Version 3
*
Expand Down Expand Up @@ -54,7 +54,7 @@ Application Options:
--force Force action.
-w|--workspace Define the workspace location.
-c|--config Use the given configuration file.
-e|--exportDir Directory to export data to.";
--export-dir Override the workspace root export directory.";

version (unittest) {
void main () {}
Expand Down Expand Up @@ -96,7 +96,7 @@ void main(string[] args)
bool showVersion;
bool forceAction;
string wdir;
string edir;
string exportDir;
string configFname;

// parse command-line options
Expand All @@ -108,7 +108,7 @@ void main(string[] args)
"force", &forceAction,
"workspace|w", &wdir,
"config|c", &configFname,
"exportDir|e", &edir);
"export-dir", &exportDir);
} catch (Exception e) {
writeln ("Unable to parse parameters: ", e.msg);
exit (1);
Expand Down Expand Up @@ -143,7 +143,7 @@ void main(string[] args)
}

try {
conf.loadFromFile (configFname, wdir, edir);
conf.loadFromFile (configFname, wdir, exportDir);
} catch (Exception e) {
writefln ("Unable to load configuration: %s", e.msg);
exit (4);
Expand Down
37 changes: 25 additions & 12 deletions src/asgen/config.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2020 Matthias Klumpp <[email protected]>
* Copyright (C) 2016-2021 Matthias Klumpp <[email protected]>
*
* Licensed under the GNU Lesser General Public License Version 3
*
Expand All @@ -22,7 +22,7 @@ module asgen.config;
import std.stdio;
import std.array;
import std.string : format, toLower;
import std.path : dirName, buildPath, buildNormalizedPath;
import std.path : dirName, buildPath, buildNormalizedPath, isAbsolute, absolutePath;
import std.conv : to;
import std.json;
import std.typecons;
Expand Down Expand Up @@ -289,6 +289,8 @@ public:
// allow overriding the workspace location
if (!enforcedWorkspaceDir.empty)
workspaceDir = enforcedWorkspaceDir;
if (!workspaceDir.isAbsolute)
workspaceDir = workspaceDir.absolutePath;

this.projectName = "Unknown";
if ("ProjectName" in root)
Expand All @@ -304,18 +306,22 @@ public:
if ("HtmlBaseUrl" in root)
this.htmlBaseUrl = root["HtmlBaseUrl"].str;

// set the default export directory locations, allow people to override them in the config
exportDir = buildPath (workspaceDir, "export");

// allow overriding the export location
if (!enforcedExportDir.empty)
// set root export directory
if (enforcedExportDir.empty) {
exportDir = buildPath (workspaceDir, "export");
} else {
exportDir = enforcedExportDir;
logInfo ("Using data export directory root from the command-line: %s", exportDir);
}
if (!exportDir.isAbsolute)
exportDir = exportDir.absolutePath;

mediaExportDir = buildPath (exportDir, "media");
dataExportDir = buildPath (exportDir, "data");
hintsExportDir = buildPath (exportDir, "hints");
htmlExportDir = buildPath (exportDir, "html");
auto extraMetainfoDir = buildPath (workspaceDir, "extra-metainfo");
// set the default export directory locations, allow people to override them in the config
// (we convert the relative to absolute paths later)
mediaExportDir = "media";
dataExportDir = "data";
hintsExportDir = "hints";
htmlExportDir = "html";

if ("ExportDirs" in root) {
auto edirs = root["ExportDirs"].object;
Expand All @@ -339,7 +345,14 @@ public:
}
}

// convert export directory paths to absolute paths if necessary
mediaExportDir = mediaExportDir.isAbsolute? mediaExportDir : buildNormalizedPath (exportDir, mediaExportDir);
dataExportDir = dataExportDir.isAbsolute? dataExportDir : buildNormalizedPath (exportDir, dataExportDir);
hintsExportDir = hintsExportDir.isAbsolute? hintsExportDir : buildNormalizedPath (exportDir, hintsExportDir);
htmlExportDir = htmlExportDir.isAbsolute? htmlExportDir : buildNormalizedPath (exportDir, htmlExportDir);

// a place where external metainfo data can be injected
auto extraMetainfoDir = buildPath (workspaceDir, "extra-metainfo");
if ("ExtraMetainfoDir" in root)
extraMetainfoDir = root["ExtraMetainfoDir"].str;

Expand Down

0 comments on commit a4a60ea

Please sign in to comment.