Files
ResourceString.Net.Contract/flake.nix
2023-05-20 15:25:01 +02:00

159 lines
5.8 KiB
Nix

{
description = "";
inputs.nixpkgs.url = "nixpkgs/nixos-22.11-small";
outputs = { self, nixpkgs }:
let
name = "ResourceString.Net";
version = "0.0.1";
supportedSystems =
[ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
scripts = forAllSystems (system: {
updateFlakeLockFile = nixpkgsFor.${system}.writeScript "update_flake_lock.sh" ''
nix --experimental-features 'nix-command flakes' flake lock --update-input nixpkgs
nix --experimental-features 'nix-command flakes' build
'';
updateNugetLockFile = nixpkgsFor.${system}.writeScript "update_nuget_lock.sh" ''
temp_package_folder=$(mktemp -d)
dotnet restore ResourceString.Net --packages "$temp_package_folder"
${nixpkgsFor.${system}.nuget-to-nix}/bin/nuget-to-nix "$temp_package_folder" > ResourceString.Net/deps.nix
rm -rf "$temp_package_folder"
'';
autoTag = nixpkgsFor.${system}.writeScript "auto_tag.sh" ''
git tag --force v${version}
git push origin v${version}
'';
updateLogicResourceFile = nixpkgsFor.${system}.writeScript "update_logic_resource_file.sh" ''
dotnet run --project ResourceString.Net.App.Console ResourceString.Net.Logic/Properties/Resources.resx ResourceString.Net.Logic.Properties > ResourceString.Net.Logic/Properties/Resources.cs.tmp
mv ResourceString.Net.Logic/Properties/Resources.cs.tmp ResourceString.Net.Logic/Properties/Resources.cs
'';
runAllTests = nixpkgsFor.${system}.writeScript "run_all_tests.sh" ''
dotnet test *.Tests
'';
buildAllAssemblies = nixpkgsFor.${system}.writeScript "build_all_assemblies.sh" ''
# Find all .csproj files below the current folder
csproj_files=$(find . -name '*.csproj')
# Create a temporary file to store the number of dependencies for each project
temp_file=$(mktemp)
# Loop through each .csproj file and count its number of ResourceString dependencies
for file in $csproj_files
do
count=$(cat "$file" | grep -c "<ProjectReference.*ResourceString")
depth=$(echo "$file" | tr -cd '/' | wc -c)
dot_count=$(echo "$file" | tr -cd '.' | wc -c)
echo "$depth $count $dot_count $file" >> "$temp_file"
done
# Sort the projects by their count of dependencies and path depth, in ascending order
sorted_projects=$(sort -nk1 -nk2 -nk3 -t' ' "$temp_file" | awk '{print $4}')
# Loop through the sorted projects and build them
while read -r file
do
echo "Building project: $file"
dotnet build "$file"
done << EOF
$sorted_projects
EOF
# Remove the temporary file
rm "$temp_file"
'';
cleanAllAssemblies = nixpkgsFor.${system}.writeScript "clean_all_assemblies.sh" ''
csproj_files=$(find . -name '*.csproj')
for file in $csproj_files
do
dotnet clean "$file"
done
'';
runDefaultApp = nixpkgsFor.${system}.writeScript "run_default_app.sh" ''
dotnet run --project ResourceString.Net.App.Console $@
'';
createNugetPackage = nixpkgsFor.${system}.writeScript "run_default_app.sh" ''
dotnet run --project ResourceString.Net.App.Console $@
'';
});
in
rec {
packages = forAllSystems (system: {
default = nixpkgsFor.${system}.buildDotnetModule {
pname = name;
version = version;
src = self;
projectFile = [
"ResourceString.Net/ResourceString.Net.csproj"
"ResourceString.Net.App.Console/ResourceString.Net.App.Console.csproj"
"example/Example.App/Example.App.csproj"
];
nugetDeps = ./ResourceString.Net/deps.nix;
projectReferences = [ ];
dotnet-sdk = nixpkgsFor.${system}.dotnet-sdk;
dotnet-runtime = nixpkgsFor.${system}.dotnet-runtime;
executables = [ "ResourceString.Net.App.Console" "Example.App" ];
packNupkg = true;
runtimeDeps = [ ];
};
});
apps = forAllSystems (system: {
default = {
type = "app";
program = "${scripts.${system}.runDefaultApp}";
};
devTasks = {
updateFlakeLock = {
type = "app";
program = "${scripts.${system}.updateFlakeLockFile}";
};
updateNugetLock = {
type = "app";
program = "${scripts.${system}.updateNugetLockFile}";
};
autoTag = {
type = "app";
program = "${scripts.${system}.autoTag}";
};
updateLogicResourceFile = {
type = "app";
program = "${scripts.${system}.updateLogicResourceFile}";
};
runAllTests = {
type = "app";
program = "${scripts.${system}.runAllTests}";
};
buildAllAssemblies = {
type = "app";
program = "${scripts.${system}.buildAllAssemblies}";
};
cleanAllAssemblies = {
type = "app";
program = "${scripts.${system}.cleanAllAssemblies}";
};
};
});
devShells = forAllSystems (system: {
default = nixpkgsFor.${system}.mkShell {
name = "dev-shell";
packages =
[ nixpkgsFor.${system}.dotnet-sdk nixpkgsFor.${system}.nixfmt ];
shellHook = ''
alias nixe="nix --experimental-features 'nix-command flakes'"
'';
};
});
};
}