Пример #1
0
 public SolutionPort(string solutionFilePath, IDEProjectResult projectResult, List <PortCoreConfiguration> solutionConfiguration)
 {
     _solutionPath     = solutionFilePath;
     SkipDownloadFiles = new ConcurrentDictionary <string, bool>();
     _projectResult    = projectResult;
     _solutionRewriter = new SolutionRewriter(projectResult, solutionConfiguration.ToList <ProjectConfiguration>());
 }
Пример #2
0
 private void InitSolutionRewriter(List<AnalyzerResult> analyzerResults, List<PortCoreConfiguration> solutionConfiguration)
 {
     CheckCache();
     CheckResources();
     InitRules(solutionConfiguration, analyzerResults);
     _solutionRewriter = new SolutionRewriter(analyzerResults, solutionConfiguration.ToList<ProjectConfiguration>());
 }
Пример #3
0
        private static void Main(string[] args)
        {
            /* TODO
             * Add InterfaceContext
             * Add Delegates to Class/Struct/Interface Contexts
             * Ensure Class/Struct/Interface Contexts's GetTypeDependencies() also check the types that they inherit from, eg: other classes/structs/etc..
             *
             * If the paketti package contains extension methods that do not use any interweaves. Then most likely the extension method is a helper for
             * some interweave in the paketti project. In which case when a user imports that interweave, we should create an internal extension method class
             * which contains these helper methods. The interweave may need to be re-written to explicitly use that extension method. We don't want to
             * pollute the consumer's namespace with extension methods with ones that might collide with their own.
             *
             * Find and remove all debugger.break()s
             * */

            Action <Project> display = DisplayProject;

            var compiler         = new Compiler();
            var log              = new ConsoleLog();
            var solutionFile     = new PhysicalFileProvider(solutionDrive).GetFileInfo(solutionPath);
            var solutionRewriter = new SolutionRewriter(log);
            var builder          = new SolutionToLibraryBuilder <MSBuildWorkspace>(compiler, solutionFile, MSBuildWorkspace.Create, solutionRewriter,
                                                                                   pc => new DependencyWalker(pc, log),
                                                                                   new PackageContentSelector(),
                                                                                   display,
                                                                                   log,
                                                                                   (ws, path) => ws.OpenSolutionAsync(path).Result);
            var result = builder.Build();

            log.LogStep("Done");
            Console.ReadKey();
        }
Пример #4
0
        private TestSolutionAnalysis runCTAFile(string solutionName, string projectName = null)
        {
            var solutionPath = CopySolutionFolderToTemp(solutionName, downloadLocation);
            var solutionDir  = Directory.GetParent(solutionPath).FullName;

            FileAssert.Exists(solutionPath);

            //Sample Web API has only one project:
            string projectFile = (projectName == null ? Utils.GetProjectPaths(Path.Combine(solutionDir, solutionName)).FirstOrDefault() : Directory.EnumerateFiles(solutionDir, projectName, SearchOption.AllDirectories).FirstOrDefault());

            FileAssert.Exists(projectFile);

            ProjectConfiguration projectConfiguration = new ProjectConfiguration()
            {
                SolutionPath   = solutionPath,
                ProjectPath    = projectFile,
                TargetVersions = new List <string> {
                    version
                },
                RulesDir             = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "CTAFiles")),
                AdditionalReferences = ctaFiles
            };

            List <ProjectConfiguration> solutionConfiguration = new List <ProjectConfiguration>
            {
                projectConfiguration
            };

            SolutionRewriter solutionRewriter = new SolutionRewriter(solutionPath, solutionConfiguration);
            var           analysisRunResult   = solutionRewriter.AnalysisRun();
            StringBuilder str = new StringBuilder();

            foreach (var projectResult in analysisRunResult.ProjectResults)
            {
                str.AppendLine(projectResult.ProjectFile);
                str.AppendLine(projectResult.ProjectActions.ToString());
            }
            var analysisResult = str.ToString();

            solutionRewriter.Run(analysisRunResult.ProjectResults.ToDictionary(p => p.ProjectFile, p => p.ProjectActions));

            TestSolutionAnalysis result = new TestSolutionAnalysis()
            {
                SolutionAnalysisResult = analysisResult,
                ProjectResults         = new List <ProjectResult>()
                {
                    new ProjectResult()
                    {
                        ProjectAnalysisResult = analysisResult,
                        CsProjectPath         = projectFile,
                        ProjectDirectory      = Directory.GetParent(projectFile).FullName,
                        CsProjectContent      = File.ReadAllText(projectFile)
                    }
                }
            };

            return(result);
        }
