void Init(IMSBuildItemEvaluated item) { if (Type == "Package") { Name = item.Metadata.GetValue("Name", ""); Version = item.Metadata.GetValue("Version", ""); Resolved = item.Metadata.GetValue("Resolved", false); IsPackage = true; } else if (Type == "Target") { Name = item.Metadata.GetValue("FrameworkName", ""); FrameworkName = Name; Version = item.Metadata.GetValue("FrameworkVersion", ""); Resolved = true; IsTargetFramework = true; } else if (Type == "Diagnostic") { GetDiagnosticNameAndVersion(item); DiagnosticCode = item.Metadata.GetValue("DiagnosticCode", ""); DiagnosticMessage = item.Metadata.GetValue("Message", ""); IsDiagnostic = true; } dependencies = item.Metadata.GetValue("Dependencies", "").Split(new char [] { ';' }, System.StringSplitOptions.RemoveEmptyEntries); }
protected override bool OnGetSupportsImportedItem(IMSBuildItemEvaluated buildItem) { if (!BuildAction.DotNetActions.Contains(buildItem.Name)) { return(false); } if (IsFSharpSdkProject()) { // Ignore imported F# files. F# files are defined in the main project. // This prevents duplicate F# files when a new project is first created. if (buildItem.Include.EndsWith(".fs", StringComparison.OrdinalIgnoreCase)) { return(false); } } if (IsFromSharedProject(buildItem)) { return(false); } // HACK: Remove any imported items that are not in the EvaluatedItems // This may happen if a condition excludes the item. All items passed to the // OnGetSupportsImportedItem are from the EvaluatedItemsIgnoringCondition return(Project.MSBuildProject.EvaluatedItems .Any(item => item.IsImported && item.Name == buildItem.Name && item.Include == buildItem.Include)); }
// Internal for testing internal static bool IsRazorDocumentItem(IMSBuildItemEvaluated item) { if (item is null) { throw new ArgumentNullException(nameof(item)); } if (item.Name != ContentItemType) { // We only inspect content items for Razor documents. return(false); } if (item.Include == null) { return(false); } if (!item.Include.EndsWith(".razor") && !item.Include.EndsWith(".cshtml")) { // Doesn't have a Razor looking file extension return(false); } return(true); }
static ProjectPackageReference CreateImportedPackageReference(IMSBuildItemEvaluated item, DotNetProject project) { var packageReference = ProjectPackageReference.Create(item); ModifyImportedPackageReference?.Invoke(packageReference, project); return(packageReference); }
/// <summary> /// Checks that the project has the HasSharedItems property set to true and the SharedGUID /// property in its global property group. Otherwise it is not considered to be a shared project. /// </summary> bool IsFromSharedProject(IMSBuildItemEvaluated buildItem) { var globalGroup = buildItem?.SourceItem?.ParentProject?.GetGlobalPropertyGroup(); return(globalGroup?.GetValue <bool> ("HasSharedItems") == true && globalGroup?.HasProperty("SharedGUID") == true); }
internal protected virtual void Read(Project project, IMSBuildItemEvaluated buildItem) { ItemName = buildItem.Name; Include = buildItem.Include; UnevaluatedInclude = buildItem.UnevaluatedInclude; Condition = buildItem.Condition; metadata = null; if (buildItem.SourceItem != null) { HashSet <string> knownProps = GetKnownMetadata(); foreach (var prop in buildItem.SourceItem.Metadata.GetProperties()) { if (!knownProps.Contains(prop.Name)) { if (metadata == null) { metadata = new ProjectItemMetadata(project.MSBuildProject); } // Get the evaluated value for the original metadata property var p = new ItemMetadataProperty(prop.Name, buildItem.Metadata.GetValue(prop.Name), prop.UnevaluatedValue); p.ParentProject = project.MSBuildProject; metadata.AddProperty(p); } } if (metadata != null) { metadata.OnLoaded(); } } buildItem.Metadata.ReadObjectProperties(this, GetType(), true); }
public void GetExtensions_SomeConfiguredExtensions_ReturnsConfiguredExtensions() { // Arrange var expectedExtension1Name = "Extension1"; var expectedExtension2Name = "Extension2"; var projectItems = new IMSBuildItemEvaluated[] { new TestMSBuildItem("RazorExtension") { Include = "UnconfiguredExtensionName", }, new TestMSBuildItem("RazorExtension") { Include = expectedExtension1Name, }, new TestMSBuildItem("RazorExtension") { Include = expectedExtension2Name, }, }; // Act var extensions = DefaultRazorProjectHost.GetExtensions(new[] { expectedExtension1Name, expectedExtension2Name }, projectItems); // Assert Assert.Collection( extensions, extension => Assert.Equal(expectedExtension1Name, extension.ExtensionName), extension => Assert.Equal(expectedExtension2Name, extension.ExtensionName)); }
protected override ProjectItem OnCreateProjectItem(IMSBuildItemEvaluated item) { if (item.Name == "Import") { return(new Import()); } return(base.OnCreateProjectItem(item)); }
void ReadProjectReference(Project project, IMSBuildItemEvaluated buildItem) { // Get the project name from the path, since the Name attribute may other stuff other than the name string path = MSBuildProjectService.FromMSBuildPath(project.ItemDirectory, buildItem.Include); string name = buildItem.Metadata.GetValue("Name", Path.GetFileNameWithoutExtension(path)); string projectGuid = buildItem.Metadata.GetValue("Project"); Init(ReferenceType.Project, name, null, projectGuid); }
void ReadReference(Project project, IMSBuildItemEvaluated buildItem) { if (buildItem.Metadata.HasProperty("HintPath")) { FilePath path; var p = buildItem.Metadata.GetProperty("HintPath"); if (p != null) { originalMSBuildReferenceHintPath = p.UnevaluatedValue; } if (!buildItem.Metadata.TryGetPathValue("HintPath", out path)) { var hp = buildItem.Metadata.GetValue("HintPath"); Init(ReferenceType.Assembly, hp, null); SetInvalid(GettextCatalog.GetString("Invalid file path")); } else { var type = File.Exists(path) ? ReferenceType.Assembly : ReferenceType.Package; Init(type, buildItem.Include, path); } } else { string asm = buildItem.Include; // This is a workaround for a VS bug. Looks like it is writing this assembly incorrectly if (asm == "System.configuration") { asm = "System.Configuration"; } else if (asm == "System.XML") { asm = "System.Xml"; } else if (asm == "system") { asm = "System"; } Init(ReferenceType.Package, asm, null); } string specificVersion = buildItem.Metadata.GetValue("SpecificVersion"); if (string.IsNullOrWhiteSpace(specificVersion)) { // If the SpecificVersion element isn't present, check if the Assembly Reference specifies a Version SpecificVersion = ReferenceStringHasVersion(buildItem.Include); } else { bool value; // if we can't parse the value, default to false which is more permissive SpecificVersion = bool.TryParse(specificVersion, out value) && value; } hasBeenRead = true; }
// Internal for testing internal static string[] GetExtensionNames(IMSBuildItemEvaluated configurationItem) { var extensionNamesValue = configurationItem.Metadata.GetValue(RazorConfigurationItemTypeExtensionsProperty); if (string.IsNullOrEmpty(extensionNamesValue)) { return(Array.Empty <string>()); } return(extensionNamesValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); }
static PackageDependency Create(string type, IMSBuildItemEvaluated item) { switch (type) { case "Target": case "Package": return(new PackageDependency(type, item)); default: return(null); } }
// Internal for testing internal static bool TryGetConfiguredExtensionNames(IMSBuildItemEvaluated configurationItem, out string[] configuredExtensionNames) { var extensionNamesValue = configurationItem.Metadata.GetValue(RazorConfigurationItemTypeExtensionsProperty); if (string.IsNullOrEmpty(extensionNamesValue)) { configuredExtensionNames = null; return(false); } configuredExtensionNames = extensionNamesValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); return(true); }
public void TryGetConfiguration_FailsIfNoDefaultConfiguration() { // Arrange var projectProperties = new MSBuildPropertyGroup(); var projectItems = new IMSBuildItemEvaluated[0]; // Act var result = DefaultRazorProjectHost.TryGetConfiguration(projectProperties, projectItems, out var configuration); // Assert Assert.False(result); Assert.Null(configuration); }
protected override bool OnGetSupportsImportedItem(IMSBuildItemEvaluated buildItem) { if (!BuildAction.DotNetActions.Contains(buildItem.Name)) { return(false); } // HACK: Remove any imported items that are not in the EvaluatedItems // This may happen if a condition excludes the item. All items passed to the // OnGetSupportsImportedItem are from the EvaluatedItemsIgnoringCondition return(Project.MSBuildProject.EvaluatedItems .Any(item => item.IsImported && item.Name == buildItem.Name && item.Include == buildItem.Include)); }
public void TryGetConfiguration_SucceedsWithAllPreRequisites() { // Arrange var expectedLanguageVersion = RazorLanguageVersion.Version_1_0; var expectedConfigurationName = "Razor-Test"; var expectedExtension1Name = "Extension1"; var expectedExtension2Name = "Extension2"; var expectedRazorConfigurationItem = new TestMSBuildItem("RazorConfiguration") { Include = expectedConfigurationName, }; expectedRazorConfigurationItem.TestMetadata.SetValue("Extensions", "Extension1;Extension2"); var projectItems = new IMSBuildItemEvaluated[] { new TestMSBuildItem("RazorConfiguration") { Include = "UnconfiguredRazorConfiguration", }, new TestMSBuildItem("RazorExtension") { Include = "UnconfiguredExtensionName", }, new TestMSBuildItem("RazorExtension") { Include = expectedExtension1Name, }, new TestMSBuildItem("RazorExtension") { Include = expectedExtension2Name, }, expectedRazorConfigurationItem, }; var projectProperties = new MSBuildPropertyGroup(); projectProperties.SetValue("RazorDefaultConfiguration", expectedConfigurationName); projectProperties.SetValue("RazorLangVersion", "1.0"); // Act var result = DefaultRazorProjectHost.TryGetConfiguration(projectProperties, projectItems, out var configuration); // Assert Assert.True(result); Assert.Equal(expectedLanguageVersion, configuration.LanguageVersion); Assert.Equal(expectedConfigurationName, configuration.ConfigurationName); Assert.Collection( configuration.Extensions, extension => Assert.Equal(expectedExtension1Name, extension.ExtensionName), extension => Assert.Equal(expectedExtension2Name, extension.ExtensionName)); }
public static ProjectPackageReference Create(IMSBuildItemEvaluated evaluatedItem) { var packageReference = Create( evaluatedItem.Include, evaluatedItem.Metadata.GetValue("Version") ); foreach (IMSBuildPropertyEvaluated property in evaluatedItem.Metadata.GetProperties()) { packageReference.Metadata.SetValue(property.Name, property.Value); } return(packageReference); }
internal protected override void Read(Project project, IMSBuildItemEvaluated buildItem) { base.Read(project, buildItem); if (buildItem.Name == "Reference") { ReadReference(project, buildItem); } else if (buildItem.Name == "ProjectReference") { ReadProjectReference(project, buildItem); } localCopy = buildItem.Metadata.GetValue <bool?> ("Private", null); ReferenceOutputAssembly = buildItem.Metadata.GetValue("ReferenceOutputAssembly", true); }
public void TryGetConfiguration_FailsIfNoConfigurationItems() { // Arrange var projectProperties = new MSBuildPropertyGroup(); projectProperties.SetValue("RazorDefaultConfiguration", "Razor-13.37"); projectProperties.SetValue("RazorLangVersion", "1.0"); var projectItems = new IMSBuildItemEvaluated[0]; // Act var result = DefaultRazorProjectHost.TryGetConfiguration(projectProperties, projectItems, out var configuration); // Assert Assert.False(result); Assert.Null(configuration); }
public void GetExtensions_NoExtensionTypes_ReturnsEmptyArray() { // Arrange var projectItems = new IMSBuildItemEvaluated[] { new TestMSBuildItem("NotAnExtension") { Include = "Extension1", }, }; // Act var extensions = DefaultMacRazorProjectHost.GetExtensions(new[] { "Extension1", "Extension2" }, projectItems); // Assert Assert.Empty(extensions); }
public void GetExtensions_UnConfiguredExtensionTypes_ReturnsEmptyArray() { // Arrange var projectItems = new IMSBuildItemEvaluated[] { new TestMSBuildItem("RazorExtension") { Include = "UnconfiguredExtensionName", }, }; // Act var extensions = DefaultRazorProjectHost.GetExtensions(new[] { "Extension1", "Extension2" }, projectItems); // Assert Assert.Empty(extensions); }
internal protected override void Read(Project project, IMSBuildItemEvaluated buildItem) { base.Read(project, buildItem); if (buildItem.Name == "Folder") { // Read folders string path = MSBuildProjectService.FromMSBuildPath(project.ItemDirectory, buildItem.Include); Name = Path.GetDirectoryName(path); Subtype = Subtype.Directory; return; } Name = MSBuildProjectService.FromMSBuildPath(project.ItemDirectory, buildItem.Include); BuildAction = buildItem.Name; DependsOn = buildItem.Metadata.GetPathValue("DependentUpon", relativeToPath: FilePath.ParentDirectory); string copy = buildItem.Metadata.GetValue("CopyToOutputDirectory"); if (!string.IsNullOrEmpty(copy)) { switch (copy) { case "None": break; case "Always": CopyToOutputDirectory = FileCopyMode.Always; break; case "PreserveNewest": CopyToOutputDirectory = FileCopyMode.PreserveNewest; break; default: LoggingService.LogWarning( "Unrecognised value {0} for CopyToOutputDirectory MSBuild property", copy); break; } } Visible = buildItem.Metadata.GetValue("Visible", true); resourceId = buildItem.Metadata.GetValue("LogicalName"); contentType = buildItem.Metadata.GetValue("SubType"); generator = buildItem.Metadata.GetValue("Generator"); customToolNamespace = buildItem.Metadata.GetValue("CustomToolNamespace"); lastGenOutput = buildItem.Metadata.GetValue("LastGenOutput"); Link = buildItem.Metadata.GetPathValue("Link", relativeToProject: false); }
// Internal for testing internal static bool TryGetConfigurationItem( string configuration, IEnumerable <IMSBuildItemEvaluated> projectItems, out IMSBuildItemEvaluated configurationItem) { foreach (var item in projectItems) { if (item.Name == RazorConfigurationItemType && item.Include == configuration) { configurationItem = item; return(true); } } configurationItem = null; return(false); }
/// <summary> /// Gets the NuGet package name and version from the identity for a NuGet diagnostic: /// /// .NETStandard,Version=v2.0/.NETStandard,Version=v2.0/System.ComponentModel.EventBasedAsync/4.0.10/NU1603 /// </summary> void GetDiagnosticNameAndVersion(IMSBuildItemEvaluated item) { if (string.IsNullOrEmpty(item.Include)) { return; } var parts = item.Include.Split(new char [] { '/' }); if (parts.Length < 5) { return; } FrameworkName = parts [1]; Name = parts [2]; Version = parts [3]; }
public void TryGetConfigurationItem_FailsNoMatchingRazorConfigurationItems() { // Arrange var projectItems = new IMSBuildItemEvaluated[] { new TestMSBuildItem("RazorConfiguration") { Include = "Razor-10.0", } }; // Act var result = DefaultRazorProjectHost.TryGetConfigurationItem("Razor-13.37", projectItems, out var configurationItem); // Assert Assert.False(result); Assert.Null(configurationItem); }
public void TryGetConfiguration_SucceedsWithoutConfiguredExtensionNames() { // Arrange var projectProperties = new MSBuildPropertyGroup(); projectProperties.SetValue("RazorDefaultConfiguration", "Razor-13.37"); projectProperties.SetValue("RazorLangVersion", "1.0"); var projectItems = new IMSBuildItemEvaluated[] { new TestMSBuildItem("RazorConfiguration") { Include = "Razor-13.37", }, }; // Act var result = DefaultRazorProjectHost.TryGetConfiguration(projectProperties, projectItems, out var configuration); // Assert Assert.True(result); Assert.Empty(configuration.Extensions); }
public void TryGetConfigurationItem_SucceedsForMatchingConfigurationItem() { // Arrange var expectedConfiguration = "Razor-13.37"; var expectedConfigurationItem = new TestMSBuildItem("RazorConfiguration") { Include = expectedConfiguration, }; var projectItems = new IMSBuildItemEvaluated[] { new TestMSBuildItem("RazorConfiguration") { Include = "Razor-10.0-DoesNotMatch", }, expectedConfigurationItem }; // Act var result = DefaultRazorProjectHost.TryGetConfigurationItem(expectedConfiguration, projectItems, out var configurationItem); // Assert Assert.True(result); Assert.Same(expectedConfigurationItem, configurationItem); }
internal protected override void Read (Project project, IMSBuildItemEvaluated buildItem) { base.Read (project, buildItem); Path = MSBuildProjectService.FromMSBuildPath (project.ItemDirectory, buildItem.Include); }
internal protected virtual void Read (Project project, IMSBuildItemEvaluated buildItem) { ItemName = buildItem.Name; Include = buildItem.Include; UnevaluatedInclude = buildItem.UnevaluatedInclude; Condition = buildItem.Condition; metadata = null; if (buildItem.SourceItem != null) { HashSet<string> knownProps = GetKnownMetadata (); foreach (var prop in buildItem.SourceItem.Metadata.GetProperties ()) { if (!knownProps.Contains (prop.Name)) { if (metadata == null) metadata = new ProjectItemMetadata (project.MSBuildProject); // Get the evaluated value for the original metadata property var p = new ItemMetadataProperty (prop.Name, buildItem.Metadata.GetValue (prop.Name), prop.UnevaluatedValue); p.ParentProject = project.MSBuildProject; metadata.AddProperty (p); } } if (metadata != null) metadata.OnLoaded (); } buildItem.Metadata.ReadObjectProperties (this, GetType (), true); }
void ReadReference (Project project, IMSBuildItemEvaluated buildItem) { if (buildItem.Metadata.HasProperty ("HintPath")) { FilePath path; var p = buildItem.Metadata.GetProperty ("HintPath"); if (p != null) originalMSBuildReferenceHintPath = p.UnevaluatedValue; if (!buildItem.Metadata.TryGetPathValue ("HintPath", out path)) { var hp = buildItem.Metadata.GetValue ("HintPath"); Init (ReferenceType.Assembly, hp, null); SetInvalid (GettextCatalog.GetString ("Invalid file path")); } else { var type = File.Exists (path) ? ReferenceType.Assembly : ReferenceType.Package; Init (type, buildItem.Include, path); } } else { string asm = buildItem.Include; // This is a workaround for a VS bug. Looks like it is writing this assembly incorrectly if (asm == "System.configuration") asm = "System.Configuration"; else if (asm == "System.XML") asm = "System.Xml"; else if (asm == "system") asm = "System"; Init (ReferenceType.Package, asm, null); } string specificVersion = buildItem.Metadata.GetValue ("SpecificVersion"); if (string.IsNullOrWhiteSpace (specificVersion)) { // If the SpecificVersion element isn't present, check if the Assembly Reference specifies a Version SpecificVersion = ReferenceStringHasVersion (buildItem.Include); } else { bool value; // if we can't parse the value, default to false which is more permissive SpecificVersion = bool.TryParse (specificVersion, out value) && value; } hasBeenRead = true; }
internal protected virtual ProjectItem OnCreateProjectItem (IMSBuildItemEvaluated item) { return next.OnCreateProjectItem (item); }
internal protected override void Read (Project project, IMSBuildItemEvaluated buildItem) { base.Read (project, buildItem); if (buildItem.Name == "Reference") ReadReference (project, buildItem); else if (buildItem.Name == "ProjectReference") ReadProjectReference (project, buildItem); LocalCopy = buildItem.Metadata.GetValue ("Private", DefaultLocalCopy); ReferenceOutputAssembly = buildItem.Metadata.GetValue ("ReferenceOutputAssembly", true); }
protected override ProjectItem OnCreateProjectItem (IMSBuildItemEvaluated item) { if (item.Name == "Import") return new Import (); return base.OnCreateProjectItem (item); }
void ReadProjectReference (Project project, IMSBuildItemEvaluated buildItem) { // Get the project name from the path, since the Name attribute may other stuff other than the name string path = MSBuildProjectService.FromMSBuildPath (project.ItemDirectory, buildItem.Include); string name = buildItem.Metadata.GetValue ("Name", Path.GetFileNameWithoutExtension (path)); string projectGuid = buildItem.Metadata.GetValue ("Project"); Init (ReferenceType.Project, name, null, projectGuid); }
internal protected override bool OnGetSupportsImportedItem(IMSBuildItemEvaluated buildItem) { return(BuildAction.DotNetCommonActions.Contains(buildItem.Name)); }
internal protected virtual ProjectItem OnCreateProjectItem(IMSBuildItemEvaluated item) { return(next.OnCreateProjectItem(item)); }
protected override ProjectItem OnCreateProjectItem (IMSBuildItemEvaluated item) { if (item.Name == "Reference" || item.Name == "ProjectReference") return new ProjectReference (); return base.OnCreateProjectItem (item); }
internal protected override void Read (Project project, IMSBuildItemEvaluated buildItem) { base.Read (project, buildItem); if (buildItem.Name == "Folder") { // Read folders string path = MSBuildProjectService.FromMSBuildPath (project.ItemDirectory, buildItem.Include); Name = Path.GetDirectoryName (path); Subtype = Subtype.Directory; return; } Name = MSBuildProjectService.FromMSBuildPath (project.ItemDirectory, buildItem.Include); BuildAction = buildItem.Name; DependsOn = buildItem.Metadata.GetPathValue ("DependentUpon", relativeToPath:FilePath.ParentDirectory); string copy = buildItem.Metadata.GetValue ("CopyToOutputDirectory"); if (!string.IsNullOrEmpty (copy)) { switch (copy) { case "None": break; case "Always": CopyToOutputDirectory = FileCopyMode.Always; break; case "PreserveNewest": CopyToOutputDirectory = FileCopyMode.PreserveNewest; break; default: LoggingService.LogWarning ( "Unrecognised value {0} for CopyToOutputDirectory MSBuild property", copy); break; } } Visible = buildItem.Metadata.GetValue ("Visible", true); resourceId = buildItem.Metadata.GetValue ("LogicalName"); contentType = buildItem.Metadata.GetValue ("SubType"); generator = buildItem.Metadata.GetValue ("Generator"); customToolNamespace = buildItem.Metadata.GetValue ("CustomToolNamespace"); lastGenOutput = buildItem.Metadata.GetValue ("LastGenOutput"); Link = buildItem.Metadata.GetPathValue ("Link", relativeToProject:false); }
internal protected virtual bool OnGetSupportsImportedItem(IMSBuildItemEvaluated buildItem) { return(next.OnGetSupportsImportedItem(buildItem)); }