Skip to content

jkarsrud/Umbraco.CodeGen

 
 

Repository files navigation

Umbraco uSync Code Generator

Synchronizes C# classes with uSync DocumentType files.

Add all assemblies to Umbraco/Bin and you're good to go. (Yes, yes, package is coming) Config is read from uSync and ~/config/CodeGen.config.

Depends on https://github.com/icsharpcode/NRefactory

Binaries available here: http://lars-erik.github.io/Umbraco.CodeGen/

##Recommended uSync setup: <usync read="true" write="false" attach="true" .../>

Having write="true" will overwrite your code, so don't use it unless you don't want to edit generated code.

DO NOT CODE FIRST BEFORE FIRST RUN OF USYNC! YOU'LL LOSE YOUR CODE!

DO NOT WRITE LOGIC IN THE CLASSES, ONLY PROPERTIES! YOU'LL LOSE YOUR CODE!

Classes are partial for this reason

##Recommended CodeGen config (~/config/CodeGen.config):

###CodeGenerator element

ModelPath

If you have ReSharper, set the Models\Synchronized folder as Namespace Provider=False. Otherwise, make a folder that has the namespace structure you want. The code generation will attemt to create DocumentType XML for ALL classes found in the specified folder.

BaseClass

A class in the same namespace as the generated/code first classes with at least the following structure:

public class DocumentTypeBase
{
    protected IPublishedContent Content; // Can be field or property, must be named Content

    protected DocumentTypeBase(IPublishedContent content) // Must implement ctor with one IPublishedContent arg
    {
        Content = content;
    }
}

The class itself can be named whatever.

It should however not be in the synced folders, keep it one step up, for instance under Models.

Namespace

Preferably the correct namespace for your model folder. :)

GenerateClasses

Whether to generate class for document types when saved. (Works with uSync attach=true)

GenerateXml

Whether to generate XML for document types when Umbraco starts. (Works with uSync read=true)

RemovePrefix

Partially implemented and not at all tested. Should remove prefixes from class and property names if you have them in your aliases.

OverwriteReadOnly

For everyone lucky enough to have a SCS locking the files.

TypeMappings

Pretty self explanatory if you should use this tool at all. ;) For some reason the CSharpCodeDomProvider adds @ to intristic type aliases. (string, int etc.) Use the class names of the types to avoid it. DataTypeId = the editor ID.

###Supported code constructs

Class attributes

  • [DisplayName] - Document type name
  • [Description] - Guess what

Class name

Alias, camelCased in XML, PascalCased in C#.

Class fields

  • String icon
  • String thumbnail
  • Boolean allowAtRoot
  • String[] allowedTemplates
  • String defaultTemplate
  • Type[] structure

Class ctor

See base class

Property attributes

  • [DisplayName] - Property name
  • [Description] - Yup
  • [Category] - Tab name, leave empty for "generic properties"
  • [DataType] - DataType definition ID (Not editor)
  • [RegularExpression] - Validation
  • [Required] - Mandatory

Property name

Alias, camelCased in XML, PascalCased in C#.

Property getter

Will be regenerated as return Content.GetPropertyValue<T>("alias")

ANY CODE BUT THIS WILL BE DELETED ON ROUNDTRIP!

###Gotchas:

  • Generated classes are partial DO NOT write code other than properties in the generated classes!
  • If document type and property has the same name, the class is suffixed with "Class"

About

Custom tool to generate classes from uSync export

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published