nApiMonkey.PackageConfigReader.writeToConfig C# (CSharp) Method

writeToConfig() public method

public writeToConfig ( string fileName, string packageId, SemanticVersion newversion ) : void
fileName string
packageId string
newversion SemanticVersion
return void
        public void writeToConfig(string fileName, string packageId, SemanticVersion newversion)
        {
            var file = new PackageReferenceFile(fileName);
            file.DeleteEntry(packageId, null);
            file.AddEntry(packageId, newversion);
        }

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::writeToConfig