Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and refactoring of typescript writer #652

Merged
merged 1 commit into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/Services/OData/Typescript/ResourceWriter.cs
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using SenseNet.ContentRepository.i18n;

namespace SenseNet.Portal.OData.Typescript
Expand All @@ -11,19 +7,19 @@ internal class ResourceWriter
{
public static void WriteResourceClasses(System.IO.TextWriter writer)
{
writer.WriteLine(@"/**
* @module Resources
* @preferred
*
* @description Module for Resource classes. Helps you to reach string resource classes with dot notation.
*
* This module is autogenerated from Sense/Net Localization files.
* ```ts
* import * as SenseNet from './Scripts/SenseNet'
*
* const editActionName = SenseNet.Resources.Actions[""Edit""]
* ```
*/ /** */");
writer.WriteLine(@"/**
* @module Resources
* @preferred
*
* @description Module for Resource classes. Helps you to reach string resource classes with dot notation.
*
* This module is autogenerated from Sense/Net Localization files.
* ```ts
* import * as SenseNet from './Scripts/SenseNet'
*
* const editActionName = SenseNet.Resources.Actions[""Edit""]
* ```
*/");

foreach (var className in SenseNetResourceManager.Current.GetClasses().OrderBy(x => x))
writer.WriteLine($" export class {className.Replace("-", "")} {{ }}");
Expand Down
23 changes: 10 additions & 13 deletions src/Services/OData/Typescript/TypescriptClassesVisitor.cs
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SenseNet.ContentRepository.i18n;
using SenseNet.ContentRepository.Schema.Metadata;

namespace SenseNet.Portal.OData.Typescript
Expand All @@ -25,18 +24,16 @@ protected override IMetaNode VisitSchema(ContentRepository.Schema.Metadata.Schem
* inheriting its properties just like Content Types in the Content Repository. This module provides us to create an objects with a type so that we can validate on its properties by their
* types or check the required ones.
*
*//** */
*/

// tslint:disable:naming-convention
import { ActionModel } from './ActionModel'
import * as ComplexTypes from './ComplexTypes'
import * as Enums from './Enums'

import * as ComplexTypes from ""./ComplexTypes"";
import * as Enums from ""./Enums"";
import { IActionModel } from ""./IActionModel"";
export type ContentReferenceField<T> = ComplexTypes.DeferredObject | T | number
export type ContentListReferenceField<T> = ComplexTypes.DeferredObject | T[] | number[]

export type ContentReferenceField<T> = ComplexTypes.DeferredObject | T | number;
export type ContentListReferenceField<T> = ComplexTypes.DeferredObject | T[] | number[];

export type BinaryField = ComplexTypes.MediaResourceObject;
export type BinaryField = ComplexTypes.MediaResourceObject
");

// Do not call base because only classes will be read.
Expand All @@ -55,7 +52,7 @@ protected override IMetaNode VisitClass(Class @class)
propertyLines.Add($" /* {fieldDescription} */");
}
var isRequired = RequiredFields.Contains(property.Name) ? "!" : "?";
propertyLines.Add($"public {property.Name}{isRequired}: {GetPropertyTypeName(property)};");
propertyLines.Add($"public {property.Name}{isRequired}: {GetPropertyTypeName(property)}");
}

var type = @class.Name;
Expand Down Expand Up @@ -85,8 +82,8 @@ protected override IMetaNode VisitClass(Class @class)
WriteLine();
if (string.IsNullOrWhiteSpace(parentName))
{
WriteLine("public Actions?: ContentListReferenceField<IActionModel>;");
WriteLine("public Type!: string;");
WriteLine("public Actions?: ContentListReferenceField<ActionModel>");
WriteLine("public Type!: string");
}
_indentCount--;
WriteLine("}");
Expand Down
Expand Up @@ -22,14 +22,14 @@ protected override IMetaNode VisitSchema(ContentRepository.Schema.Metadata.Schem
* This module is autogenerated from Sense/Net ContentRepository.
*
* ```
* let link = new Fields.HyperlinkData({
* const link = new Fields.HyperlinkData({
* Href: 'http://sensenet.com',
* Text: 'Link to sensenet.com',
* Title: 'Go to sensenet.com',
* Target: '_blank'
* });
*
* let webContent = new ContentTypes.WebContentDemo({
* const webContent = new ContentTypes.WebContentDemo({
* Id: 1,
* Name: 'MyContent',
* DisplayName: 'My Content',
Expand All @@ -38,9 +38,7 @@ protected override IMetaNode VisitSchema(ContentRepository.Schema.Metadata.Schem
* });
*
* ```
*/ /** */

// tslint:disable:naming-convention
*/
");
#endregion

Expand Down
48 changes: 24 additions & 24 deletions src/Services/OData/Typescript/TypescriptCtdVisitor.cs
Expand Up @@ -12,37 +12,37 @@ internal class TypescriptCtdVisitor : TypescriptModuleWriter
protected override IMetaNode VisitSchema(ContentRepository.Schema.Metadata.Schema schema)
{
#region Write filestart
_writer.WriteLine(@"/**
* @module Schemas
* @preferred
* @description Module for ContentType schemas.
*
* A Content Type Definition in Sense/Net is an xml-format configuration file for defining Content Types. The xml configuration (CTD) holds information about the types name and description
* properties that control how content of this type look and behave (icon, preview generation, indexing), set of fields, etc. This information about the type and its Fields helps us for example
* building forms. Based on the Field definitions we can render a Field with its DisplayName as a label or validate the Field on save by its validation related configs.
*
* This module provides us description of this Content schemas in Typesript.
*
* The ```Schema``` class represents an object that holds the basic information about the Content Type (name, icon, ect.) and an array of its ```FieldSettings``` and their full configuration.
*/

import * as FieldSettings from ""./FieldSettings"";
_writer.WriteLine(@"/**
* @module Schemas
* @preferred
* @description Module for ContentType schemas.
*
* A Content Type Definition in Sense/Net is an xml-format configuration file for defining Content Types. The xml configuration (CTD) holds information about the types name and description
* properties that control how content of this type look and behave (icon, preview generation, indexing), set of fields, etc. This information about the type and its Fields helps us for example
* building forms. Based on the Field definitions we can render a Field with its DisplayName as a label or validate the Field on save by its validation related configs.
*
* This module provides us description of this Content schemas in Typesript.
*
* The ```Schema``` class represents an object that holds the basic information about the Content Type (name, icon, ect.) and an array of its ```FieldSettings``` and their full configuration.
*/
import * as FieldSettings from './FieldSettings'

/**
* Class that represents a Schema.
*
* It represents an object that holds the basic information about the Content Type (name, icon, ect.) and an array of its ```FieldSettings``` and their full configuration.
*/
export class Schema {
public ContentTypeName!: string;
public ParentTypeName?: string;
public Icon!: string;
public DisplayName!: string;
public Description!: string;
public AllowIndexing!: boolean;
public AllowIncrementalNaming!: boolean;
public AllowedChildTypes!: string[];
public FieldSettings!: FieldSettings.FieldSetting[];
public ContentTypeName!: string
public ParentTypeName?: string
public Icon!: string
public DisplayName!: string
public Description!: string
public AllowIndexing!: boolean
public AllowIncrementalNaming!: boolean
public AllowedChildTypes!: string[]
public FieldSettings!: FieldSettings.FieldSetting[]
}

export const SchemaStore: Schema[] = [
Expand Down
6 changes: 2 additions & 4 deletions src/Services/OData/Typescript/TypescriptEnumsVisitor.cs
Expand Up @@ -24,16 +24,14 @@ protected override IMetaNode VisitSchema(ContentRepository.Schema.Metadata.Schem
* This module is autogenerated from Sense/Net metadata (/Odata/$metadata)
*
* ```
* let car = new ContentTypes.Car({
* const car = new ContentTypes.Car({
* Id: 1,
* Name: 'MyCar',
* DisplayName: 'My Car',
* Style: Style.Cabrio
* });
* ```
*/

// tslint:disable:naming-convention
");
#endregion

Expand All @@ -47,7 +45,7 @@ protected override IMetaNode VisitEnumeration(Enumeration enumeration)
{
// do not call base functionality in this method

var options = enumeration.Options.Select(o => $"{o.Name} = \"{o.Value}\"").ToArray();
var options = enumeration.Options.Select(o => $"{o.Name} = '{o.Value}'").ToArray();

var names = Context.EmittedEnumerationNames
.Where(x => x.Value == enumeration.Key)
Expand Down
Expand Up @@ -3,8 +3,6 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using SenseNet.ContentRepository.Schema;
using SenseNet.ContentRepository.Schema.Metadata;

Expand Down Expand Up @@ -61,8 +59,7 @@ protected override IMetaNode VisitSchema(ContentRepository.Schema.Metadata.Schem
* Enum for HyperLink field href values.
*/
export enum UrlFormat { Hyperlink, Picture }

// tslint:disable-next-line:only-arrow-functions

export function isFieldSettingOfType<T extends FieldSetting>(setting: FieldSetting, type: { new(): T }): setting is T {
return setting.Type === type.name;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Services/OData/Typescript/TypescriptModuleWriter.cs
Expand Up @@ -191,7 +191,7 @@ protected string GetPropertyTypeName(Enumeration enumeration)
string enumKey;
if (!Context.EmittedEnumerationNames.TryGetValue(enumName, out enumKey) || enumKey != enumeration.Key)
enumName = Context.GetEnumerationFullName(enumeration);
return $"{TypescriptGenerationContext.EnumTypesModuleName}.{enumName}";
return $"{TypescriptGenerationContext.EnumTypesModuleName}.{enumName}[]";
}

protected void WriteStart(string text)
Expand Down