private JsonFactory CreateFactory(string factoryName, Int32 factoryID)
        {
            string factorysuffix="factory";
            //string factoryName = string.Format("{0}", "json" );
            string injectedFactoryString = null;
            string returnFactoryName = string.Format("that{0}", factoryName);
            string parameterName ="resp";
            string factoryFields = null;
            string[] factoryFieldsArray =null;
            string[] injectedFactoryArray = null;
            StringBuilder bodyFunction = new StringBuilder();
            JsonFactory jfactory = new JsonFactory();

            injectedFactoryString = GetChildFactories(factoryID);
            factoryFields = GetFactoryFields( factoryID);
            injectedFactoryArray = injectedFactoryString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            factoryFieldsArray = factoryFields.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            bodyFunction.AppendLine(string.Format("function {0} (data) {1}",factoryName,"{"));

            bodyFunction.AppendLine(string.Format("this.init(data);"));

            bodyFunction.AppendLine(string.Format("{0};","}"));
            bodyFunction.AppendLine(string.Format("{0}.prototype ={1}",factoryName,"{"));
            bodyFunction.AppendLine(string.Format("init : function(data){0} ","{"));
            bodyFunction.AppendLine(string.Format("if(data){0}","{"));
            bodyFunction.AppendLine(string.Format("angular.extend(this,data);"));

            bodyFunction.AppendLine(string.Format("{0}", "}"));
            bodyFunction.AppendLine(string.Format("else {0}","{"));
            bodyFunction.AppendLine(string.Format("angular.extend(this, {0}", "{"));
            for (int i = 0; i < factoryFieldsArray.Length; i++)
            {
                bodyFunction.AppendLine(string.Format("{0} :null;",  factoryFieldsArray[i].Replace("'", ""))); //intialize factory fields to null
            }
            for (int i = 0; i < injectedFactoryArray.Length; i++)
            {
                bodyFunction.AppendLine(string.Format("{1} = {1}.init({0});", "{}", injectedFactoryArray[i].Replace("'", ""))); //call the init method of injected factories
            }
            bodyFunction.AppendLine("});");
            bodyFunction.AppendLine("}}}");
            bodyFunction.AppendLine(string.Format("return {0};", factoryName)); // return the intialized that object.

            jfactory.FactoryName = factoryName;
            if (injectedFactoryArray.Length>0)
            {
                jfactory.InjectedFactories = injectedFactoryString;
            }
            else
            {
                jfactory.InjectedFactories = string.Empty;
            }

            jfactory.FactoryBodyCode = bodyFunction.ToString();

            return jfactory;
        }
示例#2
0
        private JsonFactory CreateFactory(string factoryName, Int32 factoryID)
        {
            string factorysuffix = "factory";
            //string factoryName = string.Format("{0}", "json" );
            string injectedFactoryString = null;
            string returnFactoryName     = string.Format("that{0}", factoryName);
            string parameterName         = "resp";
            string factoryFields         = null;

            string[]      factoryFieldsArray   = null;
            string[]      injectedFactoryArray = null;
            StringBuilder bodyFunction         = new StringBuilder();
            JsonFactory   jfactory             = new JsonFactory();



            injectedFactoryString = GetChildFactories(factoryID);
            factoryFields         = GetFactoryFields(factoryID);
            injectedFactoryArray  = injectedFactoryString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            factoryFieldsArray    = factoryFields.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);


            bodyFunction.AppendLine(string.Format("function {0} (data) {1}", factoryName, "{"));

            bodyFunction.AppendLine(string.Format("this.init(data);"));

            bodyFunction.AppendLine(string.Format("{0};", "}"));
            bodyFunction.AppendLine(string.Format("{0}.prototype ={1}", factoryName, "{"));
            bodyFunction.AppendLine(string.Format("init : function(data){0} ", "{"));
            bodyFunction.AppendLine(string.Format("if(data){0}", "{"));
            bodyFunction.AppendLine(string.Format("angular.extend(this,data);"));


            bodyFunction.AppendLine(string.Format("{0}", "}"));
            bodyFunction.AppendLine(string.Format("else {0}", "{"));
            bodyFunction.AppendLine(string.Format("angular.extend(this, {0}", "{"));
            for (int i = 0; i < factoryFieldsArray.Length; i++)
            {
                bodyFunction.AppendLine(string.Format("{0} :null;", factoryFieldsArray[i].Replace("'", "")));  //intialize factory fields to null
            }
            for (int i = 0; i < injectedFactoryArray.Length; i++)
            {
                bodyFunction.AppendLine(string.Format("{1} = {1}.init({0});", "{}", injectedFactoryArray[i].Replace("'", ""))); //call the init method of injected factories
            }
            bodyFunction.AppendLine("});");
            bodyFunction.AppendLine("}}}");
            bodyFunction.AppendLine(string.Format("return {0};", factoryName)); // return the intialized that object.

            jfactory.FactoryName = factoryName;
            if (injectedFactoryArray.Length > 0)
            {
                jfactory.InjectedFactories = injectedFactoryString;
            }
            else
            {
                jfactory.InjectedFactories = string.Empty;
            }

            jfactory.FactoryBodyCode = bodyFunction.ToString();

            return(jfactory);
        }
        private void GenerateFactories()
        {
            DataTable dtParent = new DataTable();
            dtParent = GetFilteredData("Node", Helper.JSONTYPE.OBJECT, null).ToTable();
            DataRowCollection factories = dtParent.Rows;
               string factorystr = string.Empty;
            Int32 pbarValue = 1;
            string factoryFileName = string.Empty;

            string sourceTemplate = File.ReadAllText(@".\Templates\FactoryTemplate.txt");
            List<JsonFactory> jfactoryList = new List<JsonFactory>();
            JsonFactory jfactory = new JsonFactory();

            progressBar1.Maximum = factories.Count;
            foreach (DataRow factory in factories)
            {
                string strComma = string.Empty;
                if (jfactory.InjectedFactories != null)
                {
                    if (jfactory.InjectedFactories.Trim().Length > 0)
                    {
                        strComma = ",";
                    }
                }
                jfactory = CreateFactory(factory["Node"].ToString(), Convert.ToInt32(factory["Id"]));
                jfactory.FactoryFullCode = string.Format(sourceTemplate, jfactory.FactoryName, jfactory.InjectedFactories, jfactory.FactoryBodyCode,"{","}",strComma);
                jfactoryList.Add(jfactory);
            }
            foreach (JsonFactory jf in jfactoryList)
            {
                factoryFileName=string.Format(@"{0}\{1}.js", txtDest.Text, jf.FactoryName);
               File.WriteAllText(factoryFileName, jf.FactoryFullCode);
               lblProgress.Text = string.Format("Generating factories for {0} {1} of {2}", jf.FactoryName, pbarValue, progressBar1.Maximum);

               progressBar1.Value = pbarValue;
               pbarValue++;
            }
            if (MessageBox.Show("Do you want to beautify the generated code? This may take some time","Beautify JS",MessageBoxButtons.YesNo)== System.Windows.Forms.DialogResult.Yes)
            {
                pbarValue = 1;
                ProcessStartInfo ps = new ProcessStartInfo();
                ps.FileName = "cmd.exe";
                ps.CreateNoWindow = true;
                ps.UseShellExecute = false;
                ps.WindowStyle = ProcessWindowStyle.Hidden;

                foreach (JsonFactory jf in jfactoryList)
                {
                    factoryFileName = string.Format(@"{0}\{1}.js", txtDest.Text, jf.FactoryName);
                    lblProgress.Text = string.Format("Beautifying file for {0} {1} of {2}", factoryFileName, pbarValue, progressBar1.Maximum);
                    progressBar1.Value = pbarValue;
                    pbarValue++;

                    ps.Arguments = string.Format("/c js-beautify.cmd  -o {0}", factoryFileName);

                    try
                    {

                        // Start the process with the info we specified.
                        // Call WaitForExit and then the using statement will close.
                        using (Process exeProcess = Process.Start(ps))
                        {
                            exeProcess.WaitForExit();
                        }
                    }
                    catch
                    {
                        // Log error.
                    }
                }
            }
        }
