Skip to content

Commit

Permalink
Improve Exception Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmanzano committed Feb 6, 2023
1 parent d620fdb commit c4f96a8
Show file tree
Hide file tree
Showing 16 changed files with 766 additions and 683 deletions.
271 changes: 135 additions & 136 deletions src/Maui.BindableProperty.Generator/CodeWriter/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,191 +5,190 @@
using System.Text;

// Based on https://github.com/SaladLab/CodeWriter
namespace Maui.BindableProperty.Generator
namespace Maui.BindableProperty.Generator;

public class CodeWriter
{
public class CodeWriter
{
private readonly CodeWriterSettings _settings;
private readonly StringBuilder _sb;
private int _indent;
private bool _newLineOnBlockEnd;
private readonly CodeWriterSettings _settings;
private readonly StringBuilder _sb;
private int _indent;
private bool _newLineOnBlockEnd;

public CodeWriterSettings Settings => _settings;
public int Indent => _indent;
public List<string> HeadLines { get; set; }
public CodeWriterSettings Settings => _settings;
public int Indent => _indent;
public List<string> HeadLines { get; set; }

public CodeWriter(CodeWriterSettings settings)
{
_settings = settings;
_sb = new StringBuilder();
_indent = 0;
}
public CodeWriter(CodeWriterSettings settings)
{
_settings = settings;
_sb = new StringBuilder();
_indent = 0;
}

public void WriteRaw(string str = null)
public void WriteRaw(string str = null)
{
if (str != null)
_sb.Append(str);
}

public void Write(string str = null)
{
if (_newLineOnBlockEnd)
{
if (str != null)
_sb.Append(str);
_sb.Append(_settings.NewLine);

_newLineOnBlockEnd = false;
}

public void Write(string str = null)
{
if (_newLineOnBlockEnd)
{
if (str != null)
_sb.Append(_settings.NewLine);
WriteInternal(str);
}

_newLineOnBlockEnd = false;
}
public void Write(string str, params string[] strs)
{
Write(str);
foreach (var s in strs)
Write(s);
}

WriteInternal(str);
private void WriteInternal(string str = null)
{
if (str != null)
{
_sb.Append(GetIndentString());
_sb.Append(str);
_sb.Append(_settings.NewLine);
}

public void Write(string str, params string[] strs)
else
{
Write(str);
foreach (var s in strs)
Write(s);
_sb.Append(_settings.NewLine);
}
}

private void WriteInternal(string str = null)
public UsingHandle OpenBlock(string[] strs, bool newLineAfterBlockEnd = false)
{
if (_newLineOnBlockEnd)
{
if (str != null)
{
_sb.Append(GetIndentString());
_sb.Append(str);
_sb.Append(_settings.NewLine);
}
else
{
_sb.Append(_settings.NewLine);
}
_sb.Append(_settings.NewLine);
_newLineOnBlockEnd = false;
}

public UsingHandle OpenBlock(string[] strs, bool newLineAfterBlockEnd = false)
if (strs.Any())
{
if (_newLineOnBlockEnd)
for (var i = 0; i < strs.Length; i++)
{
_sb.Append(_settings.NewLine);
_newLineOnBlockEnd = false;
_sb.Append(GetIndentString(i == 0 ? 0 : 1));
_sb.Append(strs[i]);
if (i < strs.Length - 1)
_sb.Append(_settings.NewLine);
}

if (strs.Any())
if (_settings.NewLineBeforeBlockBegin)
{
for (var i = 0; i < strs.Length; i++)
{
_sb.Append(GetIndentString(i == 0 ? 0 : 1));
_sb.Append(strs[i]);
if (i < strs.Length - 1)
_sb.Append(_settings.NewLine);
}

if (_settings.NewLineBeforeBlockBegin)
{
_sb.Append(_settings.NewLineBeforeBlockBegin ? _settings.NewLine : " ");
Write(_settings.BlockBegin);
}
else
{
_sb.Append(" ");
_sb.Append(_settings.BlockBegin);
_sb.Append(_settings.NewLine);
}
_sb.Append(_settings.NewLineBeforeBlockBegin ? _settings.NewLine : " ");
Write(_settings.BlockBegin);
}
else
{
Write(_settings.BlockBegin);
_sb.Append(" ");
_sb.Append(_settings.BlockBegin);
_sb.Append(_settings.NewLine);
}

IncIndent();
return new UsingHandle(() =>
{
DecIndent();
WriteInternal(_settings.BlockEnd);
_newLineOnBlockEnd = newLineAfterBlockEnd;
});
}

public UsingHandle OpenIndent(string begin, string end, bool newLineAfterBlockEnd = false)
else
{
if (_newLineOnBlockEnd)
{
_sb.Append(_settings.NewLine);
_newLineOnBlockEnd = false;
}
Write(_settings.BlockBegin);
}

if (begin != null)
{
_sb.Append(GetIndentString());
_sb.Append(begin);
_sb.Append(_settings.NewLine);
}
IncIndent();
return new UsingHandle(() =>
{
DecIndent();
WriteInternal(_settings.BlockEnd);
IncIndent();
return new UsingHandle(() =>
{
DecIndent();
if (end != null)
WriteInternal(end);
_newLineOnBlockEnd = newLineAfterBlockEnd;
});
}

_newLineOnBlockEnd = newLineAfterBlockEnd;
});
public UsingHandle OpenIndent(string begin, string end, bool newLineAfterBlockEnd = false)
{
if (_newLineOnBlockEnd)
{
_sb.Append(_settings.NewLine);
_newLineOnBlockEnd = false;
}

public void IncIndent()
if (begin != null)
{
_indent += 1;
_sb.Append(GetIndentString());
_sb.Append(begin);
_sb.Append(_settings.NewLine);
}

public void DecIndent()
IncIndent();
return new UsingHandle(() =>
{
if (_indent == 0)
throw new InvalidOperationException("Cannot decrease indent.");
DecIndent();
if (end != null)
WriteInternal(end);
_indent -= 1;
}
_newLineOnBlockEnd = newLineAfterBlockEnd;
});
}

public string GetIndentString(int additional = 0)
{
return string.Concat(Enumerable.Repeat(_settings.Indent, _indent + additional));
}
public void IncIndent()
{
_indent += 1;
}

public override string ToString()
{
var headComment = HeadLines != null
? string.Join(_settings.NewLine, HeadLines) + _settings.NewLine
: "";
public void DecIndent()
{
if (_indent == 0)
throw new InvalidOperationException("Cannot decrease indent.");

_indent -= 1;
}

public string GetIndentString(int additional = 0)
{
return string.Concat(Enumerable.Repeat(_settings.Indent, _indent + additional));
}

public override string ToString()
{
var headComment = HeadLines != null
? string.Join(_settings.NewLine, HeadLines) + _settings.NewLine
: "";

var text = headComment + _sb.ToString();
if (_settings.TranslationMapping != null)
var text = headComment + _sb.ToString();
if (_settings.TranslationMapping != null)
{
foreach (var i in _settings.TranslationMapping)
{
foreach (var i in _settings.TranslationMapping)
{
text = text.Replace(i.Key, i.Value);
}
text = text.Replace(i.Key, i.Value);
}
return text;
}
return text;
}

public bool WriteAllText(string path, bool skipNotChanged = true)
{
return WriteAllText(path, skipNotChanged, Encoding.UTF8);
}
public bool WriteAllText(string path, bool skipNotChanged = true)
{
return WriteAllText(path, skipNotChanged, Encoding.UTF8);
}

public bool WriteAllText(string path, bool skipNotChanged, Encoding encoding)
{
var text = ToString();
public bool WriteAllText(string path, bool skipNotChanged, Encoding encoding)
{
var text = ToString();

if (skipNotChanged && File.Exists(path))
if (skipNotChanged && File.Exists(path))
{
var existingText = File.ReadAllText(path);
if (existingText == text)
{
var existingText = File.ReadAllText(path);
if (existingText == text)
{
return false;
}
return false;
}
File.WriteAllText(path, text, encoding);
return true;
}
File.WriteAllText(path, text, encoding);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
#pragma warning disable SA1300 // Element must begin with upper-case letter

// Based on https://github.com/SaladLab/CodeWriter
namespace Maui.BindableProperty.Generator
namespace Maui.BindableProperty.Generator;

public static class CodeWriterExtensions
{
public static class CodeWriterExtensions
public static void _(this CodeWriter w, string str = null)
{
public static void _(this CodeWriter w, string str = null)
{
w.Write(str);
}
w.Write(str);
}

public static void _(this CodeWriter w, string str, params string[] strs)
{
w.Write(str, strs);
}
public static void _(this CodeWriter w, string str, params string[] strs)
{
w.Write(str, strs);
}

public static UsingHandle b(this CodeWriter w, params string[] strs)
{
return w.OpenBlock(strs, newLineAfterBlockEnd: false);
}
public static UsingHandle b(this CodeWriter w, params string[] strs)
{
return w.OpenBlock(strs, newLineAfterBlockEnd: false);
}

public static UsingHandle B(this CodeWriter w, params string[] strs)
{
return w.OpenBlock(strs, newLineAfterBlockEnd: true);
}
public static UsingHandle B(this CodeWriter w, params string[] strs)
{
return w.OpenBlock(strs, newLineAfterBlockEnd: true);
}

public static UsingHandle i(this CodeWriter w, string begin = null, string end = null)
{
return w.OpenIndent(begin, end, newLineAfterBlockEnd: false);
}
public static UsingHandle i(this CodeWriter w, string begin = null, string end = null)
{
return w.OpenIndent(begin, end, newLineAfterBlockEnd: false);
}

public static UsingHandle I(this CodeWriter w, string begin = null, string end = null)
{
return w.OpenIndent(begin, end, newLineAfterBlockEnd: true);
}
public static UsingHandle I(this CodeWriter w, string begin = null, string end = null)
{
return w.OpenIndent(begin, end, newLineAfterBlockEnd: true);
}
}
Loading

0 comments on commit c4f96a8

Please sign in to comment.