forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectFinder.cs
186 lines (167 loc) · 6.39 KB
/
ProjectFinder.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
namespace QuantConnect.VisualStudioPlugin
{
/// <summary>
/// Stores associations between a list of files and a QuantConnect project
/// that is associated with them.
/// </summary>
class ProjectFinder
{
private const string PROJECT_ASSOCIATIONS_FILE = "QuantConnectProjects.xml";
private string _projectsFilePath;
private IDictionary<HashSet<string>, string> _projectForFiles
= new Dictionary<HashSet<string>, string>(HashSet<string>.CreateSetComparer());
/// <summary>
/// Create ProjectFinder
/// </summary>
/// <param name="solutionDirectory">Path to current solution's folder</param>
public ProjectFinder(string solutionDirectory)
{
_projectsFilePath = Path.Combine(solutionDirectory, PROJECT_ASSOCIATIONS_FILE);
ReadProjectAssociations();
}
private void ReadProjectAssociations()
{
try
{
var projectAssociations = ParseAssociationFile();
FillProjectAssociations(projectAssociations);
}
catch (XmlException)
{
// Failed to read projects associations. Continuing with none.
}
catch (SerializationException)
{
// Failed to parse project associations.
}
catch (FileNotFoundException)
{
// Failed to parse project associations.
}
}
private ProjectAssociations ParseAssociationFile()
{
ProjectAssociations projectAssociations;
using (var stream = new StreamReader(_projectsFilePath))
using (var reader = new XmlTextReader(stream))
{
var dataContractSerializer = new DataContractSerializer(typeof(ProjectAssociations));
projectAssociations = (ProjectAssociations)dataContractSerializer.ReadObject(reader);
}
return projectAssociations;
}
private void FillProjectAssociations(ProjectAssociations projectAssociations)
{
foreach (var projectAssociation in projectAssociations)
{
_projectForFiles.Add(new HashSet<string>(projectAssociation.FileNames), projectAssociation.ProjectName);
}
}
/// <summary>
/// Get a project name that is associated with the provided list of files
/// </summary>
/// <param name="files">List of files in a project</param>
/// <returns>A name of a project if a list of files is associated with a project, empty string otherwise</returns>
public string ProjectNameForFiles(List<string> files)
{
string projectName;
if (_projectForFiles.TryGetValue(new HashSet<string>(files), out projectName))
{
return projectName;
}
return "";
}
/// <summary>
/// Associate a project with a project name
/// </summary>
/// <param name="projectName">A project name to associate list of files with</param>
/// <param name="files">A list of files to associate with a project name</param>
public void AssociateProjectWith(string projectName, List<string> files)
{
SetProjectAssociation(projectName, files);
UpdatAssociationsFile();
}
private void SetProjectAssociation(string projectName, List<string> files)
{
_projectForFiles[new HashSet<string>(files)] = projectName;
}
private void UpdatAssociationsFile()
{
var projectAssociations = CreateProjectAssociations();
SerializeProjectAssociations(projectAssociations);
}
private void SerializeProjectAssociations(ProjectAssociations projectAssociations)
{
using (var output = new StreamWriter(_projectsFilePath, false))
using (var writer = new XmlTextWriter(output) { Formatting = Formatting.Indented })
{
var dataContractSerializer = new DataContractSerializer(typeof(ProjectAssociations));
dataContractSerializer.WriteObject(writer, projectAssociations);
}
}
private ProjectAssociations CreateProjectAssociations()
{
var projectAssociations = new ProjectAssociations();
foreach (var a in _projectForFiles)
{
projectAssociations.Add(new ProjectAssociation(a.Value, new List<string>(a.Key)));
}
return projectAssociations;
}
}
/// <summary>
/// List of project associations
/// </summary>
[CollectionDataContract(Name = "ProjectAssociations")]
class ProjectAssociations : List<ProjectAssociation>
{
}
/// <summary>
/// A pair that represent a project and list of files associated with it
/// </summary>
[DataContract(Name = "ProjectAssociation")]
class ProjectAssociation
{
[DataMember(Name = "ProjectName")]
private string _projectName;
[DataMember(Name = "FileNames")]
private List<string> _fileNames;
public ProjectAssociation(string projectName, List<string> fileNames)
{
_projectName = projectName;
_fileNames = fileNames;
}
public string ProjectName
{
get
{
return _projectName;
}
}
public List<string> FileNames
{
get
{
return _fileNames;
}
}
}
}