Lets set a few things up.
This commit is contained in:
parent
e0f66aa224
commit
945ec83feb
5 changed files with 228 additions and 0 deletions
17
cscz.sln
Normal file
17
cscz.sln
Normal file
|
@ -0,0 +1,17 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2012
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cscz", "cscz\cscz.csproj", "{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}.Debug|x86.ActiveCfg = Debug|x86
|
||||
{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}.Debug|x86.Build.0 = Debug|x86
|
||||
{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}.Release|x86.ActiveCfg = Release|x86
|
||||
{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}.Release|x86.Build.0 = Release|x86
|
||||
EndGlobalSection
|
||||
EndGlobal
|
128
cscz/ClassGenerator.cs
Normal file
128
cscz/ClassGenerator.cs
Normal file
|
@ -0,0 +1,128 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace cscz
|
||||
{
|
||||
public class ClassGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// A mapping of namespaces to using shortcuts.
|
||||
/// </summary>
|
||||
public Dictionary<string, string> UsingShortcuts = new Dictionary<string, string>()
|
||||
{
|
||||
{ "s", "System" },
|
||||
{ "c", "System.Collections.Generic" },
|
||||
{ "cc", "System.Collections.Concurrent" },
|
||||
{ "r", "System.Text.RegularExpressions" }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The using statements to add to the generated class.
|
||||
/// </summary>
|
||||
public List<string> UsingStatements = new List<string>()
|
||||
{
|
||||
"s"
|
||||
};
|
||||
public List<string> Signatures = new List<string>();
|
||||
|
||||
public string ClassName = "Carrot";
|
||||
|
||||
/// <summary>
|
||||
/// Whether to make data members private and create public properties for them instead of making the
|
||||
/// data members public.
|
||||
/// </summary>
|
||||
public bool CreateProperties = true;
|
||||
|
||||
public ClassGenerator ()
|
||||
{
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
foreach(string usingStatement in UsingStatements)
|
||||
result.AppendLine(string.Format("using {0};", expandUsingShortcut(usingStatement)));
|
||||
|
||||
result.AppendLine();
|
||||
result.AppendLine(string.Format("class {0} ", ClassName));
|
||||
result.AppendLine("{");
|
||||
|
||||
StringBuilder properties = new StringBuilder();
|
||||
StringBuilder constructorSignature = new StringBuilder(string.Format("\tpublic {0}(", ClassName));
|
||||
StringBuilder constructorBody = new StringBuilder();
|
||||
|
||||
foreach (string signature in Signatures)
|
||||
{
|
||||
string[] parts = Regex.Split(signature, @"\s+");
|
||||
string datatypeName = parts[0];
|
||||
string privateDataMemberName = LowercaseFirstLetter(parts[1]);
|
||||
string publicDataMemberName = UppercaseFirstLetter(parts[1]);
|
||||
|
||||
constructorSignature.AppendFormat("{0} in{1}, ", datatypeName, publicDataMemberName);
|
||||
|
||||
if (CreateProperties)
|
||||
{
|
||||
result.AppendLine(string.Format("\tprivate {0} {1};", datatypeName, privateDataMemberName));
|
||||
|
||||
properties.AppendLine(string.Format("\tpublic {0} {1}", datatypeName, publicDataMemberName));
|
||||
properties.AppendLine(string.Format("\t{{"));
|
||||
properties.AppendLine(string.Format("\t\tget {{ return {0}; }}", privateDataMemberName));
|
||||
properties.AppendLine(string.Format("\t\tset {{ {0} = value; }}", privateDataMemberName));
|
||||
properties.AppendLine(string.Format("\t}}"));
|
||||
|
||||
constructorBody.AppendLine(string.Format("\t\t{0} = in{0};", publicDataMemberName));
|
||||
}
|
||||
else
|
||||
{
|
||||
result.AppendLine(string.Format("\tpublic {0} {1};"));
|
||||
constructorBody.AppendLine(string.Format("\t\t{0} = in{1};", privateDataMemberName, publicDataMemberName));
|
||||
}
|
||||
}
|
||||
|
||||
// Add the properties to the output
|
||||
result.AppendLine("\t");
|
||||
result.AppendLine(properties.ToString());
|
||||
|
||||
// Add the constructor to the output
|
||||
constructorSignature.Remove(constructorSignature.Length - 2, 2); // Remove the last ", "
|
||||
constructorSignature.Append(")");
|
||||
result.AppendLine(constructorSignature.ToString());
|
||||
result.AppendLine("\t{");
|
||||
result.Append(constructorBody.ToString());
|
||||
result.AppendLine("\t}");
|
||||
|
||||
// Close the class off and end the file
|
||||
result.AppendLine("}");
|
||||
result.AppendLine();
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
protected string expandUsingShortcut(string shortcut)
|
||||
{
|
||||
if (UsingShortcuts.ContainsKey(shortcut))
|
||||
return UsingShortcuts[shortcut];
|
||||
else
|
||||
return shortcut;
|
||||
}
|
||||
|
||||
private string UppercaseFirstLetter(string str)
|
||||
{
|
||||
if (str == null)
|
||||
return null;
|
||||
if (str.Length > 1)
|
||||
return char.ToUpper(str[0]) + str.Substring(1);
|
||||
return str.ToUpper();
|
||||
}
|
||||
private string LowercaseFirstLetter(string str)
|
||||
{
|
||||
if (str == null)
|
||||
return null;
|
||||
if (str.Length > 1)
|
||||
return char.ToLower(str[0]) + str.Substring(1);
|
||||
return str.ToLower();
|
||||
}
|
||||
}
|
||||
}
|
15
cscz/Program.cs
Normal file
15
cscz/Program.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
|
||||
namespace cscz
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
ClassGenerator cg = new ClassGenerator();
|
||||
cg.Signatures.Add("float radius");
|
||||
cg.Signatures.Add("double weight");
|
||||
Console.WriteLine(cg);
|
||||
}
|
||||
}
|
||||
}
|
27
cscz/Properties/AssemblyInfo.cs
Normal file
27
cscz/Properties/AssemblyInfo.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// Information about this assembly is defined by the following attributes.
|
||||
// Change them to the values specific to your project.
|
||||
|
||||
[assembly: AssemblyTitle("cscz")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("")]
|
||||
[assembly: AssemblyCopyright("Starbeamrainbowlabs")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
||||
|
||||
[assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
// The following attributes are used to specify the signing key for the assembly,
|
||||
// if desired. See the Mono documentation for more information about signing.
|
||||
|
||||
//[assembly: AssemblyDelaySign(false)]
|
||||
//[assembly: AssemblyKeyFile("")]
|
||||
|
41
cscz/cscz.csproj
Normal file
41
cscz/cscz.csproj
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
||||
<ProjectGuid>{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>cscz</RootNamespace>
|
||||
<AssemblyName>cscz</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug</OutputPath>
|
||||
<DefineConstants>DEBUG;</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release</OutputPath>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ClassGenerator.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
Loading…
Reference in a new issue