Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created June 15, 2017 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncansmart/bd2eadca99b607238d844247603b848c to your computer and use it in GitHub Desktop.
Save duncansmart/bd2eadca99b607238d844247603b848c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace csc_net46
{
class Program
{
// to install: copy to "packages\Microsoft.Net.Compilers.x.y.z\tools" and run
static int Main(string[] args)
{
var cd = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!args.Any())
{
// install
if (!File.Exists(Path.Combine(cd, "csc-orig.exe")))
{
Console.WriteLine("Renaming csc.exe to csc-orig.exe ...");
File.Move(Path.Combine(cd, "csc.exe"), Path.Combine(cd, "csc-orig.exe"));
}
if (!File.Exists(Path.Combine(cd, "csc-orig.exe.config")))
{
Console.WriteLine("Renaming csc.exe.config to csc-orig.exe.config ...");
File.Move(Path.Combine(cd, "csc.exe.config"), Path.Combine(cd, "csc-orig.exe.config"));
}
Console.WriteLine($"Replacing csc.exe with {Path.GetFileName(Assembly.GetExecutingAssembly().Location)} ...");
File.Copy(Assembly.GetExecutingAssembly().Location, Path.Combine(cd, "csc.exe"), overwrite: true);
if (!File.Exists(Path.Combine(cd, "mscorlib-46.dll")))
Console.WriteLine("Warning: mscorlib-46.dll not found in directory");
Console.WriteLine("Installed.");
Console.ReadKey(true);
return 0;
}
else
{
// find '@C:\foo\blah.cmdline' file
var cmdlinefilearg = args.FirstOrDefault(a => a.StartsWith("@"));
if (cmdlinefilearg != null)
{
var cmdline = File.ReadAllText(cmdlinefilearg.Substring(1));
// cmdline looks like:
// /t:library /utf8output /nostdlib+ /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll"
// ... /D:DEBUG /debug+ /optimize- /warnaserror- /w:4 /nowarn:1659;1699;1701;612;618 /langversion:7 /nowarn:1659;1699;1701
// ... "C:\Users\duncan\AppData\Local\Temp\Temporary ASP.NET Files\vs\e3b3eac8\d85b4372\App_global.asax.rpag7uhc.0.cs" "C:\Users\duncan\AppData\Local\Temp\Temporary ASP.NET Files\vs\e3b3eac8\d85b4372\App_global.asax.rpag7uhc.1.cs"
// Replace reference to mscorlib with our 4.6 copy
cmdline = Regex.Replace(cmdline,
@"/R:""[^""]+mscorlib\.dll""",
@"/R:""" + cd + @"\mscorlib-46.dll""",
RegexOptions.IgnoreCase);
File.WriteAllText(cmdlinefilearg.Substring(1), cmdline);
}
// run orig
var p = Process.Start(new ProcessStartInfo
{
FileName = "csc-orig.exe",
Arguments = string.Join(" ", args.Select(a => a.Contains(' ') ? '"' + a + '"' : a)),
UseShellExecute = false,
});
p.WaitForExit();
return p.ExitCode;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment