Exemplo n.º 1
0
        public virtual string ServiceGetTypeScript()
        {
            MetadataInfo     metadata = this.ServiceGetMetadata();
            TypeScriptHelper helper   = new TypeScriptHelper(metadata);

            return(helper.CreateTypeScript());
        }
		public void AnonymousTypes_Simple()
		{
			Dictionary<string, string> expected = new Dictionary<string, string>();
			expected.Add("stringProp", TypeScriptHelper.StringTypeName);
			expected.Add("intProp", TypeScriptHelper.NumericTypeName);
			_packageTester.TestControllerWithName("SimpleController")
				.TestActionWithName("SimpleAnonymousType")
				.ReturnTypeTypescriptNameIs(TypeScriptHelper.BuildAnonymousType(expected));

		}
		public void AnonymousTypes_WithNonExtractedObject()
		{
			Dictionary<string, string> expected = new Dictionary<string, string>();
			expected.Add("testClassProp", "any");
			expected.Add("intProp", TypeScriptHelper.NumericTypeName);
			_packageTester.TestControllerWithName("SimpleController")
				.TestActionWithName("AnonymousTypeWithNonExtracted")
				.ReturnTypeTypescriptNameIs(TypeScriptHelper.BuildAnonymousType(expected));

		}
Exemplo n.º 4
0
 public void Controllers_DictionaryParams()
 {
     //DictionaryParams
     _packageTester.TestControllerWithName("SimpleController")
     .TestActionWithName("ComplexMethod")
     .ParameterTypeIs("dateStringDict", TypeScriptHelper.FormatDictionaryType(TypeScriptHelper.StringTypeName, TypeScriptHelper.StringTypeName))
     .ParameterTypeIs("intStringDict", TypeScriptHelper.FormatDictionaryType(TypeScriptHelper.NumericTypeName, TypeScriptHelper.StringTypeName))
     .ParameterTypeIs("stringStringDict", TypeScriptHelper.FormatDictionaryType(TypeScriptHelper.StringTypeName, TypeScriptHelper.StringTypeName))
     ;
 }
Exemplo n.º 5
0
        public static void OnDocumentSaved(EnvDTE.Document Document)
        {
            // handle typescript saved files
            if (!TypeScriptHelper.IsValidTsFile(Document.Name))
            {
                return;
            }

            var filesToCompile = TypeScriptHelper.RootFilesToCompileFromFile(Document.FullName);

            System.Threading.Tasks.Task.Run(() =>
            {
                MSharp.F7.TypeScript.TypescriptCompiler.Compile(filesToCompile);
            });
        }
Exemplo n.º 6
0
        public void WebApi_ActionResultAnonymousObject_GetsType()
        {
            AddControllerAction("GetActionResultForAnonymous", MvcConstants.ActionResult_AspNetCore + "<object>")
            .AddLineOfCode($"return new {MvcConstants.ActionResult_AspNetCore}{"<object>"}(new {{ prop = 1, thing = 4 }});", 0)
            .Commit();

            Dictionary <string, string> expected = new Dictionary <string, string>
            {
                { "prop", TypeScriptHelper.NumericTypeName },
                { "thing", TypeScriptHelper.NumericTypeName }
            };

            AssertThatThisControllerAction("GetActionResultForAnonymous")
            .ReturnTypeTypescriptNameIs(TypeScriptHelper.BuildAnonymousType(expected));
        }
Exemplo n.º 7
0
        public void AnonymousTypes_Simple()
        {
            AddControllerAction("SimpleAnonymousType", MvcConstants.JsonResult_AspNetCore)
            .AddLineOfCode("return Json(new { stringProp = \"Hi\", intProp = 1 });", 0)
            .Commit();

            Dictionary <string, string> expected = new Dictionary <string, string>
            {
                { "stringProp", TypeScriptHelper.StringTypeName },
                { "intProp", TypeScriptHelper.NumericTypeName }
            };

            AssertThatThisControllerAction("SimpleAnonymousType")
            .ReturnTypeTypescriptNameIs(TypeScriptHelper.BuildAnonymousType(expected));
        }
        private static void ExportDeclaration()
        {
            ExporterMod.Log($"Start exporting all declarations");

            var outputPath = Path.Combine(Exporter.outputPath, "typings");

            if (Directory.Exists(outputPath))
            {
                Directory.Delete(outputPath, true);
            }
            Directory.CreateDirectory(outputPath);
            TypeScriptHelper.SaveAllTypesDeclaration(outputPath);

            ExporterMod.Log($"Complete exporting all declarations", true);
        }
