Ejemplo n.º 1
0
        public void GenerateProject(CslaGeneratorUnit unit)
        {
            CslaTemplateHelper.PrimaryKeys.ClearCache();
            CslaObjectInfo objInfo = null;

            objFailed    = 0;
            objSuccess   = 0;
            sprocFailed  = 0;
            sprocSuccess = 0;
            Controls.OutputWindow.Current.ClearOutput();
            Metadata.GenerationParameters generationParams = unit.GenerationParams;
            _generateDatabaseClass = generationParams.GenerateDatabaseClass;
            _targetFramework       = generationParams.TargetFramework;
            _abortRequested        = false;
            _fullTemplatesPath     = _templatesDirectory + generationParams.TargetFramework.ToString() + @"\";
            templates = new Hashtable();//to recompile templates in case they changed.
            //This is just in case users add/remove objects while generating...
            List <CslaObjectInfo> list = new List <CslaObjectInfo>();

            for (int i = 0; i < unit.CslaObjects.Count; i++)
            {
                if (unit.CslaObjects[i].Generate)
                {
                    list.Add(unit.CslaObjects[i]);
                }
            }
            foreach (CslaObjectInfo info in list)
            {
                if (info.Generate)
                {
                    if (objInfo == null)
                    {
                        objInfo = info;
                    }
                    if (_abortRequested)
                    {
                        break;
                    }
                    OnStep(info.ObjectName);
                    try
                    {
                        GenerateObject(info, unit);
                    }
                    catch (Exception ex)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("Object {0} failed to generate:", info.ObjectName);
                        sb.AppendLine();
                        sb.Append("    ");
                        sb.Append(ex.Message);
                        OnGenerationInformation(sb.ToString());
                    }
                    if (_abortRequested)
                    {
                        break;
                    }

                    if (generationParams.GenerateSprocs && info.GenerateSprocs)
                    {
                        try
                        {
                            if (generationParams.OneSpFilePerObject)
                            {
                                GenerateAllSprocsFile(info, _targetDirectory, generationParams);
                            }
                            else
                            {
                                GenerateSelectProcedure(info, _targetDirectory);
                                if (_abortRequested)
                                {
                                    break;
                                }

                                if (info.ObjectType != CslaObjectType.ReadOnlyObject &&
                                    info.ObjectType != CslaObjectType.ReadOnlyCollection &&
                                    info.ObjectType != CslaObjectType.EditableRootCollection &&
                                    info.ObjectType != CslaObjectType.DynamicEditableRootCollection &&
                                    info.ObjectType != CslaObjectType.EditableChildCollection &&
                                    info.ObjectType != CslaObjectType.NameValueList)
                                {
                                    GenerateInsertProcedure(info, _targetDirectory);
                                    if (_abortRequested)
                                    {
                                        break;
                                    }

                                    GenerateDeleteProcedure(info, _targetDirectory);
                                    if (_abortRequested)
                                    {
                                        break;
                                    }

                                    GenerateUpdateProcedure(info, _targetDirectory);
                                    if (_abortRequested)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            StringBuilder GenerationErrors = new StringBuilder();
                            GenerationErrors.Append(Environment.NewLine + "	SpGeneration Crashed:");
                            GenerationErrors.Append(ex.Message);
                            GenerationErrors.AppendLine();
                            GenerationErrors.AppendLine();
                            if (ex.InnerException != null)
                            {
                                GenerationErrors.AppendLine(ex.InnerException.Message);
                                GenerationErrors.AppendLine();
                                GenerationErrors.AppendLine(ex.InnerException.StackTrace.ToString());
                            }
                            GenerationErrors.AppendLine();
                            OnGenerationInformation(GenerationErrors.ToString());
                        }
                    }
                }
            }

            if (_generateDatabaseClass)
            {
                GenerateUtilityFile("Database", objInfo, generationParams.TargetFramework, unit);
            }
            else
            {
                _fileSuccess.Add("Database", null);
            }

            GenerateUtilityFile("DataPortalHookArgs", objInfo, generationParams.TargetFramework, unit);

            if (_abortRequested)
            {
                OnGenerationInformation(Environment.NewLine + "Code Generation Cancelled!");
            }
            OnFinalized();
        }
Ejemplo n.º 2
0
        public void GenerateProject(CslaGeneratorUnit unit)
        {
            if (!TargetDeprecated(unit))
            {
                OnGenerationInformation(@"Project generation cancelled." + Environment.NewLine, 1);
                return;
            }

            objFailed    = 0;
            objSuccess   = 0;
            sprocFailed  = 0;
            sprocSuccess = 0;
            Controls.OutputWindow.Current.ClearOutput();
            Controls.OutputWindow.Current.AddOutputInfo(string.Format("Generating code in '{0}'", _targetDirectory));
            Metadata.GenerationParameters generationParams = unit.GenerationParams;
            _abortRequested    = false;
            _fullTemplatesPath = _templatesDirectory + generationParams.TargetFramework + @"\";
            templates          = new Hashtable();//to recompile templates in case they changed.
            //This is just in case users add/remove objects while generating...
            List <CslaObjectInfo> list = new List <CslaObjectInfo>();

            for (int i = 0; i < unit.CslaObjects.Count; i++)
            {
                if (unit.CslaObjects[i].Generate)
                {
                    list.Add(unit.CslaObjects[i]);
                }
            }
            foreach (CslaObjectInfo info in list)
            {
                if (info.Generate)
                {
                    if (_abortRequested)
                    {
                        break;
                    }
                    OnStep(info.ObjectName);
                    try
                    {
                        GenerateObject(info, unit);
                    }
                    catch (Exception ex)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("Object {0} failed to generate:", info.ObjectName);
                        sb.AppendLine();
                        sb.Append("\t");
                        sb.Append(ex.Message);
                        OnGenerationInformation(sb.ToString(), 2);
                    }
                    if (_abortRequested)
                    {
                        break;
                    }

                    if (generationParams.GenerateSprocs && info.GenerateSprocs)
                    {
                        try
                        {
                            if (generationParams.OneSpFilePerObject)
                            {
                                GenerateAllSprocsFile(info, GetSprocFileInfo(info.ObjectName));
                            }
                            else
                            {
                                GenerateSelectProcedure(info);
                                if (_abortRequested)
                                {
                                    break;
                                }

                                if (info.ObjectType != CslaObjectType.ReadOnlyObject &&
                                    info.ObjectType != CslaObjectType.ReadOnlyCollection &&
                                    info.ObjectType != CslaObjectType.EditableRootCollection &&
                                    info.ObjectType != CslaObjectType.EditableChildCollection &&
                                    info.ObjectType != CslaObjectType.NameValueList)
                                {
                                    GenerateInsertProcedure(info, _targetDirectory);
                                    if (_abortRequested)
                                    {
                                        break;
                                    }

                                    GenerateDeleteProcedure(info, _targetDirectory);
                                    if (_abortRequested)
                                    {
                                        break;
                                    }

                                    GenerateUpdateProcedure(info, _targetDirectory);
                                    if (_abortRequested)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            StringBuilder GenerationErrors = new StringBuilder();
                            GenerationErrors.Append(Environment.NewLine + "	SpGeneration Crashed:");
                            GenerationErrors.Append(ex.Message);
                            GenerationErrors.AppendLine();
                            GenerationErrors.AppendLine();
                            if (ex.InnerException != null)
                            {
                                GenerationErrors.AppendLine(ex.InnerException.Message);
                                GenerationErrors.AppendLine();
                                GenerationErrors.AppendLine(ex.InnerException.StackTrace.ToString());
                            }
                            GenerationErrors.AppendLine();
                            OnGenerationInformation(GenerationErrors.ToString(), 2);
                        }
                    }
                }
            }
            if (_abortRequested)
            {
                OnGenerationInformation(Environment.NewLine + "Code Generation Cancelled!", 2);
            }
            OnFinalized();
        }
Ejemplo n.º 3
0
        private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit)
        {
            Metadata.GenerationParameters generationParams = unit.GenerationParams;
            string fileName             = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false);
            string baseFileName         = GetBaseFileName(objInfo, true, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false);
            string classCommentFileName = string.Empty;

            if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix))
            {
                classCommentFileName = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, true, generationParams.SeparateClassComment);
            }
            FileStream   fsBase = null;
            StreamWriter swBase = null;
            StreamWriter sw     = null;

            try
            {
                string       tPath    = this._fullTemplatesPath + objInfo.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo);
                CodeTemplate template = GetTemplate(objInfo, tPath);
                if (template != null)
                {
                    StringBuilder errorsOutput   = new StringBuilder();
                    StringBuilder warningsOutput = new StringBuilder();
                    template.SetProperty("ActiveObjects", generationParams.ActiveObjects);
                    template.SetProperty("Errors", errorsOutput);
                    template.SetProperty("Warnings", warningsOutput);
                    template.SetProperty("CurrentUnit", unit);
                    template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme);
                    if (generationParams.BackupOldSource && File.Exists(baseFileName))
                    {
                        FileInfo oldFile = new FileInfo(baseFileName);
                        if (File.Exists(baseFileName + ".old"))
                        {
                            File.Delete(baseFileName + ".old");
                        }
                        oldFile.MoveTo(baseFileName + ".old");
                    }
                    fsBase = File.Open(baseFileName, FileMode.Create);
                    OnGenerationFileName(baseFileName);
                    swBase = new StreamWriter(fsBase);
                    template.Render(swBase);
                    errorsOutput   = (StringBuilder)template.GetProperty("Errors");
                    warningsOutput = (StringBuilder)template.GetProperty("Warnings");
                    if (errorsOutput.Length > 0)
                    {
                        objFailed++;
                        OnGenerationInformation("Failed:" + Environment.NewLine + errorsOutput);
                    }
                    else
                    {
                        if (warningsOutput != null)
                        {
                            if (warningsOutput.Length > 0)
                            {
                                objectWarnings++;
                                OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput);
                            }
                        }
                        objSuccess++;
                        //OnGenerationInformation("Success");
                    }
                }
                GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit);
                if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix))
                {
                    GenerateClassCommentFile(classCommentFileName, objInfo, generationParams.ActiveObjects, unit);
                }
            }
            catch (Exception e)
            {
                objFailed++;
                ShowExceptionInformation(e);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                if (swBase != null)
                {
                    swBase.Close();
                }
            }
        }
