-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1f21c6
commit ad9fa9c
Showing
10 changed files
with
296 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Reflection; | ||
using CliFx.Attributes; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Meadow.CLI.Commands.DeviceManagement; | ||
|
||
[Command("update", Description = Strings.Update.Description)] | ||
public class UpdateCommand : BaseCommand<UpdateCommand> | ||
{ | ||
const string MEADOW_CLI = "WildernessLabs.Meadow.CLI"; | ||
const string MEADOW_UPDATER = "Meadow.Updater"; | ||
const string CLIFX = "CliFx"; | ||
|
||
[CommandOption("version", 'v', IsRequired = false)] | ||
public string? Version { get; set; } | ||
|
||
public UpdateCommand(ILoggerFactory loggerFactory) | ||
: base(loggerFactory) | ||
{ | ||
} | ||
|
||
protected override async ValueTask ExecuteCommand() | ||
{ | ||
Logger.LogInformation(Strings.Update.Updating, MEADOW_CLI); | ||
|
||
string toVersion; | ||
if (!string.IsNullOrWhiteSpace(Version)) | ||
{ | ||
toVersion = $"v{Version}"; | ||
} | ||
else | ||
{ | ||
toVersion = "vLatest"; | ||
} | ||
; | ||
Logger.LogInformation(Strings.Update.Instruction1, MEADOW_CLI, toVersion); | ||
Logger.LogInformation(Strings.Update.Instruction2); | ||
|
||
// Path to the updater executable within the tool's output directory | ||
string meadowUpdaterPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, $"{MEADOW_UPDATER}.dll"); | ||
|
||
// Ensure the updater executable exists | ||
if (!File.Exists(meadowUpdaterPath)) | ||
{ | ||
Logger.LogError(Strings.Update.UpdaterNotFound); | ||
return; | ||
} | ||
|
||
// Copy all necessary files to a temporary location, so there aren't any access issues | ||
string tempUpdaterDir = Path.Combine(Path.GetTempPath(), MEADOW_UPDATER); | ||
if (!Directory.Exists(tempUpdaterDir)) | ||
{ | ||
Directory.CreateDirectory(tempUpdaterDir); | ||
} | ||
CopyMeadowUpdaterFiles(Path.GetDirectoryName(meadowUpdaterPath), tempUpdaterDir, MEADOW_UPDATER); | ||
|
||
// Supporting files required in the temp directory | ||
CopyMeadowUpdaterFiles(Path.GetDirectoryName(meadowUpdaterPath), tempUpdaterDir, CLIFX); | ||
|
||
string commandArguments = $"update -t {MEADOW_CLI}"; | ||
if (!string.IsNullOrWhiteSpace(Version)) | ||
{ | ||
commandArguments += $" -v {Version}"; | ||
} | ||
|
||
await AppTools.RunProcessCommand("dotnet", $"{Path.Combine(tempUpdaterDir, $"{MEADOW_UPDATER}.dll")} {commandArguments}", cancellationToken: CancellationToken); | ||
} | ||
|
||
internal static void CopyMeadowUpdaterFiles(string? sourceDirectory, string targetDirectory, string filesToCopy) | ||
{ | ||
if (sourceDirectory == null) | ||
{ | ||
return; | ||
} | ||
|
||
var toolUpdaterFiles = Directory.GetFiles(sourceDirectory, $"{filesToCopy}*"); | ||
foreach (var file in toolUpdaterFiles) | ||
{ | ||
string fileName = Path.GetFileName(file); | ||
string destFile = Path.Combine(targetDirectory, fileName); | ||
File.Copy(file, destFile, true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>11</LangVersion> | ||
<Platforms>AnyCPU</Platforms> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\MeadowCLIKey.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CliFx" Version="*" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using CliFx; | ||
using CliFx.Exceptions; | ||
using System.Diagnostics; | ||
|
||
namespace Meadow.Updater; | ||
|
||
public class Program | ||
{ | ||
public static async Task<int> Main(string[] args) | ||
{ | ||
int returnCode; | ||
|
||
try | ||
{ | ||
returnCode = await new CliApplicationBuilder() | ||
.AddCommandsFromThisAssembly() | ||
//.UseTypeActivator(serviceProvider.GetService!) | ||
.SetExecutableName("Meadow.Updater") | ||
.Build() | ||
.RunAsync(); | ||
} | ||
catch (CommandException ce) | ||
{ | ||
returnCode = ce.ExitCode; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Operation failed: {ex.Message}"); | ||
returnCode = 1; | ||
} | ||
|
||
return returnCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
namespace Meadow.Updater; | ||
|
||
public static class Strings | ||
{ | ||
public static class Update | ||
{ | ||
public const string Description = "Update the specified tool"; | ||
public const string NoToolSpecified = "No tool specified. Exiting."; | ||
} | ||
} |
Oops, something went wrong.