jeffkl / RoslynCodeTaskFactory

An MSBuild CodeTaskFactory that uses Roslyn compiler for cross platform compatibility
MIT License
26 stars 3 forks source link

Support System.IO.Path #26

Closed planasmultimedia closed 6 years ago

planasmultimedia commented 6 years ago

Hi ! I have been using your library to insert C# tasks in the Csproj and I'm getting an error when i try to use the functions from Path. The error is the following : --error CS0103: The name 'Path' doesn't exists in the actual Context --the task generator "CodeTaskFactory" cannot be loaded from the assembly "C:...\netstandard1.5\RoslynCodeTaskFactory.dll". The task generator should a value for the property "TaskType"

The url of the dll is correct and the dll exists.

I have tryied everything and still getting the error. The task that I'm trying to execute is the following:

cpp_files = new List(); List h_files = new List(); List qml_files = new List(); string[] filenames = Directory.GetFiles(pathSourceFiles); foreach (string f in filenames) { String name = Path.GetFileName(f); if (name.EndsWith(".cpp")) { cpp_files.Add(name); } if (name.EndsWith(".h")) { h_files.Add(name); } if (name.EndsWith(".qml")) { qml_files.Add(name); } } ]]>

And in the Csproj i'm Referencing the package as:

None

The project is NetCore and it is getting the Roslyn dll from the netstandard folder

Thanks in advance, Marc.

eduherminio commented 6 years ago

May be fixed by #25, but let's wait for @planasmultimedia's confirmation.

jeffkl commented 6 years ago

Sorry this is actually a limitation of how RoslynCodeTaskFactory is referencing everything. The meta package NETStandard.Library references a bunch of stuff for you automatically that RoslynCodeTaskFactory does not. I only did this because the current CodeTaskFactory didn't reference everything.

In NETStandard1.0 through NETStandard1.6 System.IO.Path is in System.Runtime.Extensions so you just need to add a reference.

<UsingTask TaskName="PathCombine"
            TaskFactory="CodeTaskFactory"
            AssemblyFile="$(RoslynCodeTaskFactory)"
            Condition=" '$(RoslynCodeTaskFactory)' != '' ">
  <ParameterGroup>
    <Paths ParameterType="System.String[]" Required="true" />
    <Combined ParameterType="System.String" Output="true" />
  </ParameterGroup>
  <Task>
    <Reference Include="System.IO.FileSystem" />
    <Reference Include="System.Runtime.Extensions" />
    <Using Namespace="System" />
    <Code Type="Fragment" Language="C#">
      <![CDATA[
          Combined = Path.Combine(Paths);
          ]]>
    </Code>
  </Task>
</UsingTask>

The key addition is the two references:

<Reference Include="System.IO.FileSystem" />
<Reference Include="System.Runtime.Extensions" />

System.IO.FileSystem.dll has classes like Directory, DirectoryInfo, File, and FileInfo. System.Runtime.Extensions.dll contains System.IO.Path.

In NETStandard2.0, there's only a single reference, netstandard.dll that contains everything so this inconvenience will go away. You can try out the beta package I just published:

https://www.nuget.org/packages/RoslynCodeTaskFactory/2.0.1-beta-ga22b9a4e4e

That will require that the dotnet SDK you're using supports NETCoreApp2.0+ and means you do not need the additional references I showed above.

jeffkl commented 6 years ago

I should also say I could add the references by default if you'd like and push a new package that still supports older NETStandard versions

eduherminio commented 6 years ago

We'll move to v2.x as soon as it's ready & our task seemed to work with beta package, but if you can add those references to a pre-2.0 version, we'd be grateful to avoid working with v2.x beta before its final release.

jeffkl commented 6 years ago

Okay v1.2.10 now has these references by default so accessing classes like Path, Directory, and File should just work without any extra XML.

https://www.nuget.org/packages/RoslynCodeTaskFactory/1.2.10

It could take a few minutes for NuGet to make the package available.

Please close this issue once you've verified the fix. Thanks for reporting it.