Commit 9d978fad authored by shrabbit's avatar shrabbit
Browse files

Add FrameworkDetect, MsPinyinAnalysis, and ArraySpan playgrounds and update dependencies

parent bc3a5055
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -8,3 +8,14 @@ public interface IPlayground
    
    public Task Run(string[] args);
}


public abstract class Playground<TSelf> : IPlayground
{
    private static readonly Type Type = typeof(TSelf);
    
    public virtual string LaunchCmd { get; } = Type.Name.ToLower();
    public virtual string? Name { get; } = Type.Name;
    public virtual string? Description { get; } = Type.FullName;
    public abstract Task Run(string[] args);
}
 No newline at end of file
+64 −0
Original line number Diff line number Diff line
namespace TestV.Playgrounds;

public class ArraySpan : IPlayground
{
    public string LaunchCmd { get; } = "arrayspan";
    public string? Name { get; } = "ArraySpan";
    public string? Description { get; } = null;
    public Task Run(string[] args)
    {
        int[] items;
        
        using (new RunningTimer("Prepare"))
        {
            items = new int[100_000];

            for (var i = 0; i < items.Length; i++)
            {
                items[i] = Random.Shared.Next(0, int.MaxValue);
            }
        }

        using (new RunningTimer("Span"))
        {
            for (var i = 0; i < 100_000; i++)
            {
                var oddItemsS = OddItemsS(items);
            }
        }
        
        using (new RunningTimer("Array"))
        {
            for (var i = 0; i < 100_000; i++)
            {
                var oddItemsA = OddItemsA(items);
            }
        }
        
        return Task.CompletedTask;
    }

    public int OddItemsA(int[] items)
    {
        var result = 0;
        foreach (var i in items)
        {
            if(i % 2 == 1)
                result++;
        }

        return result;
    }

    public int OddItemsS(ReadOnlySpan<int> items)
    {
        var result = 0;
        foreach (var i in items)
        {
            if (i % 2 == 1)
                result++;
        }

        return result;
    }
}
 No newline at end of file
+77 −0
Original line number Diff line number Diff line
using Spectre.Console;

namespace TestV.Playgrounds.FrameworkDetect;

public class DetectRun
{
    public string Path { get; init; }
    public bool IsVerbose { get; init; }

    public FrameworkDetectResult[] Detect()
    {
        Framework[] fw = [];
        using (new RunningTimer("Loading frameworks"))
        {
            fw = Frameworks.Frameworks.Items;
        }

        var list = new List<FrameworkDetectItem>();

        var typeCache = new Dictionary<Framework, Type>();
        
        AnsiConsole.WriteLine($"Scanning...");
        foreach (var file in Directory.EnumerateFiles(Path, "*", SearchOption.AllDirectories))
        {
            if(IsVerbose)
                AnsiConsole.WriteLine($"> {file}");
            
            foreach (var f in fw)
            {
                foreach (var t in f.Triggers)
                {
                    if (!t.IsDetected(file)) continue;

                    if (IsVerbose)
                        AnsiConsole.MarkupLine($"    └ [green]'{WriteTypeCache(typeCache, f)}' Detected![/]");
                    
                    list.Add(new FrameworkDetectItem
                    {
                        Framework = f,
                        PossibleItem = file
                    });
                }
            }
        }
        
        AnsiConsole.WriteLine("\nMarshaling Data...");

        var results = new List<FrameworkDetectResult>();
        var resultGroup = list.GroupBy(x => x.Framework);
        foreach (var gp in resultGroup)
        {
            var framework = gp.Key;
            var items = gp.ToList();

            results.Add(new FrameworkDetectResult
            {
                Framework = framework,
                Possibility = (float)items.Count / list.Count,
                PossibleItems = items.Select(f => f.PossibleItem).ToArray()
            });
        }

        return results.ToArray();
    }

    private Type WriteTypeCache(Dictionary<Framework, Type> cache, Framework fw)
    {
        if (cache.TryGetValue(fw, out var type))
        {
            return type;
        }

        var t = fw.GetType();
        cache.Add(fw, t);
        return t;
    }
}
 No newline at end of file
+14 −0
Original line number Diff line number Diff line
namespace TestV.Playgrounds.FrameworkDetect.Detects;

public class CppRuntimeTrigger : IDetectTrigger
{
    public bool IsDetected(string filePath)
    {
        var name = Path.GetFileName(filePath);
        
        return (name.StartsWith("api-ms-") && name.EndsWith(".dll")) ||
               (name.StartsWith("cpp")) ||
               (name.StartsWith("vcruntime") && name.EndsWith(".dll")) ||
               (name.StartsWith("msvcp") && name.EndsWith(".dll"));
    }
}
 No newline at end of file
+19 −0
Original line number Diff line number Diff line
namespace TestV.Playgrounds.FrameworkDetect.Detects;

public class DotNetCoreTrigger : IDetectTrigger
{
    public bool IsDetected(string filePath)
    {
        return filePath.EndsWith(".deps.json") ||
               filePath.EndsWith(".runtimeconfig.json");
    }
}

public class AvaloniaTrigger : IDetectTrigger
{
    public bool IsDetected(string filePath)
    {
        var name = Path.GetFileName(filePath);
        return name.StartsWith("Avalonia") && name.EndsWith(".dll");
    }
}
 No newline at end of file
Loading