Merge pull request #4692 from ethereum/devcore-path

Move absolutePath/sanitizePath helpers from CompilerStack to libdevcore
This commit is contained in:
chriseth 2018-08-07 16:15:49 +02:00 committed by GitHub
commit 901550e473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 28 deletions

View File

@ -187,3 +187,23 @@ boost::filesystem::path dev::weaklyCanonicalFilesystemPath(boost::filesystem::pa
return head / tail; return head / tail;
} }
} }
string dev::absolutePath(string const& _path, string const& _reference)
{
boost::filesystem::path p(_path);
// Anything that does not start with `.` is an absolute path.
if (p.begin() == p.end() || (*p.begin() != "." && *p.begin() != ".."))
return _path;
boost::filesystem::path result(_reference);
result.remove_filename();
for (boost::filesystem::path::iterator it = p.begin(); it != p.end(); ++it)
if (*it == "..")
result = result.parent_path();
else if (*it != ".")
result /= *it;
return result.generic_string();
}
string dev::sanitizePath(string const& _path) {
return boost::filesystem::path(_path).generic_string();
}

View File

@ -62,4 +62,10 @@ std::string toString(_T const& _t)
/// Should be replaced by the boost implementation as soon as support for boost<1.60 can be dropped. /// Should be replaced by the boost implementation as soon as support for boost<1.60 can be dropped.
boost::filesystem::path weaklyCanonicalFilesystemPath(boost::filesystem::path const &_path); boost::filesystem::path weaklyCanonicalFilesystemPath(boost::filesystem::path const &_path);
/// @returns the absolute path corresponding to @a _path relative to @a _reference.
std::string absolutePath(std::string const& _path, std::string const& _reference);
/// Helper function to return path converted strings.
std::string sanitizePath(std::string const& _path);
} }

View File

@ -577,7 +577,7 @@ StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string
for (auto const& node: _ast.nodes()) for (auto const& node: _ast.nodes())
if (ImportDirective const* import = dynamic_cast<ImportDirective*>(node.get())) if (ImportDirective const* import = dynamic_cast<ImportDirective*>(node.get()))
{ {
string importPath = absolutePath(import->path(), _sourcePath); string importPath = dev::absolutePath(import->path(), _sourcePath);
// The current value of `path` is the absolute path as seen from this source file. // The current value of `path` is the absolute path as seen from this source file.
// We first have to apply remappings before we can store the actual absolute path // We first have to apply remappings before we can store the actual absolute path
// as seen globally. // as seen globally.
@ -620,8 +620,8 @@ string CompilerStack::applyRemapping(string const& _path, string const& _context
for (auto const& redir: m_remappings) for (auto const& redir: m_remappings)
{ {
string context = sanitizePath(redir.context); string context = dev::sanitizePath(redir.context);
string prefix = sanitizePath(redir.prefix); string prefix = dev::sanitizePath(redir.prefix);
// Skip if current context is closer // Skip if current context is closer
if (context.length() < longestContext) if (context.length() < longestContext)
@ -638,7 +638,7 @@ string CompilerStack::applyRemapping(string const& _path, string const& _context
longestContext = context.length(); longestContext = context.length();
longestPrefix = prefix.length(); longestPrefix = prefix.length();
bestMatchTarget = sanitizePath(redir.target); bestMatchTarget = dev::sanitizePath(redir.target);
} }
string path = bestMatchTarget; string path = bestMatchTarget;
path.append(_path.begin() + longestPrefix, _path.end()); path.append(_path.begin() + longestPrefix, _path.end());
@ -675,23 +675,6 @@ void CompilerStack::resolveImports()
swap(m_sourceOrder, sourceOrder); swap(m_sourceOrder, sourceOrder);
} }
string CompilerStack::absolutePath(string const& _path, string const& _reference)
{
using path = boost::filesystem::path;
path p(_path);
// Anything that does not start with `.` is an absolute path.
if (p.begin() == p.end() || (*p.begin() != "." && *p.begin() != ".."))
return _path;
path result(_reference);
result.remove_filename();
for (path::iterator it = p.begin(); it != p.end(); ++it)
if (*it == "..")
result = result.parent_path();
else if (*it != ".")
result /= *it;
return result.generic_string();
}
namespace namespace
{ {
bool onlySafeExperimentalFeaturesActivated(set<ExperimentalFeature> const& features) bool onlySafeExperimentalFeaturesActivated(set<ExperimentalFeature> const& features)

View File

@ -36,7 +36,6 @@
#include <json/json.h> #include <json/json.h>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <boost/filesystem.hpp>
#include <ostream> #include <ostream>
#include <string> #include <string>
@ -267,12 +266,6 @@ private:
std::string applyRemapping(std::string const& _path, std::string const& _context); std::string applyRemapping(std::string const& _path, std::string const& _context);
void resolveImports(); void resolveImports();
/// @returns the absolute path corresponding to @a _path relative to @a _reference.
static std::string absolutePath(std::string const& _path, std::string const& _reference);
/// Helper function to return path converted strings.
static std::string sanitizePath(std::string const& _path) { return boost::filesystem::path(_path).generic_string(); }
/// @returns true if the contract is requested to be compiled. /// @returns true if the contract is requested to be compiled.
bool isRequestedContract(ContractDefinition const& _contract) const; bool isRequestedContract(ContractDefinition const& _contract) const;