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

Update version in project.json files #980

Closed
wants to merge 8 commits into from
Closed
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
12 changes: 11 additions & 1 deletion docs/usage/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ Will result in command line argument error

Will iterate through each file and update known attributes (`AssemblyVersion`, `AssemblyFileVersion`, `AssemblyInformationalVersion`).

## Replace version in project.json
`GitVersion.exe /updateprojectjson` will recursively search for all `project.json` files and set the `InformationalVersion` as
the value of the `version` element in the root of the JSON document.

The `dotnet build` command uses the `major.minor.patch` version from the `project.json` as the file and assembly version if the corresponding
attributes do not exist and also automatically adds the `AssemblyInformationalVersion` attribute with the full version excluding metadata.

### Example:
`GitVersion.exe /updateprojectjson`

## Override config
`/overrideconfig [key=value]` will override appropriate key from 'GitVersion.yml'.

Expand All @@ -64,4 +74,4 @@ At the moment only `tag-prefix` option is supported. Read more about [Configurat
It will not change config file 'GitVersion.yml'.

### Example: How to override configuration option 'tag-prefix' to use prefix 'custom'
`GitVersion.exe /output json /overrideconfig tag-prefix=custom`
`GitVersion.exe /output json /overrideconfig tag-prefix=custom`
17 changes: 16 additions & 1 deletion src/GitVersionCore.Tests/TestFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using GitVersion.Helpers;
Expand Down Expand Up @@ -61,7 +62,21 @@ public void WriteAllText(string file, string fileContents)

public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
{
throw new NotImplementedException();
var files = searchOption == SearchOption.TopDirectoryOnly
? fileSystem.Keys.Where(f => Path.GetDirectoryName(f).Equals(directory, StringComparison.CurrentCultureIgnoreCase))
: fileSystem.Keys.Where(f => Path.GetDirectoryName(f).StartsWith(directory, StringComparison.CurrentCultureIgnoreCase));

if (searchPattern.StartsWith("*"))
{
var endsWith = searchPattern.Substring(1);
if(endsWith.Contains('*') || endsWith.Contains('?'))
throw new NotImplementedException();

return files.Where(f => f.EndsWith(endsWith, StringComparison.CurrentCultureIgnoreCase));
}
if (searchPattern.Contains('*') || searchPattern.Contains('?'))
throw new NotImplementedException();
return files.Where(f => Path.GetFileName(f).Equals(searchPattern, StringComparison.CurrentCultureIgnoreCase));
}

public Stream OpenWrite(string path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0"
}
},
/*
Comments in JSON; What a world we live in
*/
"version": "1.2.3-foo.4+2.Branch.alpha.Sha.ADF.bar",
//"version": "1.1.0",
"runtimes": {
"win8-x64": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0"
}
},
"version": "1.2.3-foo.4+2.Branch.alpha.Sha.ADF.bar",
"runtimes": {
"win8-x64": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0"
}
},
"version": "1.2.3-foo.4+2.Branch.alpha.Sha.ADF.bar",
"runtimes": {
"win8-x64": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"dependencies"
:
{
"Microsoft.NETCore.App": {
"version": "1.0.0"
}
},
"version"
:
"1.2.3-foo.4+2.Branch.alpha.Sha.ADF.bar"

,


"runtimes": {
"win8-x64": {}
}
}
5 changes: 5 additions & 0 deletions src/GitVersionExe.Tests/GitVersionExe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
<Compile Include="HelpWriterTests.cs" />
<Compile Include="JsonOutputOnBuildServer.cs" />
<Compile Include="MsBuildProjectArgTest.cs" />
<Compile Include="ProjectJsonFileUpdateTests.cs" />
<Compile Include="ProjectJsonVersionReplacerTests.cs" />
<Compile Include="PullRequestInTeamCityTest.cs" />
<Compile Include="AssemblyInfoFileUpdateTests.cs" />
</ItemGroup>
Expand All @@ -126,6 +128,9 @@
<Content Include="Approved\fs\AssemblyInfoFileUpdateTests.ShouldCreateAssemblyInfoFileAtPathWhenNotExistsAndEnsureAssemblyInfo.approved.txt" />
<Content Include="Approved\fs\AssemblyInfoFileUpdateTests.ShouldCreateAssemblyInfoFilesAtPathWhenNotExistsAndEnsureAssemblyInfo.approved.txt" />
<Content Include="Approved\fs\AssemblyInfoFileUpdateTests.ShouldCreateAssemblyInfoFileWhenNotExistsAndEnsureAssemblyInfo.approved.txt" />
<Content Include="Approved\ProjectJsonVersionReplacerTests.ShouldOnlyReplaceRootVersionValue.approved.txt" />
<Content Include="Approved\ProjectJsonVersionReplacerTests.ShouldReplaceSuccessfullyWhenValueIsNull.approved.txt" />
<Content Include="Approved\ProjectJsonVersionReplacerTests.ShouldWorkAndPreserveFormattingInWeirdlyFormattedJson.approved.txt" />
<Content Include="Approved\vb\AssemblyInfoFileUpdateTests.ShouldAddAssemblyInformationalVersionWhenUpdatingAssemblyVersionFile.approved.txt" />
<Content Include="Approved\vb\AssemblyInfoFileUpdateTests.ShouldCreateAssemblyInfoFileAtPathWhenNotExistsAndEnsureAssemblyInfo.approved.txt" />
<Content Include="Approved\vb\AssemblyInfoFileUpdateTests.ShouldCreateAssemblyInfoFilesAtPathWhenNotExistsAndEnsureAssemblyInfo.approved.txt" />
Expand Down
106 changes: 106 additions & 0 deletions src/GitVersionExe.Tests/ProjectJsonFileUpdateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
namespace GitVersionExe.Tests
{
using System;
using System.IO;
using GitVersion;
using GitVersionCore.Tests;
using NUnit.Framework;
using Shouldly;

public class ProjectJsonFileUpdateTests
{

private static readonly string _testPath = Environment.OSVersion.Platform == PlatformID.Unix ? "/usr/TestPath" : @"x:\TestPath";
private static string _projectJson = "{\n\"version\": \"\"\n}".Replace("\n", Environment.NewLine);
private static string _replacedJson = "{\n\"version\": \"1.2.3-foo.4+2.Branch.alpha.Sha.ADF.bar\"\n}".Replace("\n", Environment.NewLine);

[Test]
public void ShouldCreateBackupsOfTheOriginalFilesAndRemoveThem()
{
var fs = new TestFileSystem();
var filename = CreateProjectJson(fs, "MyProj");
using (CreateTestProjectJsonFileUpdate(fs))
{
fs.ReadAllText(filename + ".bak").ShouldBe(_projectJson);
}
fs.Exists(filename + ".bak").ShouldBe(false);
}

[Test]
public void ShouldReplaceJsonAndThenRestore()
{
var fs = new TestFileSystem();
var filename = CreateProjectJson(fs, "MyProj");
using (CreateTestProjectJsonFileUpdate(fs))
{
fs.ReadAllText(filename).ShouldBe(_replacedJson);
}
fs.ReadAllText(filename).ShouldBe(_projectJson);
}


[Test]
public void ShouldReplaceJsonAndNotRestoreIfDoNotRestoreFilesCalled()
{
var fs = new TestFileSystem();
var filename = CreateProjectJson(fs, "MyProj");
using (var update = CreateTestProjectJsonFileUpdate(fs))
{
fs.ReadAllText(filename).ShouldBe(_replacedJson);
update.DoNotRestoreFiles();
}
fs.ReadAllText(filename).ShouldBe(_replacedJson);
}

[Test]
public void ShouldRemoveBackupsIfDoNotRestoreFilesCalled()
{
var fs = new TestFileSystem();
var filename = CreateProjectJson(fs, "MyProj");
using (var update = CreateTestProjectJsonFileUpdate(fs))
{
fs.Exists(filename + ".bak").ShouldBe(true);
update.DoNotRestoreFiles();
}
fs.Exists(filename + ".bak").ShouldBe(false);
}

[Test]
public void ShouldNotReplaceJsonOutsideTestPath()
{
var fs = new TestFileSystem();
var filename = CreateProjectJson(fs, "..");
using (CreateTestProjectJsonFileUpdate(fs))
{
fs.ReadAllText(filename).ShouldBe(_projectJson);
}
}


private string CreateProjectJson(TestFileSystem fs, string subdir)
{
var projectJsonFileName = Path.GetFullPath(Path.Combine(_testPath, subdir, "project.json"));
fs.WriteAllText(projectJsonFileName, _projectJson);
return projectJsonFileName;
}


private ProjectJsonFileUpdate CreateTestProjectJsonFileUpdate(TestFileSystem fs)
{

var semVer = new SemanticVersion(1, 2, 3)
{
BuildMetaData = new SemanticVersionBuildMetaData(2, "alpha", "ADF", new DateTimeOffset(2011, 2, 3, 4, 5, 6, 7, TimeSpan.FromHours(2)), "bar"),
PreReleaseTag = new SemanticVersionPreReleaseTag("foo", 4)
};
var variables = VariableProvider.GetVariablesFor(semVer, new TestEffectiveConfiguration(), true);

return new ProjectJsonFileUpdate(
new Arguments() { UpdateProjectJson = true },
_testPath,
variables,
fs
);
}
}
}
Loading