-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSystemHelper.cs
144 lines (124 loc) · 5.51 KB
/
FileSystemHelper.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace Enterwell.CI.Changelog.Shared
{
/// <summary>
/// Helper class for everything related to files and directories.
/// </summary>
public static class FileSystemHelper
{
public static string ChangeDirectoryName = "changes";
/// <summary>
/// Used to ensure that the changes directory exists. If it does not exist, the directory is created.
/// </summary>
public static void EnsureChangesDirectoryExists(string directoryPath)
{
var changesDirectoryPath = Path.Combine(directoryPath, ChangeDirectoryName);
if (!Directory.Exists(changesDirectoryPath))
{
Directory.CreateDirectory(changesDirectoryPath);
}
}
/// <summary>
/// Creates the file on the given file path.
/// </summary>
/// <param name="filePath">Path to the file that needs to be created.</param>
/// <returns>A value <see cref="Tuple"/> with 2 components. A <see cref="bool"/> that represents if the file was successfully created and a
/// <see cref="string"/> that specifies the reason if not.</returns>
public static (bool isSuccessfull, string reason) CreateFile(string filePath)
{
try
{
var file = File.Create(filePath);
file.Close();
return (true, string.Empty);
}
catch (Exception e)
{
return (false, e.Message);
}
}
/// <summary>
/// Constructs the file name using the arguments passed in by the user.
/// </summary>
/// <param name="inputType">Change type got by the user.</param>
/// <param name="inputCategory">Change category got by the user.</param>
/// <param name="inputDescription">Change description got by the user.</param>
/// <returns>Name of the file to be saved in the folder where changes are stored.</returns>
public static string ConstructFileName(string inputType, string inputCategory, string inputDescription)
{
var description = inputDescription.Trim();
string fileName;
if (string.IsNullOrWhiteSpace(inputCategory))
{
fileName = $"{inputType} {description}";
}
else
{
var category = inputCategory.Trim();
fileName = $"{inputType} [{category}] {description}";
}
// Replace multiple spaces with a single space for consistency
return Regex.Replace(fileName, @"\s+", " ");
}
/// <summary>
/// Tries to find the nearest `changes` folder.
/// </summary>
/// <returns><see cref="string"/> representing the path to the nearest `changes` folder or an empty string is no such folder exists.</returns>
public static string FindNearestChangesFolder()
{
var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
while (currentDir != null)
{
if (currentDir.EnumerateDirectories("changes").Any())
return Path.Combine(currentDir.FullName, "changes");
currentDir = currentDir.Parent;
}
return string.Empty;
}
/// <summary>
/// Tries to find the project file (either 'package.json' or a '*.csproj' file with a 'Version' tag) in the current directory.
/// </summary>
/// <returns>Project file path if one exists; <see cref="string.Empty"/> otherwise.</returns>
public static string GetTheProjectFile()
{
var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
var currentDirFiles = currentDir.EnumerateFiles().ToList();
var packageJsonFile = currentDirFiles.FirstOrDefault(f => f.Name == "package.json");
// If the package.json file exists, return it
if (packageJsonFile != null)
{
return packageJsonFile.FullName;
}
var csprojFile = currentDirFiles.FirstOrDefault(f => f.Extension == ".csproj");
// If the .csproj file exists check if it contains the 'Version' tag
if (csprojFile != null)
{
var xmlVersionTag = XElement.Load(csprojFile.FullName)
.Descendants()
.FirstOrDefault(e =>
e.Name.ToString().ToLowerInvariant() == "version" &&
e.Parent?.Name.ToString().ToLowerInvariant() == "propertygroup");
if (xmlVersionTag != null)
{
return csprojFile.FullName;
}
}
return string.Empty;
}
/// <summary>
/// Gets the correctly-cased file path equivalent.
/// </summary>
/// <param name="filePath">File path for which to find the equivalent.</param>
/// <returns>Correctly-cased file path.</returns>
public static string GetFilePathCaseInsensitive(string filePath)
{
var absolutePath = Path.GetFullPath(filePath);
var parentDirectory = Directory.GetParent(absolutePath)?.FullName ?? absolutePath;
return Directory.GetFiles(parentDirectory).FirstOrDefault(fp => string.Equals(fp, absolutePath, StringComparison.InvariantCultureIgnoreCase));
}
}
}