예제 #1
0
        public String ToCSharp()
        {
            IOutputAstVisitor outputVisitor = new CSharpOutputVisitor();

            SetPrettyPrintOptions(outputVisitor);

            using (SpecialNodesByMapInserter.Install(new Hashtable(), outputVisitor)) {
                unit.AcceptVisitor(outputVisitor, null);
            }
            String code = outputVisitor.Text;

            return(NRefactoryUtil.FixSourceFormatting(code));
        }
예제 #2
0
        public Boolean Write(FileInfo file, String contents, IStyler styler)
        {
            // Create the directory if it doesn't exist.
            if (!file.Directory.Exists)
            {
                file.Directory.Create();
            }
            Console.Out.WriteLine("processing file " + file.FullName);

            MemoryStream stream = new MemoryStream();
            StreamWriter sw     = new StreamWriter(stream);

            sw.Write(contents);
            sw.Flush();
            stream.Position = 0;

            if (!file.Exists)
            {
                //CodeUnit unit1 = null;
                try {
                    //unit1 = new CodeUnit(file.Name, stream, Log, cgOptions);
                    //String mergedContent = unit1.Generate();
                    String       mergedContent = contents;
                    StreamWriter writer        = new StreamWriter(file.FullName, false);
                    writer.Write(mergedContent);
                    writer.Close();
                    DoPostProcessingNew(file.FullName);
                    return(true);
                } catch (Exception ex) {
                    Console.Out.WriteLine("Error in File Name " + file.Name);
                    Console.Out.WriteLine(ex);
                    Log.Add("Error in File Name " + file.Name + ":" + ex.ToString());
                    return(false);
                }
            }
            else
            {
                //FileStream fs = null;
                //CodeUnit unit1 = null;
                //CodeUnit unit2 = null;
                try {
                    //fs = file.OpenRead();
                    //unit1 = new CodeUnit(file.Name, fs, Log, cgOptions);
                    //fs.Close();
                    //fs = null;
                    //unit2 = new CodeUnit(file.Name, stream, Log, cgOptions);
                    //unit1.Merge(unit2);

                    StreamReader sr = file.OpenText();
                    String       exitingContents = sr.ReadToEnd();
                    sr.Close();

                    String mergedContent = NRefactoryUtil.Merge(contents, exitingContents);

                    // determine whether anything has actually changed in the file
                    bool   fileHasChanged = false;
                    String mergedContentIgnoreWhitespace   = mergedContent.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
                    String exitingContentsIgnoreWhitespace = exitingContents.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
                    if (mergedContentIgnoreWhitespace != exitingContentsIgnoreWhitespace)
                    {
                        fileHasChanged = true;
                    }

                    if (fileHasChanged)
                    {
                        // only write out if the formatted contents of both are different (avoids the "DTG reformatting" commit messages, at least some of the time)
                        String fixedFormattingExitingContents = NRefactoryUtil.FixSourceFormatting(exitingContents);
                        String fixedFormattingMergedContents  = NRefactoryUtil.FixSourceFormatting(mergedContent);
                        if (!fixedFormattingMergedContents.Equals(fixedFormattingExitingContents))
                        {
                            // make backup
                            if (file.Exists && backupFilePath != "")
                            {
                                FileInfo backup = new FileInfo(backupFilePath);
                                if (!backup.Directory.Exists)
                                {
                                    backup.Directory.Create();
                                }
                                if (File.Exists(backupFilePath) && (File.GetAttributes(backupFilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                {
                                    File.SetAttributes(backupFilePath, File.GetAttributes(backupFilePath) ^ FileAttributes.ReadOnly);
                                }
                                file.CopyTo(backupFilePath, true);
                                if ((File.GetAttributes(backupFilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                {
                                    File.SetAttributes(backupFilePath, File.GetAttributes(backupFilePath) ^ FileAttributes.ReadOnly);
                                }
                            }

                            // Style the contents.
                            string styledContent = styler.Style(mergedContent);
                            DoPreProcessingExisting(file.FullName);
                            StreamWriter writer = new StreamWriter(file.FullName, false);
                            writer.Write(styledContent);
                            writer.Close();
                            return(true);
                        }
                    }
                } catch (Exception ex) {
                    Console.Out.WriteLine("Error in File Name " + file.Name);
                    Console.Out.WriteLine(ex);
                    Log.Add("Error in File Name " + file.Name + ":" + ex.ToString());
                } finally {
                    //if (fs != null) {
                    //    fs.Close();
                    //}
                }

                return(false);
            }
        }