Ejemplo n.º 1
0
        //SHARED
        #region Convert Functions

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Converts one or many Namespaces, classes or methods.
        /// </summary>
        /// <param name="csSource">The C# source code.</param>
        /// <param name="fixNamespaces">Comma-sepearated list of Namespaces to fix ([default] is none).</param>
        /// <returns>New instance of Class CSVBConverter</returns>
        /// <remarks>
        /// Supported is any code that can be compiled with C# compiler, standing in a single .cs file.
        /// </remarks>
        /// -----------------------------------------------------------------------------
        public static CSVBConverter Convert(string csSource, string fixNamespaces = "")
        {
            PreParseResult TmpPreResult;
            string         TmpVBSrc;
            CSVBConverter  RetConv = new CSVBConverter();

            try {
                TmpPreResult          = RetConv.PreParse(csSource);
                TmpVBSrc              = RetConv.ConvertSource(TmpPreResult.ResultString, TmpPreResult);
                RetConv.FResultSource = RetConv.PostParse(TmpVBSrc, TmpPreResult, fixNamespaces);
#if (DEBUG)
                //catch only internal exceptions
            } catch (NetVertException nEx) {
                RetConv.FHasError  = true;
                RetConv.FErrorText = nEx.Message;
#Else
                //catch all exceptions
            } catch (Exception ex) {
                RetConv.FHasError  = true;
                RetConv.FErrorText = ex.Message;
#endif
            }
            return(RetConv);
        }
Ejemplo n.º 2
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Convert single codefile.
        /// </summary>
        /// <param name="sourceFile">The full path and filename to the source codefile.</param>
        /// <param name="targetFile">The full path and filename to the destination codefile ([default] is same than sourceFile with changed extension).</param>
        /// <param name="overwriteExistingFile">True if existing file should be overwriten ([default] is false).</param>
        /// <param name="fixNamespaces">True if enable the append/remove default-using-statements as defined in the FixNamespaces Property ([default] is false).</param>
        /// -----------------------------------------------------------------------------
        public void ConvertFile(string sourceFile, string targetFile = "", bool overwriteExistingFile = false, bool fixNamespaces = false)
        {
            INetVertConverter      TmpConv;
            string                 TmpS;
            ProcessedFileEventArgs TmpE;
            string                 TmpFixNS = "";

            try {
                if (targetFile == "")
                {
                    targetFile = Path.ChangeExtension(sourceFile, "." + OutputFileExtension);
                }
                // ERROR: Not supported in C#: WithStatement

                FTotalCount += 1;
                if (File.Exists(targetFile) && !overwriteExistingFile)
                {
                    //Skipped, file exists
                    //OLD: OutputText("Skipped, file exists " & targetFile)
                    //RaiseEvent AfterFileProcessed(Me, New ProcessedFileEventArgs(sourceFile, 0, targetFile, 0, ProcessedFileEventArgs.FileStatus.Skipped_Exists, FLanguage, ""))
                    if (AfterFileProcessed != null)
                    {
                        AfterFileProcessed(this, new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.CodeFile, sourceFile, "", targetFile, "", ProcessedFileEventArgs.ConverterOperations.Skipped_Exists, FLanguage, ""));
                    }
                }
                else
                {
                    // ERROR: Not supported in C#: WithStatement

                    //create EventArgs
                    TmpE = new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.CodeFile, sourceFile, TmpS, targetFile, "", ProcessedFileEventArgs.ConverterOperations.Before_Conversion, FLanguage, "");
                    if (BeforeFileProcessed != null)
                    {
                        BeforeFileProcessed(this, TmpE);
                    }
                    //set Namespaces to fix
                    if (fixNamespaces)
                    {
                        TmpFixNS = FFixNamespaces;
                    }
                    //create Converter instance with apropreate language
                    if (FLanguage == ConverterLanguages.VBNetToCSharp)
                    {
                        TmpConv = VBCSConverter.Convert(TmpE.InputSource, TmpFixNS);
                    }
                    else if (FLanguage == ConverterLanguages.CSharpToVBNet)
                    {
                        TmpConv = CSVBConverter.Convert(TmpE.InputSource, TmpFixNS);
                    }
                    else
                    {
                        throw new Exception("Converter-Language not supported");
                    }
                    if (TmpConv.HasError)
                    {
                        //has an error
                        FFailedCount += 1;
                        //OLD: OutputText("Error in " & sourceFile & vbCrLf & TmpConv.ErrorText)
                        TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Skipped_Error, TmpConv.ErrorText);
                        if (AfterFileProcessed != null)
                        {
                            AfterFileProcessed(this, TmpE);
                        }
                    }
                    else
                    {
                        //no error
                        FConvertedCount += 1;
                        if (File.Exists(targetFile))
                        {
                            TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Success_Overwriting);
                            File.Delete(targetFile);
                        }
                        else
                        {
                            TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Success_Creating);
                        }
                        TmpE.OutputSource = TmpConv.OutputSource;
                        if (AfterFileProcessed != null)
                        {
                            AfterFileProcessed(this, TmpE);
                        }
                        //save new stream to file
                        try {
                            // ERROR: Not supported in C#: WithStatement
                        } catch (Exception ex) {
                            TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Skipped_Error, ex.Message);
                            if (AfterFileProcessed != null)
                            {
                                AfterFileProcessed(this, TmpE);
                            }
                        }
                    }
                }

#if (DEBUG)
            } catch (NetVertException ex) {
                //common error
                if (AfterFileProcessed != null)
                {
                    AfterFileProcessed(this, new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.CodeFile, sourceFile, "", targetFile, "", ProcessedFileEventArgs.ConverterOperations.Skipped_Error, FLanguage, ex.Message));
                }
#Else
            } catch (Exception ex) {
                //common error
                if (AfterFileProcessed != null)
                {
                    AfterFileProcessed(this, new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.CodeFile, sourceFile, "", targetFile, "", ProcessedFileEventArgs.ConverterOperations.Skipped_Error, FLanguage, ex.Message));
                }
#endif
            }
        }