Ejemplo n.º 1
0
        public void ShouldRetrieveClassProperties()
        {
            var          codeFile  = new CodeFileInfo(string.Empty, string.Empty);
            const string className = "Class";
            var          aClass    = new DeclerationInfo(className, codeFile);

            Assert.That(aClass.Name, Is.EqualTo(className));
            Assert.That(aClass.CodeFileInfo, Is.EqualTo(codeFile));
        }
Ejemplo n.º 2
0
        public ICodeFileInfo FromFile(FileInfo file)
        {
            var codeFile     = new CodeFileInfo(file.Name, file.DirectoryName);
            var declerations = declerationInfoLoader.ExamineSource(codeFile);

            foreach (var decleration in declerations)
            {
                codeFile.AddDecleration(decleration);
            }

            return(codeFile);
        }
        private static FileInfo WriteCodeFile(CodeFileInfo codeFileInfo, DirectoryInfo destination)
        {
            var file = new FileInfo(Path.Combine(destination.FullName, $"{codeFileInfo.className}.cs"));

            using (var writeStream = file.Open(FileMode.Create, FileAccess.Write))
            {
                using (var writer = new StreamWriter(writeStream))
                    writer.Write(codeFileInfo.fileContents);
            }

            return(file);
        }
        private static CodeFileInfo[] GenerateCodeFiles(string @namespace, IReadOnlyCollection <CodeTypeDeclaration> types)
        {
            var provider = CodeDomProvider.CreateProvider("CSharp");
            var options  = new CodeGeneratorOptions()
            {
                BracingStyle             = "C",
                IndentString             = "    ",
                BlankLinesBetweenMembers = false
            };

            var compileUnit   = new CodeCompileUnit();
            var codeNamespace = new CodeNamespace(@namespace);

            compileUnit.Namespaces.Add(codeNamespace);

            var output = new CodeFileInfo[types.Count];
            int index  = 0;

            using (var writer = new StringWriter())
            {
                foreach (var type in types)
                {
                    writer.GetStringBuilder().Clear();
                    codeNamespace.Types.Clear();

                    codeNamespace.Types.Add(type);

                    provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);

                    output[index++] = new CodeFileInfo()
                    {
                        className = type.Name, fileContents = writer.ToString()
                    };
                }
            }

            return(output);
        }