-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from hlotyaks/task/add-package-hasher
Task/add package hasher
- Loading branch information
Showing
16 changed files
with
243 additions
and
9 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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"dotnet-test-explorer.testProjectPath": "GraphBuilder/tests" | ||
"dotnet-test-explorer.testProjectPath": "*/tests" | ||
} |
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 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,48 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26124.0 | ||
MinimumVisualStudioVersion = 15.0.26124.0 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PackageHasher", "src\PackageHasher.csproj", "{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{3679A64B-8D42-400B-996B-8735D9A9C16C}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|Any CPU = Release|Any CPU | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x64.ActiveCfg = Debug|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x64.Build.0 = Debug|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x86.ActiveCfg = Debug|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Debug|x86.Build.0 = Debug|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x64.ActiveCfg = Release|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x64.Build.0 = Release|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x86.ActiveCfg = Release|Any CPU | ||
{E5CCDC89-EE49-4FE6-8AAC-81E8996712FA}.Release|x86.Build.0 = Release|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x64.ActiveCfg = Debug|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x64.Build.0 = Debug|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x86.ActiveCfg = Debug|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Debug|x86.Build.0 = Debug|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x64.ActiveCfg = Release|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x64.Build.0 = Release|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x86.ActiveCfg = Release|Any CPU | ||
{3679A64B-8D42-400B-996B-8735D9A9C16C}.Release|x86.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
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,32 @@ | ||
using System.Text; | ||
using System.Security.Cryptography; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace PackageAnalyzer | ||
{ | ||
public static class HashAlgorithmExtensions | ||
{ | ||
public static async Task<byte[]> ComputeHashAsync(this HashAlgorithm alg, IEnumerable<FileInfo> files, string rootPath) | ||
{ | ||
using (var cs = new CryptoStream(Stream.Null, alg, CryptoStreamMode.Write)) | ||
{ | ||
foreach (var file in files) | ||
{ | ||
// Will either include the rootPath or not. | ||
string fileName = file.FullName.Substring(rootPath.Length, file.FullName.Length - rootPath.Length); | ||
var pathBytes = Encoding.UTF8.GetBytes(fileName); | ||
cs.Write(pathBytes, 0, pathBytes.Length); | ||
|
||
using (var fs = file.OpenRead()) | ||
await fs.CopyToAsync(cs); | ||
} | ||
|
||
cs.FlushFinalBlock(); | ||
} | ||
|
||
return alg.Hash; | ||
} | ||
} | ||
} |
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,62 @@ | ||
using System; | ||
using System.Text; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Security.Cryptography; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace PackageAnalyzer | ||
{ | ||
public class PackageHasher | ||
{ | ||
public async Task<Dictionary<string, string>> HashFoldersAsync(List<string> folders, string rootPath) | ||
{ | ||
Dictionary<string, string> hashedFolders = new Dictionary<string, string>(); | ||
|
||
IEnumerable<Task<(string folder, string folderwithhash)>> hashFolderTasksQuery = | ||
folders.Select( f => HashFolderAsync(new DirectoryInfo(f), rootPath)); | ||
|
||
List<Task<(string folder, string folderwithhash)>> hashFolderTasks = hashFolderTasksQuery.ToList(); | ||
|
||
while (hashFolderTasks.Count > 0) | ||
{ | ||
Task<(string folder, string folderwithhash)> firstTask = await Task.WhenAny(hashFolderTasks); | ||
|
||
hashFolderTasks.Remove(firstTask); | ||
|
||
(string folder, string folderwithhash) HashFolderValue = await firstTask; | ||
|
||
hashedFolders.Add(HashFolderValue.folder, HashFolderValue.folderwithhash); | ||
} | ||
|
||
return hashedFolders; | ||
} | ||
|
||
|
||
async Task<(string folder, string folderwithhash)> HashFolderAsync(DirectoryInfo folder, string rootPath = "", string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories) | ||
{ | ||
using(var alg = MD5.Create()) | ||
{ | ||
var result = await alg.ComputeHashAsync(folder.EnumerateFiles(searchPattern, searchOption), rootPath); | ||
|
||
// Folder has starts with folder name. | ||
// Format - name.hash | ||
StringBuilder sb = new StringBuilder(); | ||
sb.Append(folder.Name); | ||
sb.Append("."); | ||
|
||
// Build the final string by converting each byte | ||
// into hex and appending it to a StringBuilder | ||
for (int i = 0; i < result.Length; i++) | ||
{ | ||
sb.Append(result[i].ToString("X2")); | ||
} | ||
|
||
// And return it | ||
return (folder: folder.Name, folderwithhash: 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,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,54 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using PackageAnalyzer; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace PackageAnalyzer.Tests | ||
{ | ||
|
||
[TestClass] | ||
public class PackageHasherTests | ||
{ | ||
[TestMethod] | ||
public void SimpleTest1() | ||
{ | ||
PackageHasher ph = new PackageHasher(); | ||
|
||
string cwd = Directory.GetCurrentDirectory(); | ||
string root = $"{cwd}\\testcases\\simple2"; | ||
|
||
List<string> paths = new List<string>(); | ||
paths.Add($"{root}\\package1"); | ||
|
||
var task = Task.Run(async () => await ph.HashFoldersAsync(paths, root)); | ||
|
||
var result = task.Result; | ||
|
||
Assert.AreEqual(1, result.Keys.Count); | ||
Assert.IsTrue(result.ContainsKey("package1")); | ||
} | ||
|
||
[TestMethod] | ||
public void SimpleTest2() | ||
{ | ||
PackageHasher ph = new PackageHasher(); | ||
|
||
string cwd = Directory.GetCurrentDirectory(); | ||
string root = $"{cwd}\\testcases\\simple2"; | ||
|
||
List<string> paths = new List<string>(); | ||
paths.Add($"{root}\\package1"); | ||
paths.Add($"{root}\\package2"); | ||
|
||
var task = Task.Run(async () => await ph.HashFoldersAsync(paths, root)); | ||
|
||
var result = task.Result; | ||
|
||
Assert.AreEqual(2, result.Keys.Count); | ||
Assert.IsTrue(result.ContainsKey("package1")); | ||
Assert.IsTrue(result.ContainsKey("package2")); | ||
} | ||
} | ||
|
||
} |
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 @@ | ||
test data for file1. Just needed to get a hash value. |
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 @@ | ||
test data for file1. Just needed to get a hash value. |
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 @@ | ||
test data for file1. Just needed to get a hash value. |
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\PackageHasher.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="testcases\**\*.*" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# Welcome to the Package Analyzer Project | ||
This repository contains the source code for: | ||
* GraphBuilder - constructs a directed graph from xml files. Identifies cycles in the graph. | ||
|
||
* PackageHasher - Hashes contents of folders to be used in identifying changes. | ||
|
||
## Project Build Status | ||
|
||
Project|Build Status | ||
---|--- | ||
GraphBuilder|![Build Status](https://github.com/hlotyaks/PackageAnalyzer/workflows/.NET%20Core/badge.svg) | ||
PackageAnalyzer|![Build Status](https://github.com/hlotyaks/PackageAnalyzer/workflows/.NET%20Core/badge.svg) | ||
|