Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Luuk Sommers committed Oct 23, 2017
2 parents 58117c8 + 0de035a commit 6ce096c
Show file tree
Hide file tree
Showing 13 changed files with 438 additions and 88 deletions.
11 changes: 10 additions & 1 deletion YmlTransform.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.13
VisualStudioVersion = 15.0.27004.2002
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YmlTransform", "src\YmlTransform\YmlTransform.csproj", "{00EC6644-0EBF-44A8-8EDF-F73D06787377}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YmlTransform.Tests", "tests\YmlTransform.Tests\YmlTransform.Tests.csproj", "{BB5287AE-B720-4C65-80B0-15F4EB459BA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,8 +17,15 @@ Global
{00EC6644-0EBF-44A8-8EDF-F73D06787377}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00EC6644-0EBF-44A8-8EDF-F73D06787377}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00EC6644-0EBF-44A8-8EDF-F73D06787377}.Release|Any CPU.Build.0 = Release|Any CPU
{BB5287AE-B720-4C65-80B0-15F4EB459BA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB5287AE-B720-4C65-80B0-15F4EB459BA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB5287AE-B720-4C65-80B0-15F4EB459BA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB5287AE-B720-4C65-80B0-15F4EB459BA4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7C44AB57-BB9F-4E95-860F-1F96C74B11EB}
EndGlobalSection
EndGlobal
23 changes: 0 additions & 23 deletions src/YmlTransform/Models/TransformItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,11 @@ namespace YmlTransform.Models
{
class TransformItem
{
/// <summary>
/// Path of the item in the tree
/// </summary>
public string Path { get; set; }

/// <summary>
/// The type of the current item
/// </summary>
public string Type { get; set; }

/// <summary>
/// The languages of the item
/// </summary>
public string Languages { get; set; }

/// <summary>
/// The field id
/// </summary>
public Guid FieldId { get; set; }

/// <summary>
/// The value of the item
/// </summary>
public string Value { get; set; }

/// <summary>
/// Hint property aka human readable id
/// </summary>
public string Hint { get; set; }
}
}
68 changes: 4 additions & 64 deletions src/YmlTransform/Program.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Rainbow.Model;
using Rainbow.Storage.Yaml;
using YmlTransform.Models;
using YmlTransform.Models;

