Repository navigation
Analyse C# code through a Roslyn fact extractor (experimental) #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6c50e6d
2c2d698
6972700
fc57948
24e4ec6
3ed179c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Optional: exercises the C# fact extractor and the tests that need it. | ||
| # The main Maven workflows need no .NET; the tests they cannot run are run here. | ||
|
|
||
| name: C# analysis | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| paths: | ||
| - 'csharp-fact-extractor/**' | ||
| - 'codebase-graph-builder/**' | ||
| - 'change-proneness-ranker/**' | ||
| - 'report/**' | ||
| - '.github/workflows/csharp.yml' | ||
| pull_request: | ||
| branches: [ main ] | ||
| paths: | ||
| - 'csharp-fact-extractor/**' | ||
| - 'codebase-graph-builder/**' | ||
| - 'change-proneness-ranker/**' | ||
| - 'report/**' | ||
| - '.github/workflows/csharp.yml' | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| csharp: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Check out Git repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up JDK 25 | ||
| uses: actions/setup-java@v4 | ||
| with: | ||
| java-version: 25 | ||
| distribution: 'zulu' | ||
|
|
||
| - name: Set up .NET 10 | ||
| uses: actions/setup-dotnet@v4 | ||
| with: | ||
| dotnet-version: '10.0.x' | ||
|
|
||
| - name: Restore the fixtures the tests analyse | ||
| run: | | ||
| dotnet restore csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.sln | ||
| dotnet restore csharp-fact-extractor/fixtures/TwoProjects/Alpha/Alpha.csproj | ||
| dotnet restore csharp-fact-extractor/fixtures/Shapes/Shapes.csproj | ||
| dotnet restore csharp-fact-extractor/fixtures/Members/Members.csproj | ||
|
|
||
| - name: Test the extractor | ||
| run: dotnet test csharp-fact-extractor/tests/CSharpFactExtractor.Tests.csproj | ||
|
|
||
| - name: Publish the extractor | ||
| run: dotnet publish csharp-fact-extractor/src -c Release -o csharp-fact-extractor/publish | ||
|
|
||
| - name: Run the Java tests that need the extractor | ||
| env: | ||
| REFACTORFIRST_CSHARP_EXTRACTOR: ${{ github.workspace }}/csharp-fact-extractor/publish/CSharpFactExtractor.dll | ||
| run: mvn -B package -Dtest='CSharp*,CompositeGraphBuilderCSharp*,GitLogReaderCSharp*' -Dsurefire.failIfNoSpecifiedTests=false | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package org.hjug.git; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.util.Map; | ||
| import org.eclipse.jgit.api.Git; | ||
| import org.eclipse.jgit.revwalk.RevCommit; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| class GitLogReaderCSharpTest { | ||
|
|
||
| @TempDir | ||
| File tempFolder; | ||
|
|
||
| private Git git; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| git = Git.init().setDirectory(tempFolder).call(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| git.getRepository().close(); | ||
| } | ||
|
|
||
| @Test | ||
| void givenSourcePaths_whenClassified_thenCSharpCountsAlongsideJavaAndKotlin() { | ||
| assertTrue(GitLogReader.isAnalyzedSourceFile("src/Greeter.cs")); | ||
| assertTrue(GitLogReader.isAnalyzedSourceFile("src/Main.java")); | ||
| assertTrue(GitLogReader.isAnalyzedSourceFile("src/Main.kt")); | ||
| assertFalse(GitLogReader.isAnalyzedSourceFile("src/App.csproj")); | ||
| assertFalse(GitLogReader.isAnalyzedSourceFile("src/App.sln")); | ||
| assertFalse(GitLogReader.isAnalyzedSourceFile("README.md")); | ||
| } | ||
|
|
||
| @Test | ||
| void givenCommitsTouchingCsFiles_whenCounted_thenTheyAreIncludedAndOtherFilesAreNot() throws Exception { | ||
| GitLogReader gitLogReader = new GitLogReader(git); | ||
|
|
||
| write("Greeter.cs", "class Greeter { }"); | ||
| write("README.md", "hello"); | ||
| git.add().addFilepattern(".").call(); | ||
| RevCommit first = git.commit().setMessage("first").call(); | ||
|
|
||
| // Commits need different timestamps; they are keyed by commit time in seconds. | ||
| Thread.sleep(1000); | ||
|
|
||
| write("Greeter.cs", "class Greeter { int x; }"); | ||
| write("MessageStore.cs", "class MessageStore { }"); | ||
| write("README.md", "hello again"); | ||
| git.add().addFilepattern(".").call(); | ||
| RevCommit second = git.commit().setMessage("second").call(); | ||
|
|
||
| Map<Integer, Integer> counts = gitLogReader.captureChangeCountByCommitTimestamp(); | ||
|
|
||
| assertEquals(1, counts.get(first.getCommitTime()).intValue(), "Greeter.cs only; README.md is not source"); | ||
| assertEquals(2, counts.get(second.getCommitTime()).intValue(), "Greeter.cs changed, MessageStore.cs added"); | ||
| } | ||
|
|
||
| @Test | ||
| void givenACsFileCommittedTwice_whenItsLogIsRead_thenBothCommitsAreCounted() throws Exception { | ||
| GitLogReader gitLogReader = new GitLogReader(git); | ||
|
|
||
| write("Greeter.cs", "class Greeter { }"); | ||
| git.add().addFilepattern(".").call(); | ||
| git.commit().setMessage("first").call(); | ||
| Thread.sleep(1000); | ||
| write("Greeter.cs", "class Greeter { int x; }"); | ||
| git.add().addFilepattern(".").call(); | ||
| git.commit().setMessage("second").call(); | ||
|
|
||
| ScmLogInfo log = gitLogReader.fileLog("Greeter.cs"); | ||
|
|
||
| assertEquals(2, log.getCommitCount()); | ||
| } | ||
|
|
||
| private void write(String name, String content) throws IOException { | ||
| Files.write(new File(git.getRepository().getWorkTree(), name).toPath(), content.getBytes(UTF_8)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import java.io.IOException; | ||
| import java.util.*; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.hjug.graphbuilder.graphbuilder.CSharpSourceFileGraphBuilder; | ||
| import org.hjug.graphbuilder.graphbuilder.JavaSourceFileGraphBuilder; | ||
| import org.hjug.graphbuilder.graphbuilder.KotlinSourceFileGraphBuilder; | ||
| import org.hjug.graphbuilder.metrics.DisharmonyDetector.ClassDisharmony; | ||
|
|
@@ -25,6 +26,17 @@ | |
| @Slf4j | ||
| public class CompositeGraphBuilder { | ||
|
|
||
| private final CSharpSourceFileGraphBuilder csharpBuilder; | ||
|
|
||
| public CompositeGraphBuilder() { | ||
| this(new CSharpSourceFileGraphBuilder()); | ||
| } | ||
|
|
||
| /** For tests: lets the C# reader be replaced so no .NET process is needed. */ | ||
| CompositeGraphBuilder(CSharpSourceFileGraphBuilder csharpBuilder) { | ||
| this.csharpBuilder = csharpBuilder; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a unified graph for Java and Kotlin source files in a repository. | ||
| * | ||
|
|
@@ -85,14 +97,29 @@ public CodebaseGraphDTO getCodebaseGraphDTO(String repositoryPath, String reposi | |
| // compile dependency of this module, so the Kotlin parser is always | ||
| // on the classpath; a failure here (parse error, IO, etc.) falls back | ||
| // to the Java-only graph. | ||
| CodebaseGraphDTO merged = javaDto; | ||
| try { | ||
| KotlinSourceFileGraphBuilder kotlinBuilder = new KotlinSourceFileGraphBuilder(); | ||
| CodebaseGraphDTO kotlinDto = kotlinBuilder.buildGraph(repositoryPath, repositoryRoot, config); | ||
| return merge(javaDto, kotlinDto); | ||
| merged = merge(javaDto, kotlinDto); | ||
| } catch (Exception e) { | ||
| log.warn("Kotlin analysis failed; falling back to Java-only graph", e); | ||
| return javaDto; | ||
| } | ||
|
|
||
| // C# needs .NET and a separate extractor program, so it is only attempted when the | ||
| // folder holds a solution or project. A failure here never costs the Java/Kotlin result. | ||
| try { | ||
| if (csharpBuilder.hasCSharpSources(repositoryPath)) { | ||
| CodebaseGraphDTO csharpDto = csharpBuilder.buildGraph(repositoryPath, repositoryRoot, config); | ||
| merged = merge(merged, csharpDto); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve distinct Java and C# source identities. If Java and C# both declare Use language-qualified identities, or another collision-safe representation, before merging these graphs. Java/Kotlin name reconciliation cannot identify a C# declaration as the same JVM class.
|
||
| } | ||
| } catch (IOException e) { | ||
| // Expected failures (no extractor configured, .NET missing, bad facts file) explain themselves. | ||
| log.warn("C# analysis failed; continuing without C#: {}", e.getMessage()); | ||
| } catch (Exception e) { | ||
| log.warn("C# analysis failed; continuing without C#", e); | ||
| } | ||
| return merged; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.