示例#4
0
        private void GenerateFactories()
        {
            DataTable dtParent = new DataTable();

            dtParent = GetFilteredData("Node", Helper.JSONTYPE.OBJECT, null).ToTable();
            DataRowCollection factories       = dtParent.Rows;
            string            factorystr      = string.Empty;
            Int32             pbarValue       = 1;
            string            factoryFileName = string.Empty;

            string             sourceTemplate = File.ReadAllText(@".\Templates\FactoryTemplate.txt");
            List <JsonFactory> jfactoryList   = new List <JsonFactory>();
            JsonFactory        jfactory       = new JsonFactory();

            progressBar1.Maximum = factories.Count;
            foreach (DataRow factory in factories)
            {
                string strComma = string.Empty;
                if (jfactory.InjectedFactories != null)
                {
                    if (jfactory.InjectedFactories.Trim().Length > 0)
                    {
                        strComma = ",";
                    }
                }
                jfactory = CreateFactory(factory["Node"].ToString(), Convert.ToInt32(factory["Id"]));
                jfactory.FactoryFullCode = string.Format(sourceTemplate, jfactory.FactoryName, jfactory.InjectedFactories, jfactory.FactoryBodyCode, "{", "}", strComma);
                jfactoryList.Add(jfactory);
            }
            foreach (JsonFactory jf in jfactoryList)
            {
                factoryFileName = string.Format(@"{0}\{1}.js", txtDest.Text, jf.FactoryName);
                File.WriteAllText(factoryFileName, jf.FactoryFullCode);
                lblProgress.Text = string.Format("Generating factories for {0} {1} of {2}", jf.FactoryName, pbarValue, progressBar1.Maximum);

                progressBar1.Value = pbarValue;
                pbarValue++;
            }
            if (MessageBox.Show("Do you want to beautify the generated code? This may take some time", "Beautify JS", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                pbarValue = 1;
                ProcessStartInfo ps = new ProcessStartInfo();
                ps.FileName        = "cmd.exe";
                ps.CreateNoWindow  = true;
                ps.UseShellExecute = false;
                ps.WindowStyle     = ProcessWindowStyle.Hidden;

                foreach (JsonFactory jf in jfactoryList)
                {
                    factoryFileName    = string.Format(@"{0}\{1}.js", txtDest.Text, jf.FactoryName);
                    lblProgress.Text   = string.Format("Beautifying file for {0} {1} of {2}", factoryFileName, pbarValue, progressBar1.Maximum);
                    progressBar1.Value = pbarValue;
                    pbarValue++;

                    ps.Arguments = string.Format("/c js-beautify.cmd  -o {0}", factoryFileName);

                    try
                    {
                        // Start the process with the info we specified.
                        // Call WaitForExit and then the using statement will close.
                        using (Process exeProcess = Process.Start(ps))
                        {
                            exeProcess.WaitForExit();
                        }
                    }
                    catch
                    {
                        // Log error.
                    }
                }
            }
        }