-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathArtifactZipper.cs
114 lines (96 loc) · 4.57 KB
/
ArtifactZipper.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System.IO.Compression;
namespace Azure.Functions.ArtifactAssembler
{
internal sealed class ArtifactZipper
{
private readonly string _rootWorkingDirectory;
public ArtifactZipper(string rootWorkingDirectory)
{
_rootWorkingDirectory = rootWorkingDirectory;
}
internal void ZipCliArtifacts()
{
Console.WriteLine("Zipping CLI Artifacts");
string stagingDirectory = Path.Combine(_rootWorkingDirectory, Constants.StagingDirName, Constants.CliOutputArtifactDirectoryName);
// Get all directories in the staging directory
var directories = Directory.EnumerateDirectories(stagingDirectory);
foreach (var dir in directories)
{
// Create worker directory if it doesn't already exist
CreateWorkerDirectoryIfDoesNotExist(dir);
// Define zip file path and name
string zipFileName = $"{new DirectoryInfo(dir).Name}.zip";
string zipFilePath = Path.Combine(stagingDirectory, zipFileName);
// Compress directory into zip file
ZipFile.CreateFromDirectory(dir, zipFilePath, CompressionLevel.Optimal, includeBaseDirectory: false);
Console.WriteLine($"Zipped: {dir} -> {zipFilePath}");
// Verify zip creation and delete original directory to free up space
if (File.Exists(zipFilePath))
{
Console.WriteLine($"Successfully created zip: {zipFilePath}");
Directory.Delete(dir, true);
Console.WriteLine($"Deleted original directory: {dir}");
}
else
{
Console.WriteLine($"Failed to create zip for: {dir}");
}
}
Console.WriteLine("All directories zipped successfully!");
}
internal void ZipVisualStudioArtifacts()
{
Console.WriteLine("Zipping Visual Studio Artifacts");
string stagingDirectory = Path.Combine(_rootWorkingDirectory, Constants.StagingDirName, Constants.VisualStudioOutputArtifactDirectoryName);
// Get all directories in the staging directory
var directories = Directory.EnumerateDirectories(stagingDirectory);
foreach (var dir in directories)
{
// Define zip file path and name
string zipFileName = $"{new DirectoryInfo(dir).Name}.zip";
string zipFilePath = Path.Combine(stagingDirectory, zipFileName);
// Compress directory into zip file
FileUtilities.CreateZipFile(dir, zipFilePath);
Console.WriteLine($"Zipped: {dir} -> {zipFilePath}");
// Verify zip creation and delete original directory to free up space
if (File.Exists(zipFilePath))
{
Console.WriteLine($"Successfully created zip: {zipFilePath}");
Directory.Delete(dir, true);
Console.WriteLine($"Deleted original directory: {dir}");
}
else
{
Console.WriteLine($"Failed to create zip for: {dir}");
}
}
Console.WriteLine("All directories zipped successfully!");
}
internal void CreateWorkerDirectoryIfDoesNotExist(string dir)
{
string workersPath = Path.Combine(dir, "workers");
// Ensure 'workers' directory exists
if (!Directory.Exists(workersPath))
{
Directory.CreateDirectory(workersPath);
Console.WriteLine($"Created missing 'workers' directory in {dir}");
}
else
{
Console.WriteLine($"'workers' directory already exists in {dir}");
}
// Add placeholder file if 'workers' directory is empty
if (Directory.GetFiles(workersPath).Length == 0 && Directory.GetDirectories(workersPath).Length == 0)
{
File.WriteAllText(Path.Combine(workersPath, "placeholder.txt"), "Placeholder file");
Console.WriteLine($"Created placeholder file in empty 'workers' directory in {dir}");
}
else
{
Console.WriteLine($"'workers' directory is not empty in {dir}");
}
}
}
}