Exemplo n.º 1
0
        /// <summary>
        /// Сохранить файл
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        /// <param name="userIdentityid"></param>
        /// <param name="exportType"></param>
        /// <returns></returns>
        public async Task <ExportedFile> SaveExportedFile(Stream stream, string fileName, int userIdentityid,
                                                          EExportType exportType)
        {
            if (stream == null)
            {
                throw new Exception("Входной файл не определен");
            }

            var exchangeFileId = await SaveFile(stream, fileName, userIdentityid);

            using (var context = new ApplicationDbContext())
            {
                //создам сущность экспортируемого файла
                var exportedFile = new ExportedFile
                {
                    UnloadType   = exportType,
                    UnloadStatus = EUnloadStatus.InProcess,
                    ExchangeFile = await context.ExchangeFiles.FindAsync(exchangeFileId),
                    Organization =
                        context.Employee.Where(x => x.ApplicationUser.Id == userIdentityid)
                        .Select(x => x.Organization)
                        .FirstOrDefault()
                };
                context.ExportedFiles.Add(exportedFile);
                await context.SaveChangesAsync();

                return(exportedFile);
            }
        }
        public override void VisitFile(ExportedFile file)
        {
            bool hasReferences = false, hasImports = false;

            foreach (var rtReference in file.FinalReferences)
            {
                Visit(rtReference);
                hasReferences = true;
            }

            foreach (var rtImport in file.FinalImports)
            {
                Visit(rtImport);
                hasImports = true;
            }
            if (hasReferences || hasImports)
            {
                Br();
            }

            foreach (var fileNamespace in file.Namespaces)
            {
                Visit(fileNamespace);
            }
        }
Exemplo n.º 3
0
        public void Export(string fileName, ExportedFile file)
        {
            StringBuilder sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                ExportCore(sw, file);
            }
            ExportedFiles[fileName] = sb.ToString();
        }
Exemplo n.º 4
0
        public void Export(string fileName, ExportedFile file, ReferenceProcessorBase refProcessor = null)
        {
            StringBuilder sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                ExportCore(sw, file, refProcessor);
            }
            ExportedFiles[fileName] = sb.ToString();
        }
Exemplo n.º 5
0
        public void Export(string fileName, ExportedFile file)
        {
            StringBuilder sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                sw.NewLine = Context.Global.NewLine;
                ExportCore(sw, file);
            }
            ExportedFiles[fileName] = sb.ToString();
        }
Exemplo n.º 6
0
        protected virtual void ExportCore(TextWriter tw, ExportedFile file, ReferenceProcessorBase refProcessor = null)
        {
            var visitor = Context.Global.ExportPureTypings
                ? new TypingsExportVisitor(tw, Context.Global.TabSymbol, Context.Global.ReorderMembers)
                : new TypeScriptExportVisitor(tw, Context.Global.TabSymbol, Context.Global.ReorderMembers);

            WriteWarning(tw);

            var references = file.References.References;

            if (refProcessor != null)
            {
                references = refProcessor.FilterReferences(references, file);
                if (references == null)
                {
                    references = new RtReference[0];
                }
            }
            bool hasReferences = false;

            foreach (var rtReference in references)
            {
                visitor.Visit(rtReference);
                hasReferences = true;
            }

            var imports = file.References.Imports;

            if (refProcessor != null)
            {
                imports = refProcessor.FilterImports(imports, file);
                if (imports == null)
                {
                    imports = new RtImport[0];
                }
            }
            bool hasImports = false;

            foreach (var rtImport in imports)
            {
                visitor.Visit(rtImport);
                hasImports = true;
            }
            if (hasReferences || hasImports)
            {
                tw.WriteLine();
            }
            foreach (var fileNamespace in file.Namespaces)
            {
                visitor.Visit(fileNamespace);
            }
        }
Exemplo n.º 7
0
        protected virtual void ExportCore(TextWriter tw, ExportedFile file)
        {
            var visitor =
                Context.Global.VisitorType == null
                    ? Context.Global.ExportPureTypings
                        ? new TypingsExportVisitor(tw, Context)
                        : new TypeScriptExportVisitor(tw, Context)
                    : (TextExportingVisitor)Activator.CreateInstance(Context.Global.VisitorType, new object[] { tw, Context });

            WriteWarning(tw);

            visitor.VisitFile(file);
        }
