Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FilteringRule to Husky tasks #115

Merged
merged 7 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# This dockerfile is used in the integration tests

# Use the official .NET SDK image as a base
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
ARG RESOURCE_REAPER_SESSION_ID="00000000-0000-0000-0000-000000000000"
Expand All @@ -8,22 +6,22 @@ LABEL "org.testcontainers.resource-reaper-session"=$RESOURCE_REAPER_SESSION_ID
# Set the working directory
WORKDIR /app

# Copy the .csproj file to the container
COPY src/Husky/Husky.csproj ./

ENV HUSKY=0

# Copy the remaining files to the container
COPY . ./
# Copy the .csproj file to the container
COPY src/Husky/Husky.csproj ./src/Husky/

# Restore dependencies
RUN dotnet restore /app/src/Husky

# Build the application
RUN dotnet build --no-restore -c Release -f net8.0 /app/src/Husky
# Copy the remaining files to the container
COPY . ./

# Create a NuGet package
RUN dotnet pack --no-build --no-restore -c Release -o out /app/src/Husky/Husky.csproj -p:TargetFrameworks=net8.0
# Build the application using the custom 'IntegrationTest' configuration
RUN dotnet build --no-restore -c IntegrationTest -f net8.0 /app/src/Husky/Husky.csproj -p:Version=99.1.1-test -p:TargetFrameworks=net8.0

# Create a NuGet package using the 'IntegrationTest' configuration
RUN dotnet pack --no-build --no-restore -c IntegrationTest -o out /app/src/Husky/Husky.csproj -p:Version=99.1.1-test -p:TargetFrameworks=net8.0

# Use the same .NET SDK image for the final stage
FROM mcr.microsoft.com/dotnet/sdk:8.0
Expand All @@ -35,14 +33,15 @@ WORKDIR /app

