示例#1
0
        public void Save(Stream stream)
        {
            // Make sure we're saving a valid package id
            PackageIdValidator.ValidatePackageId(Id);

            // Throw if the package doesn't contain any dependencies nor content
            if (!Files.Any() && !DependencySets.SelectMany(d => d.Dependencies).Any() && !FrameworkReferences.Any())
            {
                throw new InvalidOperationException(NuGetResources.CannotCreateEmptyPackage);
            }

            if (!ValidateSpecialVersionLength(Version))
            {
                throw new InvalidOperationException(NuGetResources.SemVerSpecialVersionTooLong);
            }

            ValidateDependencySets(Version, DependencySets);
            ValidateReferenceAssemblies(Files, PackageAssemblyReferences);

            bool requiresV4TargetFrameworkSchema = RequiresV4TargetFrameworkSchema(Files);

            using (Package package = Package.Open(stream, FileMode.Create))
            {
                // Validate and write the manifest
                WriteManifest(package,
                              requiresV4TargetFrameworkSchema ?
                              ManifestVersionUtility.TargetFrameworkSupportVersion :
                              ManifestVersionUtility.DefaultVersion);

                // Write the files to the package
                WriteFiles(package);

                // Copy the metadata properties back to the package
                package.PackageProperties.Creator     = String.Join(",", Authors);
                package.PackageProperties.Description = Description;
                package.PackageProperties.Identifier  = Id;
                package.PackageProperties.Version     = Version.ToString();
                package.PackageProperties.Language    = Language;
                package.PackageProperties.Keywords    = ((IPackageMetadata)this).Tags;
                package.PackageProperties.Title       = Title;
            }
        }
示例#2
0
 internal static void ValidateDependencySets(SemanticVersion version, IEnumerable <PackageDependencySet> dependencies)
 {
     if (version != null)
     {
         using (IEnumerator <PackageDependency> enumerator = (from s in dependencies select s.Dependencies).GetEnumerator())
         {
             while (enumerator.MoveNext())
             {
                 PackageIdValidator.ValidatePackageId(enumerator.Current.Id);
             }
         }
         if (string.IsNullOrEmpty(version.SpecialVersion))
         {
             PackageDependency dependency = Enumerable.FirstOrDefault <PackageDependency>(from set in dependencies select set.Dependencies, new Func <PackageDependency, bool>(PackageBuilder.IsPrereleaseDependency));
             if (dependency != null)
             {
                 object[] args = new object[] { dependency.ToString() };
                 throw new InvalidDataException(string.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_InvalidPrereleaseDependency, args));
             }
         }
     }
 }
示例#3
0
        internal static void ValidateDependencySets(SemanticVersion version, IEnumerable <PackageDependencySet> dependencies)
        {
            if (version == null)
            {
                // We have independent validation for null-versions.
                return;
            }

            foreach (var dep in dependencies.SelectMany(s => s.Dependencies))
            {
                PackageIdValidator.ValidatePackageId(dep.Id);
            }

            //if (String.IsNullOrEmpty(version.SpecialVersion))
            //{
            //    // If we are creating a production package, do not allow any of the dependencies to be a prerelease version.
            //    var prereleaseDependency = dependencies.SelectMany(set => set.Dependencies).FirstOrDefault(IsPrereleaseDependency);
            //    if (prereleaseDependency != null)
            //    {
            //        throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_InvalidPrereleaseDependency, prereleaseDependency.ToString()));
            //    }
            //}
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!String.IsNullOrEmpty(Id))
            {
                if (Id.Length > PackageIdValidator.MaxPackageIdLength)
                {
                    yield return(new ValidationResult(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_IdMaxLengthExceeded)));
                }
                else if (!PackageIdValidator.IsValidPackageId(Id))
                {
                    yield return(new ValidationResult(String.Format(CultureInfo.CurrentCulture, NuGetResources.InvalidPackageId, Id)));
                }
            }

            if (LicenseUrl == String.Empty)
            {
                yield return(new ValidationResult(
                                 String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "LicenseUrl")));
            }

            if (IconUrl == String.Empty)
            {
                yield return(new ValidationResult(
                                 String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "IconUrl")));
            }

            if (ProjectUrl == String.Empty)
            {
                yield return(new ValidationResult(
                                 String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "ProjectUrl")));
            }

            if (RequireLicenseAcceptance && String.IsNullOrWhiteSpace(LicenseUrl))
            {
                yield return(new ValidationResult(NuGetResources.Manifest_RequireLicenseAcceptanceRequiresLicenseUrl));
            }
        }
示例#5
0
 public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
 {
     if (!string.IsNullOrEmpty(this.Id))
     {
         if (this.Id.Length > 100)
         {
             yield return(new ValidationResult(string.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_IdMaxLengthExceeded, new object[0])));
         }
         else if (!PackageIdValidator.IsValidPackageId(this.Id))
         {
             object[] args = new object[] { this.Id };
             yield return(new ValidationResult(string.Format(CultureInfo.CurrentCulture, NuGetResources.InvalidPackageId, args)));
         }
     }
     if (this.LicenseUrl == string.Empty)
     {
         object[] args = new object[] { "LicenseUrl" };
         yield return(new ValidationResult(string.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, args)));
     }
     if (this.IconUrl == string.Empty)
     {
         object[] args = new object[] { "IconUrl" };
         yield return(new ValidationResult(string.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, args)));
     }
     if (this.ProjectUrl == string.Empty)
     {
         object[] args = new object[] { "ProjectUrl" };
         yield return(new ValidationResult(string.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, args)));
     }
     while (true)
     {
         if (this.RequireLicenseAcceptance && string.IsNullOrWhiteSpace(this.LicenseUrl))
         {
             yield return(new ValidationResult(NuGetResources.Manifest_RequireLicenseAcceptanceRequiresLicenseUrl));
         }
     }
 }