public static string GetReferenceTargetPath(ITaskItem item) { // Determine if the reference will be copied local. // We're only dealing with primary file references. For these RAR will // copy local if Private is true or unset. var isPrivate = MSBuildUtilities.ConvertStringToBool(item.GetMetadata(MetadataNames.Private), defaultValue: true); if (!isPrivate) { // Private = false means the reference shouldn't be copied. return(null); } return(GetTargetPath(item)); }
private string GetRuntimeFrameworkVersion(ITaskItem frameworkReference, KnownFrameworkReference knownFrameworkReference) { // Precedence order for selecting runtime framework version // - RuntimeFrameworkVersion metadata on FrameworkReference item // - RuntimeFrameworkVersion MSBuild property // - Then, use either the LatestRuntimeFrameworkVersion or the DefaultRuntimeFrameworkVersion of the KnownFrameworkReference, based on // - The value (if set) of TargetLatestRuntimePatch metadata on the FrameworkReference // - The TargetLatestRuntimePatch MSBuild property (which defaults to True if SelfContained is true, and False otherwise) string runtimeFrameworkVersion = null; if (frameworkReference != null) { runtimeFrameworkVersion = frameworkReference.GetMetadata("RuntimeFrameworkVersion"); } if (string.IsNullOrEmpty(runtimeFrameworkVersion)) { runtimeFrameworkVersion = RuntimeFrameworkVersion; } if (string.IsNullOrEmpty(runtimeFrameworkVersion)) { bool?useLatestRuntimeFrameworkVersion = null; if (frameworkReference != null) { string useLatestRuntimeFrameworkMetadata = frameworkReference.GetMetadata("TargetLatestRuntimePatch"); if (!string.IsNullOrEmpty(useLatestRuntimeFrameworkMetadata)) { useLatestRuntimeFrameworkVersion = MSBuildUtilities.ConvertStringToBool(useLatestRuntimeFrameworkMetadata, defaultValue: false); } } if (useLatestRuntimeFrameworkVersion == null) { useLatestRuntimeFrameworkVersion = TargetLatestRuntimePatch; } if (useLatestRuntimeFrameworkVersion.Value) { runtimeFrameworkVersion = knownFrameworkReference.LatestRuntimeFrameworkVersion; } else { runtimeFrameworkVersion = knownFrameworkReference.DefaultRuntimeFrameworkVersion; } } return(runtimeFrameworkVersion); }
protected override void ExecuteCore() { if (!IsExecutable) { // If current project is not executable, then we don't need to check its references return; } foreach (ITaskItem project in ReferencedProjects) { string nearestTargetFramework = project.GetMetadata("NearestTargetFramework"); if (string.IsNullOrEmpty(nearestTargetFramework) || !project.HasMetadataValue("AdditionalPropertiesFromProject")) { // Referenced project doesn't have the right metadata. This may be because it's a different project type (C++, for example) // In this case just skip the checks continue; } var additionalPropertiesXml = XElement.Parse(project.GetMetadata("AdditionalPropertiesFromProject")); XElement targetFrameworkElement = UseAttributeForTargetFrameworkInfoPropertyNames ? additionalPropertiesXml.Elements().Where(el => el.HasAttributes && el.FirstAttribute.Value.Equals(nearestTargetFramework)).Single() : additionalPropertiesXml.Element(nearestTargetFramework); Dictionary <string, string> projectAdditionalProperties = new(StringComparer.OrdinalIgnoreCase); foreach (XElement propertyElement in targetFrameworkElement.Elements()) { projectAdditionalProperties[propertyElement.Name.LocalName] = propertyElement.Value; } bool shouldBeValidatedAsExecutableReference = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["ShouldBeValidatedAsExecutableReference"], true); bool referencedProjectIsExecutable = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["_IsExecutable"]); bool referencedProjectIsSelfContained = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["SelfContained"]); if (referencedProjectIsExecutable && shouldBeValidatedAsExecutableReference) { if (SelfContained && !referencedProjectIsSelfContained) { Log.LogError(Strings.SelfContainedExeCannotReferenceNonSelfContained, project.ItemSpec); } else if (!SelfContained && referencedProjectIsSelfContained) { Log.LogError(Strings.NonSelfContainedExeCannotReferenceSelfContained, project.ItemSpec); } } } }
private RuntimePatchRequest GetRuntimePatchRequest(ITaskItem frameworkReference) { string value = frameworkReference?.GetMetadata("TargetLatestRuntimePatch"); if (!string.IsNullOrEmpty(value)) { return(MSBuildUtilities.ConvertStringToBool(value, defaultValue: false) ? RuntimePatchRequest.UseLatestVersion : RuntimePatchRequest.UseDefaultVersion); } if (TargetLatestRuntimePatch) { return(RuntimePatchRequest.UseLatestVersion); } return(TargetLatestRuntimePatchIsDefault ? RuntimePatchRequest.UseDefaultVersionWithLatestRuntimePack : RuntimePatchRequest.UseDefaultVersion); }
public static Dictionary <string, SingleProjectInfo> CreateProjectReferenceInfos( IEnumerable <ITaskItem> referencePaths, IEnumerable <ITaskItem> referenceDependencyPaths, IEnumerable <ITaskItem> referenceSatellitePaths) { Dictionary <string, SingleProjectInfo> projectReferences = new Dictionary <string, SingleProjectInfo>(StringComparer.OrdinalIgnoreCase); IEnumerable <ITaskItem> projectReferencePaths = referencePaths .Where(r => string.Equals(r.GetMetadata(MetadataKeys.ReferenceSourceTarget), "ProjectReference", StringComparison.OrdinalIgnoreCase)); foreach (ITaskItem projectReferencePath in projectReferencePaths) { string sourceProjectFile = projectReferencePath.GetMetadata(MetadataKeys.MSBuildSourceProjectFile); if (string.IsNullOrEmpty(sourceProjectFile)) { throw new BuildErrorException(Strings.MissingItemMetadata, MetadataKeys.MSBuildSourceProjectFile, "ReferencePath", projectReferencePath.ItemSpec); } string outputName = Path.GetFileName(projectReferencePath.ItemSpec); string name = Path.GetFileNameWithoutExtension(outputName); string version = null; // it isn't possible to know the version from the MSBuild info. // The version will be retrieved from the project assets file. projectReferences.Add( sourceProjectFile, new SingleProjectInfo(sourceProjectFile, name, version, outputName, dependencyReferences: null, resourceAssemblies: null)); } // Include direct references of referenced projects, but only if they are CopyLocal IEnumerable <ITaskItem> projectReferenceDependencyPaths = referenceDependencyPaths .Where(r => string.Equals(r.GetMetadata(MetadataKeys.ReferenceSourceTarget), "ProjectReference", StringComparison.OrdinalIgnoreCase) && MSBuildUtilities.ConvertStringToBool(r.GetMetadata(MetadataKeys.CopyLocal))); foreach (ITaskItem projectReferenceDependencyPath in projectReferenceDependencyPaths) { string sourceProjectFile = projectReferenceDependencyPath.GetMetadata(MetadataKeys.MSBuildSourceProjectFile); if (string.IsNullOrEmpty(sourceProjectFile)) { throw new BuildErrorException(Strings.MissingItemMetadata, MetadataKeys.MSBuildSourceProjectFile, "ReferenceDependencyPath", projectReferenceDependencyPath.ItemSpec); } SingleProjectInfo referenceProjectInfo; if (projectReferences.TryGetValue(sourceProjectFile, out referenceProjectInfo)) { ReferenceInfo dependencyReferenceInfo = ReferenceInfo.CreateReferenceInfo(projectReferenceDependencyPath); referenceProjectInfo._dependencyReferences.Add(dependencyReferenceInfo); } } IEnumerable <ITaskItem> projectReferenceSatellitePaths = referenceSatellitePaths .Where(r => string.Equals(r.GetMetadata(MetadataKeys.ReferenceSourceTarget), "ProjectReference", StringComparison.OrdinalIgnoreCase)); foreach (ITaskItem projectReferenceSatellitePath in projectReferenceSatellitePaths) { string sourceProjectFile = projectReferenceSatellitePath.GetMetadata(MetadataKeys.MSBuildSourceProjectFile); if (string.IsNullOrEmpty(sourceProjectFile)) { throw new BuildErrorException(Strings.MissingItemMetadata, MetadataKeys.MSBuildSourceProjectFile, "ReferenceSatellitePath", projectReferenceSatellitePath.ItemSpec); } SingleProjectInfo referenceProjectInfo; if (projectReferences.TryGetValue(sourceProjectFile, out referenceProjectInfo)) { string originalItemSpec = projectReferenceSatellitePath.GetMetadata(MetadataKeys.OriginalItemSpec); if (!string.IsNullOrEmpty(originalItemSpec)) { ReferenceInfo referenceInfo = referenceProjectInfo._dependencyReferences.SingleOrDefault(r => r.FullPath.Equals(originalItemSpec)); if (referenceInfo is null) { // We only want to add the reference satellite path if it isn't already covered by a dependency ResourceAssemblyInfo resourceAssemblyInfo = ResourceAssemblyInfo.CreateFromReferenceSatellitePath(projectReferenceSatellitePath); referenceProjectInfo._resourceAssemblies.Add(resourceAssemblyInfo); } } } } return(projectReferences); }