Exemplo n.º 9
0
        public void AnonymousTypes_WithExtractedObject()
        {
            AddControllerAction("AnonymousTypeWithExtracted", "object")
            .AddLineOfCode("TestClass test = new TestClass();", 0)
            .AddLineOfCode("return new { testClassProp = test, intProp = 1 };", 0)
            .Commit();

            Dictionary <string, string> expected = new Dictionary <string, string>
            {
                { "testClassProp", $"{FakeTypePrefixer.Prefix}.TestClass" },
                { "intProp", TypeScriptHelper.NumericTypeName }
            };

            AssertThatThisControllerAction("AnonymousTypeWithExtracted")
            .ReturnTypeTypescriptNameIs(TypeScriptHelper.BuildAnonymousType(expected));
        }
Exemplo n.º 10
0
        public void AnonymousTypes_WithNonExtractedObject()
        {
            AddClass("NotExtracted")
            .AddProperty("DontCare", "int")
            .Commit();

            AddControllerAction("AnonymousTypeWithNonExtracted", MvcConstants.JsonResult_AspNetCore)
            .AddLineOfCode("NotExtracted test = new NotExtracted();", 0)
            .AddLineOfCode("return Json(new { testClassProp = test, intProp = 1 });", 0)
            .Commit();

            Dictionary <string, string> expected = new Dictionary <string, string>
            {
                { "testClassProp", "any" },
                { "intProp", TypeScriptHelper.NumericTypeName }
            };

            AssertThatThisControllerAction("AnonymousTypeWithNonExtracted")
            .ReturnTypeTypescriptNameIs(TypeScriptHelper.BuildAnonymousType(expected));
        }
Exemplo n.º 11
0
        void menuItem_BeforeQueryStatus(object sender, EventArgs e)
        {
            // get the menu that fired the event
            var menuCommand = sender as OleMenuCommand;

            // disable - not needed anymore
            menuCommand.Visible = false;
            menuCommand.Enabled = false;
            return;

            if (menuCommand != null)
            {
                // ############# Visibility #############
                // start by assuming that the menu will not be shown
                menuCommand.Visible = false;
                menuCommand.Enabled = false;

                IEnumerable <string> tsFiles = TypeScriptHelper.GetSelectedTypescriptFiles();
                if (!tsFiles.Any())
                {
                    return;
                }

                menuCommand.Visible = true;
                menuCommand.Enabled = true;

                // ############# Text #############

                if (App.Settings.Typescript.RootFiles.Any(f => tsFiles.Contains(f)))
                {
                    menuCommand.Text = "Stop combining JS";
                }
                else
                {
                    menuCommand.Text = "Combine JS files";
                }
            }
        }
Exemplo n.º 12
0
        void MenuItemCallback(object sender, EventArgs e)
        {
            App.Settings.Load();

            IEnumerable <string> tsFiles = TypeScriptHelper.GetSelectedTypescriptFiles();

            // If contains, remove from settings...
            if (App.Settings.Typescript.RootFiles.Any(f => tsFiles.Contains(f)))
            {
                App.Settings.Typescript.RootFiles = App.Settings.Typescript.RootFiles.Except(tsFiles).ToArray();
                App.Settings.Save();
            }
            else // else add to settings
            {
                App.Settings.Typescript.RootFiles = App.Settings.Typescript.RootFiles.Concat(tsFiles).Distinct().ToArray();
                App.Settings.Save();

                tsFiles = tsFiles.Select(f => TypeScriptHelper.GetFullPathFromRelativePath(f));
                System.Threading.Tasks.Task.Run(() =>
                {
                    MSharp.F7.TypeScript.TypescriptCompiler.Compile(tsFiles);
                });
            }
        }
