forked from LnL7/nix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out
lookupExecutable
and other PATH improvments
This ended up motivating a good deal of other infra improvements in order to get Windows right.
- Loading branch information
1 parent
1271732
commit 57c9d8a
Showing
22 changed files
with
286 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "environment-variables.hh" | ||
#include "executable-path.hh" | ||
#include "strings-inline.hh" | ||
#include "util.hh" | ||
|
||
namespace nix { | ||
|
||
namespace fs = std::filesystem; | ||
|
||
constexpr static const OsStringView pATH_separator = | ||
#ifdef WIN32 | ||
L";" | ||
#else | ||
":" | ||
#endif | ||
; | ||
|
||
std::vector<fs::path> parsePATH() | ||
{ | ||
return parsePATH(getEnvNative(PATHNG_LITERAL("PATH")).value_or(PATHNG_LITERAL(""))); | ||
} | ||
|
||
std::vector<fs::path> parsePATH(const OsString & path) | ||
{ | ||
auto strings = basicTokenizeString<std::list<OsString>, OsString::value_type>(path, pATH_separator); | ||
|
||
std::vector<fs::path> ret; | ||
ret.reserve(strings.size()); | ||
|
||
std::transform( | ||
std::make_move_iterator(strings.begin()), | ||
std::make_move_iterator(strings.end()), | ||
std::back_inserter(ret), | ||
[](auto && str) { return fs::path{std::move(str)}; }); | ||
|
||
return ret; | ||
} | ||
|
||
OsString renderPATH(const std::vector<fs::path> & path) | ||
{ | ||
std::vector<PathViewNG> path2; | ||
for (auto && p : path) | ||
path2.push_back(p.native()); | ||
return basicConcatStringsSep(pATH_separator, path2); | ||
} | ||
|
||
std::optional<fs::path> lookupExecutable(const fs::path & exe) | ||
{ | ||
// Check if 'exe' contains any directory separators | ||
if (exe.has_root_path() || exe.has_relative_path()) { | ||
if (isExecutable(exe)) | ||
return std::filesystem::canonical(exe); | ||
else | ||
return std::nullopt; | ||
} | ||
|
||
for (auto & dir : parsePATH()) { | ||
auto candidate = dir / exe; | ||
if (isExecutable(candidate)) | ||
return std::filesystem::canonical(candidate); | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
} // namespace nix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
///@file | ||
|
||
#include "file-system.hh" | ||
|
||
namespace nix { | ||
|
||
/** | ||
* Parse the `PATH` environment variable into a list of paths. | ||
* | ||
* On Unix we split on `:`, on Windows we split on `;`. | ||
*/ | ||
std::vector<std::filesystem::path> parsePATH(); | ||
|
||
/** | ||
* Use the given string instead of the actual environment variable. | ||
*/ | ||
std::vector<std::filesystem::path> parsePATH(const OsString & path); | ||
|
||
/** | ||
* Opposite of `parsePATH` | ||
*/ | ||
OsString renderPATH(const std::vector<std::filesystem::path> & path); | ||
|
||
/** | ||
* Lookup an executable. | ||
* | ||
* If it is just a name, lookup in the `PATH` environment variable. Otherwise | ||
* (if it is an absolute path or a relative path containing a directory | ||
* separator) keep as is. | ||
* | ||
* In either case, then canonicalize the result if the path exists and is | ||
* executable, otherwise return `std::nulllopt`. | ||
*/ | ||
std::optional<std::filesystem::path> lookupExecutable(const std::filesystem::path & exe); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.