Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ copyright "Copyright © 2022, Dmytro Katyukha"
license "MPL-2.0"
targetPath "./build/"
targetType "library"
dflags "-preview=in" "-preview=dip1000"

configuration "library" {
}
Expand Down
50 changes: 18 additions & 32 deletions source/thepath/path.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ static private import std.stdio;
static private import std.process;
static private import std.algorithm;
private import std.typecons: Nullable, nullable;
private import std.path: expandTilde;
private import std.format: format;
private import std.exception: enforce;
private import thepath.utils: createTempPath, createTempDirectory;
private import thepath.utils: createTempPath, createTempDirectory, expandTilde;
private import thepath.exception: PathException;


Expand All @@ -26,7 +25,7 @@ private import thepath.exception: PathException;
* path = string representation of path to point to
**/
pure nothrow this(in string path) {
_path = path;
_path = path.dup;
}

/** Constructor that allows to build path from segments
Expand All @@ -51,7 +50,7 @@ private import thepath.exception: PathException;
/** Check if path is valid.
* Returns: true if this is valid path.
**/
pure nothrow bool isValid() const {
pure nothrow auto isValid() const {
return std.path.isValidPath(_path);
}

Expand All @@ -69,7 +68,7 @@ private import thepath.exception: PathException;
}

/// Check if path is absolute
pure nothrow bool isAbsolute() const {
pure nothrow auto isAbsolute() const {
return std.path.isAbsolute(_path);
}

Expand All @@ -89,7 +88,7 @@ private import thepath.exception: PathException;
}

/// Check if path starts at root directory (or drive letter)
pure nothrow bool isRooted() const {
pure nothrow auto isRooted() const {
return std.path.isRooted(_path);
}

Expand Down Expand Up @@ -166,17 +165,17 @@ private import thepath.exception: PathException;
}

/// Determine if path is file.
bool isFile() const {
auto isFile() const {
return std.file.isFile(_path.expandTilde);
}

/// Determine if path is directory.
bool isDir() const {
auto isDir() const {
return std.file.isDir(_path.expandTilde);
}

/// Determine if path is symlink
bool isSymlink() const {
auto isSymlink() const {
return std.file.isSymlink(_path.expandTilde);
}

Expand All @@ -188,12 +187,6 @@ private import thepath.exception: PathException;
return std.path.filenameCmp(this._path, other._path);
}

/// ditto
pure nothrow int opCmp(in ref Path other) const
{
return std.path.filenameCmp(this._path, other._path);
}

/// Test comparison operators
unittest {
import dshould;
Expand Down Expand Up @@ -235,12 +228,6 @@ private import thepath.exception: PathException;
return opCmp(other) == 0;
}

/// ditto
pure nothrow bool opEquals(in ref Path other) const
{
return opCmp(other) == 0;
}

/// Test equality comparisons
unittest {
import dshould;
Expand Down Expand Up @@ -344,7 +331,7 @@ private import thepath.exception: PathException;
}

/// Check if path exists
nothrow bool exists() const {
nothrow auto exists() const {
return std.file.exists(_path.expandTilde);
}

Expand All @@ -371,7 +358,7 @@ private import thepath.exception: PathException;
}

/// Return path as string
pure nothrow string toString() const {
pure nothrow auto toString() const {
return _path;
}

Expand Down Expand Up @@ -408,7 +395,7 @@ private import thepath.exception: PathException;
* normalization of path.
* Throws: Exception if the specified base directory is not absolute.
**/
Path toAbsolute() const {
auto toAbsolute() const {
return Path(
std.path.buildNormalizedPath(
std.path.absolutePath(_path.expandTilde)));
Expand Down Expand Up @@ -449,7 +436,7 @@ private import thepath.exception: PathException;
/** Expand tilde (~) in current path.
* Returns: New path with tilde expaded
**/
nothrow Path expandTilde() const {
nothrow auto expandTilde() const {
return Path(std.path.expandTilde(_path));
}

Expand Down Expand Up @@ -478,12 +465,11 @@ private import thepath.exception: PathException;
* New path build from current path and provided segments
**/
pure nothrow auto join(in string[] segments...) const {
auto args=[_path] ~ segments;
return Path(std.path.buildPath(args));
return Path(std.path.buildPath([_path.dup] ~ segments));
}

/// ditto
pure nothrow Path join(in Path[] segments...) const {
pure nothrow auto join(in Path[] segments...) const {
string[] args=[];
foreach(p; segments) args ~= p._path;
return this.join(args);
Expand Down Expand Up @@ -1611,7 +1597,7 @@ private import thepath.exception: PathException;
// TODO: Add support to move files between filesystems
enforce!PathException(
!to.exists,
"Destination %s already exists!".format(to));
"Destination %s already exists!".format(to.toString));
return std.file.rename(_path.expandTilde, to._path.expandTilde);
}

Expand Down Expand Up @@ -2169,11 +2155,11 @@ private import thepath.exception: PathException;
auto execute(in string[] args=[],
in string[string] env=null,
in Nullable!Path workDir=Nullable!Path.init,
in std.process.Config config=std.process.Config.none,
const std.process.Config config=std.process.Config.none,
in size_t maxOutput=size_t.max) const {
return std.process.execute(
this._path ~ args,
env,
env.dup,
config,
maxOutput,
(workDir.isNull) ? null : workDir.get.toString);
Expand All @@ -2183,7 +2169,7 @@ private import thepath.exception: PathException;
auto execute(in string[] args,
in string[string] env,
in Path workDir,
in std.process.Config config=std.process.Config.none,
const std.process.Config config=std.process.Config.none,
in size_t maxOutput=size_t.max) const {
return execute(args, env, Nullable!Path(workDir), config, maxOutput);
}
Expand Down
15 changes: 11 additions & 4 deletions source/thepath/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ private import thepath.path: Path;
private immutable ushort MAX_TMP_ATTEMPTS = 1000;


// Custom implementation to handle scoped input path
package @safe nothrow string expandTilde(in string inputPath) {
// TODO: find the way to remove dup here
return std.path.expandTilde(inputPath.dup);
}


/** Create temporary directory
* Note, that caller is responsible to remove created directory.
* The temp directory will be created inside specified path.
Expand Down Expand Up @@ -43,9 +50,9 @@ private immutable ushort MAX_TMP_ATTEMPTS = 1000;
// Prepare template for mkdtemp function.
// It have to be mutable array of chars ended with zero to be compatibale
// with mkdtemp function.
scope char[] tempname_str = std.path.buildNormalizedPath(
std.path.expandTilde(path),
prefix ~ "-XXXXXX").dup ~ "\0";
char[] tempname_str = std.path.buildNormalizedPath(
expandTilde(path),
prefix ~ "-XXXXXX").dup ~ '\0';

// mkdtemp will modify tempname_str directly.
// and res will be pointer to tempname_str in case of success.
Expand All @@ -70,7 +77,7 @@ private immutable ushort MAX_TMP_ATTEMPTS = 1000;
string suffix = "-";
for(ubyte i; i<6; i++) suffix ~= letters[uniform(0, $)];
return std.path.buildNormalizedPath(
std.path.expandTilde(path), prefix ~ suffix);
expandTilde(path), prefix ~ suffix);
}

// Make trusted funcs to get windows error code and msg
Expand Down