Exemplo n.º 13
0
        private void ProcessAssociation(Association assoc, DbSetsDictionary dbSets, AssociationsDictionary associations)
        {
            if (string.IsNullOrWhiteSpace(assoc.name))
            {
                throw new DomainServiceException(ErrorStrings.ERR_ASSOC_EMPTY_NAME);
            }
            if (!dbSets.ContainsKey(assoc.parentDbSetName))
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT, assoc.name,
                                                               assoc.parentDbSetName));
            }
            if (!dbSets.ContainsKey(assoc.childDbSetName))
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD, assoc.name,
                                                               assoc.childDbSetName));
            }

            DbSetInfo childDb  = dbSets[assoc.childDbSetName];
            DbSetInfo parentDb = dbSets[assoc.parentDbSetName];
            Dictionary <string, Field> parentDbFields = parentDb.GetFieldByNames();
            Dictionary <string, Field> childDbFields  = childDb.GetFieldByNames();

            //check navigation field
            //dont allow to define  it explicitly, the association adds the field by itself (implicitly)
            if (!string.IsNullOrEmpty(assoc.childToParentName) && childDbFields.ContainsKey(assoc.childToParentName))
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name,
                                                               assoc.childToParentName));
            }

            //check navigation field
            //dont allow to define  it explicitly, the association adds the field by itself (implicitly)
            if (!string.IsNullOrEmpty(assoc.parentToChildrenName) &&
                parentDbFields.ContainsKey(assoc.parentToChildrenName))
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name,
                                                               assoc.parentToChildrenName));
            }

            if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && !string.IsNullOrEmpty(assoc.childToParentName) &&
                assoc.childToParentName == assoc.parentToChildrenName)
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name,
                                                               assoc.parentToChildrenName));
            }

            foreach (FieldRel frel in assoc.fieldRels)
            {
                if (!parentDbFields.ContainsKey(frel.parentField))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT_FIELD,
                                                                   assoc.name, frel.parentField));
                }
                if (!childDbFields.ContainsKey(frel.childField))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD_FIELD,
                                                                   assoc.name, frel.childField));
                }
            }

            //indexed by Name
            associations.Add(assoc.name, assoc);

            if (!string.IsNullOrEmpty(assoc.childToParentName))
            {
                StringBuilder sb          = new StringBuilder(120);
                string        dependentOn =
                    assoc.fieldRels.Aggregate(sb, (a, b) => a.Append((a.Length == 0 ? "" : ",") + b.childField),
                                              a => a).ToString();

                //add navigation field to dbSet's field collection
                Field fld = new Field
                {
                    fieldName   = assoc.childToParentName,
                    fieldType   = FieldType.Navigation,
                    dataType    = DataType.None,
                    dependentOn = dependentOn
                };

                fld.SetTypeScriptDataType(TypeScriptHelper.GetEntityInterfaceName(parentDb.dbSetName));
                childDb.fieldInfos.Add(fld);
            }

            if (!string.IsNullOrEmpty(assoc.parentToChildrenName))
            {
                StringBuilder sb  = new StringBuilder(120);
                Field         fld = new Field
                {
                    fieldName = assoc.parentToChildrenName,
                    fieldType = FieldType.Navigation,
                    dataType  = DataType.None
                };

                fld.SetTypeScriptDataType($"{TypeScriptHelper.GetEntityInterfaceName(childDb.dbSetName)}[]");
                //add navigation field to dbSet's field collection
                parentDb.fieldInfos.Add(fld);
            }
        }
Exemplo n.º 14
0
        public static void Save(string path)
        {
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
            Directory.CreateDirectory(path);

            // initial
            Init();

            string ebase = "Database";

            if (typeof(EType).IsSubclassOf(typeof(ELang)))
            {
                ebase = "Langbase";
            }

            StringBuilder sbIndex = new StringBuilder();

            // import
            foreach (var data in AllDatas)
            {
                data.Save(Path.Combine(path, $"{data.defName}.ts"));
                sbIndex.AppendLine($"import {data.defName} from './{data.defName}';");
            }
            sbIndex.AppendLine();

            // collection
            sbIndex.AppendLine($"const {Category}: {ebase}{Category} = {{");
            foreach (var data in AllDatas)
            {
                sbIndex.AppendLine($"  {data.defName},");
            }
            sbIndex.AppendLine($"}};");
            sbIndex.AppendLine();

            // aggregation
            EAggr aggregation = (hook as EData)?.GetAggregation();

            if (aggregation != null)
            {
                sbIndex.AppendLine($"export const aggregation{Category}: {aggregation.GetType().Name} = {aggregation.ToTypeSrcipt()}");
                sbIndex.AppendLine();
            }

            // export
            sbIndex.AppendLine($"export default {Category};");

            File.WriteAllText(Path.Combine(path, "index.ts"), sbIndex.ToString());

            // declaration
            StringBuilder sbDeclaration = new StringBuilder();

            sbDeclaration.AppendLine($"declare interface {ebase}{Category} {{");
            foreach (var data in AllDatas)
            {
                sbDeclaration.AppendLine($"  readonly {data.defName}: {typeof(EType).Name};");
            }
            sbDeclaration.AppendLine($"}}");
            TypeScriptHelper.AddEbaseDeclaration(ebase, Category, sbDeclaration);
        }
