Skip to content

Commit

Permalink
std: Absolutise paths added with env::path::add
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Oct 8, 2024
1 parent d05e29f commit ee09be8
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Std/Directory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ static class Directory
{
[ElkFunction("exists")]
public static RuntimeBoolean Exists(RuntimeString path, ShellEnvironment env)
=> RuntimeBoolean.From(System.IO.Directory.Exists(env.GetAbsolutePath(path.Value)));
=> RuntimeBoolean.From(System.IO.Directory.Exists(ShellEnvironment.GetAbsolutePath(path.Value)));
}
2 changes: 1 addition & 1 deletion src/Std/Environment/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void Cd(RuntimeString? path = null, ShellEnvironment? env = null)

var absolutePath = path.Value == "-"
? System.Environment.GetEnvironmentVariable("OLDPWD") ?? ""
: env!.GetAbsolutePath(path.Value);
: ShellEnvironment.GetAbsolutePath(path.Value);

if (!System.IO.Directory.Exists(absolutePath))
throw new RuntimeException($"cd: The directory \"{path}\" does not exist");
Expand Down
4 changes: 3 additions & 1 deletion src/Std/Environment/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Elk.Exceptions;
using Elk.Std.Attributes;
using Elk.Std.DataTypes;
using Elk.Vm;

#endregion

Expand All @@ -23,7 +24,8 @@ public static class Path
[ElkFunction("add")]
public static void Add(RuntimeString path)
{
System.IO.File.AppendAllText(CommonPaths.PathFile, $"{path}\n");
var absolutePath = ShellEnvironment.GetAbsolutePath(path.Value);
System.IO.File.AppendAllText(CommonPaths.PathFile, $"{absolutePath}\n");

// Reload the path variable
var pathVar = System.Environment.GetEnvironmentVariable("PATH") ?? "";
Expand Down
2 changes: 1 addition & 1 deletion src/Std/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static class File
{
[ElkFunction("exists")]
public static RuntimeBoolean Exists(RuntimeString path, ShellEnvironment env)
=> RuntimeBoolean.From(System.IO.File.Exists(env.GetAbsolutePath(path.Value)));
=> RuntimeBoolean.From(System.IO.File.Exists(ShellEnvironment.GetAbsolutePath(path.Value)));

[ElkFunction("executableExists")]
public static RuntimeBoolean ExecutableExists(RuntimeString path)
Expand Down
8 changes: 4 additions & 4 deletions src/Std/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static class IO
/// <returns>Text content of the file at the provided path.</returns>
[ElkFunction("read", Reachability.Everywhere)]
public static RuntimeString ReadFile(RuntimeString path, ShellEnvironment env)
=> new(System.IO.File.ReadAllText(env.GetAbsolutePath(path.Value)));
=> new(System.IO.File.ReadAllText(ShellEnvironment.GetAbsolutePath(path.Value)));

/// <summary>Writes the provided text to a file, overwriting any previous content.</summary>
/// <param name="content">Text that should be written to the file</param>
Expand All @@ -37,7 +37,7 @@ public static RuntimeString ReadFile(RuntimeString path, ShellEnvironment env)
[ElkFunction("write", Reachability.Everywhere, ConsumesPipe = true)]
public static void WriteToFile(RuntimeObject content, RuntimeString path, ShellEnvironment env)
{
var absolutePath = env.GetAbsolutePath(path.Value);
var absolutePath = ShellEnvironment.GetAbsolutePath(path.Value);
if (content is RuntimePipe runtimePipe)
{
var fileInfo = new FileInfo(absolutePath);
Expand Down Expand Up @@ -73,7 +73,7 @@ public static void WriteToFile(RuntimeObject content, RuntimeString path, ShellE
[ElkFunction("append", Reachability.Everywhere)]
public static void AppendToFile(RuntimeObject content, RuntimeString path, ShellEnvironment env)
{
var absolutePath = env.GetAbsolutePath(path.Value);
var absolutePath = ShellEnvironment.GetAbsolutePath(path.Value);
var fileInfo = new FileInfo(absolutePath);

if (content is RuntimePipe runtimePipe)
Expand Down Expand Up @@ -118,7 +118,7 @@ public static void AppendToFile(RuntimeObject content, RuntimeString path, Shell
/// <returns>nil</returns>
[ElkFunction("appendEnd", Reachability.Everywhere)]
public static void AppendEnd(RuntimeString content, RuntimeString path, ShellEnvironment env)
=> System.IO.File.AppendAllText(env.GetAbsolutePath(path.Value), content.Value);
=> System.IO.File.AppendAllText(ShellEnvironment.GetAbsolutePath(path.Value), content.Value);

/// <summary>Reads the next line from the standard input stream. This is used to get input from the user in a terminal.</summary>
/// <param name="prompt">Text that should be printed before the input prompt</param>
Expand Down
2 changes: 1 addition & 1 deletion src/Vm/ShellEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static string WorkingDirectory

public string? ScriptPath { get; set; } = scriptPath;

public string GetAbsolutePath(string relativePath)
public static string GetAbsolutePath(string relativePath)
=> Path.GetFullPath(Path.Combine(WorkingDirectory, relativePath));

}

0 comments on commit ee09be8

Please sign in to comment.