From 5567bcad941284e51276c1ddd8a289444fbf190e Mon Sep 17 00:00:00 2001 From: tmenier Date: Sun, 22 Mar 2015 10:51:35 -0500 Subject: [PATCH] set up code generator project ahead of new features (xml, cancelation tokens, more verbs) because # of extension methods is about to explode --- Flurl.Http.CodeGen/App.config | 6 ++ Flurl.Http.CodeGen/CodeWriter.cs | 75 +++++++++++++++++++ Flurl.Http.CodeGen/Flurl.Http.CodeGen.csproj | 59 +++++++++++++++ Flurl.Http.CodeGen/Program.cs | 35 +++++++++ Flurl.Http.CodeGen/Properties/AssemblyInfo.cs | 36 +++++++++ Flurl.Http.NET45/Flurl.Http.NET45.csproj | 3 + Flurl.Http.Shared/Flurl.Http.Shared.projitems | 1 + Flurl.Http.Shared/HttpExtensions.cs | 11 +++ Flurl.sln | 32 ++++++++ 9 files changed, 258 insertions(+) create mode 100644 Flurl.Http.CodeGen/App.config create mode 100644 Flurl.Http.CodeGen/CodeWriter.cs create mode 100644 Flurl.Http.CodeGen/Flurl.Http.CodeGen.csproj create mode 100644 Flurl.Http.CodeGen/Program.cs create mode 100644 Flurl.Http.CodeGen/Properties/AssemblyInfo.cs create mode 100644 Flurl.Http.Shared/HttpExtensions.cs diff --git a/Flurl.Http.CodeGen/App.config b/Flurl.Http.CodeGen/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/Flurl.Http.CodeGen/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Flurl.Http.CodeGen/CodeWriter.cs b/Flurl.Http.CodeGen/CodeWriter.cs new file mode 100644 index 0000000..d927f4b --- /dev/null +++ b/Flurl.Http.CodeGen/CodeWriter.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Flurl.Http.CodeGen +{ + /// + /// Wraps a StreamWriter. Mainly just keeps track of indentation. + /// + public class CodeWriter : IDisposable + { + private readonly StreamWriter _sw; + private int _indent = 0; + private bool _wrapping = false; + + public CodeWriter(string filePath) { + _sw = new StreamWriter(filePath); + } + + /// + /// use @0, @1, @2, etc for tokens. ({0} would be a pain because you'd alway need to escape "{" and "}") + /// + public CodeWriter WriteLine(string line, params object[] args) { + line = line.Trim(); + + for (int i = 0; i < args.Length; i++) { + var val = (args[i] == null) ? "" : args[i].ToString(); + line = line.Replace("@" + i, val); + } + + if (line == "}" || line == "{") { + _indent--; + } + + _sw.Write(new String('\t', _indent)); + _sw.WriteLine(line); + + if (line == "" || line.StartsWith("//") || line.EndsWith("]")) { + _wrapping = false; + } + else if (line.EndsWith(";") || line.EndsWith("}")) { + if (_wrapping) _indent--; + _wrapping = false; + } + else if (line.EndsWith("{")) { + _indent++; + _wrapping = false; + } + else { + if (!_wrapping) _indent++; + _wrapping = true; + } + + return this; // fluent! + } + + public CodeWriter WriteLine() { + _sw.WriteLine(); + return this; + } + + public CodeWriter WriteLines(IEnumerable lines) { + foreach (var line in lines) + WriteLine(line); + return this; + } + + public void Dispose() { + _sw.Dispose(); + } + } +} diff --git a/Flurl.Http.CodeGen/Flurl.Http.CodeGen.csproj b/Flurl.Http.CodeGen/Flurl.Http.CodeGen.csproj new file mode 100644 index 0000000..07693b2 --- /dev/null +++ b/Flurl.Http.CodeGen/Flurl.Http.CodeGen.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + {0571F8FF-1649-433C-97F3-7341942AC886} + Exe + Properties + Flurl.Http.CodeGen + Flurl.Http.CodeGen + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Flurl.Http.CodeGen/Program.cs b/Flurl.Http.CodeGen/Program.cs new file mode 100644 index 0000000..802deb5 --- /dev/null +++ b/Flurl.Http.CodeGen/Program.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Flurl.Http.CodeGen +{ + class Program + { + static void Main(string[] args) { + if (args.Length == 0) + throw new ArgumentException("Must provide a path to the .cs output file."); + + var codePath = args[0]; + using (var writer = new CodeWriter(codePath)) { + writer + .WriteLine("using System.Net.Http;") + .WriteLine("using System.Threading.Tasks;") + .WriteLine("using Flurl.Http.Content;") + .WriteLine("") + .WriteLine("namespace Flurl.Http") + .WriteLine("{") + .WriteLine("public static class HttpExtensions") + .WriteLine("{"); + + writer.WriteLine("// extension methods here"); + + writer + .WriteLine("}") + .WriteLine("}"); + } + } + } +} diff --git a/Flurl.Http.CodeGen/Properties/AssemblyInfo.cs b/Flurl.Http.CodeGen/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..5585a66 --- /dev/null +++ b/Flurl.Http.CodeGen/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Flurl.Http.CodeGen")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Flurl.Http.CodeGen")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("ad34a4a2-e63a-492e-9b24-8907c1da92c8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Flurl.Http.NET45/Flurl.Http.NET45.csproj b/Flurl.Http.NET45/Flurl.Http.NET45.csproj index a48b6b7..f2ee3e8 100644 --- a/Flurl.Http.NET45/Flurl.Http.NET45.csproj +++ b/Flurl.Http.NET45/Flurl.Http.NET45.csproj @@ -67,6 +67,9 @@ + + "$(SolutionDir)Flurl.Http.CodeGen\bin\$(ConfigurationName)\Flurl.Http.CodeGen.exe" "$(SolutionDir)Flurl.Http.Shared\HttpExtensions.cs" +