Skip to content
Snippets Groups Projects
Commit e0054cd1 authored by ezsh's avatar ezsh
Browse files

MSBuild based works with Git

The MSBbuild is on par with the Makefile: it locates Git, inserts commit
tag, resets the file (all is done if git is found).
parent 96c48bfc
No related branches found
No related tags found
1 merge request!6699MSBuild project improvements
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Task to concat files injecting their names in between -->
<UsingTask TaskName="ConcatFiles" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Inputs ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<BaseDir ParameterType="System.String" Required="true" />
<Output ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Code Type="Fragment" Language="cs"><![CDATA[
using (StreamWriter output = new StreamWriter(Output)) {
int bdl = BaseDir.Length;
foreach (ITaskItem item in Inputs) {
string inp = item.GetMetadata("FullPath");
string rp = inp.StartsWith(BaseDir) ? inp.Substring(bdl) : inp; // a simple relative path
output.WriteLine("/* Source: {0} */", rp);
output.Write(File.ReadAllText(inp));
output.WriteLine();
}
}
]]></Code>
</Task>
</UsingTask>
<!-- https://stackoverflow.com/questions/7837644/how-to-replace-string-in-file-using-msbuild -->
<UsingTask TaskName="ReplaceFileText" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<OutputFilename ParameterType="System.String" Required="true" />
<MatchExpression ParameterType="System.String" Required="true" />
<ReplacementText ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
File.WriteAllText(
OutputFilename,
Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
);
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="GetToolPath" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<ToolName Required="true" />
<LocalPath />
<ToolPath Output="true" />
<ToolFound ParameterType="System.Boolean" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="Microsoft.Win32.Registry" />
<Code Type="Class" Language="cs" Source="$(MSBuildThisFileDirectory)FindToolPath.cs" />
</Task>
</UsingTask>
<Target Name="FindGitWindows" Condition=" '$([MSBuild]::IsOsPlatform(Windows))' ">
<GetToolPath ToolName="git">
<Output TaskParameter="ToolPath" PropertyName="GitExe"/>
<Output TaskParameter="ToolFound" PropertyName="GitExeFound"/>
</GetToolPath>
</Target>
<Target Name="FindGitUnix" Condition=" '$([MSBuild]::IsOSUnixLike())' " >
<PropertyGroup>
<GitExe>git</GitExe>
</PropertyGroup>
<Exec Command="$(GitExe) help > /dev/null" IgnoreExitCode="True">
<Output TaskParameter="ExitCode" PropertyName="GitExeExitCode" />
</Exec>
<PropertyGroup>
<GitExeFound Condition=" '$(GitExeExitCode)' == '0' " >true</GitExeFound>
<GitExeFound Condition=" '$(GitExeExitCode)' != '0' " >false</GitExeFound>
</PropertyGroup>
</Target>
<Target Name="FindGit" DependsOnTargets="FindGitWindows;FindGitUnix" />
</Project>
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Win32;
/*
Copyright (c) 2014, LoreSoft
This class was taken from https://github.com/loresoft/msbuildtasks/ and is licensed under BSD-2 license
*/
internal static class ToolPathUtil
{
public static bool SafeFileExists(string path, string toolName)
{
return SafeFileExists(Path.Combine(path, toolName));
}
public static bool SafeFileExists(string file)
{
Console.WriteLine(String.Format("Testing {0}", file));
try { return File.Exists(file); }
catch { } // eat exception
return false;
}
public static string MakeToolName(string name)
{
return (Environment.OSVersion.Platform == PlatformID.Unix) ?
name : name + ".exe";
}
public static string FindInRegistry(string toolName)
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + toolName, false);
if (key != null)
{
string possiblePath = key.GetValue(null) as string;
if (SafeFileExists(possiblePath))
return Path.GetDirectoryName(possiblePath);
}
}
catch (System.Security.SecurityException) { }
return null;
}
public static string FindInPath(string toolName)
{
string pathEnvironmentVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
string[] paths = pathEnvironmentVariable.Split(new[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
foreach (string path in paths)
{
if (SafeFileExists(path, toolName))
{
return path;
}
}
return null;
}
public static string FindInProgramFiles(string toolName, params string[] commonLocations)
{
foreach (string location in commonLocations)
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), location);
if (SafeFileExists(path, toolName))
{
return path;
}
}
return null;
}
public static string FindInLocalPath(string toolName, string localPath)
{
if (localPath == null)
return null;
string path = new DirectoryInfo(localPath).FullName;
if (SafeFileExists(localPath, toolName))
{
return path;
}
return null;
}
}
public class GetToolPath : Microsoft.Build.Utilities.Task
{
public virtual string ToolName { get; set; }
public virtual string LocalPath { get; set; }
public virtual string ToolPath { get; set; }
public virtual bool Success { get; set; }
public override bool Execute()
{
this.ToolPath = null;
this.Success = false;
string tool = ToolPathUtil.MakeToolName(ToolName);
string toolDir =
ToolPathUtil.FindInPath(tool) ??
ToolPathUtil.FindInRegistry(tool) ??
ToolPathUtil.FindInProgramFiles(tool, tool) ??
ToolPathUtil.FindInProgramFiles(tool, Path.Combine(tool, "bin")) ??
ToolPathUtil.FindInLocalPath(tool, LocalPath);
if (toolDir == null)
{
throw new Exception(String.Format("Could not find {0}. Looked in PATH locations and various common folders inside Program Files as well as LocalPath.", tool));
}
Console.WriteLine("Found {0} at {1}", tool, toolDir);
ToolPath = Path.Combine(toolDir, tool);
Success = true;
return Success;
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="FinalHTML" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="devTools\FC.targets" />
<PropertyGroup>
<OutputDirectory>.\bin\</OutputDirectory>
<JSMoudleFileName>fc.js</JSMoudleFileName>
......@@ -14,18 +15,28 @@
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(Windows))' ">
<TweeGoExe>tweego_win$(ArchitectureSuffix).exe</TweeGoExe>
</PropertyGroup>
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(Linux))' ">
<TweeGoExe>tweego_nix$(ArchitectureSuffix)</TweeGoExe>
</PropertyGroup>
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(OSX))' ">
<TweeGoExe>tweego_osx$(ArchitectureSuffix)</TweeGoExe>
</PropertyGroup>
<Target Name="DisplayMessages">
</PropertyGroup>
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(Linux))' ">
<TweeGoExe>tweego_nix$(ArchitectureSuffix)</TweeGoExe>
</PropertyGroup>
<PropertyGroup Condition=" '$([MSBuild]::IsOsPlatform(OSX))' ">
<TweeGoExe>tweego_osx$(ArchitectureSuffix)</TweeGoExe>
</PropertyGroup>
<Target Name="CollectGitInfo" DependsOnTargets="FindGit">
<Exec Command="$(GitExe) rev-parse --short HEAD" ConsoleToMsBuild="true" EchoOff="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitHash" />
</Exec>
</Target>
<Target Name="DisplayMessages" DependsOnTargets="CollectGitInfo">
<Message Text="MSBuildProjectDirectory: $(MSBuildProjectDirectory)" />
<Message Text="ArchitectureSuffix: $(ArchitectureSuffix)" Importance="high" />
<Message Text="TweeGoExe: $(TweeGoExe)" Importance="high" />
<Message Text="PA: $([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)" />
<Message Text="Git found: $(GitExeFound)" Importance="high" />
<Message Text="Git executable: $(GitExe)" Importance="high" />
<Message Text="HEAD commit hash: $(GitHash)" Importance="high" />
</Target>
<Target Name="createDirs">
......@@ -34,23 +45,35 @@
<Target Name="JSModule" DependsOnTargets="createDirs;DisplayMessages">
<ItemGroup>
<jsScripts Include=".\js\**\*.js"></jsScripts>
<jsScripts Include=".\js\**\*.js"></jsScripts> <!-- will be sorted aphabetically -->
</ItemGroup>
<ConcatFiles Inputs="@(jsSCripts)" BaseDir="$(MSBuildProjectDirectory)" Output="$(OutputDirectory)\$(JSMoudleFileName)"/>
</Target>
<!-- Read the contents of the files preserving tabs/spaces. -->
<ItemGroup>
<FileContents Include="$([System.IO.File]::ReadAllText(%(jsSCripts.FullPath)))"/>
</ItemGroup>
<Target Name="AlphaDisclaimer" DependsOnTargets="CollectGitInfo" Condition=" '$(GitExeFound)'" >
<ReplaceFileText
InputFilename="$(MSBuildProjectDirectory)\src\gui\mainMenu\AlphaDisclaimer.tw"
OutputFilename="$(MSBuildProjectDirectory)\src\gui\mainMenu\AlphaDisclaimer.tw"
MatchExpression="(build: \$releaseID)"
ReplacementText="$1, git commit: $(GitHash)"
/>
</Target>
<WriteLinesToFile File="$(OutputDirectory)\$(JSMoudleFileName)" Lines="@(FileContents)" Overwrite="true" />
<Target Name="ResetAlphaDisclaimer" DependsOnTargets="FindGit" Condition=" '$(GitExeFound)'" >
<Exec Command="$(GitExe) checkout -- src/gui/mainMenu/AlphaDisclaimer.tw" />
</Target>
<Target Name="tmpOutput" DependsOnTargets="JSModule">
<Target Name="tmpOutput" DependsOnTargets="JSModule;AlphaDisclaimer">
<Exec Command="devTools\tweeGo\$(TweeGoExe) --module=$(OutputDirectory)\$(JSMoudleFileName) --head devTools\head.html -o $(OutputDirectory)\tmp.html src\" />
</Target>
<Target Name="FinalHTML" DependsOnTargets="tmpOutput">
<Target Name="FinalHTML" DependsOnTargets="tmpOutput;ResetAlphaDisclaimer">
<Delete Files="$(OutputDirectory)\$(JSMoudleFileName)" />
<Move SourceFiles="$(OutputDirectory)\tmp.html" DestinationFiles="$(OutputDirectory)\FC_pregmod.html" />
</Target>
<Target Name="JavaSanityCheck">
<Exec Command="java -jar .\devTools\javaSanityCheck\SanityCheck.jar" />
</Target>
</Project>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment