private void InitPackageUpgradeIssues(FolderNuGetProject folderNuGetProject, NuGetProjectUpgradeDependencyItem package, NuGetFramework framework) { _notFoundPackages = new HashSet <PackageIdentity>(); var packageIdentity = new PackageIdentity(package.Id, NuGetVersion.Parse(package.Version)); // Confirm package exists var packagePath = folderNuGetProject.GetInstalledPackageFilePath(packageIdentity); if (string.IsNullOrEmpty(packagePath)) { HasIssues = true; HasNotFoundPackages = true; _notFoundPackages.Add(packageIdentity); package.Issues.Add(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, Resources.Upgrader_PackageNotFound, packageIdentity.Id), NuGetLogCode.NU5500)); } else { using (var reader = new PackageArchiveReader(packagePath)) { var packageRules = RuleSet.PackagesConfigToPackageReferenceMigrationRuleSet; var issues = package.Issues; foreach (var rule in packageRules) { var foundIssues = rule.Validate(reader).OrderBy(p => p.Code.ToString(), StringComparer.CurrentCulture); if (foundIssues != null && foundIssues.Any()) { HasIssues = true; } issues.AddRange(foundIssues); } } } }
internal static IEnumerable <PackagingLogMessage> Validate(XDocument xml, IEnumerable <string> files) { // NOTE: Most of these validators are partially extracted from // NuspecReader, because we need the raw framework strings, not // the frameworks themselves. That does end up with a bit of // duplicate code, but the alternative is to expand the scope of // NuspecReader by a lot. var metadataNode = xml.Root.Elements().Where(e => StringComparer.Ordinal.Equals(e.Name.LocalName, Metadata)).FirstOrDefault(); if (metadataNode == null) { throw new PackagingException(string.Format( CultureInfo.CurrentCulture, Strings.MissingMetadataNode, Metadata)); } var logMessages = ValidateDependencyGroups(metadataNode) .Concat(ValidateReferenceGroups(metadataNode)) .Concat(ValidateFrameworkAssemblies(xml, metadataNode)) .Concat(ValidateFiles(files)); if (logMessages.Any()) { return(logMessages.Concat(new List <PackagingLogMessage>() { PackagingLogMessage.CreateWarning(string.Format(CultureInfo.CurrentCulture, AnalysisResources.InvalidUndottedFrameworkWarning), NuGetLogCode.NU5501) })); } else { return(logMessages); } }
internal static IEnumerable <PackagingLogMessage> ValidateDependencyGroups(XElement metadataNode) { var ns = metadataNode.GetDefaultNamespace().NamespaceName; var dependencyNode = metadataNode .Elements(XName.Get(Dependencies, ns)); var dependencyGroups = dependencyNode .Elements(XName.Get(Group, ns)); var bads = new HashSet <string>(); foreach (var depGroup in dependencyGroups) { var groupFramework = GetAttributeValue(depGroup, TargetFramework); if (!string.IsNullOrEmpty(groupFramework) && !FrameworkVersionHasDesiredDots(groupFramework)) { bads.Add(groupFramework.Trim()); } } var messages = new List <PackagingLogMessage>(); if (bads.Any()) { messages.Add( PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, AnalysisResources.InvalidUndottedFrameworkInDependencyGroupsWarning, string.Join(", ", bads)), NuGetLogCode.NU5501 ) ); } return(messages); }
private bool AddFileToBuilder(ManifestFile packageFile) { if (!Files.Any(p => packageFile.Target.Equals(p.Target, StringComparison.CurrentCultureIgnoreCase))) { var fileExtension = Path.GetExtension(packageFile.Source); if (IncludeSymbols && PackArgs.SymbolPackageFormat == SymbolPackageFormat.Snupkg && !string.Equals(fileExtension, ".pdb", StringComparison.OrdinalIgnoreCase)) { return(false); } else { Files.Add(packageFile); return(true); } } else { Logger.Log(PackagingLogMessage.CreateWarning(string.Format( CultureInfo.CurrentCulture, Strings.FileNotAddedToPackage, packageFile.Source, packageFile.Target), NuGetLogCode.NU5118)); return(false); } }
internal void ExcludeFiles(ICollection <IPackageFile> packageFiles) { // Always exclude the nuspec file // Review: This exclusion should be done by the package builder because it knows which file would collide with the auto-generated // manifest file. var wildCards = _excludes.Concat(new[] { @"**\*" + NuGetConstants.ManifestExtension }); if (!_packArgs.NoDefaultExcludes) { // The user has not explicitly disabled default filtering. var excludedFiles = PathResolver.GetFilteredPackageFiles(packageFiles, ResolvePath, _defaultExcludes); if (excludedFiles != null) { foreach (var file in excludedFiles) { if (file is PhysicalPackageFile) { var physicalPackageFile = file as PhysicalPackageFile; _packArgs.Logger.Log(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, Strings.Warning_FileExcludedByDefault, physicalPackageFile.SourcePath), NuGetLogCode.NU5119)); } } } } wildCards = wildCards.Concat(_packArgs.Exclude); PathResolver.FilterPackageFiles(packageFiles, ResolvePath, wildCards); }
private PackagingLogMessage CreatePackageIssueForPrereleaseDependency(PackageDependency dependency, NuGetFramework framework) { return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat, dependency), NuGetLogCode.NU5104, dependency.Id, framework)); }
public IEnumerable <PackagingLogMessage> Validate(PackageArchiveReader builder) { var regex = new Regex(LegacyRegex, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); var nuspecReader = builder.NuspecReader; if (!regex.IsMatch(nuspecReader.GetVersion().ToFullString())) { yield return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat, nuspecReader.GetVersion().ToFullString()), NuGetLogCode.NU5105)); } }
public IEnumerable <PackagingLogMessage> Validate(PackageArchiveReader builder) { var nuspecReader = builder.NuspecReader; var icon = nuspecReader.GetIcon(); var iconUrl = nuspecReader.GetIconUrl(); if (icon == null && !string.IsNullOrEmpty(iconUrl)) { yield return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat), NuGetLogCode.NU5048)); } }
public IEnumerable <PackagingLogMessage> Validate(PackageArchiveReader builder) { var nuspecReader = builder?.NuspecReader; var licenseMetadata = nuspecReader?.GetLicenseMetadata(); var licenseUrl = nuspecReader?.GetLicenseUrl(); if (licenseMetadata == null && licenseUrl != null) { yield return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat), NuGetLogCode.NU5125)); } }
private void AddFileToBuilder(ManifestFile packageFile) { if (!Files.Any(p => packageFile.Target.Equals(p.Target, StringComparison.CurrentCultureIgnoreCase))) { Files.Add(packageFile); } else { Logger.Log(PackagingLogMessage.CreateWarning(string.Format( CultureInfo.CurrentCulture, Strings.FileNotAddedToPackage, packageFile.Source, packageFile.Target), NuGetLogCode.NU5118)); } }
private PackageBuilder CreatePackageBuilderFromProjectFile(string path) { // PackTargetArgs is only set for dotnet.exe pack code path, hence the check. if ((string.IsNullOrEmpty(_packArgs.MsBuildDirectory?.Value)) && _packArgs.PackTargetArgs == null) { throw new PackagingException(NuGetLogCode.NU5009, string.Format(CultureInfo.CurrentCulture, Strings.Error_CannotFindMsbuild)); } var factory = CreateProjectFactory(_packArgs, path); if (_packArgs.WarningProperties == null && _packArgs.PackTargetArgs == null) { _packArgs.WarningProperties = factory.GetWarningPropertiesForProject(); // Reinitialize the logger with Console as the inner logger and the obtained warning properties _packArgs.Logger = new PackCollectorLogger(_packArgs.Logger, _packArgs.WarningProperties); factory.Logger = _packArgs.Logger; } // Add the additional Properties to the properties of the Project Factory foreach (var property in _packArgs.Properties) { if (factory.GetProjectProperties().ContainsKey(property.Key)) { _packArgs.Logger.Log(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, Strings.Warning_DuplicatePropertyKey, property.Key), NuGetLogCode.NU5114)); } factory.GetProjectProperties()[property.Key] = property.Value; } NuGetVersion version = null; if (_packArgs.Version != null) { version = new NuGetVersion(_packArgs.Version); } // Create a builder for the main package as well as the sources/symbols package var mainPackageBuilder = factory.CreateBuilder(_packArgs.BasePath, version, _packArgs.Suffix, buildIfNeeded: true); if (mainPackageBuilder == null) { throw new PackagingException(string.Format(CultureInfo.CurrentCulture, Strings.Error_PackFailed, path)); } InitCommonPackageBuilderProperties(mainPackageBuilder); return(mainPackageBuilder); }
internal IEnumerable <PackagingLogMessage> Validate(IEnumerable <string> files) { var managedCodeConventions = new ManagedCodeConventions(new RuntimeGraph()); var collection = new ContentItemCollection(); collection.Load(files); List <ContentItemGroup> libItems = new(); List <ContentItemGroup> refItems = new(); List <ContentItemGroup> buildItems = new(); ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.CompileLibAssemblies, libItems); ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.CompileRefAssemblies, refItems); ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.MSBuildFiles, buildItems); var libFrameworks = ContentExtractor.GetGroupFrameworks(libItems).ToArray(); var refFrameworks = ContentExtractor.GetGroupFrameworks(refItems).ToArray(); var buildFrameworks = ContentExtractor.GetGroupFrameworks(buildItems).ToArray(); if (libFrameworks.Length == 0 && refFrameworks.Length == 0) { //if you can't find the ref and lib folder, then find the build folder if (buildFrameworks.Length != 0) { //if you can find any folders other than native or any, raise an NU5127 if (buildFrameworks.Any(t => (FrameworkConstants.DotNetAll.Satisfies(t) || FrameworkNameValidatorUtility.IsValidFrameworkName(t)) && t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.DotNet && t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.Native)) { var possibleFrameworks = buildFrameworks. Where(t => t.IsSpecificFramework && t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.DotNet && t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.Native). Select(t => t.GetShortFolderName()).ToArray(); (var tfmNames, var suggestedDirectories) = GenerateWarningString(possibleFrameworks); var issue = new List <PackagingLogMessage>(); issue.Add(PackagingLogMessage.CreateWarning(string.Format(MessageFormat, tfmNames, suggestedDirectories), NuGetLogCode.NU5127)); return(issue); } } } return(Array.Empty <PackagingLogMessage>()); }
internal static IEnumerable <PackagingLogMessage> ValidateFrameworkAssemblies(XDocument xml, XElement metadataNode) { var ns = xml.Root.GetDefaultNamespace().NamespaceName; var frameworks = new HashSet <string>(); foreach (var group in metadataNode.Elements(XName.Get(FrameworkAssemblies, ns)).Elements(XName.Get(FrameworkAssembly, ns)) .GroupBy(n => GetAttributeValue(n, TargetFramework))) { // Framework references may have multiple comma delimited frameworks if (!string.IsNullOrEmpty(group.Key)) { foreach (var fwString in group.Key.Split(CommaArray, StringSplitOptions.RemoveEmptyEntries)) { if (!string.IsNullOrEmpty(fwString)) { frameworks.Add(fwString.Trim()); } } } } var bads = new HashSet <string>(); foreach (var framework in frameworks) { if (!string.IsNullOrEmpty(framework) && !FrameworkVersionHasDesiredDots(framework)) { bads.Add(framework); } } var messages = new List <PackagingLogMessage>(); if (bads.Any()) { messages.Add( PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, AnalysisResources.InvalidUndottedFrameworkInFrameworkAssemblyGroupsWarning, string.Join(", ", bads)), NuGetLogCode.NU5501 ) ); } return(messages); }
internal PackagingLogMessage GenerateWarning(ICollection <ExpectedFile> expectedFiles) { if (expectedFiles.Count == 0) { return(null); } var warningMessage = new StringBuilder(); foreach (var expectedFile in expectedFiles) { warningMessage.AppendLine(string.Format(MessageFormat, expectedFile.Extension, expectedFile.Path, expectedFile.ExpectedPath)); } var message = PackagingLogMessage.CreateWarning(warningMessage.ToString(), NuGetLogCode.NU5129); return(message); }
public IEnumerable <PackagingLogMessage> Validate(PackageArchiveReader builder) { foreach (var toolItem in builder.GetFiles() .Select(t => PathUtility.GetPathWithDirectorySeparator(t)) .Where(t => t.StartsWith(PackagingConstants.Folders.Tools + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))) { if (toolItem.EndsWith("install.ps1", StringComparison.OrdinalIgnoreCase)) { var issue = new List <PackagingLogMessage>(); issue.Add(PackagingLogMessage.CreateWarning( string.Format(MessageFormat), NuGetLogCode.NU5120)); return(issue); } } return(new List <PackagingLogMessage>()); }
private static void InitPackageUpgradeIssues( string packagePath, NuGetProjectUpgradeDependencyItem package, HashSet <PackageIdentity> notFoundPackages, ref bool hasIssues) { var packageIdentity = new PackageIdentity(package.Id, NuGetVersion.Parse(package.Version)); if (string.IsNullOrEmpty(packagePath)) { hasIssues = true; notFoundPackages.Add(packageIdentity); package.Issues.Add(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, Resources.Upgrader_PackageNotFound, packageIdentity.Id), NuGetLogCode.NU5500)); } else { using (var reader = new PackageArchiveReader(packagePath)) { IEnumerable <IPackageRule> packageRules = RuleSet.PackagesConfigToPackageReferenceMigrationRuleSet; IList <PackagingLogMessage> issues = package.Issues; foreach (IPackageRule rule in packageRules) { IOrderedEnumerable <PackagingLogMessage> foundIssues = rule.Validate(reader) .OrderBy(p => p.Code.ToString(), StringComparer.CurrentCulture); if (foundIssues != null && foundIssues.Any()) { hasIssues = true; } issues.AddRange(foundIssues); } if (!package.InstallAsTopLevel) { PromoteToTopLevelIfNeeded(reader, package); } } } }
public IEnumerable <PackagingLogMessage> Validate(PackageArchiveReader builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var nuspecReader = builder.NuspecReader; var dependency = nuspecReader.GetDependencyGroups().SelectMany(d => d.Packages).FirstOrDefault(); if (dependency != null && dependency.VersionRange == VersionRange.All) { var issue = PackagingLogMessage.CreateWarning(string.Format( CultureInfo.CurrentCulture, AnalysisResources.UnspecifiedDependencyVersionWarning, dependency.Id), NuGetLogCode.NU5112); yield return(issue); } }
internal IEnumerable <PackagingLogMessage> GenerateWarnings(IEnumerable <ConventionViolator> conventionViolators) { var issues = new List <PackagingLogMessage>(); foreach (var folder in _folders) { var warningMessage = new StringBuilder(); var currentConventionViolators = conventionViolators.Where(t => t.ExpectedPath.StartsWith(folder + '/')); foreach (var conViolator in currentConventionViolators) { warningMessage.AppendLine(string.Format(MessageFormat, conViolator.Extension, conViolator.Path, conViolator.ExpectedPath)); } if (warningMessage.ToString() != string.Empty) { issues.Add(PackagingLogMessage.CreateWarning(warningMessage.ToString(), NuGetLogCode.NU5129)); } } return(issues); }
public IEnumerable <PackagingLogMessage> Validate(PackageArchiveReader builder) { if (builder.GetFiles() .Select(t => PathUtility.GetPathWithDirectorySeparator(t)) .Any(t => t.StartsWith (PackagingConstants.Folders.Content + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))) { // if package has content folder but no contentFiles folder if (!builder.GetFiles() .Select(t => PathUtility.GetPathWithDirectorySeparator(t)) .Any(t => t.StartsWith (PackagingConstants.Folders.ContentFiles + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))) { var issue = new List <PackagingLogMessage>(); issue.Add(PackagingLogMessage.CreateWarning( string.Format(MessageFormat, builder.GetIdentity().Id), NuGetLogCode.NU5121)); return(issue); } } return(new List <PackagingLogMessage>()); }
private void ValidateAllLicenseLeafs(NuGetLicenseExpression expression, IList <PackagingLogMessage> logMessages) { switch (expression.Type) { case LicenseExpressionType.License: var license = (NuGetLicense)expression; if (!license.IsStandardLicense) { logMessages.Add( PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat, license.Identifier), NuGetLogCode.NU5124)); } break; case LicenseExpressionType.Operator: var licenseOperator = (LicenseOperator)expression; switch (licenseOperator.OperatorType) { case LicenseOperatorType.LogicalOperator: var logicalOperator = (LogicalOperator)licenseOperator; ValidateAllLicenseLeafs(logicalOperator.Left, logMessages); ValidateAllLicenseLeafs(logicalOperator.Right, logMessages); break; case LicenseOperatorType.WithOperator: var withOperator = (WithOperator)licenseOperator; ValidateAllLicenseLeafs(withOperator.License, logMessages); break; default: break; } break; default: break; } }
internal IEnumerable <PackagingLogMessage> GenerateWarnings(HashSet <NuGetFramework> compatNotExactMatches, HashSet <NuGetFramework> noExactMatchesFromFile, HashSet <NuGetFramework> noExactMatchesFromNuspec) { (string noExactMatchString, string compatMatchString) = GenerateWarningString(noExactMatchesFromFile, noExactMatchesFromNuspec, (ICollection <NuGetFramework>)compatNotExactMatches); var issues = new List <PackagingLogMessage>(); if (noExactMatchesFromFile.Count != 0 || noExactMatchesFromNuspec.Count != 0) { issues.Add(PackagingLogMessage.CreateWarning(noExactMatchString, NuGetLogCode.NU5128)); } if (compatNotExactMatches.Count != 0) { issues.Add(PackagingLogMessage.CreateWarning(compatMatchString, NuGetLogCode.NU5130)); } if (issues.Count != 0) { return(issues); } return(Array.Empty <PackagingLogMessage>()); }
internal IEnumerable <PackagingLogMessage> GenerateWarnings(IEnumerable <MissingReference> missingReferences) { var issues = new List <PackagingLogMessage>(); if (missingReferences.Count() != 0) { var message = new StringBuilder(); message.AppendLine(MessageFormat); var referencesMissing = missingReferences.Where(t => t.MissingFrom == "nuspec"); var refFilesMissing = missingReferences.Where(t => t.MissingFrom == "ref"); foreach (var file in refFilesMissing) { foreach (var item in file.MissingItems) { message.AppendLine(string.Format(_addToRefFormat, item, file.Tfm)); } } foreach (var reference in referencesMissing) { foreach (var item in reference.MissingItems) { if (reference.Tfm.Equals("any")) { message.AppendLine(string.Format(_addToNuspecNoTfmFormat, item)); } else { message.AppendLine(string.Format(_addToNuspecFormat, item, reference.Tfm)); } } } issues.Add(PackagingLogMessage.CreateWarning(message.ToString(), NuGetLogCode.NU5131)); } return(issues); }
private PackagingLogMessage CreatePackageIssueForMisplacedScript(string target) { return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat, target), NuGetLogCode.NU5110)); }
private PackagingLogMessage CreatePackageIssueForPrereleaseDependency(string dependency) { return(PackagingLogMessage.CreateWarning( String.Format(CultureInfo.CurrentCulture, MessageFormat, dependency), NuGetLogCode.NU5104)); }
private PackagingLogMessage CreateIssueFor(string field, string value) { return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat, value, field), NuGetLogCode.NU5102)); }
private PackagingLogMessage CreatePackageIssueForTransformFiles(string path) { return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat, path), NuGetLogCode.NU5122)); }
private static PackagingLogMessage CreatePackageIssueForAssembliesOutsideLib(string target) { return(PackagingLogMessage.CreateWarning( String.Format(CultureInfo.CurrentCulture, AnalysisResources.AssemblyOutsideLibWarning, target), NuGetLogCode.NU5100)); }
private PackagingLogMessage CreatePackageIssueForAssembliesUnderLib(string target) { return(PackagingLogMessage.CreateWarning( String.Format(CultureInfo.CurrentCulture, MessageFormat, target), NuGetLogCode.NU5101)); }
private PackagingLogMessage CreateIssue(string file) { return(PackagingLogMessage.CreateWarning( string.Format(CultureInfo.CurrentCulture, MessageFormat, file), NuGetLogCode.NU5106)); }
private PackagingLogMessage CreatePackageIssueForUnrecognizedScripts(string target) { return(PackagingLogMessage.CreateWarning( String.Format(CultureInfo.CurrentCulture, MessageFormat, target), NuGetLogCode.NU5111)); }