nApiMonkey.PackageConfigReader.readConfig C# (CSharp) Method

readConfig() public method

public readConfig ( List packageFilePaths, string root ) : List>.Dictionary
packageFilePaths List
root string
return List>.Dictionary
        public Dictionary<PackageElement, List<string>> readConfig(List<string> packageFilePaths, string root)
        {
            Dictionary<PackageElement, List<string>> map = new Dictionary<PackageElement, List<string>>();
            string fileName;
            Console.WriteLine("In read config");
            foreach (string path in packageFilePaths)
            {
                //Ignore packages.config files in these folders
                if (path.Contains(@"\.nuget") || path.Contains(@"\packages") || path.Contains(@"\Samples\"))
                    continue;
                fileName = path + "packages.config";
                var file = new PackageReferenceFile(fileName);

                string path1 = path.Replace(root, "");
                Console.WriteLine(path1);
                foreach (PackageReference packageReference in file.GetPackageReferences())
                {
                    PackageElement pe = new PackageElement(packageReference.Id, packageReference.Version);
                    if (!map.ContainsKey(pe))
                    {
                        //Console.WriteLine("add to map: "+packageReference.Id);
                        List<string> ans = new List<string>();
                        ans.Add(path1);
                        map.Add(pe, ans);
                    }
                    else
                    {
                        List<string> ans= map[pe];
                        ans.Add(path1);
                        map[pe] = ans;
                        //Console.WriteLine(" contains key "+ packageReference .Id+" "+ ans.Count);
                    }
                    Console.WriteLine("PackageId={0}, Version={1}", packageReference.Id, packageReference.Version);
                }
            }

            return map;
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            // set these variables before runing the tool
            string project_name = "";
            string project_path = @"";
            string sandbox_path = @"";
            string oldRootSol = @"";
            string testLocation = @"";

            //define script paths
            string git_script = @"G:\new_demo\ApiMonkey\C#\nApiMonkey\nApiMonkey\scripts\gitcmd.sh";
            string build_script = @"G:\new_demo\ApiMonkey\C#\nApiMonkey\nApiMonkey\scripts\build.bat";
            string copy_script = @"G:\new_demo\ApiMonkey\C#\nApiMonkey\nApiMonkey\scripts\script.bat";
            //end of variables

            //Clone project from git. Can be skipped and done manually as well.
            System.Diagnostics.Process.Start(git_script, project_path).WaitForExit();

            Report repo = new Report();
            repo.ReportLocation = project_path;
            repo.ReportName = project_name + "Report.md";
            repo.removeIfExists();

            //Run msbuild and test executino on original project. and write the results to the report.
            System.Diagnostics.Process.Start(build_script, oldRootSol + " " + project_name + ".sln " + testLocation).WaitForExit();
            TRXReader tr = new TRXReader();
            repo.writeOriginalReport(oldRootSol, tr);

            PackageConfigReader reader = new PackageConfigReader();
            //Read the entire project for packages.config files and build a dictionary with packages and the projects list which reference them.
            List<string> packageFilePaths = reader.readAllConfigs(oldRootSol);
            Dictionary<PackageElement, List<string>> dictionary = reader.readConfig(packageFilePaths, oldRootSol);
            List<PackageElement> oldlist = new List<PackageElement>(dictionary.Keys);
            //Check if newer versions are available for the packages.
            List<PackageElement> update = checkUpdate(oldlist);
            StringBuilder testResult = new StringBuilder();
            if (update == null) Console.Write("No updates found");
            else
            {
                //Go through all packages where update is available and create sandboxes for each of them
                foreach (PackageElement e in update)
                {
                    bool success=true;
                    List<string> paths = dictionary[e];
                    string newRootSol = sandbox_path + @"\" + project_name + @"_" + e.Packageid.Substring(0, e.Packageid.Length / 2 + 1) + e.Version.ToNormalizedString();
                    //string newProjectPath = newRootSol + @"\" + project_name;
                    Directory.CreateDirectory(newRootSol);
                    System.Diagnostics.Process.Start(copy_script, oldRootSol + " " + newRootSol + " " + project_name + ".sln ").WaitForExit();
                    //Update references to the new version for all projects which refer to it
                    foreach (string projectPath in paths)
                    {
                        Console.WriteLine("Updating " + projectPath);
                        PackageConfigReader newConfReader = new PackageConfigReader();
                        string newPathToPackages = newRootSol + projectPath + @"packages.config";
                        string newProjectPath = newRootSol + projectPath;
                        string project = Directory.EnumerateFiles(newProjectPath, "*.csproj?").First();
                        UpdateCommand updateCmd = new UpdateCommand();
                        bool update_success=updateCmd.Execute(e.Packageid, project, e.Version, newRootSol + @"\packages");
                        if (update_success == false)
                        {
                            success = false;
                            break;
                        }
                            newConfReader.writeToConfig(newPathToPackages, e.Packageid, e.Version);
                    }
                    if (success)
                    {
                        //If uodate is successful, run the build and write results to final report.
                        System.Diagnostics.Process.Start(@"G:\new_demo\ApiMonkey\C#\nApiMonkey\nApiMonkey\scripts\build.bat", newRootSol + " " + project_name + ".sln " + testLocation).WaitForExit();
                        tr = new TRXReader();
                        testResult = tr.read(newRootSol + @"\testResults.trx");
                        repo.writeBuildReport(newRootSol, e.Packageid, e.Version, testResult,true);
                    }
                    else
                        repo.writeBuildReport(newRootSol, e.Packageid, e.Version, testResult, false);
                }

            }

            Console.ReadKey();
        }
All Usage Examples Of nApiMonkey.PackageConfigReader::readConfig