Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit b8b20f8

Browse files
committed
Merge branch 'fixing-all'
# Conflicts: # Commands/ShowDeps.cs
2 parents 356efde + 8b28aa0 commit b8b20f8

11 files changed

+148
-89
lines changed

Commands/CheckDeps.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
44
using Common;
@@ -9,6 +9,7 @@ public class CheckDeps : Command
99
{
1010
private string configuration;
1111
private bool showAll;
12+
private bool findExternal;
1213
private bool showShort;
1314

1415
public CheckDeps()
@@ -29,6 +30,7 @@ protected override void ParseArgs(string[] args)
2930
configuration = (string) parsedArgs["configuration"];
3031
showAll = (bool) parsedArgs["all"];
3132
showShort = (bool) parsedArgs["short"];
33+
findExternal = (bool)parsedArgs["external"];
3234
}
3335

3436
protected override int Execute()
@@ -38,7 +40,7 @@ protected override int Execute()
3840
configuration = configuration ?? "full-build";
3941

4042
ConsoleWriter.WriteInfo($"Checking {configuration} configuration result:");
41-
var result = new DepsChecker(cwd, configuration, Helper.GetModules()).GetCheckDepsResult();
43+
var result = new DepsChecker(cwd, configuration, Helper.GetModules()).GetCheckDepsResult(findExternal);
4244
if (result.NoYamlInstallSection.Any())
4345
{
4446
ok = false;
@@ -100,6 +102,7 @@ cm check-deps [-c configName]
100102
-c/--configuration - check deps for specific configuration
101103
-a/--all - show csproj names which has bad references
102104
-s/--short - show only section with bad references
105+
-e/--external - check references to not cement modules or to current module
103106
";
104107
}
105108
}

Commands/RefCommand.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using Common;
33

44
namespace Commands
@@ -39,7 +39,7 @@ Fixes deps and references in all csproj files to correct install files
3939
4040
Usage:
4141
cm ref fix [-e]
42-
-e/--external try to fix references to not cement modules
42+
-e/--external try to fix references to not cement modules or to current module
4343
4444
Example:
4545
change <HintPath>..\..\props\libprops\bin\Release\4.0\Kontur.Core.dll</HintPath>

Commands/RefFix.cs

