Skip to content

Commit

Permalink
Add dependency updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Nice3point committed Aug 29, 2024
1 parent 843ce66 commit 993e1c6
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 39 deletions.
32 changes: 0 additions & 32 deletions source/DependenciesReport/DependenciesParser.cs

This file was deleted.

7 changes: 5 additions & 2 deletions source/DependenciesReport/DependenciesReport.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SelfContained>false</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
Expand Down
81 changes: 81 additions & 0 deletions source/DependenciesReport/DependenciesTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace DependenciesReport;

public static class DependenciesTools
{
public static Dictionary<string, Dictionary<string, string>> CreateDependenciesMap(string[] directories)
{
var dependenciesMap = new Dictionary<string, Dictionary<string, string>>();
foreach (var directory in directories)
{
var assemblies = Directory.GetFiles(directory, "*.dll");

foreach (var assembly in assemblies)
{
var assemblyName = Path.GetFileName(assembly);
var assemblyVersion = AssemblyUtils.GetAssemblyVersion(assembly);

if (!dependenciesMap.TryGetValue(assemblyName, out var value))
{
value = new Dictionary<string, string>();
dependenciesMap[assemblyName] = value;
}

if (assemblyVersion != null)
{
value[Path.GetFileName(directory)] = assemblyVersion;
}
}
}

return dependenciesMap;
}

public static void UpgradeDependencies(Dictionary<string, Dictionary<string, Dictionary<string, string>>> dependenciesMaps)
{
foreach (var (yearDirectory, dependenciesMap) in dependenciesMaps)
{
foreach (var assemblyName in dependenciesMap.Keys)
{
string? maxVersion = null;
string? maxVersionDirectory = null;

foreach (var directory in dependenciesMap[assemblyName].Keys)
{
var version = dependenciesMap[assemblyName][directory];
if (maxVersion == null || CompareVersions(version, maxVersion) > 0)
{
maxVersion = version;
maxVersionDirectory = directory;
}
}

if (maxVersionDirectory is not null && maxVersion is not null)
{
var maxVersionFilePath = Path.Combine(yearDirectory, maxVersionDirectory, assemblyName);

foreach (var directory in dependenciesMap[assemblyName].Keys)
{
if (directory != maxVersionDirectory)
{
var targetFilePath = Path.Combine(yearDirectory, directory, assemblyName);
var targetVersion = dependenciesMap[assemblyName][directory];

if (CompareVersions(targetVersion, maxVersion) < 0)
{
File.Copy(maxVersionFilePath, targetFilePath, true);
Console.WriteLine($"Assembly {targetFilePath} was upgraded from version {targetVersion} to version {maxVersion}.");
}
}
}
}
}
}
}

private static int CompareVersions(string version1, string version2)
{
var originVersion = new Version(version1);
var targetVersion = new Version(version2);
return originVersion.CompareTo(targetVersion);
}
}
38 changes: 33 additions & 5 deletions source/DependenciesReport/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,50 @@
var addinsPath = Path.Combine(userFolder, "Autodesk", "Revit", "Addins");
var yearsDirectories = Directory.GetDirectories(addinsPath);

var reportName = $"Dependencies report - {DateTime.Now.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo)}.txt";
var reportName = $"DependenciesReport-{DateTime.Now.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo)}.txt";
var reportPath = Path.Combine(Path.GetTempPath(), reportName);
var summaryWriter = new SummaryWriter(reportPath);

var dependenciesMaps = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
foreach (var yearDirectory in yearsDirectories)
{
summaryWriter.Write($"Revit version: {Path.GetFileName(yearDirectory)}");
summaryWriter.WriteLine();

var directories = Directory.GetDirectories(yearDirectory);
var dependenciesMap = DependenciesParser.CreateDependenciesMap(directories);
var dependenciesMap = DependenciesTools.CreateDependenciesMap(directories);
var dependenciesTable = TableFormater.CreateReportTable(directories, dependenciesMap);

dependenciesMaps.Add(yearDirectory, dependenciesMap);

summaryWriter.Write(dependenciesTable.ToMinimalString());
summaryWriter.WriteLine();
}

summaryWriter.Save();
Process.Start("explorer.exe", reportPath);

Console.WriteLine($"Dependencies report saved: {reportPath}");
Console.WriteLine(@"Open the report? Y\N");
var response = Console.ReadLine();

if (string.Compare(response, "Y", StringComparison.OrdinalIgnoreCase) == 0)
{
Process.Start(new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = reportPath,
CreateNoWindow = true,
UseShellExecute = true
});
}

Console.WriteLine(@"Try to fix dependencies and upgrade to the latest version? Y\N");
response = Console.ReadLine();

if (string.Compare(response, "Y", StringComparison.OrdinalIgnoreCase) == 0)
{
DependenciesTools.UpgradeDependencies(dependenciesMaps);
Console.WriteLine("Success!");
}

Console.WriteLine("You can now close this terminal with Ctrl+C");
Console.ReadLine();

0 comments on commit 993e1c6

Please sign in to comment.