-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
438 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
Oops, something went wrong.