Skip to content

Commit

Permalink
Code gardening
Browse files Browse the repository at this point in the history
  • Loading branch information
popcatalin81 committed May 15, 2020
1 parent a158129 commit df487b4
Show file tree
Hide file tree
Showing 33 changed files with 533 additions and 476 deletions.
78 changes: 39 additions & 39 deletions Geco/Common/BaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

namespace Geco.Common
{
public abstract class BaseGenerator : IOutputRunnable, IRunableConfirmation
public abstract class BaseGenerator : IOutputRunnable, IRunnableConfirmation
{
private const string IndentString = " ";
private readonly HashSet<string> filesToDelete = new HashSet<string>();
private int _indent;

private TextWriter _tw;
private int _indent;
private bool initialized;
private readonly HashSet<string> filesToDelete = new HashSet<string>();
private bool commaNewLine;
private bool initialized;

protected BaseGenerator(IInflector inf)
{
Expand All @@ -23,7 +23,6 @@ protected BaseGenerator(IInflector inf)
protected IInflector Inf { get; }

public bool OutputToConsole { get; set; }
protected abstract void Generate();

public void Run()
{
Expand All @@ -32,31 +31,33 @@ public void Run()
CleanFiles();
}

private void CleanFiles()
{
foreach (var filePath in filesToDelete)
{
File.Delete(filePath);
}
}
public string BaseOutputPath { get; set; }
public string CleanFilesPattern { get; set; }
public bool Interactive { get; set; }

public virtual bool GetUserConfirmation()
{
if (string.IsNullOrEmpty(CleanFilesPattern))
return true;
ColorConsole.Write($"Clean all files with pattern [{(CleanFilesPattern, ConsoleColor.Yellow)}] in the target folder [{(Path.GetFullPath(BaseOutputPath), ConsoleColor.Yellow)}] (y/n)?", ConsoleColor.White);
ColorConsole.Write(
$"Clean all files with pattern [{(CleanFilesPattern, ConsoleColor.Yellow)}] in the target folder [{(Path.GetFullPath(BaseOutputPath), ConsoleColor.Yellow)}] (y/n)?",
ConsoleColor.White);
return string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase);
}

protected abstract void Generate();

private void CleanFiles()
{
foreach (var filePath in filesToDelete) File.Delete(filePath);
}

private void DetermineFilesToClean()
{
if (!String.IsNullOrWhiteSpace(CleanFilesPattern) && Directory.Exists(BaseOutputPath))
{
foreach (var file in Directory.EnumerateFiles(BaseOutputPath, CleanFilesPattern, SearchOption.TopDirectoryOnly))
{
if (!string.IsNullOrWhiteSpace(CleanFilesPattern) && Directory.Exists(BaseOutputPath))
foreach (var file in Directory.EnumerateFiles(BaseOutputPath, CleanFilesPattern,
SearchOption.TopDirectoryOnly))
filesToDelete.Add(file);
}
}
}

protected IDisposable BeginFile(string file, bool option = true)
Expand All @@ -75,13 +76,13 @@ protected IDisposable BeginFile(string file, bool option = true)
private void EnsurePath(string fileName)
{
var folders = Path.GetDirectoryName(fileName);
if (!String.IsNullOrEmpty(folders))
if (!string.IsNullOrEmpty(folders))
Directory.CreateDirectory(folders);
}


/// <summary>
/// Write semicolon ; on the previous line
/// Write semicolon ; on the previous line
/// </summary>
protected void SemiColon()
{
Expand All @@ -90,7 +91,7 @@ protected void SemiColon()
}

/// <summary>
/// Write comma , on the previous line
/// Write comma , on the previous line
/// </summary>
protected void Comma()
{
Expand All @@ -99,36 +100,35 @@ protected void Comma()
}

/// <summary>
/// Write comma , on the previous line if a new line is written
/// Write comma , on the previous line if a new line is written
/// </summary>
protected void CommaIfNewLine()
{
this.commaNewLine = true;
commaNewLine = true;
}

/// <summary>
/// Stops write comma , on the previous line if a line is written
/// Stops write comma , on the previous line if a line is written
/// </summary>
protected void NoCommaIfNewLine()
{
this.commaNewLine = false;
commaNewLine = false;
}

/// <summary>
/// Write line and increase indent
/// Write line and increase indent
/// </summary>
/// <param name="text">The text to write</param>
/// <param name="write">boolean parameter to indicate if the text should be written or not</param>
protected void WI(string text = "", bool write = true)
{

W(text, write);
if (write)
Indent();
}

/// <summary>
/// Increase indent and write line
/// Increase indent and write line
/// </summary>
/// <param name="text">The text to write</param>
/// <param name="write">boolean parameter to indicate if the text should be written or not</param>
Expand All @@ -140,7 +140,7 @@ protected void IW(string text = "", bool write = true)
}

/// <summary>
/// Decrease indent and write line
/// Decrease indent and write line
/// </summary>
/// <param name="text">The text to write</param>
/// <param name="write">boolean parameter to indicate if the text should be written or not</param>
Expand All @@ -152,7 +152,7 @@ protected void DW(string text = "", bool write = true)
}