namespace YmlTransform
{
static class Program
class Program
{
static void Main(string[] args)
{
var options = new Options();
var isValid = CommandLine.Parser.Default.ParseArgumentsStrict(args, options);
if (isValid)
{
var transformation = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TransformItem>>(File.ReadAllText(options.TransformFile));

Transform(options.Path, transformation, options.Recursive);
}
}

private static void Transform(string path, List<TransformItem> transformations, bool recursive)
{
var files = Directory.GetFiles(path, "*.yml",
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

foreach (var file in files)
{
using (var fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
var transformed = false;
var formatter = new YamlSerializationFormatter(null, null);
var item = new ProxyItem(formatter.ReadSerializedItem(fs, Path.GetFileName(file)));

foreach (var t in transformations)
{
if (item.Path == t.Path)
{
if (t.Type == "Shared")
{
if (item.SharedFields.FirstOrDefault(a => a.FieldId == t.FieldId) is ProxyFieldValue field)
{
Console.WriteLine($"Updating file {file} section {t.Type} id {t.FieldId} to {t.Value}");
field.Value = t.Value;
transformed = true;
}
}
else if (t.Type == "Languages")
{
if (item.Versions
.Where(a => t.Languages == "*")
.SelectMany(a => a.Fields)
.FirstOrDefault(a => a.FieldId == t.FieldId) is ProxyFieldValue field)
{
Console.WriteLine($"Updating file {file} section {t.Type} id {t.FieldId} to {t.Value}");
field.Value = t.Value;
transformed = true;
}
}
}
}

if (transformed)
{
Console.WriteLine($"Transformed: {file}");
fs.SetLength(0);
formatter.WriteSerializedItem(item, fs);
}
}

YmlTransformer.TransformPath(options.Path, options.TransformFile, options.Recursive);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/YmlTransform/YmlTransform.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Models\TransformItem.cs" />
<Compile Include="YmlTransformer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
90 changes: 90 additions & 0 deletions src/YmlTransform/YmlTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Rainbow.Model;
using Rainbow.Storage.Yaml;
using YmlTransform.Models;

namespace YmlTransform
{
public static class YmlTransformer
{
public static void TransformFile(string fileToTransform, string transformFile)
{
var transformation = GetTransformation(transformFile);
TransformSingle(transformation, fileToTransform);
}


public static void TransformPath(string path, string transformFile, bool recursive)
{
var transformations = GetTransformation(transformFile);
var files = Directory.GetFiles(path, "*.yml",
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

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

private static List<TransformItem> GetTransformation(string transformFile)
{
var transformation =
Newtonsoft.Json.JsonConvert.DeserializeObject<List<TransformItem>>(File.ReadAllText(transformFile));
return transformation;
}

private static void TransformSingle(List<TransformItem> transformations, string file)
{
using (var fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
var transformed = false;
var formatter = new YamlSerializationFormatter(null, null);
var item = new ProxyItem(formatter.ReadSerializedItem(fs, Path.GetFileName(file)));

foreach (var transform in transformations)
{
if (item.Path == transform.Path)
{
if (transform.Type == "Shared")
{
if (item.SharedFields.FirstOrDefault(a => a.FieldId == transform.FieldId) is ProxyFieldValue field)
{
Console.WriteLine(
$"Updating file {file} section {transform.Type} id {transform.FieldId} to {transform.Value}");
field.Value = transform.Value;
transformed = true;
}
}
else if (transform.Type == "Languages")
{
var fields = item.Versions
.Where(a => transform.Languages == "*")
.SelectMany(a => a.Fields)
.Where(a => a.FieldId == transform.FieldId);

foreach (var itemFieldValue in fields)
{
var field = (ProxyFieldValue) itemFieldValue;
Console.WriteLine(
$"Updating file {file} section {transform.Type} id {transform.FieldId} to {transform.Value}");
field.Value = transform.Value;
transformed = true;
}
}
}
}


if (transformed)
{
Console.WriteLine($"Transformed: {file}");
fs.SetLength(0);
formatter.WriteSerializedItem(item, fs);
}
}
}
}
}
47 changes: 47 additions & 0 deletions tests/YmlTransform.Tests/Data/Multi-Language.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
ID: "2a65abb0-3123-469f-a603-5f3b1804713c"
Parent: "756fff26-90f2-4889-bace-da656e66b9c1"
Template: "013bc834-c0f6-4377-924e-0de185abfbe7"
Path: /sitecore/content/Home/MultiLanguage
DB: master
Languages:
- Language: de
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: "German Field1"
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: "German Field2"
- Language: en
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: "English Field1"
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: "English Field2"
- Language: fr
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: "French Field1"
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: "French Field2"
- Language: nl
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: "Dutch Field1"
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: "Dutch Field2"
47 changes: 47 additions & 0 deletions tests/YmlTransform.Tests/Data/Multi-Language.ymlexpected
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
ID: "2a65abb0-3123-469f-a603-5f3b1804713c"
Parent: "756fff26-90f2-4889-bace-da656e66b9c1"
Template: "013bc834-c0f6-4377-924e-0de185abfbe7"
Path: /sitecore/content/Home/MultiLanguage
DB: master
Languages:
- Language: de
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: TransformedField1
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: TransformedField2
- Language: en
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: TransformedField1
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: TransformedField2
- Language: fr
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: TransformedField1
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: TransformedField2
- Language: nl
Versions:
- Version: 1
Fields:
- ID: "00000000-0000-0000-0000-000000000000"
Hint: Field1
Value: TransformedField1
- ID: "00000000-0000-0000-0000-000000000001"
Hint: Field2
Value: TransformedField2
18 changes: 18 additions & 0 deletions tests/YmlTransform.Tests/Data/Multi-Language.ymltransform
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"FieldId": "00000000-0000-0000-0000-000000000000",
"Hint": "Field1",
"Languages": "*",
"Path": "/sitecore/content/Home/MultiLanguage",
"Type": "Languages",
"Value": "TransformedField1"
},
{
"FieldId": "00000000-0000-0000-0000-000000000001",
"Hint": "Field2",
"Languages": "*",
"Path": "/sitecore/content/Home/MultiLanguage",
"Type": "Languages",
"Value": "TransformedField2"
}
]
Loading

0 comments on commit 6ce096c

Please sign in to comment.