Beispiel #1
0
        public TypeResolution Resolve(
            ITypeSymbol typeSymbol,
            ResolutionContext context,
            bool fillInheritance = false
            )
        {
            var type = typeSymbol as INamedTypeSymbol;

            var result = new TypeResolution();

            result.ConstructedFrom = type?.ConstructedFrom?.ToString();

            //Normalize type identifier
            if (!string.IsNullOrWhiteSpace(result.ConstructedFrom))
            {
                var splitted = result.ConstructedFrom.Split('<');
                var baseName = splitted[0];
                if (splitted.Length > 1)
                {
                    splitted = splitted[1].Split(',');
                    //Normalize constructedFrom. when analyzing compiled assemblies, generic args are omitted
                    result.ConstructedFrom = baseName + "<" + new StringBuilder().Append(',', splitted.Length - 1) + ">";
                }
            }

            //Get generics
            if (type != null && type.IsGenericType)
            {
                result.GenericArguments = type
                                          .TypeArguments
                                          .Select(t =>
                                                  t is INamedTypeSymbol ?
                                                  /* When type is defined */
                                                  (object)Resolve(t as INamedTypeSymbol, context)
                                                  //Just the argument name
                                : t.Name
                                                  )
                                          .ToList();
            }

            //Base Type
            if (fillInheritance && type != null && type.BaseType != null && type.BaseType.SpecialType != SpecialType.System_Object)
            {
                result.Inherits = Resolve(
                    type.BaseType,
                    context,
                    //Avoid creating unecessary extra import int the current context
                    false);
            }

            //User custom type mapping
            if (Options.CustomMap != null && result.ConstructedFrom != null && Options.CustomMap.ContainsKey(result.ConstructedFrom))
            {
                //Found definition with this full type name
                var clientType = Options.CustomMap[result.ConstructedFrom];

                var externalModule = clientType.Module;
                var externalName   = clientType.Name;
                if (string.IsNullOrWhiteSpace(externalModule))
                {
                    result.Declaration = externalName;
                }
                else
                {
                    var alias = context.GetAliasByModule(externalModule);
                    result.ModuleAlias = alias;
                    result.Declaration = $"{alias}.{externalName}";
                }

                result.IsKnown = true;
                return(result);
            }

            //Generic part of a generic class
            if (typeSymbol is ITypeParameterSymbol)
            {
                result.Declaration = typeSymbol.Name;
                return(result);
            }

            result.IsNullable = IsNullable(typeSymbol);

            var tsType = TypeFiles.ContainsKey(typeSymbol) ?
                         TypeFiles[typeSymbol]
                         //When inheriting from another generic model
                                    : TypeFiles.ContainsKey(typeSymbol.OriginalDefinition) ?
                         TypeFiles[typeSymbol.OriginalDefinition] : null;

            if (tsType != null)
            {
                //result.TsType = tsType;
                result.IsEnum      = tsType is TsEnum;
                result.IsKnown     = true;
                result.Declaration = tsType.ClassName;
                if (tsType is TsModelBase
                    &&
                    tsType != context.Target //auto-reference
                    )
                {
                    var tsModel = tsType as TsModelBase;

                    var c        = new Uri("C:\\", UriKind.Absolute);
                    var uriOther = new Uri(c, new Uri(tsModel.OutputFilePath, UriKind.Relative));
                    var uriMe    = new Uri(c, new Uri(context.Target.OutputFilePath, UriKind.Relative));


                    var module = uriMe.MakeRelativeUri(uriOther).ToString();
                    module = module.Substring(0, module.Length - 3);
                    if (module[0] != '.')
                    {
                        module = "./" + module;
                    }
                    var alias = context.GetAliasByModule(module);
                    result.ModuleAlias = alias;
                    result.Declaration = $"{alias}.{result.Declaration}";
                }
                //When inheriting from another generic model
                if (type.IsGenericType)
                {
                    var gp_ = GetGenericPart(type, result.Declaration, context);
                    result.Declaration = $"{result.Declaration}{gp_.genericPart}";
                }
                return(result);
            }

            //Array
            if (typeSymbol is IArrayTypeSymbol)
            {
                var arrTypeSymbol = typeSymbol as IArrayTypeSymbol;

                //This generic type does not really exists, just the base type "System.Array", but it will help generators easily identify arrays
                result.ConstructedFrom  = "System.Array<>";
                result.GenericArguments = new List <object>
                {
                    Resolve(arrTypeSymbol.ElementType, context)
                };


                if (arrTypeSymbol.ElementType.SpecialType == SpecialType.System_Byte)
                {
                    result.Declaration = "string";
                }
                else
                {
                    var elementTypeRes = Resolve(arrTypeSymbol.ElementType, context);
                    result.Declaration = $"Array<{elementTypeRes.Declaration}>";
                }
                return(result);
            }
            //tuples
            if (type.IsTupleType)
            {
                var tupleElements = type.TupleElements
                                    .Select(te => new
                {
                    field      = (Options.KeepPropsCase ? te.Name : te.Name.ToCamelCase()),
                    tupleField = (Options.KeepPropsCase ? te.CorrespondingTupleField.Name : te.CorrespondingTupleField.Name.ToCamelCase()),
                    type       = Resolve(te.Type as INamedTypeSymbol, context).Declaration
                });

                var tupleProps = tupleElements
                                 .Select(te => $"/** field:{te.field} */{te.tupleField}: {te.type}");

                result.Declaration = $"{{{string.Join(", ", tupleProps)}}}";
                return(result);
            }

            //string name = type.Name;
            var    members  = type.GetTypeMembers();
            string typeName = type.Name;
            //In case of nested types
            var parent = type.ContainingType;

            while (parent != null)
            {
                //Check if parent type is controller > service
                var parentName = parent.Name;
                //Adjust to check prerequisites
                if (Service.IsService(parent))
                {
                    //parentName = parentName.Replace("Controller", "Service");
                    parentName = parentName.Replace("Controller", Options.ServiceSuffix);
                }
                //For now, we'll just check if ends with "Controller" suffix
                //if (parentName.EndsWith("Controller")) {
                //	parentName = parentName.Replace("Controller", "Service");
                //}

                typeName = $"{parentName}.{typeName}";
                parent   = parent.ContainingType;
            }
            //Change type to ts type
            var tsTypeName = ToTsTypeName(type, context);

            //If contains "{" or "}" then it was converted to anonymous type, so no need to do anything else.
            if (tsTypeName.Contains("{"))
            {
                result.Declaration = tsTypeName;
                return(result);
            }

            var gp = GetGenericPart(type, tsTypeName, context);

            result.Declaration = $"{gp.modifiedTsTypeName}{gp.genericPart}";
            return(result);
        }
