Skip to content

Commit

Permalink
#1 implemented packer to create executable package & msbuild searcher
Browse files Browse the repository at this point in the history
it seems works as well
  • Loading branch information
3F committed Jun 27, 2016
1 parent 980fe96 commit b1cbeba
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ obj
/packages
/compact/gnt-compact.core
/compact/gnt-min.core
/compact/gnt.core
.nupkg
/wrapper/gnt.bat
25 changes: 25 additions & 0 deletions caller/gnt.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@echo off

set gntcore=gnt.core

for %%v in (14.0, 12.0, 15.0, 4.0, 3.5, 2.0) do (
for /F "usebackq tokens=2* skip=2" %%a in (
`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\%%v" /v MSBuildToolsPath 2^> nul`
) do if exist %%b (
set msbuild="%%bmsbuild.exe"
goto found
)
)

echo MSBuild Tools was not found. Please use it manually like: ` "full_path_to_msbuild.exe" %gntcore% arguments` 1>&2

goto exit

:found

echo MSBuild Tools: %msbuild%

%msbuild% gnt.core /nologo /v:m /m:4 %*
REM /noconlog

:exit
3 changes: 3 additions & 0 deletions compress.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

msbuild compact/.compressor /p:core="../gnt.core" /p:output="gnt.core" /nologo /v:m /m:4
23 changes: 23 additions & 0 deletions msbuild.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@echo off

for %%v in (14.0, 12.0, 15.0, 4.0, 3.5, 2.0) do (
for /F "usebackq tokens=2* skip=2" %%a in (
`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\%%v" /v MSBuildToolsPath 2^> nul`
) do if exist %%b (
set msbuild="%%bmsbuild.exe"
goto found
)
)

echo MSBuild Tools was not found. Please use it manually like: ` "full_path_to_msbuild.exe" arguments ` 1>&2

goto exit

:found

echo MSBuild Tools: %msbuild%

%msbuild% %*


:exit
3 changes: 3 additions & 0 deletions packing.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

compress & msbuild wrapper/.packer /p:core="../compact/gnt.core" /p:output="gnt.bat" /nologo /v:m /m:4
172 changes: 172 additions & 0 deletions wrapper/.packer
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2015-2016 Denis Kuzmin (reg) [ [email protected] ]
Distributed under the GetNuTool license
https://github.com/3F/GetNuTool
-->

<!--
To create executable package of the GetNuTool
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- Main settings -->
<PropertyGroup>
<core Condition="'$(core)' == ''">..\gnt.core</core>
<output Condition="'$(output)' == ''">gnt.bat</output>
<maxline Condition="'$(maxline)' == ''">1024</maxline> <!-- length of line / max 2047 or 8191 characters https://support.microsoft.com/en-us/kb/830473 -->
</PropertyGroup>

<!-- Entry point -->
<Target Name="handler" BeforeTargets="Build">
<Packing core="$(core)" output="$(output)" maxline="$(maxline)" />
</Target>

<!-- Tasks settings -->
<PropertyGroup>
<TaskCoreDllPath Condition="Exists('$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll')">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll</TaskCoreDllPath>
<TaskCoreDllPath Condition="'$(TaskCoreDllPath)' == '' and Exists('$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll')">$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll</TaskCoreDllPath>
</PropertyGroup>

<!-- Prepares list for downloader below -->
<UsingTask
TaskName="Packing"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(TaskCoreDllPath)">

<ParameterGroup>
<core ParameterType="System.String" Required="true" />
<output ParameterType="System.String" Required="true" />
<maxline ParameterType="System.Int32" Required="true" />
</ParameterGroup>