# Install Git
RUN apt-get update && \
apt-get install -y git
apt-get install -y git && \
rm -rf /var/lib/apt/lists/*

# Copy the NuGet package from the build-env to the runtime image
COPY --from=build-env /app/out/*.nupkg /app/nupkg/

# Install Husky tool and add the global tools path to the PATH
RUN dotnet tool install -g --no-cache --add-source /app/nupkg/ husky \
&& echo "export PATH=\$PATH:/root/.dotnet/tools" >> ~/.bashrc
# Install the specific version from the local source
RUN dotnet tool install -g husky --version 99.1.1-test --add-source /app/nupkg/ --no-cache

RUN echo "export PATH=\$PATH:/root/.dotnet/tools" >> ~/.bashrc

# Set the entry point to a simple shell
ENTRYPOINT ["/bin/bash"]
CMD ["/root/.dotnet/tools/husky"]
7 changes: 5 additions & 2 deletions Husky.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
IntegrationTest|Any CPU = IntegrationTest|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{12AB4B33-47A6-49D5-872A-5BA6DD634E9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12AB4B33-47A6-49D5-872A-5BA6DD634E9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12AB4B33-47A6-49D5-872A-5BA6DD634E9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12AB4B33-47A6-49D5-872A-5BA6DD634E9C}.Release|Any CPU.Build.0 = Release|Any CPU
{12AB4B33-47A6-49D5-872A-5BA6DD634E9C}.IntegrationTest|Any CPU.ActiveCfg = IntegrationTest|Any CPU
{12AB4B33-47A6-49D5-872A-5BA6DD634E9C}.IntegrationTest|Any CPU.Build.0 = IntegrationTest|Any CPU
{57EE798B-0FE0-42A4-BDB9-D168109D3E7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57EE798B-0FE0-42A4-BDB9-D168109D3E7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57EE798B-0FE0-42A4-BDB9-D168109D3E7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57EE798B-0FE0-42A4-BDB9-D168109D3E7D}.Release|Any CPU.Build.0 = Release|Any CPU
{57EE798B-0FE0-42A4-BDB9-D168109D3E7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAF049CD-6E45-4A66-A88F-C2EB007DFCC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAF049CD-6E45-4A66-A88F-C2EB007DFCC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAF049CD-6E45-4A66-A88F-C2EB007DFCC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAF049CD-6E45-4A66-A88F-C2EB007DFCC3}.Release|Any CPU.Build.0 = Release|Any CPU
{FAF049CD-6E45-4A66-A88F-C2EB007DFCC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{57EE798B-0FE0-42A4-BDB9-D168109D3E7D} = {302A5F25-CB44-4ED0-A65E-06C04648D211}
Expand Down
13 changes: 12 additions & 1 deletion docs/.vuepress/public/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
},
"description": "Glob pattern to exclude files."
},
"filteringRule": {
"$ref": "#/definitions/filteringRules",
"description": "The filtering rule for this task. Can be 'variable' or 'staged'.",
"default": "variable"
},
"windows": {
"$ref": "#/definitions/windowsOverrides",
"description": "Overrides all settings for Windows."
Expand Down Expand Up @@ -211,6 +216,12 @@
"enum": ["relative", "absolute"],
"description": "Specifies the file path style.",
"default": "relative"
}
},
"filteringRules": {
"type": "string",
"enum": ["variable", "staged"],
"default": "variable",
"description": "The filtering rule for this task. Can be 'variable' or 'staged'."
}
}
}
9 changes: 9 additions & 0 deletions src/Husky/Husky.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IncludeSymbols>true</IncludeSymbols>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Configurations>Debug;Release;IntegrationTest</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="templates\husky.sh" />
Expand Down Expand Up @@ -60,4 +62,11 @@
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High" />
<Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High" WorkingDirectory="..\.." />
</Target>

<PropertyGroup Condition="'$(Configuration)' == 'IntegrationTest'">
<DefineConstants>TEST</DefineConstants>
<Optimize>false</Optimize>
<OutputPath>bin\IntegrationTest\</OutputPath>
<IntermediateOutputPath>obj\IntegrationTest\</IntermediateOutputPath>
</PropertyGroup>
</Project>
20 changes: 20 additions & 0 deletions src/Husky/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.IO.Abstractions;
using System.Text.RegularExpressions;
using CliFx;
Expand All @@ -14,6 +15,10 @@

var exitCode = 0;

#if TEST
WaitForDebuggerIfNeeded();
#endif

#if DEBUG
"Starting development mode ... ".Log(ConsoleColor.DarkGray);
while (true)
Expand Down Expand Up @@ -73,3 +78,18 @@ ServiceProvider BuildServiceProvider()
.AddTransient<CleanupCommand>();
return services.BuildServiceProvider();
}

#if TEST
static void WaitForDebuggerIfNeeded()
{
if (Environment.GetEnvironmentVariable("HUSKY_INTEGRATION_TEST") != "1") return;
Console.WriteLine("Waiting for debugger to attach...");

while (!Debugger.IsAttached)
{
Thread.Sleep(100);
}

Console.WriteLine("Debugger attached.");
}
#endif
2 changes: 1 addition & 1 deletion src/Husky/TaskRunner/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private static void AddCustomArguments(List<ArgumentInfo> args, string[]? option
"⚠️ No arguments passed to the run command".Husky(ConsoleColor.Yellow);
}

private static Matcher GetPatternMatcher(HuskyTask task)
public static Matcher GetPatternMatcher(HuskyTask task)
{
var matcher = new Matcher();
var hasMatcher = false;
Expand Down
37 changes: 32 additions & 5 deletions src/Husky/TaskRunner/ExecutableTaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Husky.Stdout;
using Husky.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileSystemGlobbing;

namespace Husky.TaskRunner;

Expand Down Expand Up @@ -38,11 +39,8 @@ public ExecutableTaskFactory(IServiceProvider provider, IGit git, IArgumentParse
var cwd = await _git.GetTaskCwdAsync(huskyTask);
var argsInfo = await _argumentParser.ParseAsync(huskyTask, options.Arguments?.ToArray());

if (huskyTask.Args != null && huskyTask.Args.Length > argsInfo.Length)
{
"💤 Skipped, no matched files".Husky(ConsoleColor.Blue);
return null;
}
if (await CheckIfWeShouldSkipTheTask(huskyTask, argsInfo))
return null; // skip the task

// check for chunk
var totalCommandLength = argsInfo.Sum(q => q.Argument.Length) + huskyTask.Command.Length;
Expand All @@ -65,6 +63,35 @@ public ExecutableTaskFactory(IServiceProvider provider, IGit git, IArgumentParse
);
}

private async Task<bool> CheckIfWeShouldSkipTheTask(HuskyTask huskyTask, ArgumentInfo[] argsInfo)
{
if (huskyTask is { FilteringRule: FilteringRules.Variable, Args: not null } && huskyTask.Args.Length > argsInfo.Length)
{
"💤 Skipped, no matched files".Husky(ConsoleColor.Blue);
return true;
}

if (huskyTask.FilteringRule != FilteringRules.Staged) return false;

var stagedFiles = (await _git.GetStagedFilesAsync())
.Where(q => !string.IsNullOrWhiteSpace(q))
.ToArray();
if (stagedFiles.Length == 0)
{
"💤 Skipped, no staged files".Husky(ConsoleColor.Blue);
return true;
}

var matcher = ArgumentParser.GetPatternMatcher(huskyTask);

// get match staged files with glob
var matches = matcher.Match(stagedFiles);
if (matches.HasMatches) return false;
"💤 Skipped, no staged matched files".Husky(ConsoleColor.Blue);
return true;

}

private IExecutableTask CreateChunkTask(
HuskyTask huskyTask,
int totalCommandLength,
Expand Down
7 changes: 7 additions & 0 deletions src/Husky/TaskRunner/FilteringRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Husky.TaskRunner;

public enum FilteringRules
{
Variable,
Staged,
}
1 change: 1 addition & 0 deletions src/Husky/TaskRunner/HuskyTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class HuskyTask
public string? Group { get; set; }
public string? Branch { get; set; }
public HuskyTask? Windows { get; set; }
public FilteringRules FilteringRule { get; set; } = FilteringRules.Variable;
public string[]? Include { get; set; }
public string[]? Exclude { get; set; }
}
8 changes: 8 additions & 0 deletions tests/HuskyIntegrationTests/HuskyIntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Docker.DotNet" Version="3.125.15" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="Testcontainers" Version="3.6.0" />
<PackageReference Include="xunit" Version="2.4.2"/>
Expand All @@ -27,5 +29,11 @@
<ItemGroup>
<Folder Include="TestProjectBase\" />
</ItemGroup>

<ItemGroup>
<Content Include="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Loading
Loading