|
using System;
using System.IO;
using ReflectionStudio.Core.Events;
namespace ReflectionStudio.Core.Generator
{
/// <summary>
/// ClassWriter: Classe de base qui va ensuite etre d�riv�, puis compil� pour g�n�rer
/// les classes � partir des templates.
/// </summary>
internal class ClassWriter
{
private TemplateItem _ti = null;
protected StreamWriter _StreamWriter = null;
#region properties
private Context _Context = null;
protected Context Context
{
get { return _Context; }
}
#endregion
public ClassWriter()
{
}
public void Initialize(Context context, TemplateItem ti)
{
_Context = context;
_ti = ti;
if( !Directory.Exists( _ti.OutputPath ) )
{
Directory.CreateDirectory( _ti.OutputPath );
//EventManager.Instance.SendMessage( string.Format( "Create the directory {0}...", _ti.OutputPath ) );
}
string outputfile = GetOutputFile();
if (File.Exists(outputfile))
{
if (!_ti.OverwriteIfExist)
{
//EventManager.Instance.SendMessage(string.Format("Do not overwrite the {0} file", outputfile));
return;
}
if( (File.GetAttributes( outputfile ) & FileAttributes.ReadOnly ) != FileAttributes.ReadOnly )
{
File.Delete(outputfile);
}
else
{
//EventManager.Instance.SendMessage(string.Format("The file {0} is write protected", outputfile));
return;
}
}
_StreamWriter = File.CreateText(outputfile);
EventDispatcher.Instance.RaiseStatus(
string.Format( "Generate the file {0} ", outputfile ), StatusEventType.StartProgress );
}
virtual public void Generate()
{
//si le _StreamWriter est null, on ecrit rien
throw new Exception( "Generate fonction of ClassWriter not overrired" );
}
public void UnInitialize()
{
if( _StreamWriter != null )
{
_StreamWriter.Flush();
_StreamWriter.Close();
_StreamWriter = null;
}
_Context = null;
}
#region FILES
protected string GetOutputFile()
{
return string.Format( "{0}\\{1}{2}{3}.{4}",
_ti.OutputPath, _ti.OutputFilePrefix, _Context.CurrentTable.Name, _ti.OutputFileSufix, _ti.OutputFileExtension);
}
protected string GetRelativeFile()
{
return string.Format("{0}{1}{2}.cs", _ti.OutputFilePrefix, _Context.CurrentTable.Name, _ti.OutputFileSufix);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside
https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/