Inheritance: NotifyPropertyChangedObject
Beispiel #1
0
        private void ApplyFix(FixModel selectedFix)
        {
            HashSet <string> files = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            // Gather the list of files for the fix.
            foreach (var fileChanges in selectedFix.FileChanges)
            {
                files.Add(fileChanges.FilePath);
            }

            foreach (string file in files)
            {
                if (File.Exists(file))
                {
                    List <ReplacementModel> replacements = new List <ReplacementModel>();

                    var fileChanges = selectedFix.FileChanges.Where(fc => fc.FilePath.Equals(file, StringComparison.OrdinalIgnoreCase));
                    foreach (FileChangeModel fileChange in fileChanges)
                    {
                        replacements.AddRange(fileChange.Replacements);
                    }

                    byte[] fixedFile;
                    if (TryFixFile(file, replacements, out fixedFile))
                    {
                        File.WriteAllBytes(file, fixedFile.ToArray());
                    }
                }
            }
        }
Beispiel #2
0
        internal void ApplyFix(FixModel selectedFix)
        {
            HashSet <string> files = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            // Gather the list of files for the fix.
            foreach (var fileChanges in selectedFix.FileChanges)
            {
                files.Add(fileChanges.FilePath.ToLower());
            }

            foreach (string file in files)
            {
                if (_fileSystem.FileExists(file))
                {
                    List <ReplacementModel> replacements = new List <ReplacementModel>();

                    var fileChanges = selectedFix.FileChanges.Where(fc => fc.FilePath.Equals(file, StringComparison.OrdinalIgnoreCase));
                    foreach (FileChangeModel fileChange in fileChanges)
                    {
                        replacements.AddRange(fileChange.Replacements);
                    }

                    byte[] fixedFile;
                    if (TryFixFile(file, replacements, false, out fixedFile))
                    {
                        _fileSystem.WriteAllBytes(file, fixedFile.ToArray());
                        s_sourceFileFixLedger[file].LastModified = File.GetLastWriteTime(file);

                        // Save after every fix because we don't have sufficient events to
                        // guarantee data integrity
                        SaveFixLedger();
                    }
                }
            }
        }
Beispiel #3
0
        public static FixModel ToFixModel(this Fix fix)
        {
            if (fix == null)
            {
                return null;
            }

            FixModel model = new FixModel(fix.Description);

            if (fix.FileChanges != null)
            {
                foreach (FileChange fileChange in fix.FileChanges)
                {
                    model.FileChanges.Add(fileChange.ToFileChangeModel());
                }
            }

            return model;
        }
Beispiel #4
0
        private void PreviewFix(FixModel selectedFix)
        {
            HashSet <string> files = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            // Gather the list of files for the fix.
            foreach (var fileChanges in selectedFix.FileChanges)
            {
                files.Add(fileChanges.FilePath);
            }

            foreach (string file in files)
            {
                if (File.Exists(file))
                {
                    List <ReplacementModel> replacements = new List <ReplacementModel>();

                    var fileChanges = selectedFix.FileChanges.Where(fc => fc.FilePath.Equals(file, StringComparison.OrdinalIgnoreCase));
                    foreach (FileChangeModel fileChange in fileChanges)
                    {
                        replacements.AddRange(fileChange.Replacements);
                    }

                    byte[] fixedFile;
                    if (TryFixFile(file, replacements, true, out fixedFile))
                    {
                        string extension       = Path.GetExtension(file);
                        string baseFileName    = Path.GetFileNameWithoutExtension(file);
                        string previewFileName = baseFileName + ".preview" + extension;
                        string previewFilePath = Path.Combine(Path.GetTempPath(), previewFileName);

                        File.WriteAllBytes(previewFilePath, fixedFile.ToArray());

                        SarifViewerPackage.Dte.ExecuteCommand("Tools.DiffFiles",
                                                              $"\"{file}\" " +
                                                              $"\"{previewFilePath}\" " +
                                                              $"\"{Resources.FixPreviewWindow_OriginalFileTitle}\" " +
                                                              $"\"{Resources.FixPreviewWindow_PreviewFixedFileTitle}\"");
                    }
                }
            }
        }
Beispiel #5
0
        private void PreviewFix(FixModel selectedFix)
        {
            HashSet<string> files = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            // Gather the list of files for the fix.
            foreach (var fileChanges in selectedFix.FileChanges)
            {
                files.Add(fileChanges.FilePath);
            }

            foreach (string file in files)
            {
                if (File.Exists(file))
                {
                    List<ReplacementModel> replacements = new List<ReplacementModel>();

                    var fileChanges = selectedFix.FileChanges.Where(fc => fc.FilePath.Equals(file, StringComparison.OrdinalIgnoreCase));
                    foreach (FileChangeModel fileChange in fileChanges)
                    {
                        replacements.AddRange(fileChange.Replacements);
                    }

                    byte[] fixedFile;
                    if (TryFixFile(file, replacements, out fixedFile))
                    {
                        string extension = Path.GetExtension(file);
                        string baseFileName = Path.GetFileNameWithoutExtension(file);
                        string previewFileName = baseFileName + ".preview" + extension;
                        string previewFilePath = Path.Combine(Path.GetTempPath(), previewFileName);

                        File.WriteAllBytes(previewFilePath, fixedFile.ToArray());

                        SarifViewerPackage.Dte.ExecuteCommand("Tools.DiffFiles", file + " " + previewFilePath);
                    }
                }
            }
        }
