/// <summary> /// Initializes the extension handler. /// </summary> private void Initialize() { _assembly = Assembly.Load(_configuration.AssemblyName); Type[] types = _assembly.GetTypes(); foreach (Type type in types) { if (_codeParser == null && type.GetInterface(typeof(ICodeElementParser).ToString()) != null) { _codeParser = Activator.CreateInstance(type) as ICodeElementParser; } else if (_codeWriter == null && type.GetInterface(typeof(ICodeElementWriter).ToString()) != null) { _codeWriter = Activator.CreateInstance(type) as ICodeElementWriter; } } }
/// <summary> /// Arranges an individual source file. /// </summary> /// <param name="inputFile">The input file.</param> /// <param name="outputFile">The output file.</param> private void ArrangeSourceFile(string inputFile, string outputFile) { ReadOnlyCollection <ICodeElement> elements = null; string inputFileText = null; Encoding encoding = _encoding; try { FileAttributes fileAttributes = File.GetAttributes(inputFile); if (inputFile == outputFile && ((fileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)) { LogMessage(LogLevel.Warning, "File {0} is read-only", inputFile); } else { if (encoding == null) { encoding = FileUtilities.GetEncoding(inputFile); } inputFileText = File.ReadAllText(inputFile, encoding); elements = _projectManager.ParseElements(inputFile, inputFileText); LogMessage(LogLevel.Trace, "Parsed {0}", inputFile); } } catch (DirectoryNotFoundException) { LogMessage(LogLevel.Warning, "File {0} does not exist.", inputFile); } catch (FileNotFoundException) { LogMessage(LogLevel.Warning, "File {0} does not exist.", inputFile); } catch (IOException ioException) { LogMessage( LogLevel.Warning, "Unable to read file {0}: {1}", inputFile, ioException.ToString()); } catch (UnauthorizedAccessException ioException) { LogMessage( LogLevel.Warning, "Unable to read file {0}: {1}", inputFile, ioException.Message); } catch (ParseException parseException) { LogMessage( LogLevel.Warning, "Unable to parse file {0}: {1}", inputFile, parseException.Message); } catch (Exception parseException) { LogMessage( LogLevel.Warning, "Unable to parse file {0}: {1}", inputFile, parseException.Message); } if (elements != null) { try { elements = ArrangeElements(elements); } catch (InvalidOperationException invalidEx) { LogMessage( LogLevel.Warning, "Unable to arrange file {0}: {1}", inputFile, invalidEx.ToString()); elements = null; } } string outputFileText = null; if (elements != null) { ICodeElementWriter codeWriter = _projectManager.GetSourceHandler(inputFile).CodeWriter; codeWriter.Configuration = _configuration; StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); try { codeWriter.Write(elements, writer); } catch (Exception ex) { LogMessage(LogLevel.Error, ex.ToString()); throw; } outputFileText = writer.ToString(); } if (outputFileText != null) { // // Store the arranged elements so that we can create a backup before writing // _arrangeResults.Add( outputFile, new ArrangeResult(encoding, inputFile, inputFileText, outputFile, outputFileText)); } }
/// <summary> /// Initializes the extension handler. /// </summary> private void Initialize() { _assembly = Assembly.Load(_configuration.AssemblyName); Type[] types = _assembly.GetTypes(); foreach (Type type in types) { if (_codeParser == null && type.GetInterface(typeof (ICodeElementParser).ToString()) != null) { _codeParser = Activator.CreateInstance(type) as ICodeElementParser; } else if (_codeWriter == null && type.GetInterface(typeof (ICodeElementWriter).ToString()) != null) { _codeWriter = Activator.CreateInstance(type) as ICodeElementWriter; } } }
/// <summary> /// Arranges a file as a string /// </summary> /// <param name="inputFile">The input file.</param> /// <param name="inputText">The text of the file.</param> /// <param name="outputText">The arranged text of the file.</param> /// <returns>True if successful, otherwise false.</returns> public bool Arrange(string inputFile, string inputFileText, out string outputFileText) { outputFileText = null; if (!Initialize()) { return(false); } bool isProject = _projectManager.IsProject(inputFile); bool isSolution = !isProject && ProjectManager.IsSolution(inputFile); bool isDirectory = !isProject && !isSolution && string.IsNullOrEmpty(Path.GetExtension(inputFile)) && Directory.Exists(inputFile); if (isProject || isSolution || isDirectory) { return(false); } if (File.Exists(String.Concat(inputFile, ".cs"))) { inputFile = String.Concat(inputFile, ".cs"); } else if (File.Exists(String.Concat(inputFile, ".vb"))) { inputFile = String.Concat(inputFile, ".vb"); } bool canParse = _projectManager.CanParse(inputFile); if (!canParse) { LogMessage( LogLevel.Warning, "No assembly is registered to handle file {0}. Please update the configuration or select a valid file.", inputFile); return(false); } ReadOnlyCollection <ICodeElement> elements = null; Encoding encoding = _encoding; if (encoding == null) { encoding = FileUtilities.GetEncoding(inputFile); } try { elements = _projectManager.ParseElements(inputFile, inputFileText); LogMessage(LogLevel.Trace, "Parsed {0}", inputFile); } catch (ParseException parseException) { LogMessage( LogLevel.Warning, "Unable to parse file {0}: {1}", inputFile, parseException.Message); return(false); } if (elements != null) { try { if (_codeArranger == null) { _codeArranger = new CodeArranger(_configuration); } elements = _codeArranger.Arrange(elements); } catch (InvalidOperationException invalidEx) { LogMessage( LogLevel.Warning, "Unable to arrange file {0}: {1}", inputFile, invalidEx.ToString()); return(false); } } if (elements != null) { ICodeElementWriter codeWriter = _projectManager.GetSourceHandler(inputFile).CodeWriter; codeWriter.Configuration = _configuration; StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); try { codeWriter.Write(elements, writer); } catch (Exception ex) { LogMessage(LogLevel.Error, ex.ToString()); return(false); } outputFileText = writer.ToString(); } return(true); }
/// <summary> /// Initializes the extension handler. /// </summary> private void Initialize() { //_assembly = Assembly.Load(_configuration.AssemblyName); //Type[] types = _assembly.GetTypes(); //foreach (Type type in types) //{ // if (_codeParser == null && type.GetInterface(typeof(ICodeElementParser).ToString()) != null) // { // _codeParser = Activator.CreateInstance(type) as ICodeElementParser; // } // else if (_codeWriter == null && type.GetInterface(typeof(ICodeElementWriter).ToString()) != null) // { // _codeWriter = Activator.CreateInstance(type) as ICodeElementWriter; // } //} _codeParser = new NArrange.CSharp.CSharpParser(); _codeWriter = new NArrange.CSharp.CSharpWriter(); }