Skip to content

Commit

Permalink
feat: пребилд ивэнт для проставления хэша/тэга
Browse files Browse the repository at this point in the history
  • Loading branch information
Ann Dyadkova committed Dec 28, 2023
1 parent e9624ae commit 9ab8bb0
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/Unlimotion.AppName/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text;

namespace Unlimotion.BeforeBuild.Helpers;

public static class FileHelper
{
public static string? FindDirectoryWithFile(string rootDirectory, string fileName)
{
var directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.AllDirectories);

return directories.FirstOrDefault(dir => File.Exists(Path.Combine(dir, fileName)));
}

public static void CreateFileInDirectory(string directory, string newFileName, string content)
{
var newFilePath = Path.Combine(directory, newFileName);

using (var fs = File.Create(newFilePath))
{
var info = new UTF8Encoding(true).GetBytes(content);
fs.Write(info, 0, info.Length);
}

Console.WriteLine($"File with partial AppNameDefinitionService is created or overwritten: {newFilePath}");
}
}
43 changes: 43 additions & 0 deletions src/Unlimotion.AppName/Helpers/GitHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text;
using LibGit2Sharp;

namespace Unlimotion.AppName.Helpers;

public static class GitHelper
{
public static string? GetCurrentBranchNameWithShortHash()
{
var gitDirectory = FindGitRoot(Environment.CurrentDirectory);

if (gitDirectory == null)
return null;

using var repo = new Repository(gitDirectory);

var sb = new StringBuilder();
sb.Append('[');
sb.Append(repo.Head.FriendlyName);
sb.Append(" -> ");
sb.Append(repo.Head.Tip.Sha.Substring(0, 8));
if (repo.RetrieveStatus().IsDirty)
sb.Append('*');
sb.Append(']');

return sb.ToString();
}

public static string? FindGitRoot(string startDirectory)
{
var directoryInfo = new DirectoryInfo(startDirectory);

while (directoryInfo != null)
{
if (Directory.Exists(Path.Combine(directoryInfo.FullName, ".git")))
return directoryInfo.FullName;

directoryInfo = directoryInfo.Parent;
}

return null; // Каталог ".git" не найден в иерархии каталогов
}
}
66 changes: 66 additions & 0 deletions src/Unlimotion.AppName/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// See https://aka.ms/new-console-template for more information
using System.Text;
using Unlimotion.AppName.Helpers;
using Unlimotion.BeforeBuild.Helpers;

const string defaultAppName = "Unlimotion";
const string partialAppNameServiceFileName = "AppNameDefinitionService.cs";
const string solutionDirectoryName = "src";
const string defaultErrorMessage = "Failed to initialize application name, because";
const string generatedPartialAppServiceName = $"{partialAppNameServiceFileName}.pre-build.cs";
string? appFullName;

SetAppFullName();

var solutionPath = GetSolutionPath(Environment.CurrentDirectory);

if (solutionPath == null)
{
Console.WriteLine($"{defaultErrorMessage} could not find solution directory");
return;
}

var appNameServicePath = FileHelper.FindDirectoryWithFile(solutionPath, partialAppNameServiceFileName);

if (appNameServicePath == null)
{
Console.WriteLine($"{defaultErrorMessage} file ${partialAppNameServiceFileName} could not be found");
return;
}

FileHelper.CreateFileInDirectory(appNameServicePath, generatedPartialAppServiceName, GenerateCodeForPartialService());

void SetAppFullName()
{
string? additionalAppName;
#if GITHUB_ACTIONS
additionalAppName = Ennvironment.GetVariable("GITHUB_REF_NAME");
#else
additionalAppName = GitHelper.GetCurrentBranchNameWithShortHash();
#endif
appFullName = $"{defaultAppName} {additionalAppName}";
}

string? GetSolutionPath(string path)
{
var index = path.IndexOf(solutionDirectoryName, StringComparison.Ordinal);
return index != -1
? path[..(index + solutionDirectoryName.Length)]
: null;
}