Пример #5
0
        private void InitSolutionRewriter(List <AnalyzerResult> analyzerResults, List <PortCoreConfiguration> solutionConfiguration)
        {
            CheckCache();
            InitRules(solutionConfiguration, analyzerResults);

            var projectRewriterFactory = new PortCoreProjectRewriterFactory();

            _solutionRewriter = new SolutionRewriter(analyzerResults, solutionConfiguration.ToList <ProjectConfiguration>(), projectRewriterFactory);
        }
Пример #6
0
        public void TestSampleWebApiSolution()
        {
            //We don't care about version for CTA-only rules:
            string version = "net5.0";

            var sourceDir   = Directory.GetParent(Directory.EnumerateFiles(downloadLocation, "WebApiWithReferences.sln", SearchOption.AllDirectories).FirstOrDefault());
            var solutionDir = Path.Combine(tempDir, version);

            CopyDirectory(sourceDir, new DirectoryInfo(solutionDir));
            string solutionPath = Directory.EnumerateFiles(solutionDir, "WebApiWithReferences.sln", SearchOption.AllDirectories).FirstOrDefault();

            FileAssert.Exists(solutionPath);

            //Sample Web API has only one project:
            string projectFile = Directory.EnumerateFiles(solutionDir, "WebApiWithReferences.csproj", SearchOption.AllDirectories).FirstOrDefault();

            FileAssert.Exists(projectFile);

            ProjectConfiguration projectConfiguration = new ProjectConfiguration()
            {
                ProjectPath    = projectFile,
                TargetVersions = new List <string> {
                    version
                },
                RulesPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "CTAFiles"))
            };

            List <ProjectConfiguration> solutionConfiguration = new List <ProjectConfiguration>();

            solutionConfiguration.Add(projectConfiguration);

            SolutionRewriter solutionRewriter = new SolutionRewriter(solutionPath, solutionConfiguration);
            var           analysisRunResult   = solutionRewriter.AnalysisRun();
            StringBuilder str = new StringBuilder();

            foreach (var k in analysisRunResult.Keys)
            {
                str.AppendLine(k);
                str.AppendLine(analysisRunResult[k].ToString());
            }


            var analysisResult = str.ToString();

            StringAssert.Contains("IActionResult", analysisResult);


            solutionRewriter.Run(analysisRunResult);

            string projectDir = Directory.GetParent(projectFile).FullName;

            var homeControllerText   = File.ReadAllText(Path.Combine(projectDir, "Controllers", "HouseController.cs"));
            var iHouseRepositoryText = File.ReadAllText(Path.Combine(projectDir, "Repositories", "IHouseRepository.cs"));

            //Check that namespace has been added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", homeControllerText);

            //Check that attribute has been added to class, and base class changed:
            StringAssert.Contains(@"[ApiController]", homeControllerText);
            StringAssert.Contains(@"ControllerBase", homeControllerText);

            //Check that invocation expression was replaced:
            StringAssert.Contains(@"this.Response.Headers.Add", homeControllerText);

            //Check that identifier as replaced:
            StringAssert.Contains(@"IActionResult", homeControllerText);

            //Interface Tests
            StringAssert.Contains(@"AnyMethod", iHouseRepositoryText);
            StringAssert.Contains(@"using System;", iHouseRepositoryText);
            StringAssert.Contains(@"[Authorize]", iHouseRepositoryText);
            StringAssert.DoesNotContain(@"Delete", iHouseRepositoryText);
            StringAssert.Contains(@"IChangedHouseRepository", iHouseRepositoryText);
            StringAssert.Contains(@"This is a commented interface", iHouseRepositoryText);
            StringAssert.DoesNotContain(@"[Description(", iHouseRepositoryText);
        }