<Task>
<Using Namespace="System" />
<Using Namespace="System.Collections.Generic" />
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Text" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Func<char, string> quotes = delegate(char symbol)
{
return String.Format(@"
(?<!\\)
(
{0}(?:
(?:
[^{0}\\]
|
\\\\
|
\\{0}?
)*
){0}
)",
symbol);
};
using(StreamReader reader = new StreamReader(core, System.Text.Encoding.UTF8, true))
{
var content = reader.ReadToEnd();
// First, avoid special chars
// btw, we can use simply: set /P ="..." and escape only `"` as `""`,
// but later we also should replace this double quotes :( that more problem for batch
content = content.Replace("%", "%%");
content = content.Replace("^", "^^");
content = content.Replace("&", "^&");
content = content.Replace("<", "^<");
content = content.Replace(">", "^>");
content = content.Replace("|", "^|");
// Secondly, keep in mind where placed all strings.
// We will work without double quotes, so we should correctly define all pairs of "..." per line
var strings = new Dictionary<int, int>(); // positions: "(start) -> "(end)
var matches = Regex.Matches(content, quotes('"'), RegexOptions.IgnorePatternWhitespace);
foreach(Match m in matches) {
strings[m.Index] = m.Length;
}
// Now, we should split long strings
var lines = new List<string>();
for(int i = 0, len = maxline; i < content.Length; i += len)
{
int rlen = Math.Min(content.Length - i, len);
int rpos = i + rlen;
bool added = false;
foreach(var ifq in strings)
{
// Protect strings that we saw above
if(rpos >= ifq.Key && rpos <= ifq.Key + ifq.Value)
{
int repos = ((ifq.Key + ifq.Value) - rpos);
lines.Add(content.Substring(i, rlen + repos));
i += repos;
added = true;
break;
}
}
if(added) {
continue;
}
// we don't see strings, but we also have escape chars as ^ + char above
int esc = rpos - 1; // the end of the previous line that should be checked on `^` char
if(content.Length > esc && content.ElementAt(esc) == '^') {
rlen += 1;
lines.Add(content.Substring(i, rlen));
i += 1;
continue;
}
// TODO: the line cannot be started with: ` =`, `=`, ` "`, `"`
lines.Add(content.Substring(i, rlen)); // simply add new line without processing
}
// Now we can define, how it should be written into external file
const string corevar = "gntcore";
content = String.Empty;
foreach(var line in lines) {
//content += $"<nul set /P ={line}>> %{corevar}%{Environment.NewLine}";
content += String.Format("<nul set /P ={0}>> %{1}%{2}", line, corevar, Environment.NewLine);
}
// Finally, format script to work with gnt.core
using(StreamReader tplreader = new StreamReader("exec.tpl", System.Text.Encoding.UTF8, true)) {
content = tplreader.ReadToEnd().Replace("$gnt.core.logic$", content).Replace("$tpl.corevar$", corevar);
}
//
using(TextWriter writer = new StreamWriter(output, false, new UTF8Encoding(false))) { // do not use BOM ! it failed with initial signature
writer.Write(content);
}
Console.WriteLine("Executable version of `{0}` has been created -> `{1}`", core, output);
}
]]>
</Code>
</Task>
</UsingTask>

<!-- remap targets -->

<Target Name="Build" DependsOnTargets="handler" />

</Project>
27 changes: 27 additions & 0 deletions wrapper/exec.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@echo off
set gntcore=gnt.core
set $tpl.corevar$=%temp%/%gntcore%

for %%v in (14.0, 12.0, 15.0, 4.0, 3.5, 2.0) do (
for /F "usebackq tokens=2* skip=2" %%a in (
`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\%%v" /v MSBuildToolsPath 2^> nul`
) do if exist %%b (
set msbuild="%%bmsbuild.exe"
goto found
)
)

echo MSBuild was not found. Please use it manually like: ` "full_path_to_msbuild.exe" %gntcore% arguments ` 1>&2

goto exit

:found

<nul set /P ="">%$tpl.corevar$%
$gnt.core.logic$


%msbuild% %$tpl.corevar$% %* /nologo /verbosity:m


:exit

0 comments on commit b1cbeba

Please sign in to comment.