string GenerateCodeForPartialService()
{
var sb = new StringBuilder();
sb.AppendLine(@"// <auto-generated. pre-build event.>");
sb.AppendLine("using Unlimotion.ViewModel;");
sb.AppendLine("namespace Unlimotion.Services");
sb.AppendLine("{");
sb.AppendLine(" public partial class AppNameDefinitionService");
sb.AppendLine(" {");
sb.AppendLine($" private string AppName = \"{appFullName}\";");
sb.AppendLine(" }");
sb.AppendLine("}");

return sb.ToString();
}
22 changes: 22 additions & 0 deletions src/Unlimotion.AppName/Unlimotion.AppName.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp" Version="0.29.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="builds\" />
<Folder Include="builds\linux-x64\" />
<Folder Include="builds\osx-x64\" />
<Folder Include="builds\win-x64\" />
</ItemGroup>

</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@
<LinuxPath>/usr/bin/Unlimotion</LinuxPath>
</Content>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="BeforeCompile">
<Message Importance="high" Text="BeforeBuild target is starting..." />
<Exec Command="..\Unlimotion.AppName\builds\linux-x64\Unlimotion.AppName" />
</Target>
</Project>
4 changes: 4 additions & 0 deletions src/Unlimotion.Desktop/Unlimotion.Desktop.ForMacBuild.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@
<ItemGroup>
<PackageReference Include="Dotnet.Bundle" Version="*" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="BeforeCompile">
<Message Importance="high" Text="BeforeBuild target is starting..." />
<Exec Command="..\Unlimotion.AppName\builds\osx-x64\Unlimotion.AppName" />
</Target>
</Project>
4 changes: 4 additions & 0 deletions src/Unlimotion.Desktop/Unlimotion.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
<None Remove=".gitignore" />
<None Remove="Assets\Unlimotion.ico" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="BeforeCompile">
<Message Importance="high" Text="BeforeBuild target is starting..." />
<Exec Command="..\Unlimotion.AppName\builds\win-x64\Unlimotion.AppName.exe" />
</Target>
</Project>
10 changes: 5 additions & 5 deletions src/Unlimotion.sln
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unlimotion.iOS", "Unlimotio
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unlimotion.Browser", "Unlimotion.Browser\Unlimotion.Browser.csproj", "{08B3C356-E89E-4353-9B05-647B44B85F0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unlimotion.BeforeBuild", "Unlimotion.BeforeBuild\Unlimotion.BeforeBuild.csproj", "{C22182D6-5F66-46CC-8D48-8EF15184269E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unlimotion.AppName", "Unlimotion.AppName\Unlimotion.AppName.csproj", "{6D3598A3-DAB8-45A1-A82D-4D975BBB3E1F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -108,10 +108,10 @@ Global
{08B3C356-E89E-4353-9B05-647B44B85F0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08B3C356-E89E-4353-9B05-647B44B85F0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08B3C356-E89E-4353-9B05-647B44B85F0F}.Release|Any CPU.Build.0 = Release|Any CPU
{C22182D6-5F66-46CC-8D48-8EF15184269E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C22182D6-5F66-46CC-8D48-8EF15184269E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C22182D6-5F66-46CC-8D48-8EF15184269E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C22182D6-5F66-46CC-8D48-8EF15184269E}.Release|Any CPU.Build.0 = Release|Any CPU
{6D3598A3-DAB8-45A1-A82D-4D975BBB3E1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D3598A3-DAB8-45A1-A82D-4D975BBB3E1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D3598A3-DAB8-45A1-A82D-4D975BBB3E1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D3598A3-DAB8-45A1-A82D-4D975BBB3E1F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 0 additions & 6 deletions src/Unlimotion/BeforeBuild.targets

This file was deleted.

1 change: 0 additions & 1 deletion src/Unlimotion/Unlimotion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@
<ItemGroup>
<Compile Include="./Services/AppNameDefinitionService.cs.pre-build.cs" />
</ItemGroup>
<Import Project="./BeforeBuild.targets" />
</Project>

0 comments on commit 9ab8bb0

Please sign in to comment.