Example #1
0
        private void PunditToNuGetInternal()
        {
            if (!File.Exists(SourcePath))
            {
                throw new FileNotFoundException("The given file does not exist", SourcePath);
            }

            if (!PackageManifest.PunditPackageExtension.Equals(Path.GetExtension(SourcePath), StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("The given file is not a Pundit package");
            }

            _writer.Empty().Text($"Processing '{SourcePath}'...");

            DestinationFolder = string.IsNullOrEmpty(DestinationFolder) ? Path.GetDirectoryName(SourcePath) : Path.GetFullPath(DestinationFolder);

            string tmpFolder;
            string manifestPath;

            using (var stream = File.Open(SourcePath, FileMode.Open, FileAccess.Read))
            {
                var reader = _packageReaderFactory.Get(RepositoryType.Pundit, stream);

                tmpFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                Directory.CreateDirectory(tmpFolder);

                var packageSpec = reader.ReadManifest().ToPackageSpec();
                //var packageSpec =  manifest.ToPackageSpec(PackageExtensions.GetFramework(manifest.LegacyFramework));

                _writer.BeginWrite().Text($" Extracting {packageSpec.PackageId} [{packageSpec.Version}]... ");

                reader.ExtractTo(tmpFolder);

                foreach (var file in Directory.GetFiles(tmpFolder, "*", SearchOption.AllDirectories))
                {
                    var path = file.Substring(tmpFolder.Length + 1).Replace('\\', '/');

                    var sourceFile = new SourceFiles
                    {
                        BaseDirectory = tmpFolder,
                        Include       = file,
                        FileKind      = PackageReader.GetKind(path),
                    };

                    if (sourceFile.FileKind == PackageFileKind.Binary)
                    {
                        var config = path.Split('/')[1];
                        sourceFile.Configuration  = (BuildConfiguration)Enum.Parse(typeof(BuildConfiguration), config, true);
                        sourceFile.BaseDirectory += "\\bin\\" + sourceFile.Configuration.ToString().ToLower();
                    }
                    else if (sourceFile.FileKind == PackageFileKind.Include)
                    {
                        sourceFile.BaseDirectory += "\\include";
                    }
                    else if (sourceFile.FileKind == PackageFileKind.Tools)
                    {
                        sourceFile.BaseDirectory += "\\tools";
                    }
                    else if (sourceFile.FileKind == PackageFileKind.Other)
                    {
                        sourceFile.BaseDirectory += "\\other";
                    }

                    packageSpec.Files.Add(sourceFile);
                }

                if (packageSpec.Files.Count == 0)
                {
                    packageSpec.Files.Add(new SourceFiles {
                        BaseDirectory = tmpFolder, Include = "bin\\*"
                    });
                }

                manifestPath = Path.Combine(tmpFolder, "spec");
                using (var specStream = File.Open(manifestPath, FileMode.Create, FileAccess.Write))
                    _packageSerializer.SerializePackageSpec(packageSpec, specStream);

                _writer.Success("ok").EndWrite();
            }

            _packService.Type               = PackType.NuGet;
            _packService.OutputPath         = DestinationFolder;
            _packService.ManifestFileOrPath = manifestPath;

            _writer.BeginWrite().Text(" RePacking... ");
            _packService.Pack();
            _writer.Success("ok").EndWrite();

            Directory.Delete(tmpFolder, true);
        }