@@ -19,7 +19,7 @@ public TargetNameParser(IApiCatalogLookup catalog, string defaultTargets)
1919 DefaultTargets = GetDefaultTargets ( defaultTargets ) . ToList ( ) ;
2020 }
2121
22- public IEnumerable < FrameworkName > DefaultTargets { get ; private set ; }
22+ public IEnumerable < FrameworkName > DefaultTargets { get ; }
2323
2424 /// <summary>
2525 /// Maps the list of targets specified as strings to a list of supported target names.
@@ -65,46 +65,67 @@ public IEnumerable<FrameworkName> MapTargetsToExplicitVersions(IEnumerable<strin
6565 private IEnumerable < FrameworkName > GetDefaultTargets ( string defaultTargets )
6666 {
6767 // The default targets are kept in a ';' separated string.
68- string [ ] defaultTargetsSplit = defaultTargets . Split ( new char [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) ;
68+ // Trimming target names because "; .NET Core" is interpreted as
69+ // " .NET Core", which does not exist, but ".NET Core" does.
70+ var defaultTargetsSplit = defaultTargets ? . Split ( new char [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) . Select ( x => x . Trim ( ) )
71+ ?? Array . Empty < string > ( ) ;
6972
7073 // Create a hashset of all the targets specified in the configuration setting.
71- HashSet < FrameworkName > parsedDefaultTargets = new HashSet < FrameworkName > ( ParseTargets ( defaultTargetsSplit , skipNonExistent : true ) ) ;
72-
73- // return all the public targets (their latest versions) as long as they also show up the default targets set.
74- return _catalog . GetPublicTargets ( )
75- . Select ( plat => plat . Identifier )
76- . Distinct ( )
77- . Select ( name => _catalog . GetLatestVersion ( name ) )
78- . Where ( plat => plat != null && parsedDefaultTargets . Contains ( plat ) ) ;
74+ var parsedDefaultTargets = ParseTargets ( defaultTargetsSplit , skipNonExistent : true ) ;
75+
76+ // Verify that any default targets returned are part of the public interface.
77+ var publicTargets = new HashSet < FrameworkName > ( _catalog . GetPublicTargets ( ) ) ;
78+
79+ return parsedDefaultTargets . Where ( x => publicTargets . Contains ( x ) ) ;
7980 }
8081
8182 /// <summary>
8283 /// Parse a string containing target names into FrameworkNames.
8384 ///
8485 /// Try the following in order:
85- /// 1. Check if the target specified uses the 'simple' name (i.e. Windows, .NET Framework) then get the latest version for it
86- /// 2. Try to parse it as a target name. If the target was not a valid FrameworkName, an ArgumentException will be thrown and passed down to user.
87- /// <exception cref="UnknownTargetException">Thrown when a target is unknown</exception>
86+ /// 1. Check if the target specified uses a FrameworkName (ie. .NET Core, Version=1.0)
87+ /// 2. Check if the target specified uses the 'simple' name (i.e. Windows, .NET Framework)
88+ /// then get the latest version for it.
8889 /// </summary>
8990 /// <param name="skipNonExistent">true to suppress <see cref="UnknownTargetException"/>
9091 /// when a target is not found. false, will not throw and skip that target instead.</param>
92+ /// <exception cref="UnknownTargetException">Thrown when a target is unknown.</exception>
9193 private ICollection < FrameworkName > ParseTargets ( IEnumerable < string > targets , bool skipNonExistent )
9294 {
93- var list = new List < FrameworkName > ( ) ;
94-
95- foreach ( var target in targets . Distinct ( StringComparer . OrdinalIgnoreCase ) )
95+ bool TryParseFrameworkName ( string targetName , out FrameworkName frameworkName )
9696 {
9797 try
9898 {
99- list . Add ( _catalog . GetLatestVersion ( target ) ?? new FrameworkName ( target ) ) ;
99+ frameworkName = new FrameworkName ( targetName ) ;
100+ return true ;
100101 }
101102 catch ( ArgumentException )
102103 {
103104 // Catch ArgumentException because FrameworkName does not have a TryParse method
104- if ( ! skipNonExistent )
105- {
106- throw new UnknownTargetException ( target ) ;
107- }
105+ frameworkName = null ;
106+ return false ;
107+ }
108+ }
109+
110+ var list = new List < FrameworkName > ( ) ;
111+
112+ foreach ( var target in targets . Distinct ( StringComparer . OrdinalIgnoreCase ) )
113+ {
114+ var framework = TryParseFrameworkName ( target , out var name )
115+ ? name
116+ : _catalog . GetLatestVersion ( target ) ;
117+
118+ if ( framework != null )
119+ {
120+ list . Add ( framework ) ;
121+ }
122+ else if ( ! skipNonExistent )
123+ {
124+ throw new UnknownTargetException ( target ) ;
125+ }
126+ else
127+ {
128+ // Trace.TraceWarning("Could not resolve target: {0}", target);
108129 }
109130 }
110131
0 commit comments