Ejemplo n.º 1
0
        public GenerateResponse Generate(GenerateRequest request)
        {
            try
            {
                var virtualDisk = LoadStaticFiles(request);
                var clientDisk  = new VirtualDisk();

                (var serverProject, var clientProject) = InitProject(request, virtualDisk, clientDisk);
                serverProject.GenerateFiles(request.NameSpace, "http://localhost:5000");
                clientProject.GenerateFiles(request.NameSpace, "http://localhost:5000");
                virtualDisk.Mount(clientDisk, "ClientApp/src");
                var files = new List <GeneratedFile>();
                foreach (var file in virtualDisk.Files)
                {
                    files.Add(new GeneratedFile
                    {
                        Name    = file.Name,
                        Path    = file.Path.Replace("\\", "/"),
                        Content = file.Content
                    });
                }
                StatisticsService.OnGenerated(serverProject.ResourceCollection.DataModel, request.Data, StatEvent.Generate);
                return(new GenerateResponse
                {
                    Success = true,
                    GeneratedFiles = files
                });
            }
            catch (Exception e)
            {
                if (!(e is JsonReaderException))
                {
                    ErrorService.LogException(e, request.Data);
                }
                return(new GenerateResponse
                {
                    Success = false,
                    ErrorMessage = e.Message,
                    ErrorLine = e is JsonReaderException?GetLine(e) : null,
                                    GeneratedFiles = new List <GeneratedFile>()
                });
            }
        }
Ejemplo n.º 2
0
        private MemoryStream CreateDownloadStream(GenerateRequest request, string tempFile, string tempDir)
        {
            var virtualDisk = LoadStaticFiles(request);

            Directory.CreateDirectory(tempDir);
            foreach (var file in virtualDisk.Files)
            {
                var dir = Path.Combine(tempDir, file.Path);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                File.WriteAllText(Path.Combine(dir, file.Name), file.Content);
            }
            var serverDisk = new Disk(tempDir);
            var clientDisk = new Disk(Path.Combine(tempDir, "ClientApp/src"));

            ReplaceNamespace(tempDir, request.NameSpace);
            (var serverProject, var clientProject) = InitProject(request, serverDisk, clientDisk);
            serverProject.GenerateFiles(request.NameSpace, "http://localhost:5000");
            clientProject.GenerateFiles(request.NameSpace, "http://localhost:5000");
            ZipFile.CreateFromDirectory(tempDir, tempFile);
            var       reader     = new BinaryReader(File.Open(tempFile, FileMode.Open));
            const int bufferSize = 4096;
            var       ms         = new MemoryStream();

            byte[] buffer = new byte[bufferSize];
            int    count;

            while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
            {
                ms.Write(buffer, 0, count);
            }
            ms.Position = 0;
            StatisticsService.OnGenerated(serverProject.ResourceCollection.DataModel, request.Data, StatEvent.Download);
            return(ms);
        }