Skip to content

Commit

Permalink
improvement: refactor code for automatic builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalle Minkner committed Nov 11, 2024
1 parent f3115bf commit cafb3a8
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 22 deletions.
106 changes: 106 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: Build Release

env:
REPOSITORY_FOLDER: "src" # subfolder with source code
PLUGIN_NAME: "NativeMapVote" # main file of the project

on:
push:
branches:
- main
paths:
- 'src/Version.cs' # only on change of the file with the version number string in it
workflow_dispatch:

# job to create a new release
jobs:
create-release:
runs-on: ubuntu-latest

steps:
- name: Get Repository Name
run: echo "REPOSITORY_NAME=`basename ${{ github.repository }}`" >> $GITHUB_ENV

- name: Checkout Repository
uses: actions/checkout@v2

- name: Get Current Project Version Number From Version.cs
id: get_version
run: |
version=$(grep -oP '(?<=ModuleVersion => ")[^"]*' ${{ env.REPOSITORY_FOLDER }}/Version.cs)
echo "version=$version" >> $GITHUB_ENV
- name: Get Current Release Version Number
id: get_latest_release
run: |
latest_release=$(curl -s -H "Authorization: token ${{ secrets.UPLOAD_TOKEN }}" https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .tag_name)
echo "latest_release=$latest_release" >> $GITHUB_ENV
- name: Compare Versions
id: compare_versions
run: |
if [ "${{ env.version }}" != "${{ env.latest_release }}" ]; then
echo "new_version=true" >> $GITHUB_ENV
else
echo "new_version=false" >> $GITHUB_ENV
fi
- name: Install .NET
uses: actions/setup-dotnet@v2
if: env.new_version == 'true'
with:
dotnet-version: '8.0.x'

- name: Install Dependencies
if: env.new_version == 'true'
run: dotnet restore

- name: Build ${{ env.REPOSITORY_NAME }}
if: env.new_version == 'true'
run: dotnet publish -c Release

- name: Prepare ${{ env.REPOSITORY_NAME }}.info
if: env.new_version == 'true'
run: |
echo "version: ${{ env.version }}" > /home/runner/work/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_FOLDER }}/bin/Release/net8.0/publish/${{ env.PLUGIN_NAME }}.info
echo "repository: https://github.com/${{ github.repository }}" >> /home/runner/work/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_FOLDER }}/bin/Release/net8.0/publish/${{ env.PLUGIN_NAME }}.info
- name: Prepare Release
if: env.new_version == 'true'
run: |
mv /home/runner/work/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_FOLDER }}/bin/Release/net8.0/publish/ /home/runner/work/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_FOLDER }}/bin/Release/net8.0/${{ env.PLUGIN_NAME }}/
- name: Compress Release
if: env.new_version == 'true'
run: cd /home/runner/work/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_NAME }}/${{ env.REPOSITORY_FOLDER }}/bin/Release/net8.0 && zip -r /home/runner/work/${{ env.REPOSITORY_NAME }}-${{ env.version }}.zip ${{ env.PLUGIN_NAME }}

- name: Build Release Notes
if: env.new_version == 'true'
id: get_release_notes
run: |
release_notes=$(git log --pretty=format:"[%h](https://github.com/${{ github.repository }}/commit/%H) - %s (%an)" $(git describe --tags --abbrev=0)..HEAD)
echo "release_notes=$release_notes" >> $GITHUB_ENV
- name: Add Release on Github
id: create_release
if: env.new_version == 'true'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.UPLOAD_TOKEN }}
with:
tag_name: ${{ env.version }}
release_name: ${{ env.version }}
body: ${{ env.release_notes }}
draft: false
prerelease: false

- name: Upload ${{ env.REPOSITORY_NAME }}-${{ env.version }}.zip
if: env.new_version == 'true'
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.UPLOAD_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: /home/runner/work/${{ env.REPOSITORY_NAME }}-${{ env.version }}.zip
asset_name: ${{ env.REPOSITORY_NAME }}-${{ env.version }}.zip
asset_content_type: application/zip

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.idea.NativeMapVote/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
1 change: 0 additions & 1 deletion .idea/.idea.NativeMapVotePlugin/.idea/.name

This file was deleted.

2 changes: 1 addition & 1 deletion NativeMapVotePlugin.sln → cs2-native-mapvote.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeMapVotePlugin", "NativeMapVotePlugin.csproj", "{18207429-968D-48B7-A778-90B58FD0B713}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeMapVote", "src\NativeMapVote.csproj", "{18207429-968D-48B7-A778-90B58FD0B713}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
4 changes: 2 additions & 2 deletions CallVotes.cs → src/CallVotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Menu;

namespace NativeMapVotePlugin;
namespace NativeMapVote;