Пример #7
0
        public void TestMvcMusicStore()
        {
            //We don't care about version for CTA-only rules:
            string version = "net5.0";

            var sourceDir   = Directory.GetParent(Directory.EnumerateFiles(downloadLocation, "MvcMusicStore.sln", SearchOption.AllDirectories).FirstOrDefault());
            var solutionDir = Path.Combine(tempDir, version);

            CopyDirectory(sourceDir, new DirectoryInfo(solutionDir));
            string solutionPath = Directory.EnumerateFiles(solutionDir, "MvcMusicStore.sln", SearchOption.AllDirectories).FirstOrDefault();

            FileAssert.Exists(solutionPath);


            //Sample Web API has only one project:
            string projectFile = Directory.EnumerateFiles(solutionDir, "*.csproj", SearchOption.AllDirectories).FirstOrDefault();

            FileAssert.Exists(projectFile);

            ProjectConfiguration projectConfiguration = new ProjectConfiguration()
            {
                ProjectPath    = projectFile,
                TargetVersions = new List <string> {
                    version
                },
                RulesPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "CTAFiles"))
            };

            List <ProjectConfiguration> solutionConfiguration = new List <ProjectConfiguration>();

            solutionConfiguration.Add(projectConfiguration);

            SolutionRewriter solutionRewriter = new SolutionRewriter(solutionPath, solutionConfiguration);
            var           analysisRunResult   = solutionRewriter.AnalysisRun();
            StringBuilder str = new StringBuilder();

            foreach (var k in analysisRunResult.Keys)
            {
                str.AppendLine(k);
                str.AppendLine(analysisRunResult[k].ToString());
            }

            var analysisResult = str.ToString();

            StringAssert.Contains("HtmlEncoder", analysisResult);

            solutionRewriter.Run(analysisRunResult);

            string projectDir = Directory.GetParent(projectFile).FullName;

            var accountControllerText      = File.ReadAllText(Path.Combine(projectDir, "Controllers", "AccountController.cs"));
            var checkoutControllerText     = File.ReadAllText(Path.Combine(projectDir, "Controllers", "CheckoutController.cs"));
            var shoppingCartControllerText = File.ReadAllText(Path.Combine(projectDir, "Controllers", "ShoppingCartController.cs"));
            var storeManagerControllerText = File.ReadAllText(Path.Combine(projectDir, "Controllers", "StoreManagerController.cs"));
            var musicStoreEntitiesText     = File.ReadAllText(Path.Combine(projectDir, "Models", "MusicStoreEntities.cs"));
            var shoppingCartText           = File.ReadAllText(Path.Combine(projectDir, "Models", "ShoppingCart.cs"));

            var shoppingCartRemoveViewModel = File.ReadAllText(Path.Combine(projectDir, "ViewModels", "ShoppingCartRemoveViewModel.cs"));
            var shoppingCartViewModel       = File.ReadAllText(Path.Combine(projectDir, "ViewModels", "ShoppingCartViewModel.cs"));


            //Check that namespace was added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", accountControllerText);

            //Check that namespace was added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", checkoutControllerText);

            //Check that method was replaced
            StringAssert.Contains(@"HtmlEncoder.Default.Encode", shoppingCartControllerText);

            //Check that namespaces were added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", storeManagerControllerText);
            StringAssert.Contains(@"Microsoft.EntityFrameworkCore", storeManagerControllerText);

            //Check that namespace was added
            StringAssert.Contains(@"OnConfiguring", musicStoreEntitiesText);
            StringAssert.Contains(@"Microsoft.EntityFrameworkCore", musicStoreEntitiesText);

            //Check that httpcontext is added (with a space) to make sure it's not httpcontextbase
            StringAssert.Contains(@"HttpContext ", shoppingCartText);

            //Test change attribute name
            StringAssert.Contains(@"ChangedChildActionOnly", shoppingCartControllerText);

            //Test change namespace name
            StringAssert.Contains(@"MvcMusicStore.ChangedViewModel", shoppingCartRemoveViewModel);
            StringAssert.Contains(@"MvcMusicStore.ChangedViewModel", shoppingCartViewModel);

            StringAssert.Contains(@"using System;", shoppingCartControllerText);
            StringAssert.Contains(@"int PickRandomNumberRange", shoppingCartControllerText);
            StringAssert.Contains(@"Something.Something()", shoppingCartControllerText);
            StringAssert.DoesNotContain(@"new MusicStoreEntities()", shoppingCartControllerText);
            StringAssert.Contains(@"SomethingElse storeDB", shoppingCartControllerText);
        }