Skip to content

Commit

Permalink
Fix usage of relative paths (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Dec 19, 2022
1 parent 3873045 commit 49f2228
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
5 changes: 5 additions & 0 deletions nanoFirmwareFlasher.Library/Esp32Operations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
return ExitCodes.E9012;
}

// make sure path is absolute
clrFile = Utilities.MakePathAbsolute(
Environment.CurrentDirectory,
clrFile);

firmware.Verbosity = VerbosityLevel.Quiet;
}

Expand Down
9 changes: 7 additions & 2 deletions nanoFirmwareFlasher.Library/JLinkCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,19 @@ public ExitCodes ExecuteFlashBinFiles(
int index = 0;
foreach (string binFile in files)
{
// make sure path is absolute
var binFilePath = Utilities.MakePathAbsolute(
Environment.CurrentDirectory,
binFile);

if (Verbosity > VerbosityLevel.Normal)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"{Path.GetFileName(binFile)} @ {addresses.ElementAt(index)}");
Console.WriteLine($"{Path.GetFileName(binFilePath)} @ {addresses.ElementAt(index)}");
}

// compose JLink command file
var jlinkCmdContent = FlashFileCommandTemplate.Replace(FilePathToken, binFile).Replace(FlashAddressToken, addresses.ElementAt(index++));
var jlinkCmdContent = FlashFileCommandTemplate.Replace(FilePathToken, binFilePath).Replace(FlashAddressToken, addresses.ElementAt(index++));
var jlinkCmdFilePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.jlink");

// create file
Expand Down
17 changes: 14 additions & 3 deletions nanoFirmwareFlasher.Library/StmDeviceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -235,13 +236,18 @@ public ExitCodes ExecuteFlashHexFiles(
// program HEX file(s)
foreach (string hexFile in files)
{
// make sure path is absolute
var hexFilePath = Utilities.MakePathAbsolute(
Environment.CurrentDirectory,
hexFile);

if (Verbosity >= VerbosityLevel.Detailed)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{Path.GetFileName(hexFile)}");
}

var cliOutput = RunSTM32ProgrammerCLI($"-c {connectDetails} -w \"{hexFile}\"");
var cliOutput = RunSTM32ProgrammerCLI($"-c {connectDetails} -w \"{hexFilePath}\"");

if (!cliOutput.Contains("File download complete"))
{
Expand Down Expand Up @@ -336,13 +342,18 @@ public ExitCodes ExecuteFlashBinFiles(
int index = 0;
foreach (string binFile in files)
{
// make sure path is absolute
var binFilePath = Utilities.MakePathAbsolute(
Environment.CurrentDirectory,
binFile);

if (Verbosity >= VerbosityLevel.Detailed)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"{Path.GetFileName(binFile)} @ {addresses.ElementAt(index)}");
Console.WriteLine($"{Path.GetFileName(binFilePath)} @ {addresses.ElementAt(index)}");
}

var cliOutput = RunSTM32ProgrammerCLI($"-c {connectDetails} mode=UR -w \"{binFile}\" {addresses.ElementAt(index++)}");
var cliOutput = RunSTM32ProgrammerCLI($"-c {connectDetails} mode=UR -w \"{binFilePath}\" {addresses.ElementAt(index++)}");

if (!cliOutput.Contains("Programming Complete."))
{
Expand Down
25 changes: 25 additions & 0 deletions nanoFirmwareFlasher.Library/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace nanoFramework.Tools.FirmwareFlasher
{
Expand All @@ -19,5 +20,29 @@ static Utilities()
var fullPath = Path.GetFullPath(codeBase);
ExecutingPath = Path.GetDirectoryName(fullPath);
}


/// <summary>
/// Takes a pathname that MAY be relative and makes it absolute. If the path is already absolute, it is left alone.
/// </summary>
public static string MakePathAbsolute(
string baseFolder,
string possiblyRelativePathname)
{
// Should work on UN*X or Windows
string path;

if (Regex.IsMatch(possiblyRelativePathname, @"^(/|[a-zA-Z]:)"))
{
path = possiblyRelativePathname;
}
else
{
path = Path.Combine(baseFolder, possiblyRelativePathname);
}

return path.Replace(@"\.\", @"\");
}

}
}

0 comments on commit 49f2228

Please sign in to comment.