+14-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -79,7 +79,7 @@ private void Fix(BuildData buildInfo, List<Module> modules, HashSet<string> proc
7979
processedFiles.Add(file);
8080
fixReferenceResult.NotFound[file] = new List<string>();
8181
fixReferenceResult.Replaced[file] = new List<string>();
82-
var refs = vsParser.GetReferencesFromCsproj(file, fixExternal);
82+
var refs = vsParser.GetReferencesFromCsproj(file, buildInfo.Configuration, fixExternal);
8383
foreach (var r in refs)
8484
Fix(file, r);
8585
}
@@ -88,6 +88,9 @@ private void Fix(BuildData buildInfo, List<Module> modules, HashSet<string> proc
8888
private void Fix(string project, string reference)
8989
{
9090
var moduleName = Helper.GetRootFolder(reference);
91+
if (moduleName == rootModuleName && reference.ToLower().Contains("\\packages\\"))
92+
return;
93+
9194
if (!Directory.Exists(Path.Combine(Helper.CurrentWorkspace, moduleName)))
9295
{
9396
if (!missingModules.Contains(moduleName))
@@ -103,10 +106,10 @@ private void Fix(string project, string reference)
103106
return;
104107
}
105108

106-
var installFiles = GetAllInstallFiles(moduleName).ToList();
109+
var installFiles = InstallHelper.GetAllInstallFiles(moduleName).ToList();
107110
var withSameName = installFiles.Where(file => Path.GetFileName(file).Equals(Path.GetFileName(reference))).ToList();
108111
if (!withSameName.Any())
109-
withSameName = GetAllInstallFilesFromTree().Where(file => Path.GetFileName(file).Equals(Path.GetFileName(reference))).ToList();
112+
withSameName = InstallHelper.GetAllInstallFiles().Where(file => Path.GetFileName(file).Equals(Path.GetFileName(reference))).ToList();
110113
if (withSameName.Any(r => Path.GetFullPath(r) == Path.GetFullPath(reference)))
111114
{
112115
TryAddToDeps(reference, project);
@@ -151,26 +154,7 @@ private string UserChoseReplace(string project, string oldReference, List<string
151154
return answer;
152155
}
153156

154-
private List<string> allInstallFilesFromTree;
155-
156-
private List<string> GetAllInstallFilesFromTree()
157-
{
158-
if (allInstallFilesFromTree != null)
159-
return allInstallFilesFromTree;
160-
var deps = BuildPreparer.BuildConfigsGraph(rootModuleName, "full-build");
161-
var usedModules = deps.Select(dep => dep.Key.Name).Distinct().ToList();
162-
allInstallFilesFromTree = usedModules.SelectMany(GetAllInstallFiles).ToList();
163-
return allInstallFilesFromTree;
164-
}
165-
166-
private List<string> GetAllInstallFiles(string module)
167-
{
168-
if (!File.Exists(Path.Combine(Helper.CurrentWorkspace, module, Helper.YamlSpecFile)))
169-
return new List<string>();
170-
var configs = Yaml.ConfigurationParser(module).GetConfigurations();
171-
var result = configs.Select(config => Yaml.InstallParser(module).Get(config)).SelectMany(parser => parser.Artifacts);
172-
return result.Distinct().Select(file => Path.Combine(module, file)).ToList();
173-
}
157+
174158

175159
private void UpdateReference(string reference, string project)
176160
{
@@ -194,6 +178,8 @@ private void UpdateReference(string reference, string project)
194178
private void TryAddToDeps(string reference, string project)
195179
{
196180
var moduleDep = Helper.GetRootFolder(reference);
181+
if (moduleDep == rootModuleName)
182+
return;
197183

198184
var configs = Yaml.ConfigurationParser(moduleDep).GetConfigurations();
199185
var configsWithArtifact =
@@ -225,9 +211,9 @@ private string GetHintPath(XmlNode xmlNode)
225211

226212
class FixReferenceResult
227213
{
228-
public Dictionary<string, List<string>> NotFound = new Dictionary<string, List<string>>();
229-
public Dictionary<string, List<string>> Replaced = new Dictionary<string, List<string>>();
230-
public HashSet<string> NoYamlModules = new HashSet<string>();
214+
public readonly Dictionary<string, List<string>> NotFound = new Dictionary<string, List<string>>();
215+
public readonly Dictionary<string, List<string>> Replaced = new Dictionary<string, List<string>>();
216+
public readonly HashSet<string> NoYamlModules = new HashSet<string>();
231217

232218
public void Print()
233219
{
@@ -251,7 +237,7 @@ public void Print()
251237
{
252238
if (!NotFound[key].Any())
253239
continue;
254-
ConsoleWriter.WriteError(key + "\n\tnot found references in install/artifacts section of any dep module:");
240+
ConsoleWriter.WriteError(key + "\n\tnot found references in install/artifacts section of any module:");
255241
foreach (var value in NotFound[key])
256242
ConsoleWriter.WriteLine("\t" + value);
257243
}

Commands/ShowDeps.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
44
using Common;
@@ -130,7 +130,7 @@ private static List<string> GetOverhead(Dep dep)
130130
dep.Configuration,
131131
Helper.GetModules());
132132

133-
var overhead = checker.GetCheckDepsResult().ConfigOverhead;
133+
var overhead = checker.GetCheckDepsResult(false).ConfigOverhead;
134134
var names = overhead.Select(path => path.Split('/', '\\').FirstOrDefault()).Distinct().ToList();
135135
return overheadCache[dep] = names;
136136
}

Common/ArgumentParser.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -211,13 +211,15 @@ public static Dictionary<string, object> ParseCheckDeps(string[] args)
211211
{
212212
{"configuration", null},
213213
{"all", false},
214-
{"short", false}
214+
{"short", false},
215+
{"external", false}
215216
};
216217
var parser = new OptionSet
217218
{
218219
{"c|configuration=", conf => parsedArguments["configuration"] = conf},
219220
{"a|all", a => parsedArguments["all"] = true},
220-
{"s|short", s => parsedArguments["short"] = true}
221+
{"s|short", s => parsedArguments["short"] = true},
222+
{"e|external", e => parsedArguments["external"] = true}
221223
};
222224
var extraArgs = parser.Parse(args.Skip(1));
223225
ThrowIfHasExtraArgs(extraArgs);

Common/Common.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<Compile Include="CementExceptions.cs" />
8787
<Compile Include="CementFromServerUpdater.cs" />
8888
<Compile Include="CementSettings.cs" />
89+
<Compile Include="InstallHelper.cs" />
8990
<Compile Include="Logging\AsyncRollingFileAppender.cs" />
9091
<Compile Include="Logging\BoundedBuffer.cs" />
9192
<Compile Include="Extensions.cs" />

Common/DepsChecker.cs

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
44
using Common.YamlParsers;
@@ -11,6 +11,7 @@ public class DepsChecker
1111
private readonly DepsReferencesCollector depsRefsCollector;
1212
private readonly List<string> modules;
1313
private readonly string moduleDirectory;
14+
private readonly string moduleName;
1415

1516
public DepsChecker(string cwd, string config, List<Module> modules)
1617
{
@@ -20,9 +21,10 @@ public DepsChecker(string cwd, string config, List<Module> modules)
2021
depsRefsCollector = new DepsReferencesCollector(cwd, config);
2122
this.modules = modules.Select(m => m.Name).ToList();
2223
moduleDirectory = cwd;
24+
moduleName = Path.GetFileName(moduleDirectory);
2325
}
2426

25-
public CheckDepsResult GetCheckDepsResult()
27+
public CheckDepsResult GetCheckDepsResult(bool notOnlyCement)
2628
{
2729
var refsList = new List<ReferenceWithCsproj>();
2830
foreach (var bulid in buildData)
@@ -32,7 +34,7 @@ public CheckDepsResult GetCheckDepsResult()
3234
var vsParser = new VisualStudioProjectParser(Path.Combine(moduleDirectory, bulid.Target), modules);
3335
var files = vsParser.GetCsprojList(bulid.Configuration);
3436
var refs = files.SelectMany(file =>
35-
vsParser.GetReferencesFromCsproj(file).Select(reference => reference.Replace('/', '\\')).Select(r => new ReferenceWithCsproj(r, file)));
37+
vsParser.GetReferencesFromCsproj(file, bulid.Configuration, notOnlyCement).Select(reference => reference.Replace('/', '\\')).Select(r => new ReferenceWithCsproj(r, file)));
3638
refsList.AddRange(refs);
3739
}
3840
return GetCheckDepsResult(refsList);
@@ -64,10 +66,22 @@ private CheckDepsResult GetCheckDepsResult(List<ReferenceWithCsproj> csprojRefs)
6466
}
6567

6668
var lowerInDeps = inDeps.Select(r => r.ToLower()).ToList();
67-
var notInDeps = csprojRefs.Where(r => !lowerInDeps.Contains(r.Reference.ToLower())).ToList();
69+
var notInDeps = csprojRefs
70+
.Where(r => !lowerInDeps.Contains(r.Reference.ToLower()))
71+
.Where(r => GetModuleName(r.Reference) != moduleName)
72+
.ToList();
73+
74+
var innerRefs = csprojRefs
75+
.Where(r => GetModuleName(r.Reference) == moduleName)
76+
.Where(r => !r.Reference.ToLower().Contains("\\packages\\"))
77+
.ToList();
78+
var allInstalls = new HashSet<string>(
79+
InstallHelper.GetAllInstallFiles().Select(Path.GetFileName));
80+
notInDeps.AddRange(innerRefs.Where(i => allInstalls.Contains(Path.GetFileName(i.Reference))));
81+
6882
foreach (var r in csprojRefs)
6983
{
70-
var moduleName = r.Reference.Split('\\')[0];
84+
var moduleName = GetModuleName(r.Reference);
7185
notUsedDeps.Remove(moduleName);
7286
}
7387

@@ -76,10 +90,15 @@ private CheckDepsResult GetCheckDepsResult(List<ReferenceWithCsproj> csprojRefs)
7690
return new CheckDepsResult(notUsedDeps, notInDeps, noYamlInstall, configOverhead);
7791
}
7892

93+
private string GetModuleName(string reference)
94+
{
95+
return reference.Split('\\')[0];
96+
}
97+
7998
private void DeleteMsBuild(SortedSet<string> refs)
8099
{
81-
refs.RemoveWhere(r => r.StartsWith("msbuild/") || r.StartsWith("msbuild\\"));
82-
refs.RemoveWhere(r => r.StartsWith("nuget/") || r.StartsWith("nuget\\"));
100+
refs.RemoveWhere(r => r == "msbuild" || r.StartsWith("msbuild/") || r.StartsWith("msbuild\\"));
101+
refs.RemoveWhere(r => r == "nuget" || r.StartsWith("nuget/") || r.StartsWith("nuget\\"));
83102
}
84103
}
85104

Common/InstallHelper.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using Common.YamlParsers;
5+
6+
namespace Common
7+
{
8+
public static class InstallHelper
9+
{
10+
private static List<string> allInstallFiles;
11+
12+
public static List<string> GetAllInstallFiles()
13+
{
14+
if (allInstallFiles != null)
15+
return allInstallFiles;
16+
17+
var modules = Helper.GetModules().Select(m => m.Name).Where(Yaml.Exists).ToList();
18+
allInstallFiles = modules.SelectMany(GetAllInstallFiles).ToList();
19+
return allInstallFiles;
20+
}
21+
22+
public static List<string> GetAllInstallFiles(string module)
23+
{
24+
if (!File.Exists(Path.Combine(Helper.CurrentWorkspace, module, Helper.YamlSpecFile)))
25+
return new List<string>();
26+
var configs = Yaml.ConfigurationParser(module).GetConfigurations();
27+
var result = configs.Select(config => Yaml.InstallParser(module).Get(config)).SelectMany(parser => parser.Artifacts);
28+
return result.Distinct().Select(file => Path.Combine(module, file)).ToList();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)