Skip to content

Commit

Permalink
Improve yml transform error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Luuk Sommers committed Mar 8, 2018
1 parent d6a21f1 commit fdd94a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
23 changes: 18 additions & 5 deletions src/YmlTransform/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
using YmlTransform.Models;
using System;
using Sitecore.Express;
using YmlTransform.Exceptions;
using YmlTransform.Models;

namespace YmlTransform
{
class Program
{
static void Main(string[] args)
{
var options = new Options();
var isValid = CommandLine.Parser.Default.ParseArgumentsStrict(args, options);
if (isValid)
try
{
YmlTransformer.TransformPath(options.Path, options.TransformFile, options.Recursive);
var options = new Options();
var isValid = CommandLine.Parser.Default.ParseArgumentsStrict(args, options);
if (isValid)
{
YmlTransformer.TransformPath(options.Path, options.TransformFile, options.Recursive);
}
}
catch (IncompleteTransformationException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Not all fields were processed. Please verify the following transformations:");
Console.Error.WriteLine(e.Message);
System.Environment.Exit(1);
}
}
}
Expand Down
33 changes: 25 additions & 8 deletions src/YmlTransform/YmlTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,40 @@ public static void TransformFile(string fileToTransform, string transformFile)
{
var transformation = GetTransformation(transformFile);
TransformSingle(transformation, fileToTransform);

if (!transformation.All(x => x.Used))
{
throw new IncompleteTransformationException(string.Join(",", transformation.Where(a=>!a.Used).Select(a=>$"{a.Path} {a.FieldId} {a.Type}")));
}
}


public static void TransformPath(string path, string transformFile, bool recursive)
{
if (!Path.IsPathRooted(path))
{
path = Path.Combine(Directory.GetCurrentDirectory(), path);
}

var transformations = GetTransformation(transformFile);
var files = Directory.GetFiles(path, "*.yml",
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

foreach (var file in files)
{
TransformSingle(transformations, file);
try
{
TransformSingle(transformations, file);
}
catch (YamlFormatException)
{

}
}

if (!transformations.All(x => x.Used))
{
throw new IncompleteTransformationException(string.Join(",", transformations.Where(a=>!a.Used).Select(a=>$"{a.Path} {a.FieldId} {a.Type}")));
}
}

Expand Down Expand Up @@ -60,7 +82,7 @@ private static void TransformSingle(List<TransformItem> transformations, string
transformed = true;
}
}
if (transform.Type == "Unversioned")
else if (transform.Type == "Unversioned")
{
if (item.UnversionedFields.SelectMany(a => a.Fields).FirstOrDefault(a => a.FieldId == transform.FieldId) is IItemFieldValue field)
{
Expand Down Expand Up @@ -89,16 +111,11 @@ private static void TransformSingle(List<TransformItem> transformations, string
}
else
{
throw new NotImplementedException();
throw new NotImplementedException(transform.Type);
}
}
}

if (!transformations.All(x => x.Used))
{
throw new IncompleteTransformationException();
}

if (transformed)
{
Console.WriteLine($"Transformed: {file}");
Expand Down

0 comments on commit fdd94a4

Please sign in to comment.