1616// `.Stop()` call. The IDisposable/pool/local detectors remain syntactic for now
1717// (P-014 rollout: the event fact goes type-aware first).
1818//
19- // Usage: ownsharp-extract <file.cs | dir> [more ...] [-o facts.json]
19+ // Usage: ownsharp-extract <file.cs | dir | *.csproj | *.sln> [more ...] [-o facts.json]
20+ // ownsharp-extract --project App.csproj (flag twin of the positional form)
21+ // ownsharp-extract --solution App.sln
2022//
21- // Inputs may be .cs files or directories. A directory is walked recursively for
22- // *.cs, skipping build output (bin/obj), VCS/vendor dirs (.git, node_modules)
23- // and generated files (*.g.cs, *.Designer.cs) — so you can point it at a whole
24- // repo (this is what the `own-check` script / GitHub Action do).
23+ // Inputs may be .cs files, directories, a .csproj, or a .sln. A directory is walked
24+ // recursively for *.cs, skipping build output (bin/obj), VCS/vendor dirs (.git,
25+ // node_modules) and generated files (*.g.cs, *.Designer.cs) — so you can point it at a
26+ // whole repo (this is what the `own-check` script / GitHub Action do). A .csproj resolves
27+ // to its source set (SDK-style directory scan + concrete linked <Compile> files; no full
28+ // MSBuild evaluation — see ProjectCsFiles); a .sln fans out over its member projects.
2529
2630using System . Text . Json ;
2731using System . Xml . Linq ;
6771for ( int i = 0 ; i < args . Length ; i ++ )
6872{
6973 if ( args [ i ] == "-o" && i + 1 < args . Length ) outPath = args [ ++ i ] ;
74+ // `--project <App.csproj>` / `--solution <App.sln>`: the explicit-flag twin of passing the
75+ // project/solution as a positional input (both resolve through Expand). The flag form matches
76+ // the advertised `ownsharp extract --project ...` UX borrowed from the roslyn-tools CLI shape;
77+ // the positional form keeps the command unambiguous next to dotnet's own `run --project`.
78+ else if ( ( args [ i ] == "--project" || args [ i ] == "--solution" ) && i + 1 < args . Length ) rawInputs . Add ( args [ ++ i ] ) ;
7079 else if ( args [ i ] == "--ref-dir" && i + 1 < args . Length ) refDirs . Add ( args [ ++ i ] ) ;
7180 else if ( args [ i ] == "--no-event-leaks" ) emitEvents = false ;
7281 else if ( args [ i ] == "--flow-locals" ) flowLocals = true ;
7786
7887if ( rawInputs . Count == 0 )
7988{
80- Console . Error . WriteLine ( "usage: ownsharp-extract <file.cs | dir> [...] [-o facts.json] [--ref-dir <bin-dir>]" ) ;
89+ Console . Error . WriteLine ( "usage: ownsharp-extract <file.cs | dir | *.csproj | *.sln > [...] [-o facts.json] [--ref-dir <bin-dir>]" ) ;
8190 return 2 ;
8291}
8392
@@ -113,9 +122,91 @@ static bool IsSkipped(string path)
113122 return IsGenerated ( path ) ;
114123}
115124
116- // Expand directories into their .cs files; pass explicit files through as-is.
117- // IgnoreInaccessible tolerates an unreadable subdir mid-walk (otherwise the
118- // whole scan would abort with an unhandled exception on a locked directory).
125+ // Resolve a `.csproj` to its C# source set. Doing this the MSBuild way needs a full
126+ // project evaluation (and the `Microsoft.CodeAnalysis.Workspaces.MSBuild` + MSBuildLocator
127+ // dependency that P-014 / the "ProjectDependencies as a category" note deliberately parks
128+ // for the DI/solution-graph work — not for the v0 leak extractor). The pragmatic resolution
129+ // that covers the SDK-style common case WITHOUT that baggage: scan the project's directory
130+ // recursively for *.cs — exactly the SDK default-compile-items behaviour, with bin/obj and
131+ // generated files already excluded by IsSkipped — and additionally pick up any concrete
132+ // linked `<Compile Include="..\Shared\Foo.cs" />` that points OUTSIDE the project tree.
133+ // This can over-approximate a legacy explicit-list project (it analyses sibling *.cs the
134+ // project might exclude), which for a fact extractor is harmless: it never invents files,
135+ // it only reads a few extra. Globs inside <Compile Include> are left to the directory scan.
136+ // Builds a list rather than yielding so the XML read can sit in a try/catch (an iterator
137+ // may not yield from inside one) and a malformed project degrades to the directory scan.
138+ static List < string > ProjectCsFiles ( string csproj , EnumerationOptions opts )
139+ {
140+ var full = Path . GetFullPath ( csproj ) ;
141+ var dir = Path . GetDirectoryName ( full ) ?? "." ;
142+ var result = new List < string > ( ) ;
143+ foreach ( var f in Directory . EnumerateFiles ( dir , "*.cs" , opts ) )
144+ if ( ! IsSkipped ( f ) )
145+ result . Add ( f ) ;
146+
147+ XDocument ? doc = null ;
148+ try { doc = XDocument . Load ( full ) ; }
149+ catch ( Exception ex )
150+ {
151+ Console . Error . WriteLine (
152+ $ "ownsharp-extract: { csproj } : reading <Compile> links failed ({ ex . Message } ); used directory scan only") ;
153+ }
154+
155+ var dirPrefix = dir . EndsWith ( Path . DirectorySeparatorChar ) ? dir : dir + Path . DirectorySeparatorChar ;
156+ foreach ( var inc in ( doc ? . Descendants ( ) ?? Enumerable . Empty < XElement > ( ) )
157+ . Where ( e => e . Name . LocalName == "Compile" )
158+ . Select ( e => e . Attribute ( "Include" ) ? . Value )
159+ . Where ( v => ! string . IsNullOrWhiteSpace ( v ) ) )
160+ {
161+ if ( inc ! . IndexOfAny ( new [ ] { '*' , '?' } ) >= 0 ) continue ; // a glob — the dir scan covers it
162+ var path = Path . GetFullPath ( Path . Combine ( dir , inc . Replace ( '\\ ' , Path . DirectorySeparatorChar ) ) ) ;
163+ if ( path . EndsWith ( ".cs" , StringComparison . OrdinalIgnoreCase )
164+ && File . Exists ( path ) && ! IsSkipped ( path )
165+ && ! path . StartsWith ( dirPrefix , StringComparison . Ordinal ) ) // in-dir links already yielded
166+ result . Add ( path ) ;
167+ }
168+ return result ;
169+ }
170+
171+ // Resolve a classic `.sln` to its member `.csproj` paths. The solution file lists each project
172+ // as `Project("{type-guid}") = "Name", "rel\path.csproj", "{guid}"`; solution folders use the
173+ // same line shape but their path is not a `.csproj`, so filtering on the extension drops them.
174+ // Text parsing (no MSBuild) keeps this dependency-free; a missing member is reported and skipped,
175+ // never fatal — a solution-wide scan should survive one stale project reference.
176+ static List < string > SolutionProjects ( string sln )
177+ {
178+ var dir = Path . GetDirectoryName ( Path . GetFullPath ( sln ) ) ?? "." ;
179+ var projects = new List < string > ( ) ;
180+ string [ ] lines ;
181+ try { lines = File . ReadAllLines ( sln ) ; }
182+ catch ( Exception ex )
183+ {
184+ Console . Error . WriteLine ( $ "ownsharp-extract: cannot read solution { sln } ({ ex . Message } )") ;
185+ return projects ;
186+ }
187+ foreach ( var line in lines )
188+ {
189+ var t = line . TrimStart ( ) ;
190+ if ( ! t . StartsWith ( "Project(" , StringComparison . Ordinal ) ) continue ;
191+ var eq = t . IndexOf ( '=' ) ;
192+ if ( eq < 0 ) continue ;
193+ // After '=' the fields are comma-separated, each double-quoted: "Name", "relpath", "{guid}".
194+ var parts = t . Substring ( eq + 1 ) . Split ( ',' ) ;
195+ if ( parts . Length < 2 ) continue ;
196+ var rel = parts [ 1 ] . Trim ( ) . Trim ( '"' ) ;
197+ if ( ! rel . EndsWith ( ".csproj" , StringComparison . OrdinalIgnoreCase ) ) continue ;
198+ var path = Path . GetFullPath ( Path . Combine ( dir , rel . Replace ( '\\ ' , Path . DirectorySeparatorChar ) ) ) ;
199+ if ( File . Exists ( path ) ) projects . Add ( path ) ;
200+ else Console . Error . WriteLine ( $ "ownsharp-extract: { sln } : project not found: { rel } ") ;
201+ }
202+ return projects ;
203+ }
204+
205+ // Expand inputs into their .cs files. A directory is walked recursively; a `.csproj`/`.sln`
206+ // is resolved to its source set (so `ownsharp-extract App.csproj` / `App.sln` works, the
207+ // CLI-first project input borrowed from the roslyn-tools tooling shape); an explicit file
208+ // passes through as-is. IgnoreInaccessible tolerates an unreadable subdir mid-walk (otherwise
209+ // the whole scan would abort with an unhandled exception on a locked directory).
119210static IEnumerable < string > Expand ( IEnumerable < string > roots )
120211{
121212 var opts = new EnumerationOptions
@@ -131,6 +222,17 @@ static IEnumerable<string> Expand(IEnumerable<string> roots)
131222 if ( ! IsSkipped ( f ) )
132223 yield return f ;
133224 }
225+ else if ( p . EndsWith ( ".sln" , StringComparison . OrdinalIgnoreCase ) )
226+ {
227+ foreach ( var proj in SolutionProjects ( p ) )
228+ foreach ( var f in ProjectCsFiles ( proj , opts ) )
229+ yield return f ;
230+ }
231+ else if ( p . EndsWith ( ".csproj" , StringComparison . OrdinalIgnoreCase ) )
232+ {
233+ foreach ( var f in ProjectCsFiles ( p , opts ) )
234+ yield return f ;
235+ }
134236 else
135237 {
136238 yield return p ;
0 commit comments