1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . Collections . Immutable ;
4
2
using System . IO ;
5
3
using System . Linq ;
6
4
using System . Text . Json ;
5
+ using System . Threading ;
7
6
using System . Threading . Tasks ;
8
7
using Microsoft . CodeAnalysis ;
9
8
using Microsoft . CodeAnalysis . CSharp ;
10
- using Microsoft . CodeAnalysis . MSBuild ;
11
9
12
- internal partial class SourceExpanderCommand : ConsoleAppBase
10
+ partial struct SourceExpanderCommand
13
11
{
14
- [ RootCommand ]
12
+ /// <summary>
13
+ /// Expand embedded source.
14
+ /// </summary>
15
+ /// <param name="expand">Expanding file.</param>
16
+ /// <param name="output">-o,Output file</param>
17
+ /// <param name="project">-p,csproj file</param>
18
+ /// <param name="staticEmbedding">-s,Static embedding text</param>
19
+ /// <param name="cancellationToken"></param>
20
+ /// <returns></returns>
21
+ /// <exception cref="InvalidOperationException"></exception>
22
+ [ Command ( "" ) ]
15
23
public async Task Expand (
16
- [ Option ( 0 , "expanding file" ) ] string expand ,
17
- [ Option ( "o" , "output file" ) ] string ? output = null ,
18
- [ Option ( "p" , "csproj file" ) ] string ? project = null ,
19
- [ Option ( "s" , "static embedding text" ) ] string ? staticEmbedding = null )
24
+ [ Argument ] string expand ,
25
+ string ? output = null ,
26
+ string ? project = null ,
27
+ string ? staticEmbedding = null ,
28
+ CancellationToken cancellationToken = default )
20
29
{
21
30
project ??= PathUtil . GetProjectPath ( expand ) ;
22
31
project = Path . GetFullPath ( project ) ;
23
32
24
- var ( compilation , csProject ) = await GetCompilation ( project ) ;
33
+ var ( compilation , csProject ) = await GetCompilation ( project , cancellationToken : cancellationToken ) ;
25
34
if ( compilation is not CSharpCompilation csCompilation )
26
35
throw new InvalidOperationException ( "Failed to get compilation" ) ;
27
36
if ( csProject . ParseOptions is not CSharpParseOptions parseOptions )
28
37
throw new InvalidOperationException ( "Failed to get parseOptions" ) ;
29
38
30
39
var config = new ExpandConfig (
31
- matchFilePatterns : new [ ] { new FileInfo ( expand ) . FullName } ,
40
+ matchFilePatterns : [ new FileInfo ( expand ) . FullName ] ,
32
41
staticEmbeddingText : staticEmbedding ) ;
33
42
34
43
var ( _, code ) = new EmbeddedLoader ( csCompilation ,
35
44
parseOptions ,
36
45
config ,
37
- Context . CancellationToken )
46
+ cancellationToken )
38
47
. ExpandedCodes ( )
39
48
. SingleOrDefault ( ) ;
40
49
@@ -44,28 +53,37 @@ public async Task Expand(
44
53
45
54
if ( output is null )
46
55
{
47
- Console . Write ( code ) ;
56
+ Output . Write ( code ) ;
48
57
}
49
58
else
50
59
{
51
60
output = Path . GetFullPath ( output ) ;
52
61
53
- Console . WriteLine ( $ "expanding file: { project } ") ;
54
- Console . WriteLine ( $ "project: { expand } ") ;
55
- Console . WriteLine ( $ "output: { output } ") ;
62
+ Output . WriteLine ( $ "expanding file: { project } ") ;
63
+ Output . WriteLine ( $ "project: { expand } ") ;
64
+ Output . WriteLine ( $ "output: { output } ") ;
56
65
57
- await File . WriteAllTextAsync ( output , code ) ;
66
+ await File . WriteAllTextAsync ( output , code , cancellationToken : cancellationToken ) ;
58
67
}
59
68
}
60
69
61
- [ Command ( "expand-all" , "Show expanded codes json" ) ]
70
+ /// <summary>
71
+ /// Show expanded codes json.
72
+ /// </summary>
73
+ /// <param name="project">Target project(.csproj).</param>
74
+ /// <param name="staticEmbedding">-s,Static embedding text.</param>
75
+ /// <param name="cancellationToken"></param>
76
+ /// <returns></returns>
77
+ /// <exception cref="InvalidOperationException"></exception>
78
+ [ Command ( "expand-all" ) ]
62
79
public async Task ExpandAll (
63
- [ Option ( 0 , "target project(.csproj)" ) ] string project ,
64
- [ Option ( "s" , "static embedding text" ) ] string ? staticEmbedding = null )
80
+ [ Argument ] string project ,
81
+ string ? staticEmbedding = null ,
82
+ CancellationToken cancellationToken = default )
65
83
{
66
84
project = Path . GetFullPath ( project ) ;
67
85
68
- var ( compilation , csProject ) = await GetCompilation ( project ) ;
86
+ var ( compilation , csProject ) = await GetCompilation ( project , cancellationToken : cancellationToken ) ;
69
87
if ( compilation is not CSharpCompilation csCompilation )
70
88
throw new InvalidOperationException ( "Failed to get compilation" ) ;
71
89
if ( csProject . ParseOptions is not CSharpParseOptions parseOptions )
@@ -76,21 +94,14 @@ public async Task ExpandAll(
76
94
var expanded = new EmbeddedLoader ( csCompilation ,
77
95
parseOptions ,
78
96
config ,
79
- Context . CancellationToken )
97
+ cancellationToken )
80
98
. ExpandedCodes ( ) ;
81
99
82
100
var result = JsonSerializer . Serialize ( expanded . Select ( t => new
83
101
{
84
102
t . SyntaxTree . FilePath ,
85
103
t . ExpandedCode ,
86
104
} ) , DefaultSerializerOptions ) ;
87
- Console . WriteLine ( result ) ;
88
- }
89
-
90
- private async Task < ( Compilation ? Compilation , Project Project ) > GetCompilation ( string projectPath , IDictionary < string , string > ? properties = null )
91
- {
92
- var workspace = MSBuildWorkspace . Create ( properties ?? ImmutableDictionary < string , string > . Empty ) ;
93
- var project = await workspace . OpenProjectAsync ( projectPath , cancellationToken : Context . CancellationToken ) ;
94
- return ( await project . GetCompilationAsync ( Context . CancellationToken ) , project ) ;
105
+ Output . WriteLine ( result ) ;
95
106
}
96
107
}
0 commit comments