public partial class NativeMapVotePlugin
public partial class NativeMapVote
{
private string _callVoteMap = "";
private string? _mapNextRound;
Expand Down
2 changes: 1 addition & 1 deletion ChatVote.cs → src/ChatVote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using CounterStrikeSharp.API.Modules.Utils;
using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;

namespace NativeMapVotePlugin;
namespace NativeMapVote;

public class ChatVoteLocalization
{
Expand Down
17 changes: 8 additions & 9 deletions NativeMapVotePlugin.cs → src/NativeMapVote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using CounterStrikeSharp.API.Modules.Menu;
using RconSharp;

namespace NativeMapVotePlugin;
namespace NativeMapVote;

public class PluginConfig : BasePluginConfig
{
Expand Down Expand Up @@ -47,18 +47,17 @@ public class PluginConfig : BasePluginConfig
public string RtvEndMatchCommand { get; set; } = "mp_halftime false; mp_maxrounds 1";
}

public partial class NativeMapVotePlugin : BasePlugin, IPluginConfig<PluginConfig>
public partial class NativeMapVote : BasePlugin, IPluginConfig<PluginConfig>
{
public override string ModuleName => "Native Map Vote Plugin";
public override string ModuleAuthor => "Jon-Mailes Graeffe <[email protected]>";
public override string ModuleVersion => "1.0.2";

public PluginConfig Config { get; set; } = null!;

private ChatMenu? _nominationMenuAllMaps;
private ChatMenu? _callVoteMenuAllMaps;

public NativeMapVotePlugin()
public NativeMapVote()
{
_callVoteChatVote = new(this);
_rtvChatVote = new(this);
Expand Down Expand Up @@ -161,7 +160,7 @@ private void FetchMapGroupOverRcon()
var rconPasswordCvar = ConVar.Find("rcon_password");
if (rconPasswordCvar == null || rconPasswordCvar.StringValue == null || rconPasswordCvar.StringValue.Length == 0)
{
Console.WriteLine("[NativeMapVotePlugin][WARNING] Fetching map list over RCON disabled due to disabled RCON (cvar rcon_password not set)!");
Console.WriteLine("[NativeMapVote][WARNING] Fetching map list over RCON disabled due to disabled RCON (cvar rcon_password not set)!");
return;
}

Expand All @@ -174,7 +173,7 @@ private void FetchMapGroupOverRcon()

if (string.IsNullOrEmpty(output))
{
Console.WriteLine("[NativeMapVotePlugin][WARNING] Fetching mapgroup over RCON failed!");
Console.WriteLine("[NativeMapVote][WARNING] Fetching mapgroup over RCON failed!");
return;
}

Expand All @@ -185,7 +184,7 @@ private void FetchMapGroupOverRcon()
if (line.Length == 0 || line.StartsWith("Map group:")) continue;
if (line.StartsWith("No maps"))
{
Console.WriteLine("[NativeMapVotePlugin][WARNING] No maps in map group found - plugin will not work this map!");
Console.WriteLine("[NativeMapVote][WARNING] No maps in map group found - plugin will not work this map!");
return;
}

Expand All @@ -195,15 +194,15 @@ private void FetchMapGroupOverRcon()

if (mapNames.Count == 0)
{
Console.WriteLine("[NativeMapVotePlugin][ERROR] Could not parse map group over RCON!");
Console.WriteLine("[NativeMapVote][ERROR] Could not parse map group over RCON!");
return;
}

Config.Maps = ImmutableList<string>.Empty;
Config.Maps = Config.Maps.AddRange(mapNames);
OnMapGroupChange();

Console.WriteLine("[NativeMapVotePlugin][INFO] Found " + Config.Maps.Count + " maps in map group, now used for voting!");
Console.WriteLine("[NativeMapVote][INFO] Found " + Config.Maps.Count + " maps in map group, now used for voting!");
});
}

Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions Nominations.cs → src/Nominations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Menu;

namespace NativeMapVotePlugin;
namespace NativeMapVote;

public partial class NativeMapVotePlugin
public partial class NativeMapVote
{
private readonly HashSet<string> _nominatedMapNames = new();
private readonly Dictionary<SteamID, string> _playerNominations = new();
Expand Down Expand Up @@ -162,15 +162,15 @@ private void UpdateEndMatchGroupVoteOptions()
{
if (Config.Maps.Count == 0)
{
Console.WriteLine("[NativeMapVotePlugin][WARNING] No maps in map group, skipping vote manipulation!");
Console.WriteLine("[NativeMapVote][WARNING] No maps in map group, skipping vote manipulation!");
return;
}

var proxies = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules");
var proxy = proxies.FirstOrDefault();
if (proxy == null || proxy.GameRules == null)
{
Console.WriteLine("[NativeMapVotePlugin][ERROR] Could not find end match vote entity!");
Console.WriteLine("[NativeMapVote][ERROR] Could not find end match vote entity!");
return;
}

Expand All @@ -192,7 +192,7 @@ private void UpdateEndMatchGroupVoteOptions()

if (alreadyAddedOptions.Count >= Config.Maps.Count) break;
}
Console.WriteLine("[NativeMapVotePlugin][INFO] Successfully manipulated the vote (no scandal though)!");
Console.WriteLine("[NativeMapVote][INFO] Successfully manipulated the vote (no scandal though)!");
}

private void ResetNominations()
Expand Down
4 changes: 2 additions & 2 deletions Rtv.cs → src/Rtv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;

namespace NativeMapVotePlugin;
namespace NativeMapVote;

public partial class NativeMapVotePlugin
public partial class NativeMapVote
{
private readonly ChatVote _rtvChatVote;

Expand Down
6 changes: 6 additions & 0 deletions src/Version.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NativeMapVote;

public partial class NativeMapVote
{
public override string ModuleVersion => "1.0.2";
}
File renamed without changes.

0 comments on commit cafb3a8

Please sign in to comment.