question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get classes within a specific folder instead of with a prefix/suffix?

See original GitHub issue

I’d like to generate .tsx files from MyProject.Core.Models and MyProject.Api.Controllers, regardless of having a naming convention to them. Is it possible? Something like:

$Classes(Models/*)[
    export class $Name {$Properties[
        $name$TypeFormatted: $Type;]
    }
]

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
triforcelycommented, Mar 7, 2018

You should be able to do the following

$Classes(MyProject.Core.Models.*)[
    //cut
]

Which will get all classes with matching namespace.

2reactions
gregverescommented, Mar 26, 2018

I can’t find it @vcardins. So here is what I use

I define this in TypewriterAttributes.cs

    /// <summary>
    /// Marks C# (enum|class|interface) for export as equivalent type in Typescript so it can be used in the web/javascript application.
    /// </summary>
    [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Class | AttributeTargets.Interface)]
    public class ExportToTypescriptAttribute : Attribute
    {
        // Doesn't need anything in it; just its presence is enough.
    }

Then I put the attribute on the classes and enums I need to show up in typescript.

[ExportToTypescript]
public class SomeDTOTypeClass {
...
}

Then I use the following to pick up the attribute in the .TST file

 //////////////////////////////////////////////////////////////////////////////////
    // select the classes to convert to typescript
    bool ClassShouldBeExported(Class c) {
        // A class should be processed (only the relevant bits will be exported) if 
        // 1: it contains the attribute export to typescript or export with knockout
        // 2: one of its nested classes (recurrsion) contains the same two attributes
        // 3: one of its nested enums contain the export to typescript attribute
        // we short circuit as soon as possible
        // This is used by the $Classes() filter to see if there are any classes within the file
        // that need processing. We then ignore that list and go onto the other filters with the
        // actual code. This is so we can write the typescript code snippets once
        if (ExportToTypeScriptFilter(c)) {
            return true;
        }
        foreach (var nc in c.NestedClasses) {
            if (ClassShouldBeExported(nc)) {
                return true;
            }
        }
        foreach (var ne in c.NestedEnums) {
            if (ExportToTypeScriptFilter(ne)) {
                return true;
            }
        }
        return false;
    }
    // These are the filters used to determine the relavent parts of the file to transform
    bool ExportToTypeScriptFilter(Class c) {
        bool select = false;
        foreach (var attr in c.Attributes) {
            if (attr.Name == "ExportToTypescript" || attr.Name == "ExportToTypescriptWithKnockout") {
                select = true;
                break;
            }
        }
        return select;
    }
    bool ExportToTypeScriptFilter(Enum e) {
        bool select = false;
        foreach (var attr in e.Attributes) {
            if (attr.Name == "ExportToTypescript") {
                select = true;
                break;
            }
        }
        return select;
    }

and then in the body of the .TST file I do this:

namespace SkycourtApi {
$Classes(c => ClassShouldBeExported(c))[]
$Enums(e => ExportToTypeScriptFilter(e))[]
$EnumsToExportToTypescript[    export const enum $Name {
        $Values[
        $Name = $Value][,]
    }
]
$ClassesToExportToTypescript[   export interface $Name $baseClass { $Properties[
        $Name: $Type;]
    }
]
$PrintDebugInfo
}

The namespace is because I put all my interfaces into a common namespace called SkycourtApi. The interfaces are the definitions from C# that don’t have any code, they are just the shape of the classes. Same with Enums.

Hope this gets you on your way. I couldn’t imagine doing this stuff in C# without Typewriter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get list of classes with specific prefix from a result ...
Firstly: Your regex missed a _ character, use: /some_class_\d+/ instead. If you have a collection array of jQuery objects (elements) and you ...
Read more >
cp - Copy files with match prefix AND suffix with shell script
This would match all filenames starting with the string aaa and ending in the string .png and copy them all to the directory...
Read more >
Better naming convention
The idea that you don't need the 'i' prefix on an interface is missing one important point - interface names share the same...
Read more >
java.nio.file.Files Class
As we can see, we can pass folder and file name in Paths.get() ... and generates the file name using given prefix and...
Read more >
Organizing objects using prefixes
You can use prefixes to organize the data that you store in Amazon S3 buckets. A prefix is a string of characters at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found