Beispiel #2
0
    /// <summary>
    /// The SortFolder
    /// </summary>
    /// <param name="DelFile">The DelFile<see cref="bool"/></param>
    /// <param name="SortExtension">The SortExtension<see cref="bool"/></param>
    /// <param name="path">The path<see cref="string"/></param>
    public void SortFolder(bool DelFile, bool SortExtension, string path)
    {
        MainWindow.BGThread = (new Thread(() => {
            var Progress = new ProgressController();
            Progress.ShowProgressBar();

            try {
                if (!Directory.Exists(path))
                {
                    throw new ArgumentNullException("path not exists", nameof(path));
                }

                var dir = new DirectoryInfo(path);
                var FileList = dir.GetFiles();
                Progress.SetMax(DelFile == true ? FileList.Length * 2 : FileList.Length);
                path += $"/Sort ({DateTime.Now.ToString("dd.MM.yyyy")})/";

                if (!SortExtension)
                {
                    foreach (var t in FileList)
                    {
                        try {
                            Progress.AddLog("Sort:" + t.Name);

                            Directory.CreateDirectory(path + TypeFiles.GetTypePath(t));
                            t.CopyTo(path + TypeFiles.GetTypePath(t) + t.Name, true);

                            Progress.AddProgress(1);
                        }
                        catch (Exception ex) {
                            Progress.AddLog("Error:" + t.Name);
                            Progress.AddLog(ex.StackTrace.ToString());
                            Progress.AddProgress(1);
                        }
                    }
                }
                else
                {
                    foreach (var t in FileList)
                    {
                        try {
                            Progress.AddLog("Sort:" + t.Name);
                            Directory.CreateDirectory(path + t.Extension.ToString().Replace(".", ""));
                            t.CopyTo(path + t.Extension.ToString().Replace(".", "") + "/" + t.Name, true);
                            Progress.AddProgress(1);
                        }
                        catch (Exception ex) {
                            Progress.AddLog("Error:" + t.Name);
                            Progress.AddLog(ex.StackTrace.ToString());
                            Progress.AddProgress(1);
                        }
                    }
                }

                if (DelFile)
                {
                    foreach (FileInfo file in dir.GetFiles())
                    {
                        try {
                            file.Delete();
                        }
                        catch (Exception ex) {
                            Progress.AddLog("Error:" + file.Name);
                            Progress.AddLog(ex.StackTrace.ToString());
                        }
                        finally {
                            Progress.AddProgress(1);
                        }
                    }
                }

                Progress.HideProgressBar();
            }
            catch (Exception ex) {
                Progress.AddLog(ex.StackTrace);
                Progress.HideProgressBar("!Error!");
            }
        }));

        MainWindow.BGThread.IsBackground = true;
        MainWindow.BGThread.Start();
    }