Skip to content

Commit

Permalink
Cleanup null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mkestler-rtp committed Feb 6, 2023
1 parent 4cbb927 commit e4b458b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 56 deletions.
10 changes: 9 additions & 1 deletion Disassembler/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ internal class AssemblyLoader

public AssemblyLoader(string assemblyPath) {
this.assemblyPath = assemblyPath;
context = new LocalFolderLoadContext(Path.GetDirectoryName(assemblyPath));
var assemDir = Path.GetDirectoryName(assemblyPath);
if (assemDir != null)
{
context = new LocalFolderLoadContext(assemDir);
}
else
{
throw new Exception("Unexpected null getting assembly directory.");
}
}

public Assembly? Load()
Expand Down
4 changes: 2 additions & 2 deletions Disassembler/CompiledFileLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ private List<CompilationInfo> ReadCompiledFile(string file)
}
}
}
catch (Exception ex)
catch (Exception)
{

//ignore
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion DotNETDepends/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public List<SourceType> FindSourceSymbolReferences(SourceType symbol)
{
foreach (var typeRef in type.TypeReferences)
{
if (typeRef.Name.Equals(symbol.Name) && typeRef.Namespace.Equals(symbol.Namespace))
if (typeRef.Name.Equals(symbol.Name) && symbol.Namespace.Equals(typeRef.Namespace))
{
result.Add(type);
break;
Expand Down
14 changes: 9 additions & 5 deletions DotNETDepends/PublishedWebProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ private static void AddFilesToList(List<string> paths, FileInfo[] files)
private List<string> GetSourceFiles()
{
var result = new List<string>();
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(project.FilePath));
AddFilesToList(result, dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories));
AddFilesToList(result, dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories));
AddFilesToList(result, dirInfo.GetFiles("*.razor", SearchOption.AllDirectories));
var dir = Path.GetDirectoryName(project.FilePath);
if (dir != null)
{
var dirInfo = new DirectoryInfo(dir);
AddFilesToList(result, dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories));
AddFilesToList(result, dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories));
AddFilesToList(result, dirInfo.GetFiles("*.razor", SearchOption.AllDirectories));
}
return result;
}

