namespace Cake.DotNetVersion { using System; using System.Collections.Generic; using System.IO; using System.Text; using Cake.Core; using Cake.Core.IO; using Cake.Core.Tooling; using Newtonsoft.Json; using Newtonsoft.Json.Linq; /// /// Applies a version to a set of project.json files. /// public class DotNetCoreVersionRunner : Tool { /// /// Initialises a new instance of /// /// The Cake file system. public DotNetCoreVersionRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator toolLocator) : base(fileSystem, environment, processRunner, toolLocator) { } public void Run(DotNetCoreVersionSettings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (settings.Files != null) { foreach (var file in settings.Files) { RunCore(file, settings.Version); } } else if (settings.File != null) { RunCore(settings.File, settings.Version); } } protected override IEnumerable GetToolExecutableNames() { return new string[0]; } protected override string GetToolName() { return "DotNetCoreVersion"; } private void RunCore(FilePath filePath, string version) { JObject project; using (var file = new FileStream(filePath.FullPath, FileMode.Open)) using (var stream = new StreamReader(file)) using (var json = new JsonTextReader(stream)) { project = JObject.Load(json); } var versionAttr = project.Property("version"); if (versionAttr == null) { project.Add("version", new JValue(version)); } else { versionAttr.Value = version; } File.WriteAllText(filePath.FullPath, project.ToString(), Encoding.UTF8); } } }