diff --git a/docs/usage/command-line.md b/docs/usage/command-line.md index d29b18ec18..3480f97359 100644 --- a/docs/usage/command-line.md +++ b/docs/usage/command-line.md @@ -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'. @@ -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` \ No newline at end of file +`GitVersion.exe /output json /overrideconfig tag-prefix=custom` diff --git a/src/GitVersionCore.Tests/TestFileSystem.cs b/src/GitVersionCore.Tests/TestFileSystem.cs index f78d9c890c..630b041bdc 100644 --- a/src/GitVersionCore.Tests/TestFileSystem.cs +++ b/src/GitVersionCore.Tests/TestFileSystem.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using GitVersion.Helpers; @@ -61,7 +62,21 @@ public void WriteAllText(string file, string fileContents) public IEnumerable 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) diff --git a/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldOnlyReplaceRootVersionValue.approved.txt b/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldOnlyReplaceRootVersionValue.approved.txt new file mode 100644 index 0000000000..f1992dce0c --- /dev/null +++ b/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldOnlyReplaceRootVersionValue.approved.txt @@ -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": {} + } +} diff --git a/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldReplaceSuccessfullyWhenValueIsNull.approved.txt b/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldReplaceSuccessfullyWhenValueIsNull.approved.txt new file mode 100644 index 0000000000..f1992dce0c --- /dev/null +++ b/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldReplaceSuccessfullyWhenValueIsNull.approved.txt @@ -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": {} + } +} diff --git a/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldWorkAndPreserveFormattingInWeirdlyFormattedJson.approved.txt b/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldWorkAndPreserveFormattingInWeirdlyFormattedJson.approved.txt new file mode 100644 index 0000000000..4945c3fc83 --- /dev/null +++ b/src/GitVersionExe.Tests/Approved/ProjectJsonVersionReplacerTests.ShouldWorkAndPreserveFormattingInWeirdlyFormattedJson.approved.txt @@ -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": {} + } +} diff --git a/src/GitVersionExe.Tests/GitVersionExe.Tests.csproj b/src/GitVersionExe.Tests/GitVersionExe.Tests.csproj index 39fc8addb2..af993117c5 100644 --- a/src/GitVersionExe.Tests/GitVersionExe.Tests.csproj +++ b/src/GitVersionExe.Tests/GitVersionExe.Tests.csproj @@ -126,6 +126,8 @@ + + @@ -138,6 +140,9 @@ + + + diff --git a/src/GitVersionExe.Tests/ProjectJsonFileUpdateTests.cs b/src/GitVersionExe.Tests/ProjectJsonFileUpdateTests.cs new file mode 100644 index 0000000000..737eb89f96 --- /dev/null +++ b/src/GitVersionExe.Tests/ProjectJsonFileUpdateTests.cs @@ -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 + ); + } + } +} \ No newline at end of file diff --git a/src/GitVersionExe.Tests/ProjectJsonVersionReplacerTests.cs b/src/GitVersionExe.Tests/ProjectJsonVersionReplacerTests.cs new file mode 100644 index 0000000000..412393a9c6 --- /dev/null +++ b/src/GitVersionExe.Tests/ProjectJsonVersionReplacerTests.cs @@ -0,0 +1,156 @@ +namespace GitVersionExe.Tests +{ + using System; + using System.Runtime.CompilerServices; + using GitVersion; + using GitVersionCore.Tests; + using NUnit.Framework; + using Shouldly; + + public class ProjectJsonVersionReplacerTests + { + + [SetUp] + public void SetUp() + { + ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute(); + } + + [Test] + [MethodImpl(MethodImplOptions.NoInlining)] + [Category("NoMono")] + [Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")] + public void ShouldOnlyReplaceRootVersionValue() + { + var json = +@"{ + ""dependencies"": { + ""Microsoft.NETCore.App"": { + ""version"": ""1.0.0"" + } + }, + ""version"": ""1.1.0"", + ""runtimes"": { + ""win8-x64"": {} + } +} +"; + var result = ProjectJsonVersionReplacer.Replace(json, GetVariables()); + result.HasError.ShouldBe(false); + result.VersionElementNotFound.ShouldBe(false); + result.JsonWithReplacement.ShouldMatchApproved(c => c.SubFolder("Approved")); + } + + [Test] + [MethodImpl(MethodImplOptions.NoInlining)] + [Category("NoMono")] + [Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")] + public void ShouldReplaceSuccessfullyWhenValueIsNull() + { + var json = +@"{ + ""dependencies"": { + ""Microsoft.NETCore.App"": { + ""version"": ""1.0.0"" + } + }, + ""version"": null, + ""runtimes"": { + ""win8-x64"": {} + } +} +"; + var result = ProjectJsonVersionReplacer.Replace(json, GetVariables()); + result.HasError.ShouldBe(false); + result.VersionElementNotFound.ShouldBe(false); + result.JsonWithReplacement.ShouldMatchApproved(c => c.SubFolder("Approved")); + } + + [Test] + [MethodImpl(MethodImplOptions.NoInlining)] + [Category("NoMono")] + [Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")] + public void ShouldWorkAndPreserveFormattingInWeirdlyFormattedJson() + { + var json = +@"{ + ""dependencies"" + : +{ + ""Microsoft.NETCore.App"": { + ""version"": ""1.0.0"" + } + }, + ""version"" + : +""1.1.0"" + +, + + +""runtimes"": { + ""win8-x64"": {} + } +} +"; + var result = ProjectJsonVersionReplacer.Replace(json, GetVariables()); + result.HasError.ShouldBe(false); + result.VersionElementNotFound.ShouldBe(false); + result.JsonWithReplacement.ShouldMatchApproved(c => c.SubFolder("Approved")); + } + + [Test] + public void ShouldNotBombOutOnMalformedJson() + { + var json = +@"{ + ""dependencies: { + ""Microsoft.NETCore.App"": { + ""version"": ""1.0.0"" + } + }, + ""version"": ""1.1.0"", + ""runtimes"": { + ""win8-x64"": {} + } +} +"; + var result = ProjectJsonVersionReplacer.Replace(json, GetVariables()); + result.HasError.ShouldBe(true); + result.Error.ShouldBe("Invalid character after parsing property name. Expected ':' but got: M. Path '', line 3, position 5."); + result.VersionElementNotFound.ShouldBe(false); + } + + [Test] + public void ShouldIndicateWhenNoVersionElementWasFound() + { + var json = +@"{ + ""dependencies"": { + ""Microsoft.NETCore.App"": { + ""version"": ""1.0.0"" + } + }, + ""runtimes"": { + ""win8-x64"": {} + } +} +"; + var result = ProjectJsonVersionReplacer.Replace(json, GetVariables()); + result.HasError.ShouldBe(false); + result.VersionElementNotFound.ShouldBe(true); + } + + + + private static VersionVariables GetVariables() + { + SemanticVersion 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) + }; + return VariableProvider.GetVariablesFor(semVer, new TestEffectiveConfiguration(), true); + } + } +} \ No newline at end of file diff --git a/src/GitVersionExe/ArgumentParser.cs b/src/GitVersionExe/ArgumentParser.cs index 508c85d997..63fc3fceb2 100644 --- a/src/GitVersionExe/ArgumentParser.cs +++ b/src/GitVersionExe/ArgumentParser.cs @@ -268,6 +268,12 @@ public static Arguments ParseArguments(List commandLineArguments) continue; } + if (name.IsSwitch("updateprojectjson")) + { + arguments.UpdateProjectJson = !value.IsFalse(); + continue; + } + if (name.IsSwitch("overrideconfig")) { var keyValueOptions = (value ?? "").Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); diff --git a/src/GitVersionExe/Arguments.cs b/src/GitVersionExe/Arguments.cs index 929f038df3..7788a63ab6 100644 --- a/src/GitVersionExe/Arguments.cs +++ b/src/GitVersionExe/Arguments.cs @@ -40,6 +40,7 @@ public Arguments() public bool UpdateAssemblyInfo; public ISet UpdateAssemblyInfoFileName; public bool EnsureAssemblyInfo; + public bool UpdateProjectJson; public bool ShowConfig; public bool NoFetch; diff --git a/src/GitVersionExe/AssemblyInfoFileUpdate.cs b/src/GitVersionExe/AssemblyInfoFileUpdate.cs index d9d294193f..4c0cbfaeb6 100644 --- a/src/GitVersionExe/AssemblyInfoFileUpdate.cs +++ b/src/GitVersionExe/AssemblyInfoFileUpdate.cs @@ -9,11 +9,8 @@ namespace GitVersion using GitVersion.VersionAssemblyInfoResources; // TODO: Consolidate this with GitVersionTask.UpdateAssemblyInfo. @asbjornu - class AssemblyInfoFileUpdate : IDisposable + class AssemblyInfoFileUpdate : FileUpdateBase { - List restoreBackupTasks = new List(); - List cleanupBackupTasks = new List(); - public AssemblyInfoFileUpdate(Arguments args, string workingDirectory, VersionVariables variables, IFileSystem fileSystem) { if (!args.UpdateAssemblyInfo) return; @@ -129,26 +126,5 @@ static bool EnsureVersionAssemblyInfoFile(Arguments arguments, IFileSystem fileS Logger.WriteWarning(string.Format("No version assembly info template available to create source file '{0}'", arguments.UpdateAssemblyInfoFileName)); return false; } - - public void Dispose() - { - foreach (var restoreBackup in restoreBackupTasks) - { - restoreBackup(); - } - - cleanupBackupTasks.Clear(); - restoreBackupTasks.Clear(); - } - - public void DoNotRestoreAssemblyInfo() - { - foreach (var cleanupBackupTask in cleanupBackupTasks) - { - cleanupBackupTask(); - } - cleanupBackupTasks.Clear(); - restoreBackupTasks.Clear(); - } } } \ No newline at end of file diff --git a/src/GitVersionExe/Extensions.cs b/src/GitVersionExe/Extensions.cs index e5ea80e9ca..60530c0eb3 100644 --- a/src/GitVersionExe/Extensions.cs +++ b/src/GitVersionExe/Extensions.cs @@ -104,7 +104,8 @@ public static bool ArgumentRequiresValue(this string argument, int argumentIndex "init", "updateassemblyinfo", "ensureassemblyinfo", - "nofetch" + "nofetch", + "updateprojectjson" }; var argumentMightRequireValue = !booleanArguments.Contains(argument.Substring(1), StringComparer.OrdinalIgnoreCase); diff --git a/src/GitVersionExe/FileUpdateBase.cs b/src/GitVersionExe/FileUpdateBase.cs new file mode 100644 index 0000000000..a62527c1e9 --- /dev/null +++ b/src/GitVersionExe/FileUpdateBase.cs @@ -0,0 +1,32 @@ +namespace GitVersion +{ + using System; + using System.Collections.Generic; + + public abstract class FileUpdateBase : IDisposable + { + protected readonly List restoreBackupTasks = new List(); + protected readonly List cleanupBackupTasks = new List(); + + public void Dispose() + { + foreach (var restoreBackup in restoreBackupTasks) + { + restoreBackup(); + } + + cleanupBackupTasks.Clear(); + restoreBackupTasks.Clear(); + } + + public void DoNotRestoreFiles() + { + foreach (var cleanupBackupTask in cleanupBackupTasks) + { + cleanupBackupTask(); + } + cleanupBackupTasks.Clear(); + restoreBackupTasks.Clear(); + } + } +} \ No newline at end of file diff --git a/src/GitVersionExe/GitVersionExe.csproj b/src/GitVersionExe/GitVersionExe.csproj index db387204a5..41e963d5f2 100644 --- a/src/GitVersionExe/GitVersionExe.csproj +++ b/src/GitVersionExe/GitVersionExe.csproj @@ -53,6 +53,10 @@ ..\packages\LibGit2Sharp.0.23.0-pre20150419160303\lib\net40\LibGit2Sharp.dll True + + ..\packages\Newtonsoft.Json.9.0.1\lib\net40\Newtonsoft.Json.dll + True + @@ -67,10 +71,13 @@ + + + @@ -161,7 +168,7 @@ - + diff --git a/src/GitVersionExe/HelpWriter.cs b/src/GitVersionExe/HelpWriter.cs index 8a8810c56f..de189bac82 100644 --- a/src/GitVersionExe/HelpWriter.cs +++ b/src/GitVersionExe/HelpWriter.cs @@ -39,6 +39,12 @@ Specify name of AssemblyInfo file. Can also /updateAssemblyInfo GlobalAssemblyIn it be created with these attributes: AssemblyFileVersion, AssemblyVersion and AssemblyInformationalVersion --- Supports writing version info for: C#, F#, VB + + # .NET Core + /updateprojectjson + Will recursively search for all 'project.json' files and update the value of the 'version' attribute in + the root of the JSON document + # Remote repository args /url Url to remote git repository. /b Name of the branch to use on the remote repository, must be used in combination with /url. diff --git a/src/GitVersionExe/ProjectJsonFileUpdate.cs b/src/GitVersionExe/ProjectJsonFileUpdate.cs new file mode 100644 index 0000000000..2939710183 --- /dev/null +++ b/src/GitVersionExe/ProjectJsonFileUpdate.cs @@ -0,0 +1,55 @@ +using GitVersion.Helpers; + +namespace GitVersion +{ + using System.IO; + using System.Linq; + + public class ProjectJsonFileUpdate : FileUpdateBase + { + public ProjectJsonFileUpdate(Arguments arguments, string targetPath, VersionVariables variables, IFileSystem fileSystem) + { + if (!arguments.UpdateProjectJson) + return; + + if (arguments.Output != OutputType.Json) + Logger.WriteInfo("Updating project.json files"); + + var files = fileSystem.DirectoryGetFiles(targetPath, "project.json", SearchOption.AllDirectories).ToArray(); + foreach (var file in files) + { + ReplaceInFile(variables, fileSystem, file); + } + } + + private void ReplaceInFile(VersionVariables variables, IFileSystem fileSystem, string file) + { + var backupFile = file + ".bak"; + fileSystem.Copy(file, backupFile, true); + cleanupBackupTasks.Add(() => fileSystem.Delete(backupFile)); + restoreBackupTasks.Add(() => + { + if (!fileSystem.Exists(backupFile)) + return; + fileSystem.Copy(backupFile, file, true); + fileSystem.Delete(backupFile); + }); + + var json = fileSystem.ReadAllText(file); + var result = ProjectJsonVersionReplacer.Replace(json, variables); + if (result.HasError) + { + Logger.WriteError(string.Format("An error occured replacing version in {0}: {1}", file, result.Error)); + } + else if (result.VersionElementNotFound) + { + Logger.WriteWarning(string.Format("The version element was not found in {0}", file)); + } + else + { + Logger.WriteInfo(string.Format("Replacing version in {0}", file)); + fileSystem.WriteAllText(file, result.JsonWithReplacement); + } + } + } +} \ No newline at end of file diff --git a/src/GitVersionExe/ProjectJsonVersionReplacer.cs b/src/GitVersionExe/ProjectJsonVersionReplacer.cs new file mode 100644 index 0000000000..a44019de1f --- /dev/null +++ b/src/GitVersionExe/ProjectJsonVersionReplacer.cs @@ -0,0 +1,113 @@ +namespace GitVersion +{ + using System; + using System.IO; + using System.Text; + using Newtonsoft.Json; + + internal static class ProjectJsonVersionReplacer + { + + public static ReplacementResult Replace(string json, VersionVariables variables) + { + try + { + var pos = GetVersionPosition(json); + if (pos == null) + return new ReplacementResult + { + VersionElementNotFound = true + }; + + return new ReplacementResult() + { + JsonWithReplacement = ReplaceVersion(json, pos, variables.InformationalVersion) + }; + } + catch (Exception ex) + { + return new ReplacementResult() + { + Error = ex.Message, + HasError = true + }; + } + } + + private static string ReplaceVersion(string contents, VersionPosition pos, string version) + { + var sb = new StringBuilder(); + using (var reader = new StringReader(contents)) + using (var writer = new StringWriter(sb)) + { + for (var x = 1; x < pos.LineNumber; x++) + writer.WriteLine(reader.ReadLine()); + + var str = reader.ReadLine(); + if (str != null) + { + if (pos.IsNull) + { + writer.Write(str.Substring(0, pos.LinePosition - 4)); + writer.Write("\""); + writer.Write(version); + writer.Write("\""); + writer.WriteLine(str.Substring(pos.LinePosition)); + } + else + { + writer.Write(str.Substring(0, pos.LinePosition - pos.Length - 1)); + writer.Write(version); + writer.WriteLine(str.Substring(pos.LinePosition - 1)); + } + + } + writer.Write(reader.ReadToEnd()); + } + return sb.ToString(); + } + + private static VersionPosition GetVersionPosition(string contents) + { + using (var r = new JsonTextReader(new StringReader(contents))) + while (r.Read()) + { + if (r.Depth == 1 && r.TokenType == JsonToken.PropertyName && (string)r.Value == "version") + { + r.Read(); + if(r.TokenType == JsonToken.Null) + return new VersionPosition(r.LineNumber, r.LinePosition, 4, true); + + if (r.TokenType == JsonToken.String) + return new VersionPosition(r.LineNumber, r.LinePosition, ((string)r.Value).Length, false); + } + } + + return null; + } + + class VersionPosition + { + public VersionPosition(int lineNumber, int linePosition, int length, bool isNull) + { + LineNumber = lineNumber; + LinePosition = linePosition; + Length = length; + IsNull = isNull; + } + + public int LineNumber { get; set; } + public int LinePosition { get; set; } + public int Length { get; set; } + public bool IsNull { get; set; } + } + + public class ReplacementResult + { + public bool VersionElementNotFound { get; set; } + public string JsonWithReplacement { get; set; } + public string Error { get; set; } + public bool HasError { get; set; } + } + } +} \ No newline at end of file diff --git a/src/GitVersionExe/SpecifiedArgumentRunner.cs b/src/GitVersionExe/SpecifiedArgumentRunner.cs index 3b75c31298..b107f870a6 100644 --- a/src/GitVersionExe/SpecifiedArgumentRunner.cs +++ b/src/GitVersionExe/SpecifiedArgumentRunner.cs @@ -57,12 +57,14 @@ public static void Run(Arguments arguments, IFileSystem fileSystem) } using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, targetPath, variables, fileSystem)) + using (var projectJsonUpdate = new ProjectJsonFileUpdate(arguments, targetPath, variables, fileSystem)) { var execRun = RunExecCommandIfNeeded(arguments, targetPath, variables); var msbuildRun = RunMsBuildIfNeeded(arguments, targetPath, variables); if (!execRun && !msbuildRun) { - assemblyInfoUpdate.DoNotRestoreAssemblyInfo(); + assemblyInfoUpdate.DoNotRestoreFiles(); + projectJsonUpdate.DoNotRestoreFiles(); //TODO Put warning back //if (!context.CurrentBuildServer.IsRunningInBuildAgent()) //{ @@ -72,6 +74,7 @@ public static void Run(Arguments arguments, IFileSystem fileSystem) //} } } + } static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory, VersionVariables variables) diff --git a/src/GitVersionExe/packages.config b/src/GitVersionExe/packages.config index 2dc57dc5c0..c0f409740d 100644 --- a/src/GitVersionExe/packages.config +++ b/src/GitVersionExe/packages.config @@ -7,6 +7,7 @@ + \ No newline at end of file