Esempio n. 1
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                ShowFileMissingErrorMessage();
                return;
            }

            try
            {
                // String which contains whole file contents
                string fileContent = FileContentUtil.GetFileContent(filePath);

                // Get option values for generator options
                JsGeneratorOptions options = GetOptions();

                // Create compiler and execute content of file
                Assembly asm = BuildAssemblyUtil.CompileCode(fileContent);

                // Get all types from assembly using reflection
                List <Type> rawTypes = BuildAssemblyUtil.GetExportedTypes(asm);

                // Filter types, and take only parent ones (the ones that are not referenced in parent, and standalone one)
                // If referenced children will be generated automatically, so no need to duplicate stuff.
                List <Type> types = BuildAssemblyUtil.FilterExportedTypes(rawTypes);

                // Finally generate
                StringBuilder sbFinal = new StringBuilder();
                foreach (Type type in types)
                {
                    switch (options.ConversionType)
                    {
                    case EGenerateOptions.Javascript:
                        sbFinal.AppendLine(JsGenerator.Generate(new[] { type }, options));
                        break;

                    case EGenerateOptions.Ecma6:
                        sbFinal.AppendLine(Ecma6Generator.Generate(new[] { type }, options));
                        break;

                    case EGenerateOptions.KnockoutEcma6:
                        sbFinal.AppendLine(Ecma6KnockoutGenerator.Generate(new[] { type }, options));
                        break;

                    default:
                        throw new Exception(
                                  "Somehow Conversion type is not set or is out of bounds. I have no idea what happened.");
                    }
                }

                codeTextEditor.Text = sbFinal.ToString();
            }
            catch (Exception exception)
            {
                // Since we presumably should know what we are doing here, we are just showing message from exception
                // Would be good idea to log this somewhere tho.
                ShowErrorMessage(exception.Message);
            }
        }
Esempio n. 2
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                ShowErrorMessage();
                return;
            }

            List <string> listOfClassNames = new List <string>();
            List <FileLinesOverviewModel> listOfProperties = new List <FileLinesOverviewModel>();
            List <string> listOfVarTypes = new List <string>
            {
                "Int",
                "Int16",
                "Int32",
                "Int64",
                "UInt",
                "Short",
                "Bool",
                "Boolean",
                "Byte",
                "SByte",
                "Char",
                // "Date", // TODO: Check what to do with Date type vars, cause it is interfeering with DateTime vars
                "DateTime",
                "Decimal",
                "Double",
                "Float",
                "String",
                "Object",
                "Long"
            };
            List <string> listOfLowerVarTypes = listOfVarTypes.Select(d => d.ToLower()).ToList();

            listOfLowerVarTypes.AddRange(listOfVarTypes.Select(d => d.ToLower() + "?").ToList());

            string line;
            // Read the file and display it line by line.
            StreamReader file = new StreamReader(filePath);

            while ((line = file.ReadLine()) != null)
            {
                // Add all class names (Classes are imidiately stripped)
                if (ClassNamesUtility.IsClass(line))
                {
                    listOfClassNames.Add(ClassNamesUtility.StripClassName(line));
                }
                // And their properties (Properties are stripped down below)
                else if (PropertyNamesUtility.IsProperty(line))
                {
                    listOfProperties.Add(new FileLinesOverviewModel
                    {
                        ClassName            = listOfClassNames.Last(),
                        OriginalPropertyLine = line,
                        LineType             = new LineType()
                    });
                }
            }
            file.Close();

            // First determine property type
            foreach (var pair in listOfProperties)
            {
                pair.LineType = PropertyNamesUtility.GetPropertyType(pair.OriginalPropertyLine, listOfClassNames, listOfLowerVarTypes);
            }

            // Then get property name
            foreach (var pair in listOfProperties)
            {
                pair.PropertyName = PropertyNamesUtility.StripPropertyName(pair.OriginalPropertyLine.Trim(), pair.LineType);
            }

            // Map it into proper model, so data is purposely there
            JSBuilderModel model = ModelsMapper.GetBuilderModel(listOfProperties);

            // Get options model
            var options = GetOptionsModel();

            // Finally generate
            string result = null;

            switch (options.ConversionType)
            {
            case EGenerateOptions.Javascript:
                result = JavascriptGenerator.GenerateJs(model, options);
                break;

            case EGenerateOptions.Ecma6:
                result = Ecma6Generator.GenerateJs(model, options);
                break;

            case EGenerateOptions.KnockoutEcma6:
                result = Ecma6WithKnockoutGenerator.GenerateJs(model, options);
                break;
            }
            // result = JavascriptClassGenerator.GenerateJs(model, options);

            codeTextEditor.Text = result;
        }