Esempio n. 1
0
        public CodeGenResult Generate()
        {
            try
            {
                NewContext();
                var modelsScript = "";
                foreach (var param in _modelsGeneratorInputs)
                {
                    modelsScript += GenerateModelSourceCode(param) + "\n";
                }

                var usings = CurrentContext.GetNamespaces();
                if (usings.Contains(_namespaceStr))
                {
                    usings.Remove(_namespaceStr);
                }
                modelsScript = CodeGenExtensions.WrapClass(modelsScript, _namespaceStr, usings);

                var res = new CodeGenResult
                {
                    Context    = CurrentContext,
                    CSharpCode = modelsScript
                };
                return(res);
            }
            catch (Exception ex)
            {
                throw new CodeGenException($"Exception in {GetType().Name}.", ex);
            }
        }
        public Task CodeGen(TextWriter textWriter, bool addHeader = true)
        {
            MSBuildWorkspace workspace = MSBuildWorkspace.Create();
            Project          project   = workspace.OpenProjectAsync(_csProjPath).Result;

            if (project == null)
            {
                throw new ArgumentException("Could not open the project located at " + _csProjPath);
            }

            StringWriter classes = new StringWriter();
            var          usings  = new HashSet <string>();

            foreach (ICodeGenParticipant codeGenParticipant in _codeGenParticipants)
            {
                CodeGenResult result = codeGenParticipant.CodeGen(workspace, project).Result;
                classes.Write(result.Code);
                usings.UnionWith(result.Usings);
                if (codeGenParticipant != _codeGenParticipants.Last())
                {
                    classes.Write(Environment.NewLine);
                    classes.Write(Environment.NewLine);
                }
            }

            if (addHeader)
            {
                AddHeader(textWriter);
                textWriter.Write(Environment.NewLine);
            }

            foreach (string nameSpace in usings)
            {
                textWriter.Write("using {0};", nameSpace);
                textWriter.Write(Environment.NewLine);
            }
            textWriter.Write(Environment.NewLine);
            textWriter.Write(classes);
            workspace.CloseSolution();
            return(Task.FromResult(false));
        }