Ejemplo n.º 1
0
        public void LoadProject()
        {
            var path = Path.GetFullPath(@"C:\github\NgAnalyze\TestProject\tsconfig.json");

            _tsConfig = new TsConfigReader().LoadFromFile(path);

            AutoMapper.Mapper.Initialize(config => Mapping.Configuration(config, _tsConfig.RootDir));
            _project = TsProject <TypescriptFile> .Load(_tsConfig, TypescriptFile.Load);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //var specs = Directory.GetFiles(
            //    @"C:\Arbeid\etoto\code\beta.rikstoto.no\src\Rikstoto.Toto\App\Components\", "*component.spec.ts", SearchOption.AllDirectories).Select(Path.GetFileName).ToList();
            //foreach (var file in Directory.GetFiles(
            //    @"C:\Arbeid\etoto\code\beta.rikstoto.no\src\Rikstoto.Toto\App\Components\", "*component.ts",
            //    SearchOption.AllDirectories))
            //{
            //    if (!specs.Contains(Path.GetFileNameWithoutExtension(file) + ".spec.ts"))
            //    {
            //        GenerateSpecIfApplicable(file);
            //    }
            //}

            //string fileName =
            //    @"C:\Arbeid\etoto\code\beta.rikstoto.no\src\Rikstoto.Toto\App\Components\MyBets\MyBetsDate\MyBetsRaceday\MyBetsBet\MyBetsBetDetails\Prize\my-bet-prize.component.ts";
            //TypeScriptAST ast = new TypeScriptAST(File.ReadAllText(fileName), fileName);
            var path = Path.GetFullPath(@"..\..\..\TestProject\tsconfig.json");//
            //path = @"C:\Arbeid\etoto\code\beta.rikstoto.no\src\Rikstoto.Toto\tsconfig.test.json";
            //path = @"C:\github\beta.rikstoto.no\src\Rikstoto.Toto\tsconfig.test.json";
            TsConfig tsConfig = new TsConfigReader().LoadFromFile(path);

            AutoMapper.Mapper.Initialize(config => Mapping.Configuration(config, tsConfig.RootDir));
            TsProject <TypescriptFile> projectV2 = TsProject <TypescriptFile> .Load <TypescriptFile>(tsConfig, TypescriptFile.Load);

            projectV2.RunCodeChanger(new ConvertToNgrx8Actions(@"C:\github\ng-analyze\TestProject\src\actions.ts"));


            List <HtmlDocument>             templates  = projectV2.RunTraverser(new ExtractHtmlDocuments()).ToList();
            List <Angular.ComponentOptions> components = projectV2.RunTraverser(new ExtractAllComponentOptions()).ToList();

            foreach (var component in components)
            {
                var usagesInTemplates =
                    templates.Where(t => t.DocumentNode.GetElementsByTagName(component.Selector).Any());
                if (!usagesInTemplates.Any())
                {
                    Console.WriteLine($"{component.Selector} does not seem to be in use");
                }
            }

            File.WriteAllText(@"..\..\..\TestProject\compiled.json", JsonConvert.SerializeObject(projectV2, Formatting.Indented));
            //TsProject<TypescriptCompilation> project = TsProject<TypescriptCompilation>.Load(tsConfig, (TypescriptCompilation.CreateCompiled));
            Console.Clear();
            Console.WriteLine($"Compilation done - {projectV2.Files.Count} files");

            //var allClasses = project.Compiled.SelectMany(p => p.Classes);

            ////var projectWideImports = project.Compiled.SelectMany(c => c.Imports).Select(i => i.AbsolutePath).Distinct().ToList();
            ////var allFilenames = project.Files.Select(c => c.SourceStr).Distinct().ToList();
            ////var filesNotReferencced = allFilenames.Where(filename =>
            ////    projectWideImports.All(import => import != filename)).ToList();

            //ImportedModule storeActionType = new ImportedModule("Action", "@ngrx/store");
            //ImportedModule effectsActions = new ImportedModule("Actions", "@ngrx/effects");
            //var allActions = allClasses.Where(c => c.Inherits.Any(i => i.Equals(storeActionType)))
            //    .Where(a => a.FileName.EndsWith("form-rows-gallop.actions.ts"))
            //    .ToList();
            //var allUsagesOfActions = project.FindUsages(effectsActions);
            //var changes = new Changes();
            //foreach (var group in allActions.GroupBy(a => a.FileName))
            //{
            //    var file = group.First().Compilation;
            //    var change = changes.Get(file);
            //    List<ImportedModule> newImports = new List<ImportedModule>();
            //    List<ImportedModule> removedImports = new List<ImportedModule>();
            //    string union =
            //        $"\n\nconst all = union({{ {string.Join(", ", group.Select(action => action.Name.ToCamelCase()))} }});";

            //    newImports.Add(new ImportedModule("union", "@ngrx/store"));
            //    change.Append(union);

            //    var typeAlias = file.TypeAliases.LastOrDefault();
            //    var unionUsages = project.FindUsages(typeAlias);
            //    var typeAliasName = typeAlias?.Name;
            //    if (typeAlias != null)
            //    {
            //        if (typeAliasName.Equals("Actions"))
            //        {
            //            typeAliasName = Path.GetFileNameWithoutExtension(file.FileName).Replace(".", "-")
            //                .ConvertDashToCamelCase();
            //            Console.WriteLine($"Name Actions not recommended for type union. Will rename to {Path.GetFileNameWithoutExtension(file.FileName).Replace(".", "-").ConvertDashToCamelCase()}");
            //        }
            //        change.Delete(typeAlias.Node);
            //        string newTypeAlias = $"\nexport type {typeAliasName} = typeof all;\n";
            //        change.Append(newTypeAlias);
            //    }
            //    ImportedModule typeAliasImport = new ImportedModule(typeAliasName, file.FileName, true);

            //    foreach (var action in group)
            //    {
            //        var conversion = new ClassToCreateAction().Convert(action);
            //        change.ChangeNode(action.Node, conversion.Output);
            //        newImports.AddRange(conversion.RequiredImports);
            //        removedImports.AddRange(conversion.RemovedImports);
            //        var usages = project.FindUsages(action).ToList();
            //        var importsToAdd = new List<ImportedModule>();
            //        var importsToRemove = new List<ImportedModule>();

            //        foreach (var usagesByFile in usages.GroupBy(a => a.Compilation.FileName))
            //        {
            //            var usagesFile = usagesByFile.First().Compilation;
            //            var changesInUsages = changes.Get(usagesFile);
            //            var effectsActionsInFile = allUsagesOfActions.Where(c => c.Compilation.FileName == usagesFile.FileName);

            //            List<ImportedModule> newUsagesImports = new List<ImportedModule>();
            //            List<ImportedModule> removedUsagesImports = new List<ImportedModule>();
            //            if (typeAlias != null)
            //            {
            //                foreach (var actionsReference in effectsActionsInFile)
            //                {
            //                    try
            //                    {
            //                        changesInUsages.ChangeNode(actionsReference.Node, $"Actions<{typeAliasName}>");
            //                        newUsagesImports.Add(typeAliasImport);
            //                    }
            //                    catch
            //                    {
            //                        // fails second time, but that's okay.
            //                    }
            //                }
            //            }



            //            foreach (var usage in usagesByFile)
            //            {
            //                var usageConversion = new UsageToNewAction().Convert(usage);
            //                changesInUsages.ChangeNode(usageConversion.NodeToTarget as Node, usageConversion.Output);
            //                newUsagesImports.AddRange(usageConversion.RequiredImports);
            //                removedUsagesImports.AddRange(usageConversion.RemovedImports);
            //            }
            //            importsToAdd.AddRange(newUsagesImports);
            //            importsToRemove.AddRange(removedUsagesImports);
            //            changesInUsages.AddRemoveImports(newUsagesImports.Distinct(), removedUsagesImports);


            //            //File.WriteAllText(Path.Combine(Path.GetDirectoryName(usagesFile.FileName), $"{Path.GetFileNameWithoutExtension(usagesFile.FileName)}.ts"), newUsagesSource);
            //        }

            //    }
            //    // TODO: Create actions-union

            //    foreach (var queuedChange in changes.All)
            //    {
            //        //usagesFile.AddRemoveImports(changesInUsages.ChangeAst, newUsagesImports.Distinct(), removedUsagesImports);
            //        var newUsagesSource = queuedChange.GetChangedSource();
            //        //File.WriteAllText(usagesFile.FileName, newUsagesSource);

            //        File.WriteAllText(Path.Combine(Path.GetDirectoryName(queuedChange.FileName), $"{Path.GetFileName(queuedChange.FileName)}"), newUsagesSource);
            //    }
            //    change.AddRemoveImports(newImports.Distinct(), removedImports.Distinct());

            //    var newSource = change.GetChangedSource();
            //    //File.WriteAllText(Path.Combine(Path.GetDirectoryName(file.FileName), $"{Path.GetFileNameWithoutExtension(file.FileName)}.ts"), newSource);
            //    File.WriteAllText(Path.Combine(Path.GetDirectoryName(file.FileName), $"{Path.GetFileName(file.FileName)}"), newSource);
            //}

            //////foreach (var inherits in firstAction.Classes.First().Inherits)
            //////{
            //////    Console.WriteLine(inherits);
            //////}


            ////var firstUsage = usages.First();
            ////var change = new ChangeAST();
            ////change.ChangeNode(firstUsage.Node, "/* New value goes here */");
            ////var newSource = change.GetChangedSource(firstUsage.Compilation.Ast.SourceStr);
            ////File.WriteAllText(firstUsage.Compilation.FileName, newSource);
            //Console.WriteLine("Done!");
            //Console.ReadLine();
        }