forked from dotnet/ResXResourceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectFile.cs
More file actions
148 lines (117 loc) · 4.65 KB
/
Copy pathProjectFile.cs
File metadata and controls
148 lines (117 loc) · 4.65 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
namespace tomenglertde.ResXManager.Model
{
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using JetBrains.Annotations;
using TomsToolbox.Wpf;
/// <summary>
/// Represents a file associated with a project.
/// </summary>
public class ProjectFile : ObservableObject
{
[CanBeNull]
private string _fingerPrint;
/// <summary>
/// Initializes a new instance of the <see cref="ProjectFile" /> class.
/// </summary>
/// <param name="filePath">Name of the file.</param>
/// <param name="rootFolder">The root folder to calculate the relative path from.</param>
/// <param name="projectName">Name of the project.</param>
/// <param name="uniqueProjectName">Unique name of the project file.</param>
public ProjectFile([NotNull] string filePath, [NotNull] string rootFolder, [CanBeNull] string projectName, [CanBeNull] string uniqueProjectName)
{
FilePath = filePath;
RelativeFilePath = GetRelativePath(rootFolder, filePath);
Extension = Path.GetExtension(FilePath);
ProjectName = projectName;
UniqueProjectName = uniqueProjectName;
}
/// <summary>
/// Gets the file name of the file.
/// </summary>
[NotNull]
public string FilePath { get; }
[NotNull]
public string Extension { get; }
/// <summary>
/// Gets or sets the name of the project containing the file.
/// </summary>
[CanBeNull]
public string ProjectName { get; set; }
[CanBeNull]
public string UniqueProjectName { get; set; }
[NotNull]
public string RelativeFilePath { get; }
public bool HasChanges { get; protected set; }
[NotNull]
public XDocument Load()
{
var document = InternalLoad();
_fingerPrint = document.ToString(SaveOptions.DisableFormatting);
HasChanges = false;
return document;
}
[NotNull]
protected virtual XDocument InternalLoad()
{
return XDocument.Load(FilePath);
}
public void Changed([CanBeNull] XDocument document, bool willSaveImmediately)
{
if (document == null)
return;
InternalChanged(document, willSaveImmediately);
}
protected virtual void InternalChanged([NotNull] XDocument document, bool willSaveImmediately)
{
HasChanges = _fingerPrint != document.ToString(SaveOptions.DisableFormatting);
}
public void Save([CanBeNull] XDocument document)
{
if (document == null)
return;
InternalSave(document);
HasChanges = false;
_fingerPrint = document.ToString(SaveOptions.DisableFormatting);
}
protected virtual void InternalSave([NotNull] XDocument document)
{
document.Save(FilePath);
}
/// <summary>
/// Gets a value indicating whether the file associated with this instance can be written.
/// </summary>
public virtual bool IsWritable
{
get
{
try
{
if ((File.GetAttributes(FilePath) & (FileAttributes.ReadOnly | FileAttributes.System)) != 0)
return false;
using (File.Open(FilePath, FileMode.Open, FileAccess.Write))
{
return true;
}
}
catch (IOException) { }
catch (UnauthorizedAccessException) { }
return false;
}
}
public virtual bool IsWinFormsDesignerResource => false;
[NotNull]
private static string GetRelativePath([NotNull] string solutionFolder, [NotNull] string filePath)
{
solutionFolder = solutionFolder.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
filePath = filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (!solutionFolder.Any() || (solutionFolder.Last() != Path.DirectorySeparatorChar))
{
solutionFolder += Path.DirectorySeparatorChar;
}
return filePath.StartsWith(solutionFolder, StringComparison.OrdinalIgnoreCase) ? filePath.Substring(solutionFolder.Length) : filePath;
}
}
}