/****************************************************************************/ private static void TransformFile(JTran.Transformer transformer, string sourceFile, string output, TransformerContext context) { var result = transformer.Transform(sourceFile, context); var path = output; if (string.IsNullOrWhiteSpace(Path.GetExtension(output))) { path = Path.Combine(output, Path.GetFileName(sourceFile)); } File.WriteAllText(path, result); }
/****************************************************************************/ public string Transform(string source, string transform) { var transformer = new JTran.Transformer(transform, includeSource: new FileIncludeRepository(_preferences.IncludePath)); var context = new TransformerContext(); context.DocumentRepositories = new DocumentsRepository(_preferences.DocumentPath); var result = transformer.Transform(source, context); var expando = result.JsonToExpando() as ExpandoObject; return(_formatter.Format(expando)); }
/****************************************************************************/ private static void TransformFiles(string configPath, string transform, string source, string output, string includes, string documents) { var sTransform = ""; string[] sourceFiles = null; Config config = null; if (!string.IsNullOrWhiteSpace(configPath)) { try { if (!configPath.Contains(Path.DirectorySeparatorChar)) { configPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), configPath); } var configSource = File.ReadAllText(configPath); config = JsonConvert.DeserializeObject <Config>(configSource); if (!string.IsNullOrWhiteSpace(includes)) { config.IncludePath = includes; } } catch (Exception ex) { System.Console.WriteLine("Specified config file does not exist or is invalid: " + ex.Message); } } try { sTransform = File.ReadAllText(transform); } catch (Exception ex) { System.Console.WriteLine(ex.Message); return; } try { var transformer = new JTran.Transformer(sTransform, includeSource: config.IncludeRepository); var context = config.ToContext(documents); // Single file if (!string.IsNullOrWhiteSpace(Path.GetExtension(source))) { TransformFile(transformer, File.ReadAllText(source), output, context); return; } sourceFiles = Directory.GetFiles(source); System.Console.WriteLine($"{sourceFiles.Length} source files found"); if (sourceFiles.Length == 0) { return; } foreach (var sourceFile in sourceFiles) { TransformFile(transformer, File.ReadAllText(sourceFile), output, context); } } catch (Exception ex2) { System.Console.WriteLine(ex2.Message); return; } }