Expand Down Expand Up @@ -105,7 +109,7 @@ private void ReadProjectFile(IErrorReporter errorReporter)
{
if (project.FilePath != null && assemblyName != null)
{
var binRoot = Path.Combine(new string[] { Path.GetDirectoryName(project.FilePath), "bin", config });
var binRoot = Path.Combine(new string[] { Path.GetDirectoryName(project.FilePath)?? "", "bin", config });
DirectoryInfo dirInfo = new(binRoot);
var dirs = dirInfo.GetDirectories();
string? assemPath = null;
Expand Down
5 changes: 3 additions & 2 deletions DotNETDepends/RoslynProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ internal class RoslynProject
protected readonly Project project;
private readonly IErrorReporter errorReporter;
private readonly Dependencies dependencies;
private readonly string solutionRoot;
private readonly string? solutionRoot;
public RoslynProject(Project project, Dependencies dependencies, IErrorReporter errorReporter)
{
this.project = project;
this.dependencies = dependencies;
this.errorReporter = errorReporter;
solutionRoot = Path.GetDirectoryName(project.Solution.FilePath);

}

public async Task Analyze()
{
var compilation = await project.GetCompilationAsync().ConfigureAwait(false);
if (compilation != null)
if (compilation != null && solutionRoot != null)
{

foreach (var tree in compilation.SyntaxTrees)
Expand Down
32 changes: 18 additions & 14 deletions DotNETDepends/SolutionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,25 @@ private static bool ProjectContainsNETWebFiles(Project project)
{
if (project.FilePath != null)
{
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(project.FilePath));
var cshtml = dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories);
if (cshtml.Length > 0)
var projDir = Path.GetDirectoryName(project.FilePath);
if (projDir != null)
{
return true;
}
var vbhtml = dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories);
if (vbhtml.Length > 0)
{
return true;
}
var razor = dirInfo.GetFiles("*.razor", SearchOption.AllDirectories);
if (razor.Length > 0)
{
return true;
var dirInfo = new DirectoryInfo(projDir);
var cshtml = dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories);
if (cshtml.Length > 0)
{
return true;
}
var vbhtml = dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories);
if (vbhtml.Length > 0)
{
return true;
}
var razor = dirInfo.GetFiles("*.razor", SearchOption.AllDirectories);
if (razor.Length > 0)
{
return true;
}
}
}
return false;
Expand Down
58 changes: 32 additions & 26 deletions DotNETDepends/SourceTypeBuilderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal abstract class SourceTypeBuilderBase : SourceTypeBuilder
{
return null;
}
return FindNamespaceFromViewImports(Path.GetDirectoryName(startDir), projectDir, importFileName, importFileExtension, out typeRoot);
return FindNamespaceFromViewImports(Path.GetDirectoryName(startDir) ?? "", projectDir, importFileName, importFileExtension, out typeRoot);

}
/*
Expand All @@ -89,28 +89,30 @@ protected string DetermineNamespaceFromPath(string path, string? rootNamespace,
{
var relativePath = Path.GetRelativePath(projectDir, path);
relativePath = Path.GetDirectoryName(relativePath);
if (relativePath.StartsWith(Path.DirectorySeparatorChar))
if (relativePath != null)
{
relativePath = relativePath.Substring(1);
}
if (relativePath.EndsWith(Path.DirectorySeparatorChar))
{
relativePath = relativePath[..^1];
}
relativePath = relativePath.Replace(Path.DirectorySeparatorChar, '.');
if (rootNamespace != null && rootNamespace.Length != 0)
{
if (relativePath.Length > 0)
if (relativePath.StartsWith(Path.DirectorySeparatorChar))
{
return rootNamespace + "." + relativePath;
relativePath = relativePath.Substring(1);
}
else
if (relativePath.EndsWith(Path.DirectorySeparatorChar))
{
return rootNamespace;
relativePath = relativePath[..^1];
}
relativePath = relativePath.Replace(Path.DirectorySeparatorChar, '.');
if (rootNamespace != null && rootNamespace.Length != 0)
{
if (relativePath.Length > 0)
{
return rootNamespace + "." + relativePath;
}
else
{
return rootNamespace;
}
}
}

return relativePath;
return relativePath ?? "";
}
}

Expand All @@ -124,7 +126,7 @@ public override SourceType Build(string? rootNamespace, string filePath, string
var ns = ReadNamespaceFromFile(filePath);
if (ns == null)
{
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath), projectDir, "_Imports", ".razor", out string? typeRoot);
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath) ?? "", projectDir, "_Imports", ".razor", out string? typeRoot);
ns = DetermineNamespaceFromPath(filePath, ns ?? rootNamespace, typeRoot ?? projectDir);
}
ns ??= "";
Expand All @@ -145,7 +147,7 @@ public override SourceType Build(string? rootNamespace, string filePath, string
var ns = ReadNamespaceFromFile(filePath);
if (ns == null)
{
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath), projectDir, "_ViewImports", Path.GetExtension(filePath), out string? typeRoot);
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath) ?? "", projectDir, "_ViewImports", Path.GetExtension(filePath), out string? typeRoot);
ns = DetermineNamespaceFromPath(filePath, ns ?? rootNamespace, typeRoot ?? projectDir);
}
ns ??= "";
Expand All @@ -160,15 +162,19 @@ private string GetTypeName(string filePath, string projectDir)
{
var relativeFile = Path.GetRelativePath(projectDir, filePath);
var relativeDir = Path.GetDirectoryName(relativeFile);
if (relativeDir.StartsWith(Path.DirectorySeparatorChar))
{
relativeDir = relativeDir.Substring(1);
}
if (relativeDir.EndsWith(Path.DirectorySeparatorChar))
if (relativeDir != null)
{
relativeDir = relativeDir[..^1];
if (relativeDir.StartsWith(Path.DirectorySeparatorChar))
{
relativeDir = relativeDir.Substring(1);
}
if (relativeDir.EndsWith(Path.DirectorySeparatorChar))
{
relativeDir = relativeDir[..^1];
}
return relativeDir.Replace(Path.DirectorySeparatorChar, '_') + "_" + Path.GetFileNameWithoutExtension(filePath);
}
return relativeDir.Replace(Path.DirectorySeparatorChar, '_') + "_" + Path.GetFileNameWithoutExtension(filePath);
return "";
}
}
internal class SourceTypeBuilderFactory
Expand Down
16 changes: 11 additions & 5 deletions TestDependencyReading/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ namespace TestDependencyReading
{
internal class TestUtils
{
public static string? GetFixturesPath()
public static string GetFixturesPath()
{
string workingDir = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
while(workingDir != null && !workingDir.EndsWith("TestDependencyReading")) {
workingDir= Path.GetDirectoryName(workingDir);
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
if (assembly != null && assembly.Location != null)
{
string? workingDir = Path.GetDirectoryName(assembly.Location);
while (workingDir != null && !workingDir.EndsWith("TestDependencyReading"))
{
workingDir = Path.GetDirectoryName(workingDir);
}
return Path.Combine(workingDir ?? "", "Fixtures");
}
return Path.Combine(workingDir, "Fixtures");
throw new Exception("Unable to determine fixture directory.");
}
}
}

0 comments on commit e4b458b

Please sign in to comment.