-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: пребилд ивэнт для проставления хэша/тэга
- Loading branch information
Ann Dyadkova
committed
Dec 28, 2023
1 parent
e9624ae
commit 9ab8bb0
Showing
13 changed files
with
174 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" не найден в иерархии каталогов | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters