Skip to content

Commit b5d35f0

Browse files
committed
feat: search for solution when only the project is build
feat: write a warning when a new project id is generated fix: errors on initial run
1 parent b367d0f commit b5d35f0

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

Common/Commands/ReadIdCommand.cs

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Linq;
45
using KY.Core;
@@ -26,19 +27,19 @@ public override IGeneratorCommandResult Run()
2627
VisualStudioSolutionProject project = parser.ParseProject(this.Parameters.Project);
2728
if (project == null || project.Id == Guid.Empty)
2829
{
29-
VisualStudioSolution solution = parser.ParseSolution(this.Parameters.Solution);
30+
VisualStudioSolution solution = parser.ParseSolution(this.Parameters.Solution) ?? this.FindSolution(parser);
3031
project = solution?.Projects.FirstOrDefault(x => x.Path.EndsWith(projectFileName)) ?? project;
3132
}
3233
if (project != null && project.Id == Guid.Empty)
3334
{
3435
project.Id = Guid.NewGuid();
36+
Logger.Warning("Project has no id and solution could not be found. A new id was generated and set to project.");
3537
parser.SetProjectGuid(this.Parameters.Project, project.Id);
3638
}
3739
if (project != null && project.Name == null)
3840
{
3941
project.Name = projectFileName.Replace(Path.GetExtension(projectFileName), string.Empty);
4042
}
41-
4243
if (project == null || project.Id == Guid.Empty)
4344
{
4445
Logger.Warning($"Can not read project id. No solution for project '{this.Parameters.Project}' found. Automatic file cleanup deactivated!");
@@ -53,5 +54,41 @@ public override IGeneratorCommandResult Run()
5354

5455
return this.Success().ForceRerunOnAsync();
5556
}
57+
58+
private VisualStudioSolution FindSolution(VisualStudioParser parser, int levelToGoUp = 3)
59+
{
60+
Stopwatch stopwatch = new();
61+
stopwatch.Start();
62+
try
63+
{
64+
string projectFileName = FileSystem.GetFileName(this.Parameters.Project);
65+
string solutionDirectory = this.Parameters.Project;
66+
while (levelToGoUp > 0)
67+
{
68+
solutionDirectory = FileSystem.Parent(solutionDirectory);
69+
string[] solutionFiles = FileSystem.GetFiles(solutionDirectory, "*.sln");
70+
foreach (string solutionFile in solutionFiles)
71+
{
72+
VisualStudioSolution solution = parser.ParseSolution(solutionFile);
73+
if (solution.Projects.Any(x => x.Path.EndsWith(projectFileName)))
74+
{
75+
return solution;
76+
}
77+
}
78+
levelToGoUp--;
79+
}
80+
}
81+
catch (Exception exception)
82+
{
83+
Logger.Warning($"Can not find solution for project '{this.Parameters.Project}'. {exception.Message}");
84+
}
85+
finally
86+
{
87+
stopwatch.Stop();
88+
Logger.Trace($"Searching for solution in {(stopwatch.ElapsedMilliseconds >= 1 ? stopwatch.ElapsedMilliseconds.ToString() : "<1")} ms");
89+
Logger.Trace("To skip the previous step, build the solution instead the project or set a <ProjectGuid> in the project file.");
90+
}
91+
return null;
92+
}
5693
}
5794
}

Common/Licensing/LicenseService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public void Check()
2929
{
3030
try
3131
{
32-
Guid licenseId = this.globalSettingsService.Read()?.License ?? Guid.Empty;
32+
Guid licenseId = this.globalSettingsService.Read().License ;
3333
SignedLicense license = this.globalLicenseService.Read();
34-
if (license.License.Id == licenseId && (license.License.ValidUntil.Date - DateTime.Today).TotalDays >= 7 && license.Validate())
34+
if (license.License != null && license.License.Id == licenseId && (license.License.ValidUntil.Date - DateTime.Today).TotalDays >= 7 && license.Validate())
3535
{
3636
this.globalLicenseService.Set(license);
3737
}
@@ -58,7 +58,7 @@ public void WaitOrKill()
5858
// If the cached license is valid, we can terminate faster
5959
if (this.IsValid)
6060
{
61-
Logger.Trace($"License check skipped. Stored license is valid until {this.ValidUntil}");
61+
Logger.Trace($"License check skipped. Stored license is valid until {this.ValidUntil.ToShortDateString()}");
6262
this.waitForCheck.Set();
6363
}
6464
else

0 commit comments

Comments
 (0)