Example #1
0
        public string TransposeCgClassModToString(CgClassModifier cm)
        {
            switch (cm)
            {
            case CgClassModifier.AsAbstract:
                return("abstract");

            case CgClassModifier.AsPartial:
                return("partial");

            case CgClassModifier.AsStatic:
                return("static");

            default:
                return(String.Empty);
            }
        }
Example #2
0
 public string TransposeCgClassModToString(CgClassModifier cm)
 {
     switch (cm)
     {
         case CgClassModifier.AsAbstract:
             return "abstract";
         case CgClassModifier.AsPartial:
             return "partial";
         case CgClassModifier.AsStatic:
             return "static";
         default:
             return String.Empty;
     }
 }
Example #3
0
        public string ToClass(CgType cgType, CgAccessModifier cgClsAccess, CgClassModifier typeModifier, string[] nsImports)
        {
            var hasNs = !string.IsNullOrWhiteSpace(cgType.Namespace);
            var splitFileContent = new StringBuilder();
            if (nsImports != null && nsImports.Length > 0)
            {
                splitFileContent.AppendLine(string.Join(Environment.NewLine, nsImports));
            }

            if (hasNs)
            {
                splitFileContent.AppendFormat("namespace {0}{1}", cgType.Namespace, Environment.NewLine);
                splitFileContent.AppendLine(C_OPEN_CURLY.ToString());

            }
            var otherModifer = typeModifier == CgClassModifier.AsIs
                ? string.Empty
                : string.Format(" {0}", Settings.LangStyle.TransposeCgClassModToString(typeModifier));
            splitFileContent.AppendLine("    [Serializable]");
            splitFileContent.AppendLine(string.Format("    {0}{1} class {2}",
                Settings.LangStyle.TransposeCgAccessModToString(cgClsAccess), otherModifer, cgType.Name));
            splitFileContent.AppendLine("    " + C_OPEN_CURLY);
            foreach (var cg in cgType.Fields)
                splitFileContent.AppendLine(string.Join(Environment.NewLine, cg.GetMyCgLines()));
            foreach (var cg in cgType.Properties)
                splitFileContent.AppendLine(string.Join(Environment.NewLine, cg.GetMyCgLines()));
            foreach (var cg in cgType.Methods)
                splitFileContent.AppendLine(string.Join(Environment.NewLine, cg.GetMyCgLines()));
            splitFileContent.AppendLine("    " + C_CLOSE_CURLY);

            if (hasNs)
                splitFileContent.AppendLine(C_CLOSE_CURLY.ToString());

            return splitFileContent.ToString();
        }
Example #4
0
        /// <summary>
        /// Moves the given code-gen source code definition to another file
        /// </summary>
        /// <returns>The full paths to the created source code files </returns>
        public static string[] MoveType(CgType cgType, string outputDirectory = null, CgClassModifier typeModifier = CgClassModifier.AsIs)
        {
            if (cgType == null)
            {
                return(new string[0]);
            }

            outputDirectory = outputDirectory ?? NfSettings.AppData;

            //TODO - this is inconsistent where enclosure is one but src files are many
            var srcCodeFile = cgType.GetMySrcCodeFiles()?.FirstOrDefault() ??
                              Path.Combine(NfSettings.AppData, cgType.Name + Settings.LangStyle.FileExtension);

            var fileName      = cgType.Name;
            var fileExtension = Path.GetExtension(srcCodeFile);
            var outFilePath   = Path.Combine(outputDirectory, fileName + fileExtension);

            //don't overwrite whatever is already there
            if (File.Exists(outFilePath))
            {
                outFilePath = Path.Combine(outputDirectory, fileName + NfString.GetNfRandomName() + fileExtension);
            }

            var newlines = new List <string>();
            var hasNs    = !string.IsNullOrWhiteSpace(cgType.Namespace);

            if (hasNs)
            {
                newlines.AddRange(Settings.LangStyle.ToNamespaceDecl(cgType).ConvertToLf().Split('\n'));
            }

            if (File.Exists(srcCodeFile))
            {
                var start    = cgType.GetMyStartEnclosure().Item1;
                var end      = cgType.GetMyEndEnclosure().Item1;
                var srcLines = File.ReadAllLines(srcCodeFile);
                newlines.AddRange(srcLines.Skip(start - 1).Take(end + 1 - start));
            }
            else
            {
                var genLines = typeModifier == CgClassModifier.AsInterface
                    ? Settings.LangStyle.ToInterface(cgType)
                    : Settings.LangStyle.ToClass(cgType, CgAccessModifier.Public, typeModifier);
                newlines.AddRange(genLines.ConvertToLf().Split('\n'));
            }
            if (hasNs)
            {
                newlines.Add(Settings.LangStyle.GetEnclosureCloseToken(null));
            }

            //write the type def to file
            File.WriteAllLines(outFilePath, newlines, Encoding.UTF8);

            return(new[] { outFilePath });
        }
Example #5
0
        public string ToClass(CgType cgType, CgAccessModifier cgClsAccess = CgAccessModifier.Public, CgClassModifier typeModifier = CgClassModifier.AsIs, string[] nsImports = null)
        {
            var splitFileContent = new StringBuilder();

            if (nsImports != null && nsImports.Length > 0)
            {
                splitFileContent.AppendLine(string.Join(Environment.NewLine, nsImports));
            }

            var otherModifer = typeModifier == CgClassModifier.AsIs
                ? string.Empty
                : string.Format(" {0}", Settings.LangStyle.TransposeCgClassModToString(typeModifier));

            splitFileContent.AppendLine("    [Serializable]");
            splitFileContent.AppendLine(string.Format("    {0}{1} class {2}",
                                                      Settings.LangStyle.TransposeCgAccessModToString(cgClsAccess), otherModifer, cgType.Name));
            splitFileContent.AppendLine("    " + C_OPEN_CURLY);
            foreach (var cg in cgType.Fields)
            {
                splitFileContent.AppendLine(string.Join(Environment.NewLine, cg.GetMyCgLines()));
            }
            foreach (var cg in cgType.Properties)
            {
                splitFileContent.AppendLine(string.Join(Environment.NewLine, cg.GetMyCgLines()));
            }
            foreach (var cg in cgType.Methods)
            {
                splitFileContent.AppendLine(string.Join(Environment.NewLine, cg.GetMyCgLines()));
            }
            splitFileContent.AppendLine("    " + C_CLOSE_CURLY);

            return(splitFileContent.ToString());
        }