Example #1
0
        public void BuildPackage(string basePath, IList <string> includes, ManifestMetadata metadata, string outFolder, bool overwrite)
        {
            var filename = metadata.Id + "." + metadata.Version + ".zip";
            var output   = fileSystem.GetFullPath(Path.Combine(outFolder, filename));

            if (fileSystem.FileExists(output) && !overwrite)
            {
                throw new CommandException("The package file already exists and --overwrite was not specified");
            }

            log.InfoFormat("Saving {0} to {1}...", filename, outFolder);

            fileSystem.EnsureDirectoryExists(outFolder);

            var basePathLength = fileSystem.GetFullPath(basePath).Length;

            using (var stream = fileSystem.OpenFile(output, FileAccess.Write))
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create))
                {
                    foreach (var pattern in includes)
                    {
                        log.DebugFormat("Adding files from '{0}' matching pattern '{1}'", basePath, pattern);
                        foreach (var file in PathResolver.PerformWildcardSearch(basePath, pattern))
                        {
                            var fullFilePath = fileSystem.GetFullPath(file);
                            if (string.Equals(fullFilePath, output, StringComparison.InvariantCultureIgnoreCase))
                            {
                                continue;
                            }

                            var relativePath = fullFilePath.Substring(basePathLength).TrimStart('\\');
                            log.Debug("Adding file: " + relativePath);

                            var entry = archive.CreateEntry(relativePath, CompressionLevel.Optimal);
                            entry.LastWriteTime = new DateTimeOffset(new FileInfo(file).LastWriteTimeUtc);
                            using (var entryStream = entry.Open())
                                using (var sourceStream = File.OpenRead(file))
                                {
                                    sourceStream.CopyTo(entryStream);
                                }
                        }
                    }
                }
        }
        public void BuildPackage(string basePath,
                                 IList <string> includes,
                                 ManifestMetadata metadata,
                                 string outFolder,
                                 bool overwrite,
                                 bool verboseInfo)
        {
            var nugetPkgBuilder = new PackageBuilder();

            var manifestFiles = includes.Select(i => new ManifestFile {
                Source = i
            }).ToList();

            nugetPkgBuilder.PopulateFiles(basePath, manifestFiles);
            nugetPkgBuilder.Populate(metadata);

            if (verboseInfo)
            {
                foreach (var file in nugetPkgBuilder.Files)
                {
                    commandOutputProvider.Information($"Added file: {file.Path}");
                }
            }
            files.AddRange(nugetPkgBuilder.Files.Select(x => x.Path).ToArray());

            var filename = $"{metadata.Id}.{metadata.Version}.nupkg";
            var output   = Path.Combine(outFolder, filename);

            if (fileSystem.FileExists(output) && !overwrite)
            {
                throw new CommandException("The package file already exists and --overwrite was not specified");
            }

            commandOutputProvider.Information("Saving {Filename} to {OutFolder}...", filename, outFolder);

            fileSystem.EnsureDirectoryExists(outFolder);

            using (var outStream = fileSystem.OpenFile(output, FileMode.Create))
            {
                nugetPkgBuilder.Save(outStream);
            }
        }
