Skip to content

Instantly share code, notes, and snippets.

@trcio
Created August 17, 2014 06:54
Show Gist options
  • Save trcio/ab535622c7c3b82f3c9a to your computer and use it in GitHub Desktop.
Save trcio/ab535622c7c3b82f3c9a to your computer and use it in GitHub Desktop.
Small, easy CodeDom wrapper
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Resources;
using System.Text;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
namespace PCompile
{
public enum PCompilerLanguage
{
VisualBasic,
CSharp
}
public enum PCompilerTarget
{
AppContainer,
Console,
Library,
Module,
WindowsForms
}
public enum PCompilerVersion
{
V2,
V3,
V35,
V4,
V45
}
public enum PCompilerPlatform
{
AnyCpu,
AnyCpu32BitPreferred,
Arm,
X64,
X86,
Itanium
}
public static class PCompiler
{
private static readonly Dictionary<object, string> StringValues;
private static readonly Dictionary<object, Dictionary<string, string>> ProviderOptions;
static PCompiler()
{
StringValues = new Dictionary<object, string>
{
{PCompilerTarget.AppContainer, "appcontainerexe"},
{PCompilerTarget.Console, "exe"},
{PCompilerTarget.Library, "library"},
{PCompilerTarget.Module, "module"},
{PCompilerTarget.WindowsForms, "winexe"},
};
ProviderOptions = new Dictionary<object, Dictionary<string, string>>
{
{PCompilerVersion.V2, new Dictionary<string, string> {{"CompilerVersion", "v2.0"}}},
{PCompilerVersion.V3, new Dictionary<string, string> {{"CompilerVersion", "v3.0"}}},
{PCompilerVersion.V35, new Dictionary<string, string> {{"CompilerVersion", "v3.5"}}},
{PCompilerVersion.V4, new Dictionary<string, string> {{"CompilerVersion", "v4.0"}}},
{PCompilerVersion.V45, new Dictionary<string, string> {{"CompilerVersion", "v4.5"}}}
};
}
public static PCompilerResults Compile(PCompilerSettings settings)
{
if (settings.IsNull())
throw new ArgumentNullException("settings", @"No property in 'settings' can be null");
if (settings.Resources.Length > 0)
{
var writer = new ResourceWriter(settings.ResourceFileName);
Array.ForEach(settings.Resources, r => writer.AddResource(r.Name, r.Data));
}
var compileParams = new CompilerParameters {OutputAssembly = settings.OutputFile, GenerateExecutable = true};
compileParams.ReferencedAssemblies.AddRange(settings.References);
compileParams.EmbeddedResources.Add(settings.ResourceFileName);
var options = new StringBuilder("/warnaserror+ /unsafe");
options.AppendFormat(" /win32icon:\"{0}\"", settings.Icon);
options.AppendFormat(" /target:{0}", StringValues[settings.Target]);
options.AppendFormat(" /platform:{0}", settings.Platform.ToString().ToLower());
compileParams.CompilerOptions = options.ToString();
CompilerResults results;
if (settings.Language == PCompilerLanguage.CSharp)
results =
new CSharpCodeProvider(ProviderOptions[settings.DotNetVersion]).CompileAssemblyFromSource(
compileParams, settings.SourceFiles);
else
results =
new VBCodeProvider(ProviderOptions[settings.DotNetVersion]).CompileAssemblyFromSource(
compileParams, settings.SourceFiles);
results.TempFiles.Delete();
return new PCompilerResults(!results.Errors.HasErrors, results.Errors);
}
}
public class PCompilerResults
{
public bool Success { get; private set; }
public CompilerErrorCollection Errors { get; private set; }
public PCompilerResults(bool success, CompilerErrorCollection errors)
{
Success = success;
Errors = errors;
}
}
public class PCompilerSettings
{
public PCompilerLanguage Language { get; set; }
public PCompilerTarget Target { get; set; }
public PCompilerVersion DotNetVersion { get; set; }
public PCompilerPlatform Platform { get; set; }
public PCompilerResource[] Resources { get; set; }
public string[] References { get; set; }
public string[] SourceFiles { get; set; }
public string OutputFile { get; set; }
public string ResourceFileName { get; set; }
public string CompilerOptions { get; set; }
public string Icon { get; set; }
public bool IsNull()
{
return Resources == null || References == null || SourceFiles == null ||
string.IsNullOrWhiteSpace(OutputFile) ||
string.IsNullOrWhiteSpace(ResourceFileName) ||
string.IsNullOrWhiteSpace(CompilerOptions) || string.IsNullOrWhiteSpace(Icon);
}
}
public class PCompilerResource
{
public string Name { get; set; }
public byte[] Data { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment