Expose Imports in IProjectFile (#615)
* Simple implementation to expose Imports in IProjectFile * Change to use a custom Collection for Imports * Add a null check to fix tests * Add implementation for CopyTo * Changing GetEnumerator impl to fix tests
This commit is contained in:
Родитель
a9a1b91303
Коммит
ee9618b1fb
|
@ -41,6 +41,6 @@ namespace Microsoft.DotNet.UpgradeAssistant
|
|||
|
||||
void SetTFM(TargetFrameworkMoniker targetTFM);
|
||||
|
||||
IEnumerable<string> Imports { get; }
|
||||
ICollection<string> Imports { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.MSBuild
|
|||
// Check imports and references
|
||||
var references = project.References.Select(r => r.Name);
|
||||
|
||||
if (file.Imports.Contains(MSBuildConstants.WebApplicationTargets, StringComparer.OrdinalIgnoreCase) ||
|
||||
if ((file.Imports != null && file.Imports.Contains(MSBuildConstants.WebApplicationTargets, StringComparer.OrdinalIgnoreCase)) ||
|
||||
references.Any(r => MSBuildConstants.WebReferences.Contains(r, StringComparer.OrdinalIgnoreCase)))
|
||||
{
|
||||
components |= ProjectComponents.AspNet;
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Build.Construction;
|
||||
|
||||
namespace Microsoft.DotNet.UpgradeAssistant.MSBuild
|
||||
{
|
||||
public class ImportsCollection : ICollection<string>
|
||||
{
|
||||
private readonly ProjectRootElement _projectRoot;
|
||||
|
||||
public ImportsCollection(ProjectRootElement projectRoot)
|
||||
{
|
||||
_projectRoot = projectRoot;
|
||||
}
|
||||
|
||||
public bool Contains(string item)
|
||||
{
|
||||
if (_projectRoot.Imports.Any(p => item.Equals(p.Project, StringComparison.Ordinal)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Add(string item)
|
||||
{
|
||||
if (!Contains(item))
|
||||
{
|
||||
_projectRoot.AddImport(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_projectRoot.Imports.Clear();
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return _projectRoot.Imports.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public bool Remove(string item)
|
||||
{
|
||||
var element = _projectRoot.Imports.FirstOrDefault(p => item.Equals(p.Project, StringComparison.Ordinal));
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
element.RemoveElement();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void CopyTo(string[] array, int arrayIndex)
|
||||
{
|
||||
((ICollection<string>)this).CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public IEnumerator<string> GetEnumerator()
|
||||
{
|
||||
return _projectRoot.Imports.Select(p => p.Project).GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return _projectRoot.Imports.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -56,7 +56,7 @@ namespace Microsoft.DotNet.UpgradeAssistant.MSBuild
|
|||
public bool IsSdk =>
|
||||
ProjectRoot.Sdk is not null && ProjectRoot.Sdk.Contains(MSBuildConstants.DefaultSDK, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public IEnumerable<string> Imports => ProjectRoot.Imports.Select(p => Path.GetFileName(p.Project));
|
||||
public ICollection<string> Imports => new ImportsCollection(ProjectRoot);
|
||||
|
||||
public void SetTFM(TargetFrameworkMoniker tfm) => new TargetFrameworkMonikerCollection(this, _comparer).SetTargetFramework(tfm);
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<clear />
|
||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
</packageSources>
|
||||
</configuration>
|
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<clear />
|
||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
</packageSources>
|
||||
</configuration>
|
Загрузка…
Ссылка в новой задаче