Skip to content

Commit

Permalink
Merge pull request #3269 from d-torrance/change-directory
Browse files Browse the repository at this point in the history
Export changeDirectory
  • Loading branch information
DanGrayson authored Jun 5, 2024
2 parents da09945 + bbbd5e1 commit 77ac190
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
20 changes: 15 additions & 5 deletions M2/Macaulay2/d/actors5.d
Original file line number Diff line number Diff line change
Expand Up @@ -1316,11 +1316,6 @@ readlinkfun(e:Expr):Expr := (
else WrongArgString());
setupfun("readlink",readlinkfun);

changeDirectory(e:Expr):Expr := (
when e is filename:stringCell do if chdir(filename.v) == -1 then buildErrorPacket(syscallErrorMessage("changing directory")) else nullE
else WrongArgString());
setupfun("changeDirectory",changeDirectory);

realpathfun(e:Expr):Expr := (
when e is f:stringCell do (
when realpath(expandFileName(f.v))
Expand Down Expand Up @@ -1732,6 +1727,21 @@ getcwdfun(e:Expr):Expr := ( -- this has to be a function, because getcwd
else WrongNumArgs(0));
setupfun("currentDirectory",getcwdfun);

changeDirectory(dir:string):Expr := (
if chdir(expandFileName(dir)) == -1
then buildErrorPacket(syscallErrorMessage("changing directory"))
else getcwdfun(emptySequenceE));

changeDirectory(e:Expr):Expr := (
when e
is filename:stringCell do changeDirectory(filename.v)
is a:Sequence do (
if length(a) == 0
then changeDirectory("~")
else WrongArg("a string or ()"))
else WrongArg("a string or ()"));
setupfun("changeDirectory",changeDirectory);

export debuggerHook := nullE;

backtraceS := dummySymbol;
Expand Down
1 change: 1 addition & 0 deletions M2/Macaulay2/m2/exports.m2
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ export {
"centerString",
"chainComplex",
"changeBase",
"changeDirectory",
"char",
"characters",
"check",
Expand Down
3 changes: 2 additions & 1 deletion M2/Macaulay2/m2/files.m2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ makeDirectory String := name -> ( -- make the whole path, too
name = minimizeFilename name;
parts := separate("/", name);
if last parts === "" then parts = drop(parts,-1);
makeDir fold((a,b) -> ( makeDir a; a|"/"|b ), parts))
makeDir fold((a,b) -> ( makeDir a; a|"/"|b ), parts);
name)

copyFile = method(Options => new OptionTable from { Verbose => false, UpdateOnly => false })
copyFile(String,String) := opts -> (src,tar) -> (
Expand Down
34 changes: 33 additions & 1 deletion M2/Macaulay2/packages/Macaulay2Doc/system.m2
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,39 @@ document {
///,
PARA {
"If a component of the path to the current directory no longer exist, an error will be signalled."
}
},
SeeAlso => {changeDirectory}
}

doc ///
Key
changeDirectory
Headline
change the current working directory
Usage
changeDirectory dir
Inputs
dir:String
Outputs
:String -- the new working directory
Description
Text
Change the current working directory to @VAR "dir"@.
Example
dir = temporaryFileName()
makeDirectory dir
changeDirectory dir
currentDirectory()
Text
If @VAR "dir"@ is omitted, then the current working directory
is changed to the user's home directory.
Example
changeDirectory()
currentDirectory()
SeeAlso
currentDirectory
///

document {
Key => exec,
Headline => "execute another program",
Expand Down Expand Up @@ -1192,6 +1223,7 @@ document {
Headline => "make a directory",
Usage => "makeDirectory dir",
Inputs => { "dir" => String => "a path to the desired directory" },
Outputs => { String => "the name of the newly made directory" },
Consequences => { { "the directory is made, with as many new path components as needed" } },
EXAMPLE lines ///
dir = temporaryFileName()
Expand Down
8 changes: 8 additions & 0 deletions M2/Macaulay2/tests/normal/files.m2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- TODO: add more unit tests for file operations

dir = temporaryFileName()
assert(changeDirectory makeDirectory dir == dir | "/")
assert(currentDirectory() == dir | "/")
assert(changeDirectory() == homeDirectory)
assert(currentDirectory() == homeDirectory)
removeDirectory dir

0 comments on commit 77ac190

Please sign in to comment.