Example #3
0
        public void BuildPackage(string basePath, IList <string> includes, ManifestMetadata metadata, string outFolder, bool overwrite)
        {
            var package = new PackageBuilder();

            package.PopulateFiles(basePath, includes.Select(i => new ManifestFile {
                Source = i
            }));
            package.Populate(metadata);

            var filename = metadata.Id + "." + metadata.Version + ".nupkg";
            var output   = Path.Combine(outFolder, filename);

            if (fileSystem.FileExists(output) && !overwrite)
            {
                throw new CommandException("The package file already exists and --overwrite was not specified");
            }

            log.InfoFormat("Saving {0} to {1}...", filename, outFolder);

            fileSystem.EnsureDirectoryExists(outFolder);

            using (var outStream = fileSystem.OpenFile(output, FileMode.Create))
                package.Save(outStream);
        }
        public void Execute(string[] commandLineArguments)
        {
            options.Parse(commandLineArguments);

            if (string.IsNullOrWhiteSpace(id))
            {
                throw new CommandException("An ID is required");
            }

            if (includes.All(string.IsNullOrWhiteSpace))
            {
                includes.Add("**");
            }

            if (string.IsNullOrWhiteSpace(basePath))
            {
                basePath = Path.GetFullPath(Environment.CurrentDirectory);
            }

            if (string.IsNullOrWhiteSpace(outFolder))
            {
                outFolder = Path.GetFullPath(Environment.CurrentDirectory);
            }

            if (version == null)
            {
                var now = DateTime.Now;
                version = new SemanticVersion(now.Year, now.Month, now.Day, now.Hour * 10000 + now.Minute * 100 + now.Second);
            }
            else
            {
                // Make sure SpecialVersion has 20 characters maximum (Limit imposed by NuGet)
                // https://nuget.codeplex.com/workitem/3426

                const int nugetSpecialVersionMaxLength = 20;
                if (!string.IsNullOrWhiteSpace(version.SpecialVersion) && version.SpecialVersion.Length > nugetSpecialVersionMaxLength)
                {
                    log.WarnFormat("SpecialVersion '{0}' will be truncated to {1} characters (NuGet limit)",
                                   version.SpecialVersion, nugetSpecialVersionMaxLength);

                    var specialVersion = version.SpecialVersion;
                    specialVersion = specialVersion.Substring(0, Math.Min(nugetSpecialVersionMaxLength, specialVersion.Length));

                    version = new SemanticVersion(version.Version, specialVersion);
                }
            }

            if (authors.All(string.IsNullOrWhiteSpace))
            {
                authors.Add(Environment.UserName + "@" + Environment.UserDomainName);
            }

            if (string.IsNullOrWhiteSpace(description))
            {
                description = "A deployment package created from files on disk.";
            }

            string allReleaseNotes = null;

            if (!string.IsNullOrWhiteSpace(releaseNotesFile))
            {
                if (!File.Exists(releaseNotesFile))
                {
                    log.WarnFormat("The release notes file '{0}' could not be found", releaseNotesFile);
                }
                else
                {
                    allReleaseNotes = fileSystem.ReadFile(releaseNotesFile);
                }
            }

            if (!string.IsNullOrWhiteSpace(releaseNotes))
            {
                if (allReleaseNotes != null)
                {
                    allReleaseNotes += Environment.NewLine + releaseNotes;
                }
                else
                {
                    allReleaseNotes = releaseNotes;
                }
            }

            var metadata = new ManifestMetadata
            {
                Id          = id,
                Authors     = string.Join(", ", authors),
                Description = description,
                Version     = version.ToString(),
            };

            if (!string.IsNullOrWhiteSpace(allReleaseNotes))
            {
                metadata.ReleaseNotes = allReleaseNotes;
            }

            if (!string.IsNullOrWhiteSpace(title))
            {
                metadata.Title = title;
            }

            log.InfoFormat("Packing {0} version {1}...", id, version);

            var package = new PackageBuilder();

            package.PopulateFiles(basePath, includes.Select(i => new ManifestFile {
                Source = i
            }));
            package.Populate(metadata);

            var filename = metadata.Id + "." + metadata.Version + ".nupkg";
            var output   = Path.Combine(outFolder, filename);

            if (fileSystem.FileExists(output) && !overwrite)
            {
                throw new CommandException("The package file already exists and --overwrite was not specified");
            }

            log.InfoFormat("Saving {0} to {1}...", filename, outFolder);

            fileSystem.EnsureDirectoryExists(outFolder);

            using (var outStream = fileSystem.OpenFile(output, FileMode.Create))
                package.Save(outStream);

            log.InfoFormat("Done.");
        }