1+ using System ;
2+ using System . IO ;
3+ using System . Linq ;
4+ using Microsoft . Build . Framework ;
5+ using Microsoft . Build . Utilities ;
6+ using UnityModStudio . Common ;
7+
8+ namespace UnityModStudio . Build . Tasks ;
9+
10+ public class ResolveTargetPath : Task
11+ {
12+ private const string TargetPathMetadataName = "TargetPath" ;
13+
14+ [ Required ]
15+ public string RelativeToPath { get ; set ; } = "" ;
16+
17+ [ Required ]
18+ public ITaskItem [ ] Input { get ; set ; } = [ ] ;
19+
20+ [ Output ]
21+ public ITaskItem [ ] ? Output { get ; set ; }
22+
23+ public override bool Execute ( )
24+ {
25+ var relativeToFullPath = Utils . AppendTrailingSlash ( Path . GetFullPath ( RelativeToPath ) ) ;
26+
27+ Output = new ITaskItem [ Input . Length ] ;
28+ for ( var i = 0 ; i < Input . Length ; i ++ )
29+ {
30+ var outputItem = new TaskItem ( Input [ i ] . ItemSpec ) ;
31+ Input [ i ] . CopyMetadataTo ( outputItem ) ;
32+
33+ string ? targetPath = null ;
34+ if ( ! outputItem . MetadataNames . Cast < string > ( ) . Contains ( TargetPathMetadataName ) )
35+ {
36+ var fullPath = outputItem . GetMetadata ( "FullPath" ) ;
37+ var directoryName = Path . GetDirectoryName ( fullPath ) ?? "" ;
38+ if ( directoryName . StartsWith ( relativeToFullPath , StringComparison . OrdinalIgnoreCase ) )
39+ targetPath = directoryName . Substring ( relativeToFullPath . Length ) ;
40+ }
41+ else
42+ targetPath = outputItem . GetMetadata ( TargetPathMetadataName ) ;
43+
44+ if ( ! string . IsNullOrEmpty ( targetPath ) )
45+ targetPath = Utils . AppendTrailingSlash ( targetPath ! ) ;
46+
47+ if ( targetPath is not null )
48+ outputItem . SetMetadata ( TargetPathMetadataName , targetPath ) ;
49+
50+ Output [ i ] = outputItem ;
51+ }
52+
53+ return true ;
54+ }
55+ }
0 commit comments