DevToys-app / DevToys

A Swiss Army knife for developers.
https://devtoys.app/
MIT License
26.94k stars 1.45k forks source link

Custom converters #1245

Closed pekspro closed 3 months ago

pekspro commented 3 months ago

What feature or new tool do you think should be added to DevToys?

I would be nice if you could code your own converter in some simple way.

Why do you think this is needed?

I'm using this tool were you can do this with Javascript:

https://github.com/felixse/Woop

Would be nice to have something similar i DevToys.

Solution/Idea

I'm not an expert on this. but here is a short sample with C#. It dynamically compiles C# code in memory and then call a function:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System.Reflection;
using System.Text;

string result1 = CompileAndConvert("Hello, World!", """

    public class Program
    {
        public static string Convert(string input)
        {
            return "You said: " + input;
        }
    }

    """);

Console.WriteLine(result1);

static string CompileAndConvert(string input, string code)
{
    SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);

    string assemblyName = Path.GetRandomFileName();
    MetadataReference[] references = new MetadataReference[]
    {
        MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
        MetadataReference.CreateFromFile(typeof(Console).Assembly.Location)
    };

    CSharpCompilation compilation = CSharpCompilation.Create(
        assemblyName,
        new[] { syntaxTree },
        references,
        new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

    using (var ms = new MemoryStream())
    {
        EmitResult result = compilation.Emit(ms);

        if (!result.Success)
        {
            IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                    diagnostic.IsWarningAsError ||
                    diagnostic.Severity == DiagnosticSeverity.Error);

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("Compilation failed:");

            foreach (Diagnostic diagnostic in failures)
            {
                stringBuilder.AppendFormat("{0}: {1}\n", diagnostic.Id, diagnostic.GetMessage());
            }

            throw new Exception(stringBuilder.ToString());
        }

        ms.Seek(0, SeekOrigin.Begin);

        Assembly assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(ms);
        MethodInfo method = assembly.GetType("Program")!.GetMethod("Convert")!;

        string output = (string)method.Invoke(null, new object[] { input })!;

        return output;
    }
}

Comments

No response

veler commented 3 months ago

If I'm not wrong you are basically asking for a way to extend a tool or develop your own tool in DevToys. This is already possible. We have an SDK and documentation here: https://devtoys.app/doc/articles/introduction.html

For your information, every default tools in DevToys are bundled in an extension already. image

pekspro commented 3 months ago

Kind of, but I'm asking for something much simpler 😊. I just want to add the code for a simple function that converts a string directly into the application. And then I want to be able to call my function to convert a string.