Skip to content

Commit

Permalink
+ workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawkynt committed Sep 1, 2024
1 parent f6aa0d3 commit c7d8d9e
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: [Hawkynt]
custom: ["https://www.paypal.me/hawkynt"]
92 changes: 92 additions & 0 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: NewBuild
on:
schedule:
- cron: '59 23 * * 0'
workflow_dispatch:
jobs:

# This job tries to run the prerequisites, just to see if this works
TryInit:
runs-on: ubuntu-latest
steps:
- name: Checking-out repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x

- name: Show Info
run: dotnet --info

- name: Receive Versions
run: perl ".github/workflows/UpdateVersions.pl" "."

# this is the real job needed to build and publish
BuildAndPublishApplications:
needs: TryInit
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
csproj-file:
- 'Randomizer/Randomizer.csproj'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x

- run: |
projectDir=$(dirname ${{ matrix.csproj-file }})
projectName=$(basename $projectDir)
echo "ProjectDir=$projectDir" >> $GITHUB_ENV
echo "ProjectName=$projectName" >> $GITHUB_ENV
- run: perl ".github/workflows/UpdateVersions.pl" "."
- run: dotnet restore "${{ matrix.csproj-file }}"
- run: dotnet build "${{ matrix.csproj-file }}" --no-restore --configuration Release

- uses: actions/upload-artifact@v4
with:
name: ${{env.ProjectName}}
path: ${{env.ProjectDir}}/bin/Release

BuildAndPublishNuGet:
needs: TryInit
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
csproj-file:
- 'RandomNumberGenerators/RandomNumberGenerators.csproj'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x

- run: |
projectDir=$(dirname ${{ matrix.csproj-file }})
projectName=$(basename $projectDir)
echo "ProjectDir=$projectDir" >> $GITHUB_ENV
echo "ProjectName=$projectName" >> $GITHUB_ENV
- run: perl ".github/workflows/UpdateVersions.pl" "."
- run: dotnet restore "${{ matrix.csproj-file }}"
- run: dotnet build "${{ matrix.csproj-file }}" --no-restore --configuration Release

- name: Pack NuGet package
run: dotnet pack "${{ matrix.csproj-file }}" --configuration Release --output ./artifacts

- name: Publish the package to nuget.org
run: dotnet nuget push ./artifacts/*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json --skip-duplicate
env:
NUGET_AUTH_TOKEN: ${{ secrets.NUGET_TOKEN }}
113 changes: 113 additions & 0 deletions .github/workflows/UpdateVersions.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/perl
use strict;
use warnings;

use Cwd;
use File::Basename;
use File::Copy;
use File::Find;

sub main(@) {
my($rootDirectoryName)=@_;
die "[Error]Usage $0 <rootDirectory>\n" unless $rootDirectoryName;
my $realName=Cwd::realpath($rootDirectoryName);
die "[Error]Could not parse directory: $!, $rootDirectoryName\n" unless $realName;
die "[Error]Directory not found: $realName\n" unless -d $realName;

print "[Info]Querying for project files...";
my $projectFiles=_FindProjectFiles($realName);
print "Found ".scalar(@$projectFiles)."\n";

print "[Info]Retrieving versions\n";
my $versions=_QueryVersions($projectFiles);

my $removedFiles=scalar(@$projectFiles)-scalar(keys(%$versions));
print "[Warning]$removedFiles files did not have a version tag and must not be referenced!" if($removedFiles>0);

_UpdateVersions($versions);
}

sub _DebugDump($){
my ($var)=@_;
require Data::Dumper;
my $dumper=Data::Dumper->new([$var],["var"]);
$dumper->Indent(1);
$dumper->Useqq(1);
$dumper->Sortkeys(1);
print $dumper->Dump();
}

sub _UpdateVersions($) {
my($filesWithVersion)=@_;
foreach my $fileName (keys(%$filesWithVersion)) {
my $version=$filesWithVersion->{$fileName};
my $outputFileName="$fileName.\$\$\$";
open my $fh,"<",$fileName or die "[Error]Could not read file $fileName:$!";
open my $fo,">",$outputFileName or die "[Error]Could not write file $outputFileName:$!";
while(my $line=<$fh>) {
$line="<version>$version</version>\n" if($line =~ /<version>\s*([\d\.]+)\s*<\/version>/i);
print $fo $line;
}
close $fo;
close $fh;
File::Copy::move($outputFileName,$fileName) or die "[Error]Could not replace file $fileName:$!";
}
}

sub _QueryVersions($) {
my($fileNames)=@_;
my %results;
foreach my $fileName (@$fileNames) {
my $directoryName=File::Basename::dirname($fileName);
my $version = _QueryVersionFromFile($fileName);
unless ($version) {
print "[Warning]No version tag present in $fileName\n";
next;
}

# tidy if they have more than three segments
$version =~ s/^(\d+)(\.\d+)?(\.\d+)?.*/$1$2$3/;

