Ejemplo n.º 1
0
        /// <summary>
        /// Create a CSV file that contains all packages of project
        /// Create a stream that contains the CSV file
        /// </summary>
        /// <param name="projectId">Id of the project</param>
        private Stream ExportPackagesFromProject(string projectId, char delimiter = ';')
        {
            var project = _context.Projects.Find(projectId);

            if (project == null)
            {
                return(null);
            }
            var baseStream = new MemoryStream();

            using var stream = new StreamWriter(baseStream, System.Text.Encoding.UTF8, -1, true)
                  {
                      NewLine = "\r\n"// RFC 4180
                  };
            _context.Entry(project).Collection(p => p.PackageConsumers).Load();

            // Create header
            stream.WriteLine(CSVFileHelper.SerializeToCvsLine(new string[]
            {
                "PackageId",
                "Version",
                "Type",
                "PackageUrl",
                "RepositoryType",
                "RepositoryUrl",
                "RepositoryCommit",
                "Description",
                "License",
                "LicenseType"
            }, delimiter));
            var packages = _context.PackageConsumerPackages.Where(p => p.PackageConsumer.ProjectId == projectId).Select(p => p.Package).Distinct();

            foreach (var p in packages)
            {
                stream.WriteLine(CSVFileHelper.SerializeToCvsLine(new string[]
                {
                    p.PackageId,
                    p.Version,
                    p.PackageType.ToString(),
                    p.ProjectUrl,
                    p.RepositoryType,
                    p.RepositoryUrl,
                    p.RepositoryCommit,
                    p.Description,
                    p.License,
                    p.LicenseType.ToString()
                }, delimiter));
            }
            stream.Flush();                       // Empty the stream to the base stream
            baseStream.Seek(0, SeekOrigin.Begin); // Move the position to the start of the stream
            return(baseStream);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a CVS of packages found
        /// </summary>
        /// <param name="packageId">The package id must contains</param>
        /// <param name="delimiter">Delimiter to apply between elements of a line</param>
        /// <returns>The stream of CSV data</returns>
        private Stream ExportPackagesFromId(string packageId, char delimiter = ';')
        {
            var packages   = SearchPackage(packageId);
            var baseStream = new MemoryStream();

            using var stream = new StreamWriter(baseStream, System.Text.Encoding.UTF8, -1, true)
                  {
                      NewLine = "\r\n"// RFC 4180
                  };
            // Create header
            stream.WriteLine(CSVFileHelper.SerializeToCvsLine(new string[]
            {
                "PackageId",
                "Version",
                "ProjectUrl",
                "RepositoryType",
                "RepositoryUrl",
                "RepositoryCommit",
                "Description",
                "Type",
                "License",
                "LicenseType",
                "sous-projet",
                "Projets"
            },
                                                              delimiter));
            foreach (var p in packages)
            {
                foreach (var c in p.packageConsumers)
                {
                    stream.WriteLine(CSVFileHelper.SerializeToCvsLine(new string[]
                    {
                        p.packageDto.PackageId,
                        p.packageDto.Version,
                        p.packageDto.ProjectUrl,
                        p.packageDto.RepositoryType,
                        p.packageDto.RepositoryUrl,
                        p.packageDto.RepositoryCommit,
                        p.packageDto.PackageType.ToString(),
                        p.packageDto.License,
                        p.packageDto.LicenseType.ToString(),
                        c.name,
                        c.projectId,
                    },
                                                                      delimiter));
                }
            }
            stream.Flush();                       // Empty the stream to the base stream
            baseStream.Seek(0, SeekOrigin.Begin); // Move the position to the start of the stream
            return(baseStream);
        }