Skip to content

Commit

Permalink
Merge pull request #2 from luuksommers/hotfix/Issue-1
Browse files Browse the repository at this point in the history
Hotfix/issue 1
  • Loading branch information
berendhaan authored Oct 23, 2017
2 parents 59cfbe6 + 3123491 commit 0de035a
Show file tree
Hide file tree
Showing 13 changed files with 443 additions and 70 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
70 changes: 1 addition & 69 deletions src/YmlTransform/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,7 @@ static void Main(string[] args)
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")
{
var field = item.SharedFields.FirstOrDefault(a => a.FieldId == t.FieldId) as ProxyFieldValue;
if (field != null)
{
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")
{
var field = item.Versions
.Where(a => t.Languages == "*")
.SelectMany(a => a.Fields)
.FirstOrDefault(a => a.FieldId == t.FieldId) as ProxyFieldValue;
if (field != null)
{
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);
}
}

Expand All @@ -91,15 +33,5 @@ class Options
[Option('t', "transform", Required = true, HelpText = "Transformation file")]
public string TransformFile { get; set; }
}

class TransformItem
{
public string Path { get; set; }
public string Type { get; set; }
public string Languages { get; set; }
public Guid FieldId { get; set; }
public string Value { get; set; }
public string Hint { get; set; }
}
}
}
14 changes: 14 additions & 0 deletions src/YmlTransform/TransformItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace YmlTransform
{
class TransformItem
{
public string Path { get; set; }
public string Type { get; set; }
public string Languages { get; set; }
public Guid FieldId { get; set; }
public string Value { get; set; }
public string Hint { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/YmlTransform/YmlTransform.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TransformItem.cs" />
<Compile Include="YmlTransformer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
89 changes: 89 additions & 0 deletions src/YmlTransform/YmlTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Rainbow.Model;
using Rainbow.Storage.Yaml;

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"
}
]
36 changes: 36 additions & 0 deletions tests/YmlTransform.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("YmlTransform.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("YmlTransform.Tests")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("bb5287ae-b720-4c65-80b0-15f4eb459ba4")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 0de035a

Please sign in to comment.