#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using MiniSqlQuery.Core;
using MiniSqlQuery.Core.Template;
namespace MiniSqlQuery.PlugIns.TemplateViewer
{
/// The template model.
public class TemplateModel
{
/// The extension.
public const string Extension = "extension";
/// The _formatter.
private readonly ITextFormatter _formatter;
/// The _services.
private readonly IApplicationServices _services;
/// Initializes a new instance of the class.
/// The services.
/// The formatter.
public TemplateModel(IApplicationServices services, ITextFormatter formatter)
{
_services = services;
_formatter = formatter;
}
/// The get value for parameter.
/// The parameter.
public delegate string GetValueForParameter(string parameter);
/// The create nodes.
///
public TreeNode[] CreateNodes()
{
string path = GetTemplatePath();
return CreateNodes(path, GetFilesForFolder(path));
}
/// The create nodes.
/// The root path.
/// The files.
///
public TreeNode[] CreateNodes(string rootPath, string[] files)
{
List nodes = new List();
foreach (string file in files)
{
if (file.StartsWith(rootPath))
{
FileInfo fi = new FileInfo(file);
TreeNode node = new TreeNode(Path.GetFileNameWithoutExtension(fi.FullName));
node.ImageKey = "script_code";
node.SelectedImageKey = "script_code";
node.Tag = fi; // store file info on tag
node.ToolTipText = fi.FullName;
nodes.Add(node);
}
}
return nodes.ToArray();
}
/// The get files for folder.
/// The path.
///
public string[] GetFilesForFolder(string path)
{
return Directory.GetFiles(path, "*.mt", SearchOption.TopDirectoryOnly);
}
/// The get template path.
/// The get template path.
public string GetTemplatePath()
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, TemplateResources.TemplatesDirectoryName);
return path;
}
/// The infer extension from filename.
/// The filename.
/// The items.
/// The infer extension from filename.
public string InferExtensionFromFilename(string filename, Dictionary items)
{
string ext = Path.GetExtension(filename);
string templateFilename = Path.GetFileNameWithoutExtension(filename);
if (ext.ToLower() == ".mt" && templateFilename.Contains("."))
{
return Path.GetExtension(templateFilename).Substring(1);
}
// default
return "sql";
}
/// The pre process template.
/// The lines.
/// The get value for parameter.
/// The items.
/// The pre process template.
public string PreProcessTemplate(
string[] lines,
GetValueForParameter getValueForParameter,
Dictionary items)
{
int i = 0;
for (; i < lines.Length; i++)
{
string line = lines[i];
if (line.StartsWith("#@"))
{
// process cmd
if (line.StartsWith("#@get ", StringComparison.CurrentCultureIgnoreCase))
{
string name = line.Substring("#@get ".Length);
string val = getValueForParameter(name);
items.Add(name, val);
}
else if (line.StartsWith("#@set extension ", StringComparison.CurrentCultureIgnoreCase))
{
items[Extension] = line.Substring("#@set extension ".Length);
}
else if (line.StartsWith("#@import-plugin ", StringComparison.CurrentCultureIgnoreCase))
{
string pluginKeyName = line.Substring("#@import-plugin ".Length);
items[pluginKeyName.Replace(".", "_")] = _services.Resolve(pluginKeyName);
}
}
else
{
break;
}
}
string text = string.Join(Environment.NewLine, lines, i, lines.Length - i);
return text;
}
/// The process template.
/// The text.
/// The items.
///
public TemplateResult ProcessTemplate(string text, Dictionary items)
{
if (items == null)
{
items = new Dictionary();
}
TemplateResult result;
using (TemplateHost host = _services.Resolve())
{
items.Add("Host", host);
items.Add("Data", host.Data);
result = new TemplateResult();
result.Text = _formatter.Format(text, items);
result.Extension = "sql";
if (items.ContainsKey(Extension))
{
result.Extension = (string)items[Extension];
}
}
return result;
}
/// The process template file.
/// The filename.
/// The get value for parameter.
///
public TemplateResult ProcessTemplateFile(string filename, GetValueForParameter getValueForParameter)
{
Dictionary items = new Dictionary();
string[] lines = File.ReadAllLines(filename);
items[Extension] = InferExtensionFromFilename(filename, items);
string text = PreProcessTemplate(lines, getValueForParameter, items);
return ProcessTemplate(text, items);
}
}
}