-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionManager.cs
More file actions
151 lines (134 loc) · 6.18 KB
/
Copy pathVersionManager.cs
File metadata and controls
151 lines (134 loc) · 6.18 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
using System;
using System.IO;
namespace VersionManager
{
public static class VersionManager
{
public static void CreateVersion(string projectName, ArchiveMode archiveMode)
{
ConsoleUI.PrintHeader();
ConsoleUI.PrintProjectInfo(projectName);
ConsoleUI.PrintStep("Checking project...");
string projectPath = ProjectManager.GetProjectPath(projectName);
if (!ProjectManager.ProjectExists(projectName))
{
Logger.LogOperation(projectName, "Version failed", "project not found");
ConsoleUI.PrintErrorProjectMissing(projectName, ProjectManager.GetExpectedProjectLocations(projectName));
return;
}
ConsoleUI.PrintStepResult("OK");
ConsoleUI.PrintStep("Checking Git...");
if (!GitManager.IsGitInstalled())
{
ConsoleUI.PrintErrorGitMissing();
return;
}
ConsoleUI.PrintStepResult("OK");
ConsoleUI.PrintStep("Detecting changes...");
if (!GitManager.IsGitRepository(projectPath))
{
GitManager.InitializeRepository(projectPath);
}
if (!GitManager.HasChanges(projectPath))
{
Logger.LogOperation(projectName, "Version skipped", "no changes detected");
ConsoleUI.PrintErrorNoChanges();
return;
}
ConsoleUI.PrintStepResult("Changes detected");
string comment = ConsoleUI.PromptComment();
if (string.IsNullOrWhiteSpace(comment))
{
comment = "Version created by Version Manager";
}
ConsoleUI.PrintStep("Creating version...");
try
{
GitManager.StageAll(projectPath);
GitManager.Commit(projectPath, comment);
string commitHash = GitManager.GetHeadCommitHash(projectPath);
ProjectManager.EnsureStorageExists(projectName);
string storagePath = ProjectManager.GetStoragePath(projectName);
string seq = HistoryManager.GetNextVersionSequence(storagePath);
DateTime versionDate = DateTime.Now;
HistoryManager.AppendEntry(storagePath, projectName, seq, versionDate, comment, commitHash, archiveMode);
if (archiveMode == ArchiveMode.Git)
{
string zipFileName = HistoryManager.GetGitArchiveFileName(seq);
string zipFilePath = Path.Combine(storagePath, zipFileName);
ArchiveManager.CreateVersionArchive(projectPath, zipFilePath);
ConsoleUI.PrintStepResult("Success");
ConsoleUI.PrintSuccess(zipFileName);
Logger.LogOperation(projectName, $"Version {seq} committed and archived (git)", zipFileName);
}
else if (archiveMode == ArchiveMode.Project)
{
string zipFileName = HistoryManager.GetProjectArchiveFileName(seq);
string zipFilePath = Path.Combine(storagePath, zipFileName);
ArchiveManager.CreateSnapshotArchive(projectPath, zipFilePath);
ConsoleUI.PrintStepResult("Success");
ConsoleUI.PrintSuccess(zipFileName);
Logger.LogOperation(projectName, $"Version {seq} committed and archived (project)", zipFileName);
}
else
{
ConsoleUI.PrintStepResult("Success");
ConsoleUI.PrintSuccessCommit(seq);
Logger.LogOperation(projectName, $"Version {seq} committed", "no archive");
}
}
catch (Exception ex)
{
Logger.LogError($"Project '{projectName}' - Failed to create version", ex);
ConsoleUI.PrintErrorArchiveFailed();
}
}
public static void CreateSnapshot(string projectName)
{
ConsoleUI.PrintHeader();
ConsoleUI.PrintProjectInfo(projectName);
ConsoleUI.PrintStep("Checking project...");
string projectPath = ProjectManager.GetProjectPath(projectName);
if (!ProjectManager.ProjectExists(projectName))
{
Logger.LogOperation(projectName, "Version failed", "project not found");
ConsoleUI.PrintErrorProjectMissing(projectName, ProjectManager.GetExpectedProjectLocations(projectName));
return;
}
ConsoleUI.PrintStepResult("OK");
ConsoleUI.PrintStep("Creating snapshot...");
try
{
ProjectManager.EnsureStorageExists(projectName);
string storagePath = ProjectManager.GetStoragePath(projectName);
string seq = GetNextSnapshotSequence(storagePath);
string zipFileName = $"Snapshot{seq}.zip";
string zipFilePath = Path.Combine(storagePath, zipFileName);
ArchiveManager.CreateSnapshotArchive(projectPath, zipFilePath);
ConsoleUI.PrintStepResult("Success");
ConsoleUI.PrintSuccess(zipFileName);
Logger.LogOperation(projectName, "Snapshot created", zipFileName);
}
catch (Exception ex)
{
Logger.LogError($"Project '{projectName}' - Failed to create snapshot", ex);
ConsoleUI.PrintErrorArchiveFailed();
}
}
private static string GetNextSnapshotSequence(string storagePath)
{
int maxSeq = 0;
if (Directory.Exists(storagePath))
{
string[] files = Directory.GetFiles(storagePath, "Snapshot*.zip");
foreach (string file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
if (fileName.Length > 8 && int.TryParse(fileName.Substring(8), out int num) && num > maxSeq)
maxSeq = num;
}
}
return (maxSeq + 1).ToString("D3");
}
}
}