Exemplo n.º 15
0
        private void InitMetadata(object state)
        {
            BaseDomainService self     = (BaseDomainService)state;
            ServiceMetadata   metadata = null;

            if (BaseDomainService._metadataCache.TryGetValue(self.GetType(), out metadata))
            {
                self._serviceMetadata = metadata;
                return;
            }
            var metadataInfo = self.GetMetadata();

            metadata = new ServiceMetadata();
            foreach (var dbSetInfo in metadataInfo.DbSets)
            {
                dbSetInfo.Initialize(self.GetType());
                //indexed by dbSetName
                metadata.dbSets.Add(dbSetInfo.dbSetName, dbSetInfo);
            }

            foreach (var assoc in metadataInfo.Associations)
            {
                if (string.IsNullOrWhiteSpace(assoc.name))
                {
                    throw new DomainServiceException(ErrorStrings.ERR_ASSOC_EMPTY_NAME);
                }
                if (!metadata.dbSets.ContainsKey(assoc.parentDbSetName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT, assoc.name, assoc.parentDbSetName));
                }
                if (!metadata.dbSets.ContainsKey(assoc.childDbSetName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD, assoc.name, assoc.childDbSetName));
                }
                var childDb        = metadata.dbSets[assoc.childDbSetName];
                var parentDb       = metadata.dbSets[assoc.parentDbSetName];
                var parentDbFields = parentDb.GetFieldByNames();
                var childDbFields  = childDb.GetFieldByNames();

                //check navigation field
                //dont allow to define  it explicitly, the association adds the field by itself (implicitly)
                if (!string.IsNullOrEmpty(assoc.childToParentName) && childDbFields.ContainsKey(assoc.childToParentName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.childToParentName));
                }

                //check navigation field
                //dont allow to define  it explicitly, the association adds the field by itself (implicitly)
                if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && parentDbFields.ContainsKey(assoc.parentToChildrenName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName));
                }

                if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && !string.IsNullOrEmpty(assoc.childToParentName) && assoc.childToParentName == assoc.parentToChildrenName)
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName));
                }

                foreach (var frel in assoc.fieldRels)
                {
                    if (!parentDbFields.ContainsKey(frel.parentField))
                    {
                        throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT_FIELD, assoc.name, frel.parentField));
                    }
                    if (!childDbFields.ContainsKey(frel.childField))
                    {
                        throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD_FIELD, assoc.name, frel.childField));
                    }
                }
                //indexed by Name
                metadata.associations.Add(assoc.name, assoc);

                if (!string.IsNullOrEmpty(assoc.childToParentName))
                {
                    var sb          = new System.Text.StringBuilder(120);
                    var dependentOn = assoc.fieldRels.Aggregate(sb, (a, b) => a.Append((a.Length == 0 ? "" : ",") + b.childField), a => a).ToString();
                    //add navigation field to dbSet's field collection
                    childDb.fieldInfos.Add(new FieldInfo()
                    {
                        fieldName           = assoc.childToParentName,
                        isNavigation        = true,
                        isClientOnly        = true, dataType = DataType.None,
                        dependentOn         = dependentOn,
                        _TypeScriptDataType = TypeScriptHelper.GetEntityTypeName(parentDb.dbSetName)
                    });
                }

                if (!string.IsNullOrEmpty(assoc.parentToChildrenName))
                {
                    var sb = new System.Text.StringBuilder(120);
                    //add navigation field to dbSet's field collection
                    parentDb.fieldInfos.Add(new FieldInfo()
                    {
                        fieldName           = assoc.parentToChildrenName, isNavigation = true,
                        isClientOnly        = true, dataType = DataType.None,
                        _TypeScriptDataType = string.Format("{0}[]", TypeScriptHelper.GetEntityTypeName(childDb.dbSetName))
                    });
                }
            }
            metadata.methodDescriptions = this.GetMethodDescriptions(self.GetType());
            self._serviceMetadata       = metadata;
            BaseDomainService._metadataCache.TryAdd(self.GetType(), metadata);
        }