public static string ConvertLineEndings(string inputText, LineEndingsMode lineEndings) { // checking for a really messed up situation that happens when mixing max/pc sometimes if (inputText.Contains("\r\r\n")) { inputText = inputText.Replace("\r\r\n", "\n"); } // if it has windows line escaping, then convert everything to Unix if (inputText.Contains("\r\n")) { inputText = inputText.Replace("\r\n", "\n"); } if (lineEndings == LineEndingsMode.Unix) { // do nothing, already in Unix } else if (lineEndings == LineEndingsMode.Windows) { // convert all unix to windows inputText = inputText.Replace("\n", "\r\n"); } else if (lineEndings == LineEndingsMode.OSNative) { // convert all os native to windows inputText = inputText.Replace("\n", System.Environment.NewLine); } else { Debug.LogErrorFormat("Unable to convert line endings, unknown line ending type found: {0}", lineEndings); } return(inputText); }
internal static string SetLineEndings(string content, LineEndingsMode lineEndingsMode) { string replacement; switch (lineEndingsMode) { case LineEndingsMode.OSNative: if (Application.platform == RuntimePlatform.WindowsEditor) { replacement = "\r\n"; } else { replacement = "\n"; } break; case LineEndingsMode.Unix: replacement = "\n"; break; case LineEndingsMode.Windows: replacement = "\r\n"; break; default: replacement = "\n"; break; } content = Regex.Replace(content, "\\r\\n?|\\n", replacement); return(content); }
private static string SetLineEndings(string content, LineEndingsMode lineEndingsMode) { string replacement; switch (lineEndingsMode) { case LineEndingsMode.OSNative: replacement = Application.platform != RuntimePlatform.WindowsEditor ? "\n" : "\r\n"; break; case LineEndingsMode.Unix: replacement = "\n"; break; case LineEndingsMode.Windows: replacement = "\r\n"; break; default: replacement = "\n"; break; } content = System.Text.RegularExpressions.Regex.Replace(content, "\\r\\n?|\\n", replacement); return(content); }
private static string GetTemplateWithCorrectLineEndings(LineEndingsMode lineEndingsMode) { bool removeLineFeed = false; switch (lineEndingsMode) { case LineEndingsMode.Windows: removeLineFeed = false; break; case LineEndingsMode.OSNative: if (Application.platform != RuntimePlatform.WindowsEditor) { removeLineFeed = true; } break; case LineEndingsMode.Unix: default: removeLineFeed = true; break; } if (removeLineFeed) { return(TEMPLATE.Replace("\r", string.Empty)); } else { return(TEMPLATE); } }
public virtual Microsoft.CodeAnalysis.SyntaxTree OnTranslate(VSGraphModel graphModel, AssemblyType assemblyType, CompilationOptions compilationOptions, ref CompilationResult compilationResult) { const string windowsLineEndings = "\r\n"; const string unixLineEndings = "\n"; Microsoft.CodeAnalysis.SyntaxTree syntaxTree = Translate(graphModel, compilationOptions); // we will measure plugins time later string preferredLineEndings; LineEndingsMode lineEndingsForNewScripts = EditorSettings.lineEndingsForNewScripts; switch (lineEndingsForNewScripts) { case LineEndingsMode.OSNative: preferredLineEndings = Application.platform == RuntimePlatform.WindowsEditor ? windowsLineEndings : unixLineEndings; break; case LineEndingsMode.Unix: preferredLineEndings = unixLineEndings; break; case LineEndingsMode.Windows: preferredLineEndings = windowsLineEndings; break; default: preferredLineEndings = unixLineEndings; break; } var adHocWorkspace = new AdhocWorkspace(); var options = adHocWorkspace.Options .WithChangedOption(CSharpFormattingOptions.NewLineForMembersInObjectInit, true) .WithChangedOption(CSharpFormattingOptions.WrappingPreserveSingleLine, false) .WithChangedOption(CSharpFormattingOptions.WrappingKeepStatementsOnSingleLine, false) .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInObjectCollectionArrayInitializers, true) .WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, preferredLineEndings); compilationResult.sourceCode[(int)SourceCodePhases.Initial] = syntaxTree.GetText().ToString(); var formattedTree = Formatter.Format(syntaxTree.GetCompilationUnitRoot(), adHocWorkspace, options); formattedTree = new VisualScriptingCSharpFormatter().Visit(formattedTree); string codeText = formattedTree.GetText().ToString(); compilationResult.sourceCode[(int)SourceCodePhases.Final] = codeText; return(syntaxTree); }
static void AppendLine(FileEncoding encoding, LineEndingsMode mode, byte[] new_bytes, ref int new_bytes_index) { switch (mode) { case LineEndingsMode.LineFeed: if (encoding == FileEncoding.Utf16BigEndian) { new_bytes[new_bytes_index++] = 0; new_bytes[new_bytes_index++] = ByteLineFeed; } else if (encoding == FileEncoding.Utf16LittleEndian) { new_bytes[new_bytes_index++] = ByteLineFeed; new_bytes[new_bytes_index++] = 0; } else { new_bytes[new_bytes_index++] = ByteLineFeed; } break; case LineEndingsMode.CarriageReturnLineFeed: if (encoding == FileEncoding.Utf16BigEndian) { new_bytes[new_bytes_index++] = 0; new_bytes[new_bytes_index++] = ByteCarriageReturn; new_bytes[new_bytes_index++] = 0; new_bytes[new_bytes_index++] = ByteLineFeed; } else if (encoding == FileEncoding.Utf16LittleEndian) { new_bytes[new_bytes_index++] = ByteCarriageReturn; new_bytes[new_bytes_index++] = 0; new_bytes[new_bytes_index++] = ByteLineFeed; new_bytes[new_bytes_index++] = 0; } else { new_bytes[new_bytes_index++] = ByteCarriageReturn; new_bytes[new_bytes_index++] = ByteLineFeed; } break; } }
internal static string SetLineEndings(string content, LineEndingsMode lineEndingsMode) { const string windowsLineEndings = "\r\n"; const string unixLineEndings = "\n"; string preferredLineEndings; switch (lineEndingsMode) { case LineEndingsMode.OSNative: if (Application.platform == RuntimePlatform.WindowsEditor) { preferredLineEndings = windowsLineEndings; } else { preferredLineEndings = unixLineEndings; } break; case LineEndingsMode.Unix: preferredLineEndings = unixLineEndings; break; case LineEndingsMode.Windows: preferredLineEndings = windowsLineEndings; break; default: preferredLineEndings = unixLineEndings; break; } content = Regex.Replace(content, @"\r\n?|\n", preferredLineEndings); return(content); }
public static void CopyFile(TextAsset textAsset, string destinationFile, bool sourceControlCheckout, LineEndingsMode lineEndings) { string fileContents = ConvertLineEndings(textAsset.text, lineEndings); if (fileContents != File.ReadAllText(destinationFile)) { // checking out the file if (sourceControlCheckout && Provider.enabled && Provider.isActive) { Provider.Checkout(destinationFile, CheckoutMode.Asset).Wait(); } // actually writing out the contents File.WriteAllText(destinationFile, fileContents); } }
public static void CopyFile(string sourceFile, string destinationFile, bool sourceControlCheckout, LineEndingsMode lineEndings) { if (File.Exists(sourceFile) == false) { Debug.LogErrorFormat("Unable to copy file {0} to {1}. Source file does not exist!", sourceFile, destinationFile); } string fileContents = ConvertLineEndings(File.ReadAllText(sourceFile), lineEndings); if (fileContents != File.ReadAllText(destinationFile)) { // checking out the file if (sourceControlCheckout && Provider.enabled && Provider.isActive) { Provider.Checkout(destinationFile, CheckoutMode.Asset).Wait(); } // actually writing out the contents File.WriteAllText(destinationFile, fileContents); } }
public static void CreateFile(string contents, string destinationFile, bool sourceControlAdd, LineEndingsMode lineEndings) { string fileContents = ConvertLineEndings(contents, lineEndings); // actually writing out the contents File.WriteAllText(destinationFile, fileContents); // telling source control to add the file if (sourceControlAdd && Provider.enabled && Provider.isActive) { AssetDatabase.Refresh(); Provider.Add(new Asset(destinationFile), false).Wait(); } }
public static void UpdateFile(string contents, string path, bool useSourceControl, LineEndingsMode lineEndings) { string fileContents = ConvertLineEndings(contents, lineEndings); // Early out if nothing has changed if (File.ReadAllText(path) == fileContents) { return; } // Checking out the file if (useSourceControl && Provider.enabled && Provider.isActive) { Provider.Checkout(path, CheckoutMode.Asset).Wait(); } File.WriteAllText(path, fileContents); }
public static void CreateOrUpdateFile(string contents, string path, bool useSourceControl, LineEndingsMode lineEndings) { if (File.Exists(path)) { UpdateFile(contents, path, useSourceControl, lineEndings); } else { CreateFile(contents, path, useSourceControl, lineEndings); } }