/// <summary> /// Write Source /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { //sec check if (Condition == null) { throw new ArgumentNullException("Condition in TsConditionStatement not set"); } //get condition source string conditionSource = Condition.GetSource(options, info); //create statement string statementSource = options.GetPreLineIndentString(info.Depth) + string.Format(TsDomConstants.TS_IF_STATEMENT_FORMAT, conditionSource); //add start bracket for true condition statementSource += TsDomConstants.STATEMENT_BRACKET_BEGIN; //write writer.WriteLine(statementSource); //if there are true statements (add them) if (TrueStatements.Any()) { //write statements TrueStatements.ToList().ForEach(el => el.WriteSource(writer, options, info.Clone(info.Depth + 1))); } //write end bracket writer.WriteLine(GetStatementEnd(options, info.Depth)); }
/// <summary> /// write source /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { var source = options.GetPreLineIndentString(info.Depth); source += string.Format(TsDomConstants.ASSIGN_FORMAT, Left.GetSource(options, info), Right.GetSource(options, info)); source += TsDomConstants.EXPRESSION_END; //write writer.WriteLine(source); }
/// <summary> /// WriteSource /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { if (Expression == null) { throw new ArgumentNullException("Expression in TsCodeMethodReturnStatement is null"); } var source = options.GetPreLineIndentString(info.Depth) + string.Format(TsDomConstants.TS_METHOD_RETURN_FORMAT, Expression.GetSource(options, info)) + TsDomConstants.EXPRESSION_END; //write source writer.WriteLine(source); }
/// <summary> /// Write Source /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { if (Comment == null) { throw new ArgumentNullException("Comment in TsCodeCommentStatement is null"); } //write comment var source = options.GetPreLineIndentString(info.Depth); source += Comment.GetSource(options, info); writer.WriteLine(source); }
/// <summary> /// Write Source /// </summary> /// <param name="writer"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { //write base stuff base.WriteSource(writer, options.Clone(options.IndentString, false), info); //get line Intend string var intendLineString = options.GetPreLineIndentString(info.Depth); //membername (nullable for inline objects not allowed) var memberName = IsNullable && info.ForType != TsElementTypes.InlineObject ? string.Format(TsDomConstants.NULLABLE_FORMAT, Name) : Name; //prepare source var source = string.Empty; //we dont need the type for constant if (info.ForType == TsElementTypes.Constant || info.ForType == TsElementTypes.InlineObject || info.ForType == TsElementTypes.Enumerations) { //if its an inline object and membername as string => add string signs if (info.ForType == TsElementTypes.InlineObject && info.MemberNameAsString) { memberName = string.Format(TsDomConstants.STRING_VALUE_FORMAT, memberName); } source += memberName; } else { source += string.Format(TsDomConstants.TS_ELEMENT_TYPE_FORMAT, memberName, GetTypeSource()); } //typeattributes are only interresting in classes if (info.ForType == TsElementTypes.Class) { //get type attributes var typeAttributes = TsTypeAttributeMappings.TypeMappings.Where(el => TypeAttributes.HasFlag(el.Key)).OrderBy(el => el.Key).ToList(); //combine attriubtes var typeAttributeSource = string.Join(TsDomConstants.ATTRIBUTE_SEPEARATOR, typeAttributes.Select(el => el.Value)); //add type attribute source = string.Format(TsDomConstants.TS_ATTRIBUTE_COMBINE_FORMAT, typeAttributeSource, source); } //add line intendent string source = intendLineString + source; //add init statement if its set if (InitStatement != null) { AddInitStatement(ref source, info.ForType, options, info); } //add end seperator (for enum its different var endSeperator = info.ForType == TsElementTypes.Enumerations || info.ForType == TsElementTypes.Constant || info.ForType == TsElementTypes.InlineObject ? TsDomConstants.LIST_ELEMENT_SEPERATOR : TsDomConstants.EXPRESSION_END; //add end seperator source += endSeperator; //write writer.WriteLine(source); }
/// <summary> /// WriteSource /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { //sec check if (Expression == null) { throw new Exception(string.Format("TsCodeExpressionStatement: Expression is null")); } var source = options.GetPreLineIndentString(info.Depth); source += Expression.GetSource(options, info) + TsDomConstants.EXPRESSION_END; //write writer.WriteLine(source); }
/// <summary> /// AddCustomAttributes /// </summary> /// <param name="writer"></param> private void AddCustomAttributes(StreamWriter writer, TsGeneratorOptions options, int depth) { //check if there are custom attributes if (Decorators.Any()) { //add indent var customAttributeString = options.GetPreLineIndentString(depth); //get source var attributeList = Decorators.Select(el => el.GetSource()); customAttributeString += string.Join(TsDomConstants.ATTRIBUTE_SEPEARATOR, attributeList); //write custom attribute string writer.WriteLine(customAttributeString); } }
/// <summary> /// Write Source /// </summary> /// <param name="writer"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { //write base stuff base.WriteSource(writer, options.Clone(options.IndentString, false), info); //sec check if (info.ForType != TsElementTypes.Class) { throw new Exception("TsCodeMemberProperty can only be defined for class"); } if (!Types.Any()) { throw new Exception("TsCodeMemberProperty type not defined"); } //prepare source var source = options.GetPreLineIndentString(info.Depth); //add attributres if (Attributes != TsMemberAttributes.None) { source += TsMemberAttributeMappings.TypeMappings[Attributes] + TsDomConstants.ATTRIBUTE_SEPEARATOR; } //if there is no setter and getter this is a bit useless if (!HasSet && !HasGet) { throw new Exception("TsCodeMemberProperty there is no setter and getter for MemberProperty" + Name); } //if we have a getter write getter if (HasGet) { var getSource = source + string.Format(TsDomConstants.GETTER_FORMAT, Name, GetTypeSource()) + TsDomConstants.STATEMENT_BRACKET_BEGIN; //write begin line writer.WriteLine(getSource); //write statemetns GetStatements.ToList().ForEach(el => el.WriteSource(writer, options, info.Clone(info.Depth + 1))); //write end source writer.WriteLine(GetStatementEnd(options, info.Depth)); } //as setter if (HasSet) { var setSource = source + string.Format(TsDomConstants.SETTER_FORMAT, Name, SetParameterName, GetTypeSource()) + TsDomConstants.STATEMENT_BRACKET_BEGIN; //write begin line writer.WriteLine(setSource); //write statemetns SetStatements.ToList().ForEach(el => el.WriteSource(writer, options, info.Clone(info.Depth + 1))); //write end source writer.WriteLine(GetStatementEnd(options, info.Depth)); } }
/// <summary> /// Write Source /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { //sec check string detailError = null; if (!IsValid(out detailError)) { throw new Exception(detailError); } //get type string string typeString; //get typestring (if there isnt a type, use all selector) if (ImportTypes.Count <= 0) { typeString = TsDomConstants.START_SIGN; } else { var importTypeSourceList = ImportTypes.OrderBy(el => el.Name).Select(el => el.GetSource()); var importTypeIsNotSpecific = ImportTypes.Count == 1 && ImportTypes[0].IsImportAll; // if the import type is import all and its only one type, we dont need to wrap it in brackets typeString = importTypeIsNotSpecific ? ImportTypes[0].GetSource() : string.Format(TsDomConstants.CURLY_INLINE_BRACKETS_FORMAT, string.Join(TsDomConstants.PARAMETER_SEPERATOR, importTypeSourceList)); } string source = null; // export if (IsExport) { source = string.Format(TsDomConstants.TS_EXPORT_STATEMENT_FORMAT, typeString, Path); } // import else { source = string.Format(TsDomConstants.TS_IMPORT_FORMAT, typeString, Path); } //add end expression source += TsDomConstants.EXPRESSION_END; //write import writer.WriteLine(options.GetPreLineIndentString(info.Depth) + source); }
/// <summary> /// GetSource /// </summary> /// <returns></returns> internal override string GetSource(TsGeneratorOptions options, TsWriteInformation info) { var source = TsDomConstants.STATEMENT_BRACKET_BEGIN; //if there are properties if (Properties.Any()) { var newInfo = info.Clone(info.Depth + 1); newInfo.ForType = TsElementTypes.InlineObject; newInfo.MemberNameAsString = MemberNameAsString; //add new Line source += Environment.NewLine; Properties.ToList().ForEach(el => source += el.GetStringFromWriteSource(options, newInfo)); source += options.GetPreLineIndentString(info.Depth); } source += TsDomConstants.STATEMENT_BRACKET_END; //return return(source); }
/// <summary> /// Write Source /// attribute methodname() : returntype /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { //write base stuff base.WriteSource(writer, options, info); //check if (Decorators.Any()) { throw new NotImplementedException("CodeMemberMethod, CustomAttributes not implemented yet!"); } //set attribute var methodTypeSource = GetSource(); if (Attributes != TsMemberAttributes.None) { methodTypeSource = TsMemberAttributeMappings.TypeMappings[Attributes] + TsDomConstants.ATTRIBUTE_SEPEARATOR + methodTypeSource; } //add method source var methodSource = options.GetPreLineIndentString(info.Depth) + string.Format(TsDomConstants.TS_MEMBERMETHOD_FORMAT, methodTypeSource, GetParameterSource(options, info)); //return type (empty string if not available) if (ReturnType != null) { methodSource = string.Format(TsDomConstants.TS_ELEMENT_TYPE_FORMAT, methodSource, ReturnType.TsTypeName); } // we only write method info for interfaces if (info.ForType != TsElementTypes.Interface) { //add statement begin methodSource += TsDomConstants.ATTRIBUTE_SEPEARATOR + TsDomConstants.STATEMENT_BRACKET_BEGIN; //write begin line writer.WriteLine(methodSource); //write statemetns Statements.ToList().ForEach(el => el.WriteSource(writer, options, info.Clone(info.Depth + 1))); //write end source writer.WriteLine(GetStatementEnd(options, info.Depth)); } else { methodSource += TsDomConstants.EXPRESSION_END; writer.WriteLine(methodSource); } }
/// <summary> /// Get Source /// </summary> /// <param name="options"></param> /// <param name="info"></param> /// <returns></returns> internal string GetSource(TsGeneratorOptions options, TsWriteInformation info) { //add begin var result = TsDomConstants.COMMENT_BEGIN; if (!string.IsNullOrEmpty(Text)) { //check if there are more lines var commentLines = Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); //add first line result += commentLines.First(); //iterate all other lines for (int i = 1; i < commentLines.Length; i++) { result += Environment.NewLine + options.GetPreLineIndentString(info.Depth) + TsDomConstants.COMMENT_IN_LINE + commentLines[i]; } result += TsDomConstants.COMMENT_END; } return(result); }
/// <summary> /// Write Soruce /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="info"></param> internal override void WriteSource(System.IO.StreamWriter writer, TsGeneratorOptions options, TsWriteInformation info) { //type variableName = initexpression //add line indent string var source = options.GetPreLineIndentString(info.Depth); //add type and variable name source += InitKeyReference.TsTypeName + TsDomConstants.ATTRIBUTE_SEPEARATOR + Name; //if type is set we will add type if (Type != null) { source = string.Format(TsDomConstants.TS_ELEMENT_TYPE_FORMAT, source, Type.TsTypeName); } //if there is an init expression add init expression if (InitExpression != null) { source = string.Format(TsDomConstants.ASSIGN_FORMAT, source, InitExpression.GetSource(options, info)); } source += TsDomConstants.EXPRESSION_END; //write source writer.WriteLine(source); }
/// <summary> /// Add Statement End /// </summary> /// <param name="statement"></param> /// <returns></returns> protected string GetStatementEnd(TsGeneratorOptions options, int depth) { return(options.GetPreLineIndentString(depth) + TsDomConstants.STATEMENT_BRACKET_END); }