Exemplo n.º 8
0
            public override void VisitFile(ExportedFile file)
            {
                base.VisitFile(file);
                var ns = file.Namespaces.FirstOrDefault();

                if (ns != null)
                {
                    WriteLines($@"
export = {ns.Name};

");
                }
            }
Exemplo n.º 9
0
        /// <summary>
        /// Запуск экспорта
        /// </summary>
        /// <returns></returns>
        public async Task Run(IPrincipal principal)
        {
            try
            {
                var userIdentityid = principal.Identity.GetUserId <int>();

                try
                {
                    var exportedFile = (await
                                        FileStorageManager.SaveExportedFile(new MemoryStream(), "empty.txt", userIdentityid, UnloadType));

                    var exportResult = ExportData(userIdentityid, exportedFile.Id).Result;

                    FileStorageManager.UpdateFile(exportedFile.ExchangeFileId, exportResult.Data, exportResult.FileName,
                                                  userIdentityid);

                    using (var context = new ApplicationDbContext())
                    {
                        context.ExportedFiles.Find(exportedFile.Id).UnloadStatus = exportResult.Status;
                        var file = context.ExchangeFiles.Find(exportedFile.ExchangeFileId);
                        file.FinishTime = DateTime.Now;
                        file.Progress   = 1m;

                        await context.SaveChangesAsync();
                    }
                }
                catch
                {
                    using (var context = new ApplicationDbContext())
                    {
                        var unloadedFile = new ExportedFile
                        {
                            UnloadType   = UnloadType,
                            UnloadStatus = EUnloadStatus.UnloadError,
                            ExchangeFile = null
                        };

                        context.ExportedFiles.Add(unloadedFile);
                        await context.SaveChangesAsync();
                    }
                    throw;
                }
            }
            catch (Exception ex)
            {
                LogUtils.WriteException(ex, LogUtils.ELogType.Error);
            }
        }
        /// <summary>
        /// Ensures that imports for specified type presents in specified file
        /// </summary>
        /// <param name="t">Type to import</param>
        /// <param name="typeName">Type name (probably overriden)</param>
        /// <param name="file">Exported file</param>
        /// <returns>Import AST node or null if no import needed. Returns existing import in case if type is already imported</returns>
        public RtImport EnsureImport(Type t, string typeName, ExportedFile file)
        {
            if (file.TypesToExport.Contains(t))
            {
                return(null);
            }
            if (file.AllTypesIsSingleFile)
            {
                return(null);
            }

            var relPath = GetRelativePathForType(t, file.FileName);

            if (string.IsNullOrEmpty(relPath))
            {
                return(null);
            }

            RtImport result = null;

            if (file.References.StarImports.ContainsKey(relPath))
            {
                return(file.References.StarImports[relPath]);
            }

            if (_context.Global.DiscardNamespacesWhenUsingModules)
            {
                var target = string.Format("{{ {0} }}", typeName);
                result = new RtImport()
                {
                    From = relPath, Target = target
                };
                file.References.AddImport(result);
            }
            else
            {
                var alias  = Path.GetFileNameWithoutExtension(relPath);
                var target = string.Format("* as {0}", alias);
                result = new RtImport()
                {
                    From = relPath, Target = target
                };
                file.References.AddImport(result);
            }
            return(result);
        }
        /// <summary>
        /// Ensures that reference for specified type presents in specified file
        /// </summary>
        /// <param name="t">Type to reference</param>
        /// <param name="file">Exported file</param>
        /// <returns>Reference AST node or null if no reference needed. Returns existing reference in case if type is already referenced</returns>
        public RtReference EnsureReference(Type t, ExportedFile file)
        {
            if (file.TypesToExport.Contains(t))
            {
                return(null);
            }
            if (file.AllTypesIsSingleFile)
            {
                return(null);
            }
            var relPath = GetRelativePathForType(t, file.FileName);
            var result  = new RtReference()
            {
                Path = relPath
            };

            file.References.AddReference(result);
            return(result);
        }
Exemplo n.º 12
0
        protected virtual void ExportCore(TextWriter tw, ExportedFile file)
        {
            var visitor = Context.Global.ExportPureTypings ? new TypingsExportVisitor(tw, Context.Global.TabSymbol) : new TypeScriptExportVisitor(tw, Context.Global.TabSymbol);

            WriteWarning(tw);
            foreach (var rtReference in file.References.References)
            {
                visitor.Visit(rtReference);
            }

            foreach (var rtImport in file.References.Imports)
            {
                visitor.Visit(rtImport);
            }
            if (file.References.References.Any() || file.References.Imports.Any())
            {
                tw.WriteLine();
            }
            foreach (var fileNamespace in file.Namespaces)
            {
                visitor.Visit(fileNamespace);
            }
        }
 public abstract T VisitFile(ExportedFile file);
 /// <summary>
 /// Returns refiltered and reordered reference directives that must appear in resulting file
 /// Return null to remain imports list untouched
 /// </summary>
 /// <param name="references">Set on initially computed references</param>
 /// <param name="file">File that is being exported currently</param>
 /// <returns>Set of refiltered/reordered references</returns>
 public override IEnumerable <RtReference> FilterReferences(IEnumerable <RtReference> references, ExportedFile file)
 {
     // same rules as for imports
     // keep in mind that references are ALWAYS being written
     // whereas imports are not being used until .UseModules set to true
     if (file.FileName.EndsWith("kendo.ts"))
     {
         return(references);
     }
     return(FilterMainFileReferences(references));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Returns refiltered and reordered reference directives that must appear in resulting file
 /// Return null to remain imports list untouched
 /// </summary>
 /// <param name="references">Set on initially computed references</param>
 /// <param name="file">File that is being exported currently</param>
 /// <returns>Set of refiltered/reordered references</returns>
 public abstract IEnumerable <RtReference> FilterReferences(IEnumerable <RtReference> references, ExportedFile file);
Exemplo n.º 16
0
 /// <summary>
 /// Returns refiltered and reordered import directives that must appear in resulting file.
 /// Return null to remain references list untouched
 /// </summary>
 /// <param name="imports">Set on initially computed imports</param>
 /// <param name="file">File that is being exported currently</param>
 /// <returns>Set of refiltered/reordered imports</returns>
 public abstract IEnumerable <RtImport> FilterImports(IEnumerable <RtImport> imports, ExportedFile file);
Exemplo n.º 17
0
 public abstract void VisitFile(ExportedFile file);
 public override void VisitFile(ExportedFile file)
 {
 }
            /// <summary>
            /// Returns refiltered and reordered import directives that must appear in resulting file.
            /// Return null to remain references list untouched
            /// </summary>
            /// <param name="imports">Set on initially computed imports</param>
            /// <param name="file">File that is being exported currently</param>
            /// <returns>Set of refiltered/reordered imports</returns>
            public override IEnumerable <RtImport> FilterImports(IEnumerable <RtImport> imports, ExportedFile file)
            {
                // here we also can return null to quickly reset references/imports list
                if (file.FileName.EndsWith("kendo.ts"))
                {
                    return(imports);
                }

                // modify imports only in meaningful file
                return(FilterMainFileImports(imports));
            }