Skip to content
Merged

Dev #1780

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Common/XSharpConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ internal static class XSharpConstants
internal const string DialectPropertiesPage = "2652FCA6-1C45-4D25-942D-4C5D5EDE9539";
internal const string LanguagePropertiesPage = "0DFC7EF7-3F1A-4ACB-AFD8-DF56AEF9467A";
internal const string BuildEventsPropertiesPage = "49306259-9119-466E-8780-486CFBE2597D";
internal const string GlobalUsingsPropertiesPage = "5BF73771-1E08-40D9-8A48-EC06552C138B";
internal const string DebuggerWorkareasPane = "B5B41BAB-62F9-48E0-80D8-947F2F14D1C5";
internal const string DebuggerSettingsPane = "F7ED7826-137A-462D-8757-37A02BEF4DCF";
internal const string DebuggerGlobalsPane = "53B7968B-251B-44E0-BDF5-A225BF0DBC77";
Expand Down Expand Up @@ -128,6 +129,7 @@ internal static class XSharpProjectFileConstants
internal const string Fox1 = nameof(Fox1);
internal const string Fox2 = nameof(Fox2);
internal const string GetTargetPath = nameof(GetTargetPath);
internal const string ImplicitUsings = nameof(ImplicitUsings);
internal const string IncludePaths = nameof(IncludePaths);
internal const string IncludeSearchPaths = nameof(IncludeSearchPaths);
internal const string InitLocals = nameof(InitLocals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ internal void AppendSwitchWithSplitting(string switchName, string parameter, str
}
#if NOTUSED
/// <summary>
/// Returns true if the parameter is empty in spirits,
/// Returns true if the parameter is empty in spirits,
/// even if it contains the separators and white space only
/// Split on the characters provided.
/// </summary>
Expand All @@ -199,7 +199,7 @@ internal static bool IsParameterEmpty(string parameter, params char[] splitOn)
///
/// /embed[resource]:&lt;filename>[,&lt;name>[,Private]]
/// /link[resource]:&lt;filename>[,&lt;name>[,Private]]
///
///
/// Where the last flag--Private--is either present or not present
/// depending on whether the ITaskItem has a Private="True" attribute.
/// </summary>
Expand Down Expand Up @@ -241,7 +241,7 @@ internal virtual void AppendSwitchIfNotNull
// A boolean flag.
bool flagSet = false;

flagSet = Utilities.TryConvertItemMetadataToBool(parameter, metadataNames[i]);
flagSet = parameter.GetBool(metadataNames[i]);

if (flagSet)
{
Expand Down
120 changes: 120 additions & 0 deletions src/Compiler/src/Compiler/XSharpBuildTask/GenerateGlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace XSharp.Build
{

public sealed class GenerateGlobalUsings : Task
{
[Required]
public ITaskItem[] Usings { get; set; }

[Output]
public string[] Lines { get; set; }

public override bool Execute()
{
if (Usings.Length == 0)
{
Lines = Array.Empty<string>();
return true;
}

var usings = Usings.Select(UsingInfo.Read)
.OrderBy(static k => k, UsingInfoComparer.Instance)
.Distinct(UsingInfoComparer.Instance);

var lines = new string[Usings.Length + 1];
lines[0] = "// <auto-generated/>";

var index = 1;
var lineBuilder = new StringBuilder();
foreach (var @using in usings)
{
lineBuilder.Clear();
lineBuilder.Append("global using ");

if (@using.Static)
{
lineBuilder.Append("static ");
}

if (!string.IsNullOrEmpty(@using.Alias))
{
lineBuilder.Append(@using.Alias)
.Append(" = ");
}

lineBuilder.Append(@using.Namespace);

lines[index++] = lineBuilder.ToString();
}

Lines = lines;
return true;
}

private readonly struct UsingInfo
{
public static UsingInfo Read(ITaskItem taskItem)
{
return new UsingInfo(
taskItem.ItemSpec,
taskItem.GetBool("Static"),
taskItem.GetMetadata("Alias"));
}

private UsingInfo(string @namespace, bool @static, string alias)
{
Namespace = @namespace;
Static = @static;
Alias = alias;
}

public string Namespace { get; }
public bool Static { get; }
public string Alias { get; }
}

private sealed class UsingInfoComparer : IComparer<UsingInfo>, IEqualityComparer<UsingInfo>
{
public static readonly UsingInfoComparer Instance = new();

public int Compare(UsingInfo x, UsingInfo y)
{
var @static = x.Static.CompareTo(y.Static);
if (@static != 0)
{
return @static;
}

var alias = x.Alias.CompareTo(y.Alias);
if (alias != 0)
{
return alias;
}

return StringComparer.Ordinal.Compare(x.Namespace, y.Namespace);
}

public bool Equals(UsingInfo x, UsingInfo y)
{
return Compare(x, y) == 0;
}

public int GetHashCode(UsingInfo obj)
{
return StringComparer.Ordinal.GetHashCode(obj.Namespace);
}
}
}
}

This file was deleted.

This file was deleted.

Loading