nDepend - how to modify "JustMyCode" queries using nDepend API?

183 views Asked by At

My goal is to modify "JustMyCode" queries using nDepend API. I am using code like:

var justMyCodeGroup = prj.CodeQueries.CodeQueriesSet.ChildGroups.Single(x => x.Name.Contains("JustMyCode"));
var originalQuery = justMyCodeGroup.ChildQueries
            .Single(x => x.QueryString.Contains("Discard generated Types from JustMyCode"));
var changedQuery = originalQuery.Controller.CreateQuery(originalQuery.IsActive,
            query,
            originalQuery.
                DisplayStatInReport,
            originalQuery.DisplayListInReport,
            originalQuery.DisplaySelectionViewInReport,
            originalQuery.IsCriticalRule);

var justMyCodeGroupWithModifiedQuery = justMyCodeGroup.ReplaceQuery(originalQuery, changedQuery);
 prj.CodeQueries.CodeQueriesSet.ReplaceGroup(justMyCodeGroup, justMyCodeGroupWithModifiedQuery);

However, when I run the code above I get ArgumentException with message:

newGroup.Controller is different than this groupOfGroups.Controller

Any help ?

Update 1: I also tried code:

       var justMyCodeGroup = prj.CodeQueries.CodeQueriesSet.ChildGroups.Single(x => x.Name.Contains("JustMyCode"));
        var originalQuery = justMyCodeGroup.ChildQueries
            .Single(x => x.QueryString.Contains("Discard generated Types from JustMyCode"));
        var changedQuery = originalQuery.Controller.CreateQuery(originalQuery.IsActive,
            query,
            originalQuery.
                DisplayStatInReport,
            originalQuery.DisplayListInReport,
            originalQuery.DisplaySelectionViewInReport,
            originalQuery.IsCriticalRule);

        var justMyCodeGroupWithModifiedQuery = justMyCodeGroup.ReplaceQuery(originalQuery, changedQuery);

        var newQueries = new List<IQuery>();
        foreach (var q in justMyCodeGroup.ChildQueries)
        {
            if (q.QueryString.Contains("Discard generated Types from JustMyCode"))
            {
                continue;
            }

            newQueries.Add(prj.CodeQueries.CodeQueriesSet.Controller.CreateQuery(q.IsActive, q.QueryString,
                q.DisplayStatInReport, q.DisplayListInReport, q.DisplaySelectionViewInReport, q.IsCriticalRule));
        }
        newQueries.Add(prj.CodeQueries.CodeQueriesSet.Controller.CreateQuery(originalQuery.IsActive, query, originalQuery.DisplayStatInReport, originalQuery.DisplayListInReport, originalQuery.DisplaySelectionViewInReport, originalQuery.IsCriticalRule));

        var newGroup = prj.CodeQueries.CodeQueriesSet.Controller.CreateGroup(justMyCodeGroup.Name,
            justMyCodeGroup.IsActive, justMyCodeGroup.ShownInReport, newQueries, new List<IGroup>());

        prj.CodeQueries.CodeQueriesSet.RemoveGroup(justMyCodeGroup);
        prj.CodeQueries.CodeQueriesSet.AddGroup(newGroup);

Right now, RemoveGroup throws exception:

this group of groups doesn't contain groupToRemove.

Update 2: And I also wonder, why does this code return false ?

 var justMyCodeGroup = prj.CodeQueries.CodeQueriesSet.ChildGroups.Single(x => x.Name.Contains("JustMyCode"));
 prj.CodeQueries.CodeQueriesSet.ContainsGroup(justMyCodeGroup)
1

There are 1 answers

0
Patrick from NDepend team On BEST ANSWER

Refer to the PowerTools source file:

$NDependInstallDir$\NDepend.PowerTools.SourceCode\CQL2CQLinq\CQL2CQLinqPowerTool.cs

This PowerTools convert code queries written with old CQL syntax into code queries written with new CQLinq syntax, hence it loads the queries set from a project, update CQL queries, and then save the new queries set in the project.

The queriesController is gathered this way...

     var queriesSet = project.CodeQueries.CodeQueriesSet;
     var queriesController = queriesSet.Controller;

... and then used this way to modify the queries set:

queriesController.DoUpdateQueryObject(query, newQuery);