Changed the Console-app's arguments to take source-directory and target-directory. The whole app does not crash on one failed file.
This commit is contained in:
Родитель
1d3d3db962
Коммит
9fc24539f4
|
@ -1,77 +1,72 @@
|
|||
namespace aspx2razor
|
||||
{
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace aspx2razor {
|
||||
|
||||
/// <summary>
|
||||
/// An object that will handle the traversing of directories and collection of files
|
||||
/// </summary>
|
||||
public class DirectoryHandler
|
||||
{
|
||||
public class DirectoryHandler {
|
||||
private static IEnumerable<string> extensionFilter = new List<string>() {
|
||||
".aspx", ".ascx"
|
||||
};
|
||||
|
||||
private string InputDirectory { get; set; }
|
||||
private string InputFilter { get; set; }
|
||||
private string OutputDirectory { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new DirectoryHandler instance
|
||||
/// </summary>
|
||||
/// <param name="inputDirectory">The initial directory to start inspections at</param>
|
||||
/// <param name="outputDirectory">The output directory to output to</param>
|
||||
public DirectoryHandler(string inputFile, string outputDirectory)
|
||||
{
|
||||
InputFilter = Path.GetFileName(inputFile) ?? "";
|
||||
InputDirectory = GetFullPathOrDefault(inputFile);
|
||||
public DirectoryHandler(string inputDirectory, string outputDirectory) {
|
||||
InputDirectory = GetFullPathOrDefault(inputDirectory);
|
||||
|
||||
if (string.IsNullOrEmpty(outputDirectory))
|
||||
{
|
||||
if(string.IsNullOrEmpty(outputDirectory)) {
|
||||
OutputDirectory = InputDirectory;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
OutputDirectory = Path.GetFullPath(outputDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
public string[] GetFiles(bool includeSubdirectories)
|
||||
{
|
||||
return GetFiles(InputDirectory, InputFilter, includeSubdirectories);
|
||||
public IEnumerable<string> GetFiles(bool includeSubdirectories) {
|
||||
return GetFiles(InputDirectory, includeSubdirectories);
|
||||
}
|
||||
|
||||
public string GetOutputFileName(string fileName)
|
||||
{
|
||||
public string GetOutputFileName(string fileName) {
|
||||
var fullFileName = Path.GetFullPath(fileName);
|
||||
var relativeFileName = fullFileName.Remove(0, InputDirectory.Length + 1);
|
||||
|
||||
return Path.Combine(OutputDirectory, relativeFileName);
|
||||
}
|
||||
|
||||
private static string[] GetFiles(string inputDirectory, string inputFilter, bool includeSubdirectories)
|
||||
{
|
||||
if (!includeSubdirectories)
|
||||
{
|
||||
return Directory.GetFiles(inputDirectory, inputFilter);
|
||||
}
|
||||
|
||||
string[] outFiles = Directory.GetFiles(inputDirectory, inputFilter);
|
||||
var di = new DirectoryInfo(inputDirectory);
|
||||
if (di.GetDirectories().Length > 0)
|
||||
{
|
||||
var directories = di.GetDirectories();
|
||||
outFiles =
|
||||
directories.Aggregate(outFiles,
|
||||
(current, subdirectory) => current.Union(GetFiles(subdirectory.FullName, inputFilter, true)).ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
return outFiles;
|
||||
private static List<string> GetFiles(string inputDirectory, bool includeSubdirectories) {
|
||||
var files = GetFileRecursive(new List<string>(), inputDirectory, includeSubdirectories);
|
||||
return files;
|
||||
}
|
||||
|
||||
private static string GetFullPathOrDefault(string directory)
|
||||
{
|
||||
directory = Path.GetDirectoryName(directory);
|
||||
private static List<string> GetFileRecursive(List<string> list, string directoryPath, bool recursive) {
|
||||
var directory = new DirectoryInfo(directoryPath);
|
||||
var files = directory.GetFiles().Where(file => extensionFilter.Contains(file.Extension));
|
||||
|
||||
if (string.IsNullOrEmpty(directory))
|
||||
{
|
||||
list.AddRange(files.Select(file => file.FullName));
|
||||
|
||||
if(recursive) {
|
||||
var subDirectories = directory.GetDirectories();
|
||||
foreach(var subDirectory in subDirectories) {
|
||||
GetFileRecursive(list, subDirectory.FullName, recursive);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static string GetFullPathOrDefault(string directory) {
|
||||
if(!Directory.Exists(directory)) {
|
||||
directory = Path.GetDirectoryName(directory);
|
||||
}
|
||||
|
||||
if(string.IsNullOrEmpty(directory)) {
|
||||
directory = Directory.GetCurrentDirectory();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,106 +1,120 @@
|
|||
namespace aspx2razor
|
||||
{
|
||||
using System;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.ComponentModel.Composition.Hosting;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Telerik.RazorConverter;
|
||||
using Telerik.RazorConverter.Razor.DOM;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.ComponentModel.Composition.Hosting;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
class Program
|
||||
{
|
||||
using Telerik.RazorConverter;
|
||||
using Telerik.RazorConverter.Razor.DOM;
|
||||
|
||||
namespace aspx2razor {
|
||||
|
||||
class Program {
|
||||
[Import]
|
||||
private IWebFormsParser Parser
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
private IWebFormsParser Parser { get; set; }
|
||||
|
||||
[Import]
|
||||
private IWebFormsConverter<IRazorNode> Converter
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
private IWebFormsConverter<IRazorNode> Converter { get; set; }
|
||||
|
||||
[Import]
|
||||
private IRenderer<IRazorNode> Renderer
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
private IRenderer<IRazorNode> Renderer { get; set; }
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
private static void Main(string[] args) {
|
||||
var p = new Program();
|
||||
p.Run(args);
|
||||
}
|
||||
|
||||
private Program()
|
||||
{
|
||||
private Program() {
|
||||
var catalog = new AssemblyCatalog(typeof(IWebFormsParser).Assembly);
|
||||
var container = new CompositionContainer(catalog);
|
||||
container.ComposeParts(this);
|
||||
}
|
||||
|
||||
public void Run(string[] args)
|
||||
{
|
||||
public void Run(string[] args) {
|
||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||
|
||||
if (args.Length < 1)
|
||||
{
|
||||
if(args.Length < 1) {
|
||||
DisplayUsage();
|
||||
return;
|
||||
}
|
||||
|
||||
var failList = new List<string>();
|
||||
int successCount = 0;
|
||||
|
||||
var outputDirectory = (args.Length >= 2 && !args[1].StartsWith("-")) ? args[1] : "";
|
||||
var directoryHandler = new DirectoryHandler(args[0], outputDirectory);
|
||||
|
||||
DirectoryHandler directoryHandler;
|
||||
try {
|
||||
directoryHandler = new DirectoryHandler(args[0], outputDirectory);
|
||||
}
|
||||
catch(ArgumentException ex) {
|
||||
Console.WriteLine("The given directories were not valid: {0}", ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
var recursive = args.Contains("-r", StringComparer.InvariantCultureIgnoreCase);
|
||||
var files = directoryHandler.GetFiles(recursive);
|
||||
foreach (var file in files)
|
||||
{
|
||||
foreach(var file in files) {
|
||||
Console.WriteLine("Converting {0}", file);
|
||||
|
||||
var webFormsPageSource = File.ReadAllText(file, Encoding.UTF8);
|
||||
var webFormsDocument = Parser.Parse(webFormsPageSource);
|
||||
var razorDom = Converter.Convert(webFormsDocument);
|
||||
var razorPage = Renderer.Render(razorDom);
|
||||
|
||||
var outputFileName = ReplaceExtension(directoryHandler.GetOutputFileName(file), ".cshtml");
|
||||
Console.WriteLine("Writing {0}", outputFileName);
|
||||
EnsureDirectory(Path.GetDirectoryName(outputFileName));
|
||||
File.WriteAllText(outputFileName, razorPage, Encoding.UTF8);
|
||||
try {
|
||||
var webFormsPageSource = File.ReadAllText(file, Encoding.UTF8);
|
||||
var webFormsDocument = Parser.Parse(webFormsPageSource);
|
||||
var razorDom = Converter.Convert(webFormsDocument);
|
||||
var razorPage = Renderer.Render(razorDom);
|
||||
|
||||
Console.WriteLine("Done\n");
|
||||
var outputFileName = ReplaceExtension(directoryHandler.GetOutputFileName(file), ".cshtml");
|
||||
Console.WriteLine("Writing {0}", outputFileName);
|
||||
EnsureDirectory(Path.GetDirectoryName(outputFileName));
|
||||
File.WriteAllText(outputFileName, razorPage, Encoding.UTF8);
|
||||
|
||||
Console.WriteLine("Done");
|
||||
successCount++;
|
||||
}
|
||||
catch(Exception ex) {
|
||||
Console.WriteLine("Exception Thrown!");
|
||||
AddFail(file, ex.Message, failList);
|
||||
}
|
||||
finally {
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
var elapsed = stopwatch.Elapsed;
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("{0} files converted", files.Length);
|
||||
Console.WriteLine("{0} files converted", successCount);
|
||||
|
||||
if(failList.Any()) {
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("{0} files failed:", failList.Count);
|
||||
failList.ForEach(fail => Console.WriteLine(fail));
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Elapsed: {0} seconds", elapsed.TotalSeconds);
|
||||
}
|
||||
|
||||
private static void DisplayUsage()
|
||||
{
|
||||
private static void AddFail(string fileName, string errorMessage, IList<string> failList) {
|
||||
string fail = string.Format("- '{1}':{0} {2}", Environment.NewLine, fileName, errorMessage);
|
||||
failList.Add(fail);
|
||||
}
|
||||
|
||||
private static void DisplayUsage() {
|
||||
Console.WriteLine("Converts WebForms pages (.aspx, .ascx) into a Razor views (.cshtml)");
|
||||
Console.WriteLine("Usage: aspx2razor <input file / wildcard> [output-directory] [options]");
|
||||
Console.WriteLine("Usage: aspx2razor <input-directory> [output-directory] [options]");
|
||||
Console.WriteLine("Options available:\r");
|
||||
Console.WriteLine("-r: Convert directories and their contents recursively");
|
||||
}
|
||||
|
||||
private static void EnsureDirectory(string directory)
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
private static void EnsureDirectory(string directory) {
|
||||
if(!Directory.Exists(directory)) {
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
}
|
||||
|
||||
private static string ReplaceExtension(string fileName, string newExtension)
|
||||
{
|
||||
private static string ReplaceExtension(string fileName, string newExtension) {
|
||||
var targetFolder = Path.GetDirectoryName(fileName);
|
||||
return Path.Combine(targetFolder, Path.GetFileNameWithoutExtension(fileName) + newExtension);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче