-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add meadow update command. #576
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updates to the build actions should not be in with code changes. If this had been a stand-along PR, it would have already been merged.