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

Making UpdateAssemblyInfo to only consider files under the workingDirectory #304

Merged
merged 2 commits into from
Nov 21, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions GitVersionExe.Tests/AssemblyInfoFileUpdateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace GitVersionExe.Tests
{
using System.Collections.Generic;
using System.IO;
using GitVersion;
using NSubstitute;
using NUnit.Framework;

[TestFixture]
public class AssemblyInfoFileUpdateTests
{
[Test]
public void ShouldStartSearchFromWorkingDirectory()
{
var fileSystem = Substitute.For<IFileSystem>();
const string workingDir = "C:\\Testing";
using (new AssemblyInfoFileUpdate(new Arguments{ UpdateAssemblyInfo = true }, workingDir, new Dictionary<string, string>(), fileSystem))
{
fileSystem.Received().DirectoryGetFiles(Arg.Is(workingDir), Arg.Any<string>(), Arg.Any<SearchOption>());
}
}
}
}
4 changes: 4 additions & 0 deletions GitVersionExe.Tests/GitVersionExe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<Reference Include="LibGit2Sharp">
<HintPath>..\packages\LibGit2Sharp.0.19.0.0\lib\net40\LibGit2Sharp.dll</HintPath>
</Reference>
<Reference Include="NSubstitute">
<HintPath>..\packages\NSubstitute.1.8.0.0\lib\net45\NSubstitute.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -80,6 +83,7 @@
<Compile Include="GitVersionHelper.cs" />
<Compile Include="MsBuildProjectArgTest.cs" />
<Compile Include="PullRequestInTeamCityTest.cs" />
<Compile Include="AssemblyInfoFileUpdateTests.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions GitVersionExe.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<package id="ApprovalTests" version="3.0.7" targetFramework="net45" />
<package id="ApprovalUtilities" version="3.0.7" targetFramework="net45" />
<package id="LibGit2Sharp" version="0.19.0.0" targetFramework="net45" />
<package id="NSubstitute" version="1.8.0.0" targetFramework="net45" />
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<package id="Shouldly" version="2.2.0" targetFramework="net45" />
</packages>
28 changes: 15 additions & 13 deletions GitVersionExe/AssemblyInfoFileUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,53 @@ class AssemblyInfoFileUpdate : IDisposable
List<Action> restoreBackupTasks = new List<Action>();
List<Action> cleanupBackupTasks = new List<Action>();

public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, Dictionary<string, string> variables)
public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, Dictionary<string, string> variables, IFileSystem fileSystem)
{
if (!args.UpdateAssemblyInfo) return;

if (args.Output != OutputType.Json)
Console.WriteLine("Updating assembly info files");

var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args);
var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, args, fileSystem);

foreach (var assemblyInfoFile in assemblyInfoFiles)
{
var backupAssemblyInfo = assemblyInfoFile + ".bak";
var localAssemblyInfo = assemblyInfoFile;
File.Copy(assemblyInfoFile, backupAssemblyInfo, true);
fileSystem.Copy(assemblyInfoFile, backupAssemblyInfo, true);
restoreBackupTasks.Add(() =>
{
if (File.Exists(localAssemblyInfo))
File.Delete(localAssemblyInfo);
File.Move(backupAssemblyInfo, localAssemblyInfo);
if (fileSystem.Exists(localAssemblyInfo))
fileSystem.Delete(localAssemblyInfo);
fileSystem.Move(backupAssemblyInfo, localAssemblyInfo);
});
cleanupBackupTasks.Add(() => File.Delete(backupAssemblyInfo));
cleanupBackupTasks.Add(() => fileSystem.Delete(backupAssemblyInfo));

var assemblyVersion = string.Format("{0}.{1}.0.0", variables[VariableProvider.Major], variables[VariableProvider.Minor]);
var assemblyInfoVersion = variables[VariableProvider.InformationalVersion];
var assemblyFileVersion = variables[VariableProvider.AssemblySemVer];
var fileContents = File.ReadAllText(assemblyInfoFile)
var fileContents = fileSystem.ReadAllText(assemblyInfoFile)
.RegexReplace(@"AssemblyVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyVersion(\"{0}\")", assemblyVersion))
.RegexReplace(@"AssemblyInformationalVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyInformationalVersion(\"{0}\")", assemblyInfoVersion))
.RegexReplace(@"AssemblyFileVersion\(""\d+.\d+.\d+(.\d+|\*)?""\)", string.Format("AssemblyFileVersion(\"{0}\")", assemblyFileVersion));

File.WriteAllText(assemblyInfoFile, fileContents);
fileSystem.WriteAllText(assemblyInfoFile, fileContents);
}
}

static IEnumerable<string> GetAssemblyInfoFiles(string workingDirectory, Arguments args)
static IEnumerable<string> GetAssemblyInfoFiles(string workingDirectory, Arguments args, IFileSystem fileSystem)
{
if (args.UpdateAssemblyInfoFileName != null)
{
if (File.Exists(args.UpdateAssemblyInfoFileName))
var fullPath = Path.Combine(workingDirectory, args.UpdateAssemblyInfoFileName);

if (fileSystem.Exists(fullPath))
{
return new[] { Path.GetFullPath(args.UpdateAssemblyInfoFileName) };
return new[] { fullPath };
}
}

return Directory.GetFiles(workingDirectory, "AssemblyInfo.cs", SearchOption.AllDirectories);
return fileSystem.DirectoryGetFiles(workingDirectory, "AssemblyInfo.cs", SearchOption.AllDirectories);
}

public void Dispose()
Expand Down
43 changes: 43 additions & 0 deletions GitVersionExe/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace GitVersion
{
using System.Collections.Generic;
using System.IO;

class FileSystem : IFileSystem
{
public void Copy(string @from, string to, bool overwrite)
{
File.Copy(from, to, overwrite);
}

public void Move(string @from, string to)
{
File.Move(from, to);
}

public bool Exists(string file)
{
return File.Exists(file);
}

public void Delete(string path)
{
File.Delete(path);
}

public string ReadAllText(string path)
{
return File.ReadAllText(path);
}

public void WriteAllText(string file, string fileContents)
{
File.WriteAllText(file, fileContents);
}

public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
{
return Directory.GetFiles(directory, searchPattern, searchOption);
}
}
}
2 changes: 2 additions & 0 deletions GitVersionExe/GitVersionExe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
<Compile Include="ArgumentParser.cs" />
<Compile Include="Arguments.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="FileSystem.cs" />
<Compile Include="GitPreparer.cs" />
<Compile Include="HelpWriter.cs" />
<Compile Include="IFileSystem.cs" />
<Compile Include="ProcessHelper.cs" />
<Compile Include="Program.cs" />
<Compile Include="AssemblyInfo.cs" />
Expand Down
16 changes: 16 additions & 0 deletions GitVersionExe/IFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace GitVersion
{
using System.Collections.Generic;
using System.IO;

public interface IFileSystem
{
void Copy(string from, string to, bool overwrite);
void Move(string from, string to);
bool Exists(string file);
void Delete(string path);
string ReadAllText(string path);
void WriteAllText(string file, string fileContents);
IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption);
}
}
2 changes: 1 addition & 1 deletion GitVersionExe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static int Run()
}
}

using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables))
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables, new FileSystem()))
{
var execRun = RunExecCommandIfNeeded(arguments, workingDirectory, variables);
var msbuildRun = RunMsBuildIfNeeded(arguments, workingDirectory, variables);
Expand Down