Skip to content

Instantly share code, notes, and snippets.

@TheVice
Last active November 22, 2018 14:38
Show Gist options
  • Save TheVice/d635167fb937cc899e340feb4a968289 to your computer and use it in GitHub Desktop.
Save TheVice/d635167fb937cc899e340feb4a968289 to your computer and use it in GitHub Desktop.
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 https://github.com/TheVice/
*
*/
using System;
using System.IO;
#if !NET20
using System.Resources;
#else
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Build.Tasks.Windows;
#endif
namespace Utils
{
public class BinaryIntoResources
{
public static void Main(string[] args)
{
if (1 < args.Length)
{
#if NET20
ResourcesGenerator resourcesGenerator = new ResourcesGenerator();
resourcesGenerator.BuildEngine = new InternalEngine();
TaskItem[] taskItems = new TaskItem[args.Length - 1];
for (int i = 0; i < args.Length - 1; ++i)
{
taskItems[i] = new TaskItem(args[i]);
}
resourcesGenerator.ResourceFiles = taskItems;
resourcesGenerator.OutputPath = Path.GetDirectoryName(args[args.Length - 1]);
resourcesGenerator.OutputResourcesFile = new ITaskItem[] { new TaskItem(args[args.Length - 1]) };
resourcesGenerator.Execute();
#else
using (var resourceWriter = new ResourceWriter(args[args.Length - 1]))
{
var baseUri = new Uri("file://foo/");
var output = Path.GetDirectoryName(args[args.Length - 1]);
for (var i = 0; i < args.Length - 1; ++i)
{
var input = args[i];
var resourceKey = input.Contains(output) ?
input.Substring(input.IndexOf(output) + output.Length + 1) :
Path.GetFileName(input);
var uri = new Uri(baseUri, resourceKey);
resourceKey = uri.GetComponents(UriComponents.Path, UriFormat.UriEscaped);
resourceKey = resourceKey.ToLower();
using (var fileReader = new FileStream(input, FileMode.Open, FileAccess.Read))
{
var array = new byte[fileReader.Length];
fileReader.Read(array, 0, array.Length);
resourceWriter.AddResource(resourceKey, new MemoryStream(array));
}
}
resourceWriter.Generate();
}
#endif
}
}
}
}
:: The MIT License (MIT)
:: Copyright (c) 2018 https://github.com/TheVice/
@echo off
set CSC.EXE=
if exist "%ProgramFiles(x86)%\MSBuild\14.0\Bin\csc.exe" set CSC.EXE="%ProgramFiles(x86)%\MSBuild\14.0\Bin\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc_4_plus
if exist "%ProgramFiles%\MSBuild\14.0\Bin\csc.exe" set CSC.EXE="%ProgramFiles%\MSBuild\14.0\Bin\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc_4_plus
if exist "%ProgramFiles(x86)%\MSBuild\12.0\Bin\csc.exe" set CSC.EXE="%ProgramFiles(x86)%\MSBuild\12.0\Bin\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc_4_plus
if exist "%ProgramFiles%\MSBuild\12.0\Bin\csc.exe" set CSC.EXE="%ProgramFiles%\MSBuild\12.0\Bin\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc_4_plus
if exist "%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe" set CSC.EXE="%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc_4_plus
if exist "%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\csc.exe" set CSC.EXE="%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc_4_plus
if exist "%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe" set CSC.EXE="%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc
if exist "%SystemRoot%\Microsoft.NET\Framework64\v3.5\csc.exe" set CSC.EXE="%SystemRoot%\Microsoft.NET\Framework64\v3.5\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc
if exist "%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\csc.exe" set CSC.EXE="%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc
if exist "%SystemRoot%\Microsoft.NET\Framework64\2.0.50727\csc.exe" set CSC.EXE="%SystemRoot%\Microsoft.NET\Framework64\2.0.50727\csc.exe"
if [%CSC.EXE%] NEQ [] goto founded_csc
goto exit_build
:founded_csc_4_plus
if ["%ProgramFiles(x86)%"] == [""] goto x86-net40
set REFERENCES=
if exist "%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Build.Framework.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Build.Framework.dll"
if exist "%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Build.Utilities.v4.0.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Build.Utilities.v4.0.dll"
if exist "%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationBuildTasks.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationBuildTasks.dll"
set REFERENCES_XML=
if exist "%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\System.XML.dll" set REFERENCES_XML=%REFERENCES_XML%,"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\System.XML.dll"
goto build
:x86-net40
set REFERENCES=
if exist "%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Framework.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Framework.dll"
if exist "%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Utilities.v4.0.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Utilities.v4.0.dll"
if exist "%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationBuildTasks.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationBuildTasks.dll"
set REFERENCES_XML=
if exist "%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\System.XML.dll" set REFERENCES_XML=%REFERENCES_XML%,"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\System.XML.dll"
:build
if not exist "%~dp0bin" mkdir "%~dp0bin"
%CSC.EXE% /out:"%~dp0bin\BinaryIntoResources.exe" "%~dp0BinaryIntoResources.cs"
%CSC.EXE% /out:"%~dp0bin\MarkupCompilePass.exe" "%~dp0MarkupCompilePass.cs" "%~dp0InternalEngine.cs" /r:%REFERENCES%,%REFERENCES_XML%
goto exit_build
:founded_csc
set REFERENCES=
if exist "%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Build.Framework.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Build.Framework.dll"
if exist "%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Build.Utilities.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Build.Utilities.dll"
if exist "%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" set REFERENCES=%REFERENCES%,"%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll"
set REFERENCES_XML=
if exist "%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\System.XML.dll" set REFERENCES_XML=%REFERENCES_XML%,"%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\System.XML.dll"
if ["%ProgramFiles(x86)%"] NEQ [""] goto x86-references_used
set REFERENCES=
if exist "%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\Microsoft.Build.Framework.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\Microsoft.Build.Framework.dll"
if exist "%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\Microsoft.Build.Utilities.dll" set REFERENCES=%REFERENCES%,"%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\Microsoft.Build.Utilities.dll"
if exist "%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" set REFERENCES=%REFERENCES%,"%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll"
set REFERENCES_XML=
if exist "%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\System.XML.dll" set REFERENCES_XML=%REFERENCES_XML%,"%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\System.XML.dll"
:x86-references_used
if not exist "%~dp0bin" mkdir "%~dp0bin"
%CSC.EXE% /out:"%~dp0bin\BinaryIntoResources.exe" "%~dp0BinaryIntoResources.cs" "%~dp0InternalEngine.cs" /D:NET20 /r:%REFERENCES%
%CSC.EXE% /out:"%~dp0bin\MarkupCompilePass.exe" "%~dp0MarkupCompilePass.cs" "%~dp0InternalEngine.cs" /D:NET20 /r:%REFERENCES%,%REFERENCES_XML%
:exit_build
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 https://github.com/TheVice/
*
*/
using System;
using System.Collections;
using Microsoft.Build.Framework;
using System.Collections.Generic;
namespace Utils
{
internal class InternalEngine : IBuildEngine
{
IList<BuildMessageEventArgs> logMessageEvents;
IList<BuildWarningEventArgs> logWarningEvent;
IList<BuildErrorEventArgs> logErrorEvents;
public InternalEngine()
{
logMessageEvents = new List<BuildMessageEventArgs>();
logWarningEvent = new List<BuildWarningEventArgs>();
logErrorEvents = new List<BuildErrorEventArgs>();
}
public int ColumnNumberOfTaskNode
{
get
{
throw new NotImplementedException();
}
}
public bool ContinueOnError
{
get
{
throw new NotImplementedException();
}
}
public int LineNumberOfTaskNode
{
get
{
throw new NotImplementedException();
}
}
public string ProjectFileOfTaskNode
{
get
{
throw new NotImplementedException();
}
}
public bool BuildProjectFile(string projectFileName, string[] targetNames, IDictionary globalProperties, IDictionary targetOutputs)
{
throw new NotImplementedException();
}
public void LogCustomEvent(CustomBuildEventArgs e)
{
throw new NotImplementedException();
}
public void LogErrorEvent(BuildErrorEventArgs e)
{
logErrorEvents.Add(e);
}
public void LogMessageEvent(BuildMessageEventArgs e)
{
logMessageEvents.Add(e);
}
public void LogWarningEvent(BuildWarningEventArgs e)
{
logWarningEvent.Add(e);
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 https://github.com/TheVice/
*
*/
using System;
using System.IO;
using System.Xml;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks.Windows;
namespace Utils
{
class MarkupCompilePass
{
public static void Main(string[] args)
{
if (args.Length < 2) return;
#if NET20
string pathToDotNetFramework = ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version20);
string pathToReference = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
if (string.IsNullOrEmpty(pathToReference))
pathToReference = Environment.GetEnvironmentVariable("ProgramFiles");
if (string.IsNullOrEmpty(pathToReference))
pathToReference = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
pathToReference = Path.Combine(pathToReference, string.Format("Reference Assemblies{0}Microsoft{0}Framework{0}v3.0", Path.DirectorySeparatorChar));
ITaskItem[] references = new ITaskItem[]
{
new TaskItem(Path.Combine(pathToDotNetFramework, "mscorlib.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "System.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "System.Xml.dll")),
new TaskItem(Path.Combine(pathToReference, "WindowsBase.dll")),
new TaskItem(Path.Combine(pathToReference, "ReachFramework.dll")),
new TaskItem(Path.Combine(pathToReference, "System.Printing.dll")),
new TaskItem(Path.Combine(pathToReference, "PresentationCore.dll")),
new TaskItem(Path.Combine(pathToReference, "PresentationFramework.dll"))
};
#else
string pathToDotNetFramework = ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version40);
ITaskItem[] references = new ITaskItem[]
{
new TaskItem(Path.Combine(pathToDotNetFramework, "mscorlib.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "System.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "System.Xml.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "System.Xaml.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "WPF", "WindowsBase.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "WPF", "ReachFramework.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "WPF", "System.Printing.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "WPF", "PresentationCore.dll")),
new TaskItem(Path.Combine(pathToDotNetFramework, "WPF", "PresentationFramework.dll"))
};
#endif
string additionReference = string.Empty;
if (3 < args.Length)
{
string ext = Path.GetExtension(args[args.Length - 3]).ToLower();
if (".exe" == ext || ".dll" == ext)
{
additionReference = args[args.Length - 3];
}
}
int count = args.Length - (string.IsNullOrEmpty(additionReference) ? 2 : 3);
int aCount = 0;
int pCount = 0;
ITaskItem[] applicationsCandidates = new ITaskItem[count];
ITaskItem[] pagesCandidates = new ITaskItem[count];
for (int i = 0; i < count; i++)
{
bool isApplication = false;
using (FileStream fileStream = new FileStream(args[i], FileMode.Open))
{
using (XmlReader xmlReader = XmlReader.Create(fileStream))
{
while (xmlReader.Read())
{
isApplication = (xmlReader.Depth == 0 &&
xmlReader.NodeType == XmlNodeType.Element &&
xmlReader.Name == "Application");
if (isApplication || xmlReader.Depth > 0) break;
}
}
}
if (isApplication)
{
applicationsCandidates[aCount] = new TaskItem(args[i]);
aCount++;
}
else
{
pagesCandidates[pCount] = new TaskItem(args[i]);
pCount++;
}
}
ITaskItem[] applications = ((aCount > 0) ? new ITaskItem[aCount] : null);
ITaskItem[] pages = ((pCount > 0 ) ? new ITaskItem[pCount] : null);
if (aCount > 0)
{
Array.Copy(applicationsCandidates, applications, aCount);
}
if (pCount > 0)
{
Array.Copy(pagesCandidates, pages, pCount);
}
MarkupCompilePass1 markupCompilePass1 = new MarkupCompilePass1();
markupCompilePass1.Language = "C#";
markupCompilePass1.BuildEngine = new InternalEngine();
markupCompilePass1.References = references;
markupCompilePass1.ApplicationMarkup = applications;
markupCompilePass1.PageMarkup = pages;
markupCompilePass1.AssemblyName = args[args.Length - 2];
markupCompilePass1.OutputPath = args[args.Length - 1];
bool isExecute = markupCompilePass1.Execute();
if (isExecute && !string.IsNullOrEmpty(additionReference) && markupCompilePass1.RequirePass2ForMainAssembly)
{
MarkupCompilePass2 markupCompilePass2 = new MarkupCompilePass2();
markupCompilePass2.Language = markupCompilePass1.Language;
markupCompilePass2.BuildEngine = markupCompilePass1.BuildEngine;
ITaskItem[] referencesEx = new ITaskItem[references.Length + 1];
Array.Copy(references, 0, referencesEx, 1, references.Length);
referencesEx[0] = new TaskItem(additionReference);
markupCompilePass2.References = referencesEx;
markupCompilePass2.AssemblyName = markupCompilePass1.AssemblyName;
markupCompilePass2.OutputPath = markupCompilePass1.OutputPath;
/*isExecute = */markupCompilePass2.Execute();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment