-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial commit of list command, wip tests are missing
- Loading branch information
Showing
11 changed files
with
316 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using CommandLine; | ||
using NLog.Fluent; | ||
using NuGet.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Twinpack.Core; | ||
using Twinpack.Models; | ||
using Twinpack.Protocol; | ||
|
||
namespace Twinpack.Commands | ||
{ | ||
[Verb("list", HelpText = @"Searches all uses packages sources defined ./Zeugwerk/config.json or in the first solution found in the current directory")] | ||
public class ListCommand : Command | ||
{ | ||
[Value(0, MetaName = "search term", Required = false)] | ||
public string? SearchTerm { get; set; } | ||
|
||
[Option("take", Required = false, Default = null, HelpText = "Limit the number of results to return")] | ||
public int? Take { get; set; } | ||
|
||
[Option("plc", Required = false, Default = null, HelpText = "Filter for specific plcs, by default all plcs are considered")] | ||
public IEnumerable<string> PlcFilter { get; set; } | ||
|
||
[Option("outdated", Required = false, Default = null, HelpText = "Only show outdated packages")] | ||
public bool Outdated { get; set; } | ||
|
||
public override async Task<int> ExecuteAsync() | ||
{ | ||
await PackagingServerRegistry.InitializeAsync(); | ||
_twinpack = new TwinpackService(PackagingServerRegistry.Servers); | ||
|
||
await _twinpack.LoginAsync(); | ||
|
||
var rootPath = Environment.CurrentDirectory; | ||
var config = ConfigFactory.Load(rootPath); | ||
|
||
if (config == null) | ||
{ | ||
config = await ConfigFactory.CreateFromSolutionFileAsync( | ||
rootPath, | ||
continueWithoutSolution: false, | ||
packageServers: PackagingServerRegistry.Servers.Where(x => x.Connected), | ||
plcTypeFilter: null); | ||
} | ||
|
||
foreach (var project in config.Projects) | ||
{ | ||
project.Plcs = project.Plcs.Where(x => !PlcFilter.Any() || PlcFilter.Contains(x.Name)).ToList(); | ||
} | ||
|
||
var packages = await _twinpack.RetrieveInstalledPackagesAsync(config, SearchTerm); | ||
foreach (var package in packages.Where(x => !Outdated || x.IsUpdateable)) | ||
{ | ||
Console.WriteLine($"{package.Name} {package.InstalledVersion} {(package.IsUpdateable ? $"-> {package.UpdateVersion}" : "")}"); | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
} |
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,120 @@ | ||
using EnvDTE80; | ||
using System; | ||
using TCatSysManagerLib; | ||
|
||
namespace Twinpack.Core | ||
{ | ||
public class AutomationInterfaceService | ||
{ | ||
DTE2 _dte; | ||
|
||
public AutomationInterfaceService(DTE2 dte) | ||
{ | ||
_dte = dte; | ||
} | ||
|
||
public string ResolveEffectiveVersion(string projectName, string placeholderName) | ||
{ | ||
ResolvePlaceholder(LibraryManager(projectName), placeholderName, out _, out string effectiveVersion); | ||
|
||
return effectiveVersion; | ||
} | ||
|
||
private static ITcPlcLibrary ResolvePlaceholder(ITcPlcLibraryManager libManager, string placeholderName, out string distributorName, out string effectiveVersion) | ||
{ | ||
// Try to remove the already existing reference | ||
foreach (ITcPlcLibRef item in libManager.References) | ||
{ | ||
string itemPlaceholderName; | ||
ITcPlcLibrary plcLibrary; | ||
|
||
try | ||
{ | ||
ITcPlcPlaceholderRef2 plcPlaceholder; // this will throw if the library is currently not installed | ||
plcPlaceholder = (ITcPlcPlaceholderRef2)item; | ||
|
||
itemPlaceholderName = plcPlaceholder.PlaceholderName; | ||
|
||
if (plcPlaceholder.EffectiveResolution != null) | ||
plcLibrary = plcPlaceholder.EffectiveResolution; | ||
else | ||
plcLibrary = plcPlaceholder.DefaultResolution; | ||
|
||
effectiveVersion = plcLibrary.Version; | ||
distributorName = plcLibrary.Distributor; | ||
} | ||
catch | ||
{ | ||
plcLibrary = (ITcPlcLibrary)item; | ||
effectiveVersion = null; | ||
itemPlaceholderName = plcLibrary.Name.Split(',')[0]; | ||
distributorName = plcLibrary.Distributor; | ||
} | ||
|
||
if (string.Equals(itemPlaceholderName, placeholderName, StringComparison.InvariantCultureIgnoreCase)) | ||
return plcLibrary; | ||
} | ||
|
||
distributorName = null; | ||
effectiveVersion = null; | ||
return null; | ||
} | ||
|
||
private ITcSysManager SystemManager(string projectName = null) | ||
{ | ||
var ready = false; | ||
while (!ready) | ||
{ | ||
ready = true; | ||
foreach (EnvDTE.Project project in _dte.Solution.Projects) | ||
{ | ||
if (project == null) | ||
ready = false; | ||
else if ((projectName == null || project?.Name == projectName) && project.Object as ITcSysManager != null) | ||
return project.Object as ITcSysManager; | ||
} | ||
|
||
if (!ready) | ||
System.Threading.Thread.Sleep(1000); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private ITcPlcLibraryManager LibraryManager(string projectName = null) | ||
{ | ||
var systemManager = SystemManager(projectName); | ||
|
||
if (projectName == null) | ||
{ | ||
var plcConfiguration = systemManager.LookupTreeItem("TIPC"); | ||
for (var j = 1; j <= plcConfiguration.ChildCount; j++) | ||
{ | ||
var plc = (plcConfiguration.Child[j] as ITcProjectRoot)?.NestedProject; | ||
for (var k = 1; k <= (plc?.ChildCount ?? 0); k++) | ||
{ | ||
if (plc.Child[k] as ITcPlcLibraryManager != null) | ||
{ | ||
return plc.Child[k] as ITcPlcLibraryManager; | ||
} | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
var projectRoot = systemManager.LookupTreeItem($"TIPC^{projectName}"); | ||
var plc = (projectRoot as ITcProjectRoot)?.NestedProject; | ||
for (var k = 1; k <= (plc?.ChildCount ?? 0); k++) | ||
{ | ||
if (plc.Child[k] as ITcPlcLibraryManager != null) | ||
{ | ||
return plc.Child[k] as ITcPlcLibraryManager; | ||
} | ||
} | ||
|
||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.