Esempio n. 1
0
        bool FilterExcludedNamespaces(ITestCase testCase)
        {
            // No assemblies in the filter == everything is okay
            if (ExcludedNamespaces.Count == 0)
            {
                return(true);
            }

            if (ExcludedNamespaces.Count != 0 && ExcludedNamespaces.Any(a => testCase.TestMethod.TestClass.Class.Name.StartsWith($"{a}.", StringComparison.Ordinal)))
            {
                return(false);
            }
            return(true);
        }
        //TODO: This needs to be split up, not very SOLID atm
        public bool Convert()
        {
            if (!NoClean)
            {
                CleanOutputDirectory();
            }
            CreateOutputDirectory();

            foreach (var assemblyDetails in config.Assemblies)
            {
                Console.WriteLine($"Getting types in {assemblyDetails.Name}");
                var assembly = Assembly.LoadFrom(Path.Combine(config.AssembliesPath, assemblyDetails.Name + ".dll"));

                if (config.UseNugetCacheResolver)
                {
                    LoadReferencesViaNugetCache(assembly);
                }

                foreach (var ns in assemblyDetails.Include)
                {
                    var typeResolver = new AssemblyTypeResolver(assembly, ns, ExcludedNamespaces);
                    var foundTypes   = typeResolver.Resolve().ToList();

                    Console.WriteLine($"Found {foundTypes.Count} types in {ns}");

                    foreach (var type in foundTypes)
                    {
                        var jsProperties = new Collection <JsProperty>();
                        var dependencies = new List <Type>();
                        var isDerived    = IncludedNamespaces.Any(a => type.BaseType.Namespace.Contains(a)) &&
                                           !ExcludedNamespaces.Contains(type.BaseType.Namespace);

                        if (isDerived)
                        {
                            dependencies.Add(type.BaseType);
                        }

                        var props = PropertyResolver.GetProperties(type);

                        var instance = Activator.CreateInstance(type);

                        var relativeOutputPath = GetRelativeOutputPath(type, ns);
                        var filePath           = Path.Combine(OutputPath, assemblyDetails.SubFolder ?? string.Empty,
                                                              relativeOutputPath, $"{type.Name}.js");

                        foreach (var prop in props)
                        {
                            var jsProp = new JsPropertyConverter(prop, prop.GetValue(instance), IncludedNamespaces, ExcludedNamespaces).Convert();
                            if (jsProp.PropertyType == JsPropertyType.Instance)
                            {
                                dependencies.Add(prop.PropertyType);
                            }
                            jsProperties.Add(jsProp);
                        }

                        var jsClass = new JsClass
                        {
                            Properties   = jsProperties,
                            Name         = type.Name,
                            Dependencies = dependencies.Distinct(),
                            FilePath     = filePath,
                            OriginalType = type,
                        };
                        JsClasses.Add(jsClass);
                    }
                }
            }

            var dependencyResolver = new JsClassDependencyResolver(JsClasses);
            var writer             = new JsClassWriter(dependencyResolver);

            //TODO: Should be its own class for writing files
            foreach (var jsClass in JsClasses)
            {
                var res = writer.Write(jsClass);

                Directory.CreateDirectory(Path.GetDirectoryName(jsClass.FilePath));
                Console.WriteLine($"Writing {jsClass.FilePath}");
                File.WriteAllText(jsClass.FilePath, res);
            }

            Console.WriteLine("Done");

            return(true);
        }