Open modernist opened 7 years ago
While this isn't possible with the current implementations, weren't opposed to the idea, this would need to be investigated how to best support static classes. Might be something we postpone until we unified around one scripting engine to reduce effort duplication.
Lifting type declarations and methods out of a Roslyn script is actually one of my next blog posts :).
I use a walker on the syntax tree to find stuff that should be lifted: https://github.com/Wyamio/Wyam/blob/9a83529aecef73cfacba849b28f892c251b39748/src/core/Wyam.Configuration/ConfigScript/LiftingWalker.cs Then you create the container class and method and stuff the remainder into it, placing all the stuff you lifted outside.
If there's interest I can take a look at what it would take to apply this approach in Cake. The alternate script host might be problematic though because the approach above obviously only works with Roslyn.
@daveaglick sounds interesting, wonder if it could be done as a WIP module to Cake.
Yes, if it requires Roslyn we can add this as soon as we unify the script engines.
Any progress on this issue? I have the same problem. Is there some workarounds?
Workaround is to just do a static extension method without a surrounding class.
Thanks, it works!
Repro:
build.cake
public static class MyBuildExtensions
{
public static int ToInt(this string value)
{
return Convert.ToInt32(value);
}
}
What You Are Seeing?
I have added a static class containing extension methods for the
AssemblyInforParseResult
class in order to simplify the instantiation of anAssemblyInfoSettings
object. When running the build, I get the following message:What is Expected?
I would expect the build script to be compiled without errors. I am guessing this happens because Cake converts the entire cake.build into a single class that is then complied with Roslyn, thus my custom class turns into a nested class, and, correctly, the compiler doesn't allow declaring extension methods in it. I am able to circumvent this by creating a class library with the custom functionality and referencing it, but this is not very convenient as the cake script is no longer self-contained.
What version of Cake are you using? Are you running on a 32 or 64 bit system? What environment are you running on? Windows? Linux? Mac? Are you running on a CI Server? If so, which one?
I am running Cake 0.16.2 on Windows (64bit), both locally and on (on-premise) TFS.
How Did You Get This To Happen? (Steps to Reproduce)
Add a static class containing extension methods to a cake script
Output Log