/* * 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 { /// /// Stores associations between a list of files and a QuantConnect project /// that is associated with them. /// internal class ProjectFinder { private const string _projectAssociationsFile = "QuantConnectProjects.xml"; private readonly string _projectsFilePath; private readonly IDictionary, string> _projectForFiles = new Dictionary, string>(HashSet.CreateSetComparer()); /// /// Create ProjectFinder /// /// Path to current solution's folder public ProjectFinder(string solutionDirectory) { _projectsFilePath = Path.Combine(solutionDirectory, _projectAssociationsFile); 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(projectAssociation.FileNames), projectAssociation.ProjectName); } } /// /// Get a project name that is associated with the provided list of files /// /// List of files in a project /// A name of a project if a list of files is associated with a project, empty string otherwise public string ProjectNameForFiles(List files) { string projectName; if (_projectForFiles.TryGetValue(new HashSet(files), out projectName)) { return projectName; } return ""; } /// /// Associate a project with a project name /// /// A project name to associate list of files with /// A list of files to associate with a project name public void AssociateProjectWith(string projectName, List files) { SetProjectAssociation(projectName, files); UpdatAssociationsFile(); } private void SetProjectAssociation(string projectName, List files) { _projectForFiles[new HashSet(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(a.Key))); } return projectAssociations; } /// /// List of project associations /// [CollectionDataContract(Name = "ProjectAssociations")] private class ProjectAssociations : List { } /// /// A pair that represent a project and list of files associated with it /// [DataContract(Name = "ProjectAssociation")] private class ProjectAssociation { [DataMember(Name = "ProjectName")] public readonly string ProjectName; [DataMember(Name = "FileNames")] public readonly List FileNames; public ProjectAssociation(string projectName, List fileNames) { ProjectName = projectName; FileNames = fileNames; } } } }