/// <summary>
/// Write line and decrease indent
/// Write line and decrease indent
/// </summary>
/// <param name="text">The text to write</param>
/// <param name="write">boolean parameter to indicate if the text should be written or not</param>
Expand All @@ -164,7 +164,7 @@ protected void WD(string text = "", bool write = true)
}

/// <summary>
/// Write line with current indent
/// Write line with current indent
/// </summary>
/// <param name="text">The text to write</param>
/// <param name="write">boolean parameter to indicate if the text should be written or not</param>
Expand All @@ -179,34 +179,38 @@ protected void W(string text = "", bool write = true)
_tw.Write(",");
if (OutputToConsole) Console.Write(",");
}

_tw.WriteLine();
if (OutputToConsole) Console.WriteLine();
}
else
{
initialized = true;
}

if (string.IsNullOrWhiteSpace(text))
return;

for (int i = 0; i < _indent; i++)
for (var i = 0; i < _indent; i++)
{
_tw.Write(IndentString);
if (OutputToConsole) Console.Write(IndentString);
}

_tw.Write(text);
if (OutputToConsole) Console.Write(text);
}

/// <summary>
/// Increase indent
/// Increase indent
/// </summary>
protected void Indent()
{
_indent++;
}

/// <summary>
/// Decrease indent
/// Decrease indent
/// </summary>
protected void Dedent()
{
Expand All @@ -223,10 +227,6 @@ protected string CommaJoin<T>(IEnumerable<T> values, Func<T, string> selector)
return string.Join(", ", values.Select(selector));
}

public string BaseOutputPath { get; set; }
public string CleanFilesPattern { get; set; }
public bool Interactive { get; set; }

protected IDisposable OnBlockEnd(Action action = null, bool write = true)
{
return new DisposableAction(write ? action : null);
Expand Down
10 changes: 5 additions & 5 deletions Geco/Common/BaseGeneratorWithMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ namespace Geco.Common
public abstract class BaseGeneratorWithMetadata : BaseGenerator
{
protected readonly string ConnectionName;
public DatabaseMetadata Db => Provider.GetMetadata(ConnectionName);
public IMetadataProvider Provider { get; }

protected BaseGeneratorWithMetadata(IMetadataProvider provider, IInflector inf, string connectionName)
: base(inf)
{
this.ConnectionName = connectionName;
ConnectionName = connectionName;
Provider = provider;
}

public DatabaseMetadata Db => Provider.GetMetadata(ConnectionName);
public IMetadataProvider Provider { get; }

protected void ReloadMetadata()
{
Provider.Reload();
this.Db.Freeze();
Db.Freeze();
}

protected virtual void OnMetadataLoaded(DatabaseMetadata db)
{

}

protected string GetCharpTypeName(Type type)
Expand Down
29 changes: 19 additions & 10 deletions Geco/Common/IRunnable.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
namespace Geco.Common
{
/// <summary>
/// Represents a runable task, for code generation or purposes or others
/// Represents a runnable task, for code generation or purposes or others
/// </summary>
public interface IRunnable
{
/// <summary>
/// Invoked when this task is executed
/// Invoked when this task is executed
/// </summary>
void Run();
}

/// <summary>
/// Represents a runable task that outputs files to a location
/// Represents a runnable task that outputs files to a location
/// </summary>
public interface IOutputRunnable : IRunnable
{

/// <summary>
/// <c>true</c> if the task output should be mirrored to the console. <c>false</c> otherwise
/// <c>true</c> if the task output should be mirrored to the console. <c>false</c> otherwise
/// </summary>
bool OutputToConsole { get; set; }

/// <summary>
/// Base path which will be combined with the file names of the files output by this task.
/// Base path which will be combined with the file names of the files output by this task.
/// </summary>
string BaseOutputPath { get; set; }

/// <summary>
/// A wild card base pattern for deleting the files from the <see cref="BaseOutputPath"/> prior to generation.
/// A wild card base pattern for deleting the files from the <see cref="BaseOutputPath" /> prior to generation.
/// </summary>
string CleanFilesPattern { get; set; }

/// <summary>
/// <c>true</c> if the Geco is running in interactive mode and the task may ask for additional user input. <c>false</c> otherwise
/// <c>true</c> if the Geco is running in interactive mode and the task may ask for additional user input. <c>false</c>
/// otherwise
/// </summary>
bool Interactive { get; set; }
}


public interface IRunableConfirmation
/// <summary>
/// Represents a task that asks for user confirmation when running in <see cref="Program.Interactive"/> mode
/// </summary>
public interface IRunnableConfirmation
{
/// <summary>
/// Called to get user confirmation before running the task
/// </summary>
/// <returns><c>true</c> if the user confirmed the action, <c>false</c> otherwise</returns>
bool GetUserConfirmation();
}
}
2 changes: 1 addition & 1 deletion Geco/Common/Inflector/HumanizerInflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public string Camelise(string lowercaseAndUnderscoredWord)

public string Underscore(string pascalCasedWord)
{
return pascalCasedWord.Underscore().Replace(" ", "");
return pascalCasedWord.Underscore().Replace(" ", "");
}

public string Capitalise(string word)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static T GetOrAdd<T>(this MetadataCollection<T> collection, string key, F
writable[key] = item;
return item;
}

return collection[key];
}
}
Expand Down
Loading

0 comments on commit df487b4

Please sign in to comment.