-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionHelper.cs
More file actions
165 lines (137 loc) · 5.47 KB
/
Copy pathVersionHelper.cs
File metadata and controls
165 lines (137 loc) · 5.47 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace EasyVersionBackup
{
public static class VersionHelper
{
public static string GetSuggestedVersion(AppSettings settings, BackupPathPair pair)
{
string defaultVersioning = !string.IsNullOrWhiteSpace(pair.Versioning)
? pair.Versioning.Trim()
: settings.DefaultVersioning?.Trim() ?? "none";
if (string.Equals(defaultVersioning, "none", StringComparison.OrdinalIgnoreCase))
{
return string.Empty;
}
string defaultVersion = VersionPatternHelper.CreateVersionFromPattern(defaultVersioning);
if (VersionPatternHelper.IsDatePattern(defaultVersioning))
{
return defaultVersion;
}
string pairKey = SettingsStorage.CreatePairKey(pair.SourceDirectory, pair.TargetDirectory);
string lastUsedVersion = settings.LastUsedVersionsByPair.TryGetValue(pairKey, out string? lastUsed)
? lastUsed
: string.Empty;
string compatibleLastUsedVersion =
VersionPatternHelper.GetHighestCompatibleVersion(
defaultVersion,
new[]
{
lastUsedVersion
});
string highestExistingVersion = GetHighestExistingVersion(pair, defaultVersion, settings.ZipDestinationFiles);
string highestKnownVersion =
VersionPatternHelper.GetHighestCompatibleVersion(
defaultVersion,
new[]
{
defaultVersion,
compatibleLastUsedVersion,
highestExistingVersion
});
if (string.IsNullOrWhiteSpace(highestKnownVersion))
{
return defaultVersion;
}
if (settings.AutoIncrementVersion)
{
return VersionPatternHelper.IncrementVersion(highestKnownVersion);
}
return highestKnownVersion;
}
public static bool IsValidVersion(string version)
{
return VersionPatternHelper.IsValidVersionValue(version);
}
public static string BuildVersionedName(string folderName, string version)
{
if (string.IsNullOrWhiteSpace(version))
{
return folderName;
}
return $"{folderName}_{version}";
}
private static string GetHighestExistingVersion(
BackupPathPair pair,
string defaultVersion,
bool zipDestinationFiles)
{
if (string.IsNullOrWhiteSpace(pair.SourceDirectory) ||
string.IsNullOrWhiteSpace(pair.TargetDirectory) ||
!Directory.Exists(pair.TargetDirectory))
{
return string.Empty;
}
try
{
string sourceFolderName =
new DirectoryInfo(pair.SourceDirectory).Name;
string escapedSourceFolderName =
Regex.Escape(sourceFolderName);
Regex zipRegex = new Regex(
$"^{escapedSourceFolderName}_(?<version>.+)\\.zip$",
RegexOptions.IgnoreCase);
Regex directoryRegex = new Regex(
$"^{escapedSourceFolderName}_(?<version>.+)$",
RegexOptions.IgnoreCase);
List<string> foundVersions = new List<string>();
if (zipDestinationFiles)
{
foreach (string filePath in Directory.GetFiles(
pair.TargetDirectory,
"*.zip",
SearchOption.TopDirectoryOnly))
{
string fileName = Path.GetFileName(filePath);
Match match = zipRegex.Match(fileName);
if (match.Success)
{
foundVersions.Add(
match.Groups["version"].Value);
}
}
}
else
{
foreach (string directoryPath in Directory.GetDirectories(
pair.TargetDirectory,
"*",
SearchOption.TopDirectoryOnly))
{
string directoryName =
Path.GetFileName(directoryPath);
Match match =
directoryRegex.Match(directoryName);
if (match.Success)
{
foundVersions.Add(
match.Groups["version"].Value);
}
}
}
return VersionPatternHelper.GetHighestCompatibleVersion(
defaultVersion,
foundVersions);
}
catch (Exception exception)
{
BackupLogger.WriteNormalLine(
$"VERSION SCAN WARNING | source=\"{pair.SourceDirectory}\" | target=\"{pair.TargetDirectory}\" | error=\"{exception.Message}\"");
return string.Empty;
}
}
}
}