How to add files dynamically to the filesystem for analysis


i would like to know how to add files dynamically (programmatically) for analysis !
Actually, i have some groovy and javascript source codes which are embedded into huge XML files.
Those XML files are currently analyzed by the default XML Sensor and also by a custom sensor i created which run some custom rules and compute custom measures as well.

I was thinking about extracting those groovy & javascript codes by creating thet related .groovy files and .js files, and then hope for the Groovy & Javascript sensors to automatically do the job.
But i kind of guess it’s not really going to work that way.

What do you advice me to do ? You will notice that i avoided to extract the groovy & javascript before the execution of the sonar analysis. But if it’s really necessary, i will do it.



AFAIK this is the only (future-proof) way to solve such situation. You will have to create a physical layout of files/folders before starting the scanner.

Hmm, ok.
And is it possible to exclude those physical layout generated on the fly for CPD, blame, line count, and other types of measures ?

CPD has a property to exclude files, but you may be interested into finding duplications in your JS/Groovy code.
The big problem with this approach is the blame, since all files will be considered as uncommited. So all SCM related features will be broken (issue auto-assignment, PR analysis, …).

To better understand the big picture, are your XML files something standard?

actually, my main goal is to find duplications within those embedded groovy and javascript codes.
But indeed, the blame information will not work in this case, but it’s not an issue for my use case.
I mainly try to detect similarities and duplications.
Because currently, when those groovy codes are xml encoded into a big single line, CPD will not detect the duplications.
And yes, the XML files are standard (i mean well formed XML files). Otherwise, i don’t really know what you meant.

I wanted to know if the fact to have embedded JS/Groovy in XML files is coming from a well known framework, to evaluate if we should consider supporting that use case out of the box. Fox example our JS and CSS analyzers are able to analyze code embedded into HTML files.

Hmm, we are using a proprietary solution which embed some source code in some XML nodes.
Though, if you tell me there are some analyzers which do it for html files, why wouldn’t it be possible for the xml files by looking some specific nodes ?

Technically, everything is possible :slight_smile: But we currently don’t have a generic support of multi-language files, and no plan to work on this at short term. We are only covering specific cases that are very common (like JS into HTML).

OK, I understand.
Would you please help me find the source code which does that job for html files please !?

My example was wrong, we don’t even support currently JS into HTML. What we support is JS into Vue.js template files. I found this in our parser:

Ok, i see.
I think that the simplest way is to extract those groovy and javascript codes before the real sonar analysis.
Do you think there is a way to include that extract in my sonar plugin ? … so that it get included into the “maven sonar:sonar” step ? … that would help me to avoid creating a specific tool only for that purpose.
Some step before the filesystem get initialized or fully initialized … maybe there is a trick, workaround :slight_smile: