-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileWriter.cs
121 lines (90 loc) · 3.73 KB
/
FileWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Diagnostics;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.RegularExpressions;
using ReleaseTool.PackageManifest;
namespace ReleaseTool;
internal class FileWriter
{
public FileWriter(PackageParser packageParser)
{
_packageParser = packageParser;
}
public void WriteVersionFile(Version version, string versionFilePath)
{
var sb = new StringBuilder();
var allLines = File.ReadAllLines(versionFilePath);
foreach (var line in allLines)
{
if (!line.Contains("new Version"))
{
sb.AppendLine(line);
continue;
}
var newVersionString = $"new Version({version.Major}, {version.Minor}, {version.Build})";
const string regexPattern = @"new Version\([\s]*([0-9]+)[\s]*,[\s]*([0-9]+)[\s]*,[\s]*([0-9]+)[s]*\)";
var regex = new Regex(regexPattern);
var replaced = regex.Replace(line, newVersionString);
if (replaced == line)
{
throw new Exception($"Failed to regex replace `{versionFilePath}`");
}
sb.AppendLine(replaced);
}
File.WriteAllText(versionFilePath, sb.ToString());
Console.WriteLine($"Updated the version file in {versionFilePath}");
}
public void WriteChangelogFile(Version version, string changelog, string changelogFilePath)
{
var sb = new StringBuilder();
var versionLine = $"{version.Major}.{version.Minor}.{version.Build}:";
sb.AppendLine(versionLine);
sb.AppendLine();
changelog = changelog.Trim();
var newChangelogLines = changelog.Split("\n");
var oldChangelogLines = File.ReadAllLines(changelogFilePath);
foreach (var line in newChangelogLines)
{
sb.AppendLine(line);
}
sb.AppendLine();
foreach (var line in oldChangelogLines)
{
sb.AppendLine(line);
}
File.WriteAllText(changelogFilePath, sb.ToString());
Console.WriteLine($"Updated the changelog file in `{changelogFilePath}`");
}
public void WritePackageJsonFile(Version version, string packageJsonFilePath)
{
_packageParser.GetVersionProperty(packageJsonFilePath, out var json);
json[PackageParser.PackageJsonVersionPropertyName] = $"{version.Major}.{version.Minor}.{version.Build}";
var options = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
var updatedJsonContent = json.ToJsonString(options);
File.WriteAllText(packageJsonFilePath, updatedJsonContent);
Console.WriteLine($"Updated the package.json file in `{packageJsonFilePath}`");
}
public void MoveImportedSamples(PackageInfo packageInfo, ReleaseFilesInfo filesInfo, Version currentVersion,
Version newVersion)
{
var currentDirPath = Path.Combine(filesInfo.AssetsSamplesDirectory, packageInfo.DisplayName,
currentVersion.ToString());
var currentDirMetaPath = $"{currentDirPath}.meta";
var newDirPath = Path.Combine(filesInfo.AssetsSamplesDirectory, packageInfo.DisplayName,
newVersion.ToString());
var newDirMetaPath = $"{newDirPath}.meta";
RunCommand("git", $"mv \"{currentDirPath}\" \"{newDirPath}\"");
RunCommand("git", $"mv \"{currentDirMetaPath}\" \"{newDirMetaPath}\"");
}
private readonly PackageParser _packageParser;
private void RunCommand(string command, string args)
{
Console.WriteLine($"Run command: {command} {args}");
Process.Start(command, args);
}
}