Skip to content

Commit 49f2228

Browse files
authored
Fix usage of relative paths (#197)
1 parent 3873045 commit 49f2228

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

nanoFirmwareFlasher.Library/Esp32Operations.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
303303
return ExitCodes.E9012;
304304
}
305305

306+
// make sure path is absolute
307+
clrFile = Utilities.MakePathAbsolute(
308+
Environment.CurrentDirectory,
309+
clrFile);
310+
306311
firmware.Verbosity = VerbosityLevel.Quiet;
307312
}
308313

nanoFirmwareFlasher.Library/JLinkCli.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,19 @@ public ExitCodes ExecuteFlashBinFiles(
228228
int index = 0;
229229
foreach (string binFile in files)
230230
{
231+
// make sure path is absolute
232+
var binFilePath = Utilities.MakePathAbsolute(
233+
Environment.CurrentDirectory,
234+
binFile);
235+
231236
if (Verbosity > VerbosityLevel.Normal)
232237
{
233238
Console.ForegroundColor = ConsoleColor.Cyan;
234-
Console.WriteLine($"{Path.GetFileName(binFile)} @ {addresses.ElementAt(index)}");
239+
Console.WriteLine($"{Path.GetFileName(binFilePath)} @ {addresses.ElementAt(index)}");
235240
}
236241

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

241246
// create file

nanoFirmwareFlasher.Library/StmDeviceBase.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Diagnostics;
99
using System.IO;
10+
using System.IO.Compression;
1011
using System.Linq;
1112
using System.Runtime.InteropServices;
1213
using System.Text;
@@ -235,13 +236,18 @@ public ExitCodes ExecuteFlashHexFiles(
235236
// program HEX file(s)
236237
foreach (string hexFile in files)
237238
{
239+
// make sure path is absolute
240+
var hexFilePath = Utilities.MakePathAbsolute(
241+
Environment.CurrentDirectory,
242+
hexFile);
243+
238244
if (Verbosity >= VerbosityLevel.Detailed)
239245
{
240246
Console.ForegroundColor = ConsoleColor.Yellow;
241247
Console.WriteLine($"{Path.GetFileName(hexFile)}");
242248
}
243249

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

246252
if (!cliOutput.Contains("File download complete"))
247253
{
@@ -336,13 +342,18 @@ public ExitCodes ExecuteFlashBinFiles(
336342
int index = 0;
337343
foreach (string binFile in files)
338344
{
345+
// make sure path is absolute
346+
var binFilePath = Utilities.MakePathAbsolute(
347+
Environment.CurrentDirectory,
348+
binFile);
349+
339350
if (Verbosity >= VerbosityLevel.Detailed)
340351
{
341352
Console.ForegroundColor = ConsoleColor.Cyan;
342-
Console.WriteLine($"{Path.GetFileName(binFile)} @ {addresses.ElementAt(index)}");
353+
Console.WriteLine($"{Path.GetFileName(binFilePath)} @ {addresses.ElementAt(index)}");
343354
}
344355

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

347358
if (!cliOutput.Contains("Programming Complete."))
348359
{

nanoFirmwareFlasher.Library/Utilities.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.IO;
77
using System.Reflection;
8+
using System.Text.RegularExpressions;
89

910
namespace nanoFramework.Tools.FirmwareFlasher
1011
{
@@ -19,5 +20,29 @@ static Utilities()
1920
var fullPath = Path.GetFullPath(codeBase);
2021
ExecutingPath = Path.GetDirectoryName(fullPath);
2122
}
23+
24+
25+
/// <summary>
26+
/// Takes a pathname that MAY be relative and makes it absolute. If the path is already absolute, it is left alone.
27+
/// </summary>
28+
public static string MakePathAbsolute(
29+
string baseFolder,
30+
string possiblyRelativePathname)
31+
{
32+
// Should work on UN*X or Windows
33+
string path;
34+
35+
if (Regex.IsMatch(possiblyRelativePathname, @"^(/|[a-zA-Z]:)"))
36+
{
37+
path = possiblyRelativePathname;
38+
}
39+
else
40+
{
41+
path = Path.Combine(baseFolder, possiblyRelativePathname);
42+
}
43+
44+
return path.Replace(@"\.\", @"\");
45+
}
46+
2247
}
2348
}

0 commit comments

Comments
 (0)