private static string GetCaption(string path, string originalItemSpec, IImmutableDictionary <string, string> properties) { string version = properties.GetStringProperty(ProjectItemMetadata.Version) ?? string.Empty; string?baseCaption = new LazyStringSplit(path, ',').FirstOrDefault(); return((string.IsNullOrEmpty(version) ? baseCaption : $"{baseCaption} ({version})") ?? originalItemSpec); }
/// <summary> /// Makes a proper namespace from the specified name. /// </summary> /// <param name="name"> /// A <see cref="string"/> containing the name. /// </param> /// <returns> /// A proper namespace which meets the C# language specification. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="name"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="name"/> is an empty string (""). /// </exception> public string MakeProperNamespace(string name) { Requires.NotNullOrEmpty(name, nameof(name)); IEnumerable <string> namespaceNames = new LazyStringSplit(name, '.').Select(MakeProperIdentifier); return(string.Join(".", namespaceNames)); }
/// <remarks> /// This extension method has special knowledge of the <see cref="LazyStringSplit"/> type and /// can compute its result without allocation. /// </remarks> /// <inheritdoc cref="System.Linq.Enumerable.FirstOrDefault{TSource}(IEnumerable{TSource})"/> public static string?FirstOrDefault(this LazyStringSplit lazyStringSplit) { LazyStringSplit.Enumerator enumerator = lazyStringSplit.GetEnumerator(); return(enumerator.MoveNext() ? enumerator.Current : null); }
static string GetTargetFromDependencyId(string dependencyId) { string firstPart = new LazyStringSplit(dependencyId, '/').FirstOrDefault(); if (firstPart == null) { // should never happen throw new ArgumentException(nameof(dependencyId)); } return(firstPart); }
protected virtual async Task <Version> GetDefaultVersionAsync(IProjectProperties defaultProperties) { string versionStr = await defaultProperties.GetEvaluatedPropertyValueAsync(PackageVersionMSBuildProperty); if (string.IsNullOrEmpty(versionStr)) { return(DefaultVersion); } // Ignore the semantic version suffix (e.g. "1.0.0-beta1" => "1.0.0") versionStr = new LazyStringSplit(versionStr, '-').First(); return(Version.TryParse(versionStr, out Version version) ? version : DefaultVersion); }
private IEnumerable <string> GetDependencyItemSpecs() { if (Properties.TryGetValue(ProjectItemMetadata.Dependencies, out string dependencies) && !string.IsNullOrWhiteSpace(dependencies)) { var dependenciesItemSpecs = new HashSet <string>(StringComparers.PropertyValues); var dependencyIds = new LazyStringSplit(dependencies, ';'); // store only unique dependency IDs foreach (string dependencyId in dependencyIds) { dependenciesItemSpecs.Add($"{Target}/{dependencyId}"); } } return(Array.Empty <string>()); }
public SdkDependencyModel( string path, string originalItemSpec, bool isResolved, bool isImplicit, IImmutableDictionary <string, string> properties) : base( path, originalItemSpec, flags: s_flagCache.Get(isResolved, isImplicit), isResolved, isImplicit, properties) { string version = properties.GetStringProperty(ProjectItemMetadata.Version) ?? string.Empty; string baseCaption = new LazyStringSplit(path, ',').FirstOrDefault(); Caption = string.IsNullOrEmpty(version) ? baseCaption : $"{baseCaption} ({version})"; }
public void RemoveEmptyEntries(string input, char delimiter, string[] expected) { // This boxes IEnumerable <string> actual = new LazyStringSplit(input, delimiter, StringSplitOptions.RemoveEmptyEntries); AssertEx.SequenceEqual(expected, actual); // Non boxing foreach var list = new List <string>(); foreach (var s in new LazyStringSplit(input, delimiter, StringSplitOptions.RemoveEmptyEntries)) { list.Add(s); } AssertEx.SequenceEqual(expected, list); // Equivalence with string.Split AssertEx.SequenceEqual(expected, input.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries)); }
public override async Task <string?> OnSetPropertyValueAsync(string propertyName, string unevaluatedPropertyValue, IProjectProperties defaultProperties, IReadOnlyDictionary <string, string>?dimensionalConditions = null) { if (bool.TryParse(unevaluatedPropertyValue, out bool value)) { if (value) { // Move TargetFramework -> TargetFrameworks string?targetFramework = await defaultProperties.GetUnevaluatedPropertyValueAsync(TargetFrameworkProperty); if (!Strings.IsNullOrEmpty(targetFramework)) { await defaultProperties.SetPropertyValueAsync(TargetFrameworksProperty, targetFramework); await defaultProperties.DeletePropertyAsync(TargetFrameworkProperty); } } else { // Move TargetFrameworks -> TargetFramework // // Requires TargetFrameworks to contain a valid string string?targetFrameworks = await defaultProperties.GetUnevaluatedPropertyValueAsync(TargetFrameworksProperty); if (!Strings.IsNullOrEmpty(targetFrameworks)) { string?firstTargetFramework = new LazyStringSplit(targetFrameworks, ';').FirstOrDefault(); if (!Strings.IsNullOrEmpty(firstTargetFramework)) { await defaultProperties.SetPropertyValueAsync(TargetFrameworkProperty, firstTargetFramework); await defaultProperties.DeletePropertyAsync(TargetFrameworksProperty); } } } } return(null); }
public static bool TryGetMetadata( string itemSpec, bool isResolved, IImmutableDictionary <string, string> properties, HashSet <string> unresolvedChanges, ITargetFramework targetFramework, ITargetFrameworkProvider targetFrameworkProvider, out PackageDependencyMetadata metadata) { Requires.NotNull(itemSpec, nameof(itemSpec)); Requires.NotNull(properties, nameof(properties)); // unresolvedChanges can be null Requires.NotNull(targetFramework, nameof(targetFramework)); Requires.NotNull(targetFrameworkProvider, nameof(targetFrameworkProvider)); bool isTopLevel; string target = GetTargetFromDependencyId(itemSpec); DependencyType dependencyType = properties.GetEnumProperty <DependencyType>(ProjectItemMetadata.Type) ?? (isResolved ? DependencyType.Unknown : DependencyType.Package); string name = properties.GetStringProperty(ProjectItemMetadata.Name) ?? itemSpec; bool isImplicitlyDefined = properties.GetBoolProperty(ProjectItemMetadata.IsImplicitlyDefined) ?? false; if (isResolved) { isTopLevel = isImplicitlyDefined || (dependencyType == DependencyType.Package && unresolvedChanges?.Contains(name) == true); bool isTarget = itemSpec.IndexOf('/') == -1; if (isTarget) { metadata = default; return(false); } ITargetFramework packageTargetFramework = targetFrameworkProvider.GetTargetFramework(target); if (packageTargetFramework?.Equals(targetFramework) != true) { metadata = default; return(false); } } else { isTopLevel = true; } string originalItemSpec = isResolved && isTopLevel ? name : itemSpec; metadata = new PackageDependencyMetadata( dependencyType, target, itemSpec, originalItemSpec, name, isResolved, isImplicitlyDefined, isTopLevel, properties); return(true); string GetTargetFromDependencyId(string dependencyId) { string firstPart = new LazyStringSplit(dependencyId, '/').FirstOrDefault(); if (firstPart == null) { // should never happen throw new ArgumentException(nameof(dependencyId)); } return(firstPart); } }