Drizin / CodegenCS

C# Toolkit for Code Generation (T4 alternative!)
MIT License
223 stars 30 forks source link

Reference DLL #19

Closed jano-kucera closed 1 year ago

jano-kucera commented 1 year ago

Hi, I found this project just yesterday, seemed pretty easy to use. I need to reference a DLL which I will 'parse' to another language. Using #r to reference it made IntelliSense work, but the generator does not handle it.

CS7011: Line 0 #r is only allowed in scripts

Any advice on how to reference DLL in your Codegen templates (with working IntelliSense if possible)?

jano-kucera commented 1 year ago

To provide more info, I have created a new Project for this tool and I want to use functions from other projects. image

My code has a working intellisense, but its not executed properly.

namespace CodegenCS
{
    using System;
    using System.IO;
    using System.Linq;
    using QicsUnity.CodeGeneration.MetadataReader;

    class LocalizationMarkersTemplate
    {
        public FormattableString Main()
        {
            var projectRoot = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            var excelpath = Path.Combine(projectRoot, @"..\QicsUnity.Common.Localization\ResourceStrings.xlsx");
            var translationRows = TranslationSheetRow.FromExcelFile(excelpath);

            return $@"{translationRows.Count()}";
        }
    }
}

image

jano-kucera commented 1 year ago

Been playing around with it for couple of hours now, I cannot manage to use my DLLs even if they are in the same folder, I tried out the dotnet-codegencs template run/build, still not recognizing my namespaces.

Drizin commented 1 year ago

I assume you're running the templates from Visual Studio Extension, right? If so, please note that we suggest to use the CSX extension only to get some intellisense, but our extension is NOT a CSX compiler and currently it does not allow #r directives.

If you want to get the best intellisense (which will also let you use your own dll references), you should use a full CSPROJ. See the sample template SimplePocos.

jano-kucera commented 1 year ago

Yes, I work in VS22, and managed to make the intellisense work (the example above), but building/running the templates does not work, as the referenced projects and their namespaces are not found.

Drizin commented 1 year ago

Maybe it's a namespace conflict? Looks like you're using the same namespace as the library, which sounds like a bad idea.

jano-kucera commented 1 year ago

I've moved it to new project in the Solution, with a new namespace, still the same problem. My project reference is ignored in the template build/run :/.

Drizin commented 1 year ago

dotnet-codegencs cli tool just combines the inputs (*.cs), compiles, then looks for the entrypoint and invokes it (by providing injected types). But it does not have support for external references. You'll have to run the output (from main()) of your own project.

Drizin commented 1 year ago

In other words it's not like csi (CSX compiler) which supports directives like #r, etc.

Drizin commented 1 year ago

You can probably use dotnet-codegencs template run using your output DLL. If external DLL is in same folder it should probably be loaded. All you can't do is use template build.

jano-kucera commented 1 year ago

This approach did not work as creation of the template dll depended on the referenced DLL.

However, the previous suggestion seems to work, I've created an extra file from which I manually run the template and I got an exception from my external DLL so it is finally running 👍 .