public void Given_Class_Name_Person_When_Testing_Generating_Interface_IPerson()
        {
            var solution_dir     = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var path             = solution_dir.Replace("bin/Debug/netcoreapp2.0", "");
            var expectedFileName = path + "IPerson.cs";

            var dependencies = new List <string>
            {
                "System",
                "System.Collections.Generic"
            };

            var personEntityDto = new EntityDto
            {
                Name   = "Person",
                Fields = new List <FieldTypePairDto>
                {
                    {
                        new FieldTypePairDto
                        {
                            FieldName = "Name",
                            Type      = "string"
                        }
                    },
                    {
                        new FieldTypePairDto
                        {
                            FieldName = "Age",
                            Type      = "int"
                        }
                    },
                    {
                        new FieldTypePairDto
                        {
                            FieldName = "Friends",
                            Type      = "IEnumerable<IPerson>"
                        }
                    }
                }
            };

            var dependencyGen = new DependenciesGenerator(dependencies);
            var namespaceGen  = new NamespaceGenerator();
            var fieldsGen     = new FieldTypePairGenerator();
            var bracketGen    = new ClosedCurlyBracketGenerator();

            var sut = new InterfaceFileGenerator(
                path,
                "Autogeneration.Entities.Test.Contracts",
                dependencyGen,
                namespaceGen,
                fieldsGen,
                bracketGen);

            sut.GenerateFile("Person", personEntityDto);


            Assert.True(File.Exists(expectedFileName));
        }
        public void Given_Empty_List_Of_Strings_When_Testing_DependenciesGenerator()
        {
            var input = new List <string>();

            var sut    = new DependenciesGenerator(input);
            var result = sut.Generate();

            var expected = "";

            Assert.Equal(expected, result);
        }
        public void Given_List_Of_Strings_When_Testing_DependenciesGenerator()
        {
            var input = new List <string>
            {
                "System",
                "System.Collections.Generic",
                "Autogeneration.Domain",
                "Xunit"
            };

            var sut    = new DependenciesGenerator(input);
            var result = sut.Generate();

            var expected = String.Join(
                Environment.NewLine,
                "using System;",
                "using System.Collections.Generic;",
                "using Autogeneration.Domain;",
                "using Xunit;",
                "");

            Assert.Equal(expected, result);
        }
Пример #4
0
        public static int Run(string?project, string?workspace, string?msbuildPath, string?tfm, bool forceWebConversion, bool preview, bool diffOnly, bool noBackup, bool keepCurrentTfms, decimal?generateDependenciesVersion)
        {
            if (!string.IsNullOrWhiteSpace(project) && !string.IsNullOrWhiteSpace(workspace))
            {
                Console.WriteLine("Cannot specify both a project and a workspace.");
                return(-1);
            }

            if (!string.IsNullOrWhiteSpace(tfm) && keepCurrentTfms)
            {
                Console.WriteLine($"Both '{nameof(tfm)}' and '{nameof(keepCurrentTfms)}' cannot be specified. Please pick one.");
                return(-1);
            }

            try
            {
                if (generateDependenciesVersion != null)
                {
                    Console.WriteLine($"Generating dependencies for {generateDependenciesVersion} version");
                    DependenciesGenerator.Generate(generateDependenciesVersion.Value);
                    return(0);
                }

                msbuildPath = MSBuildHelpers.HookAssemblyResolveForMSBuild(msbuildPath);
                if (string.IsNullOrWhiteSpace(msbuildPath))
                {
                    Console.WriteLine("Could not find an MSBuild.");
                    return(-1);
                }

                if (!string.IsNullOrWhiteSpace(tfm))
                {
                    tfm = tfm.Trim();
                    if (!TargetFrameworkHelper.IsValidTargetFramework(tfm))
                    {
                        Console.WriteLine($"Invalid framework specified for --target-framework: '{tfm}'");
                        return(-1);
                    }
                }
                else
                {
                    tfm = TargetFrameworkHelper.FindHighestInstalledTargetFramework(preview);
                }

                var workspacePath = string.Empty;
                MSBuildConversionWorkspaceType workspaceType;

                if (!string.IsNullOrWhiteSpace(project))
                {
                    workspacePath = Path.GetFullPath(project, Environment.CurrentDirectory);
                    workspaceType = MSBuildConversionWorkspaceType.Project;
                }
                else
                {
                    var(isSolution, workspaceFilePath) = MSBuildConversionWorkspaceFinder.FindWorkspace(Environment.CurrentDirectory, workspace);
                    workspaceType = isSolution ? MSBuildConversionWorkspaceType.Solution : MSBuildConversionWorkspaceType.Project;
                    workspacePath = workspaceFilePath;
                }

                var workspaceLoader = new MSBuildConversionWorkspaceLoader(workspacePath, workspaceType);
                // do not create backup if --diff-only specified
                noBackup = noBackup || diffOnly;
                var msbuildWorkspace = workspaceLoader.LoadWorkspace(workspacePath, noBackup, tfm, keepCurrentTfms, forceWebConversion);

                if (msbuildWorkspace.WorkspaceItems.Length is 0)
                {
                    Console.WriteLine("No projects converted.");
                    return(0);
                }

                foreach (var item in msbuildWorkspace.WorkspaceItems)
                {
                    if (diffOnly)
                    {
                        var differ = new Differ(item.UnconfiguredProject.FirstConfiguredProject, item.SdkBaselineProject.Project.FirstConfiguredProject);
                        var parent = Directory.GetParent(workspacePath);
                        if (parent is null)
                        {
                            differ.GenerateReport(workspacePath);
                        }
                        else
                        {
                            differ.GenerateReport(parent.FullName);
                        }
                    }
                    else
                    {
                        var converter = new Converter(item.UnconfiguredProject, item.SdkBaselineProject, item.ProjectRootElement, noBackup);
                        converter.Convert(item.ProjectRootElement.FullPath);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(-1);
            }

            Console.WriteLine("Conversion complete!");
            return(0);
        }