my $commitCount=_QueryGitCommitCount($directoryName);
$results{$fileName}="$version.$commitCount";
print "[Info]Using base version $version -> $results{$fileName}\n";
}
return \%results;
}

sub _QueryVersionFromFile($){
my ($inputFileName) = @_;
open my $fh,"<",$inputFileName or die "[Error]Could not read file $inputFileName:$!\n";
while(my $line=<$fh>){
next unless($line =~ /<version>\s*([\d\.]+)\s*<\/version>/i);

close $fh;
my ($result) = $line =~ /<version>\s*([\d\.]+)\s*<\/version>/i;
print "[Verbose]Found version in file $inputFileName: $result\n";
return $result;
}
close $fh;
return undef;
}

sub _QueryGitCommitCount(;$) {
my ($workingCopyFileOrFolder)=@_;
my $command = "git rev-list HEAD --all --branches --full-history --count $workingCopyFileOrFolder";
print "[Verbose]Executing git> $command\n";
my $result=`$command`;
chomp $result;
return $result;
}


sub _FindProjectFiles($) {
my($rootDirectoryName)=@_;
my @results;
File::Find::find(sub{
my $current=$File::Find::name;
push(@results,$current) if ($current=~/\.csproj$/i && -f $current);
},$rootDirectoryName);
return \@results;
}

main(@ARGV);
2 changes: 1 addition & 1 deletion RandomNumberGenerators/RandomNumberGenerators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FrameworkExtensions.Corlib" Version="1.0.2.328" />
<PackageReference Include="FrameworkExtensions.Corlib" Version="1.0.2.*" />
<None Include="..\Readme.md" Pack="True" PackagePath=".">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
Expand Down
10 changes: 10 additions & 0 deletions Randomizer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GitHub", "GitHub", "{3C5B2B
ProjectSection(SolutionItems) = preProject
.gitattributes = .gitattributes
.gitignore = .gitignore
.github\FUNDING.yml = .github\FUNDING.yml
LICENSE = LICENSE
Readme.md = Readme.md
EndProjectSection
Expand All @@ -30,6 +31,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{B2
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RandomNumberGenerators", "RandomNumberGenerators\RandomNumberGenerators.csproj", "{23FD653A-C700-403F-8837-DFBE1FAB5DED}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{561D8524-81C4-4F5A-9B7D-15323130EBE9}"
ProjectSection(SolutionItems) = preProject
.github\workflows\Build.yml = .github\workflows\Build.yml
.github\workflows\UpdateVersions.pl = .github\workflows\UpdateVersions.pl
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -48,6 +55,9 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{561D8524-81C4-4F5A-9B7D-15323130EBE9} = {3C5B2B72-BA8D-4058-9D5D-15211B190E9F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {09FC203F-BDA4-4E74-A6B1-E9B87D24B8E1}
EndGlobalSection
Expand Down
2 changes: 1 addition & 1 deletion Randomizer/Randomizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="FrameworkExtensions.Corlib" Version="1.0.2.328" />
<PackageReference Include="FrameworkExtensions.Corlib" Version="1.0.2.*" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit c7d8d9e

Please sign in to comment.