Ejemplo n.º 4
0
        private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit)
        {
            Metadata.GenerationParameters generationParams = unit.GenerationParams;
            string fileName     = GetFileName(objInfo, generationParams.SeparateNamespaces, generationParams.OutputLanguage);
            string baseFileName = GetBaseFileName(objInfo, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.UseDotDesignerFileNameConvention, generationParams.OutputLanguage);

            try
            {
                GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit);
                string       tPath    = this._fullTemplatesPath + generationParams.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo);
                CodeTemplate template = GetTemplate(objInfo, tPath);
                if (template != null)
                {
                    StringBuilder errorsOutput   = new StringBuilder();
                    StringBuilder warningsOutput = new StringBuilder();
                    template.SetProperty("ActiveObjects", generationParams.ActiveObjects);
                    template.SetProperty("Errors", errorsOutput);
                    template.SetProperty("Warnings", warningsOutput);
                    template.SetProperty("CurrentUnit", unit);
                    template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme);
                    if (generationParams.BackupOldSource && File.Exists(baseFileName))
                    {
                        FileInfo oldFile = new FileInfo(baseFileName);
                        if (File.Exists(baseFileName + ".old"))
                        {
                            File.Delete(baseFileName + ".old");
                        }
                        oldFile.MoveTo(baseFileName + ".old");
                    }
                    FileInfo fi = new FileInfo(baseFileName);
                    using (FileStream fs = fi.Open(FileMode.Create))
                    {
                        OnGenerationFileName(fi.FullName);
                        using (StreamWriter swBase = new StreamWriter(fs))
                        {
                            template.Render(swBase);
                            errorsOutput   = (StringBuilder)template.GetProperty("Errors");
                            warningsOutput = (StringBuilder)template.GetProperty("Warnings");
                            if (errorsOutput.Length > 0)
                            {
                                objFailed++;
                                OnGenerationInformation("Failed:" + Environment.NewLine +
                                                        errorsOutput.ToString(), 2);
                            }
                            else
                            {
                                if (warningsOutput != null)
                                {
                                    if (warningsOutput.Length > 0)
                                    {
                                        objectWarnings++;
                                        OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput, 2);
                                    }
                                }
                                objSuccess++;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                objFailed++;
                ShowExceptionInformation(e);
            }
        }