Beispiel #6
0
        private void ApplyFix(FixModel selectedFix)
        {
            HashSet<string> files = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            // Gather the list of files for the fix.
            foreach (var fileChanges in selectedFix.FileChanges)
            {
                files.Add(fileChanges.FilePath);
            }

            foreach (string file in files)
            {
                if (File.Exists(file))
                {
                    List<ReplacementModel> replacements = new List<ReplacementModel>();

                    var fileChanges = selectedFix.FileChanges.Where(fc => fc.FilePath.Equals(file, StringComparison.OrdinalIgnoreCase));
                    foreach (FileChangeModel fileChange in fileChanges)
                    {
                        replacements.AddRange(fileChange.Replacements);
                    }

                    byte[] fixedFile;
                    if (TryFixFile(file, replacements, out fixedFile))
                    {
                        File.WriteAllBytes(file, fixedFile.ToArray());
                    }
                }
            }
        }
        private static SarifErrorListItem GetDesignTimeViewModel1()
        {
            SarifErrorListItem viewModel = new SarifErrorListItem();
            viewModel.Message = "Potential mismatch between sizeof and countof quantities. Use sizeof() to scale byte sizes.";

            viewModel.Tool = new ToolModel()
            {
                Name = "FxCop",
                Version = "1.0.0.0",
            };

            viewModel.Rule = new RuleModel()
            {
                Id = "CA1823",
                Name = "Avoid unused private fields",
                HelpUri = "http://aka.ms/analysis/ca1823",
                DefaultLevel = "Unknown"
            };

            viewModel.Invocation = new InvocationModel()
            {
                CommandLine = @"""C:\Temp\Foo.exe"" target.file /o out.sarif",
                FileName = @"C:\Temp\Foo.exe",
            };

            viewModel.Locations.Add(new Models.AnnotatedCodeLocationModel()
            {
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Region = new CodeAnalysis.Sarif.Region(11, 1, 11, 2, 0, 0),
            });

            viewModel.Locations.Add(new Models.AnnotatedCodeLocationModel()
            {
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Region = new CodeAnalysis.Sarif.Region(12, 1, 12, 2, 0, 0),
            });

            viewModel.RelatedLocations.Add(new Models.AnnotatedCodeLocationModel()
            {
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Region = new CodeAnalysis.Sarif.Region(21, 1, 21, 2, 0, 0),
            });

            viewModel.RelatedLocations.Add(new Models.AnnotatedCodeLocationModel()
            {
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Region = new CodeAnalysis.Sarif.Region(22, 1, 22, 2, 0, 0),
            });

            viewModel.RelatedLocations.Add(new Models.AnnotatedCodeLocationModel()
            {
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Region = new CodeAnalysis.Sarif.Region(23, 1, 23, 2, 0, 0),
            });

            viewModel.CallTrees.Add(new CallTree(
                new List<CallTreeNode>
                {
                    new CallTreeNode
                    {
                        Location = new AnnotatedCodeLocation
                        {
                            Kind = AnnotatedCodeLocationKind.Assignment
                        }
                    },

                    new CallTreeNode
                    {
                        Location = new AnnotatedCodeLocation
                        {
                            Kind = AnnotatedCodeLocationKind.Call,
                            Target = "my_func"
                        },
                        Children = new List<CallTreeNode>
                        {
                            new CallTreeNode
                            {
                                Location = new AnnotatedCodeLocation
                                {
                                    Kind = AnnotatedCodeLocationKind.CallReturn
                                }
                            }
                        }
                    }
                }));

            StackCollection stack1 = new StackCollection("Stack A1");
            stack1.Add(new StackFrameModel()
            {
                Message = "Message A1.1",
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Line = 11,
                Column = 1,
                FullyQualifiedLogicalName ="My.Assembly.Main(string[] args)",
                Module = "My.Module.dll",
            });
            stack1.Add(new StackFrameModel()
            {
                Message = "Message A1.2",
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Line = 12,
                Column = 1,
                FullyQualifiedLogicalName = "Our.Shared.Library.Method(int param)",
                Module = "My.Module.dll",
            });
            stack1.Add(new StackFrameModel()
            {
                Message = "Message A1.3",
                FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs",
                Line = 1,
                Column = 1,
                FullyQualifiedLogicalName = "Your.PIA.External()",
            });
            viewModel.Stacks.Add(stack1);

            FixModel fix1 = new FixModel("Replace *.Close() with *.Dispose().");
            FileChangeModel fileChange11 = new FileChangeModel();
            fileChange11.FilePath = @"D:\GitHub\NuGet.Services.Metadata\src\Ng\Catalog2Dnx.cs";
            fileChange11.Replacements.Add(new ReplacementModel()
            {
                Offset = 1234,
                DeletedLength = ".Close()".Length,
                InsertedString = ".Dispose()",
            });
            fix1.FileChanges.Add(fileChange11);
            viewModel.Fixes.Add(fix1);

            return viewModel;
        }