Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SolutionWriter.cs
Go to the documentation of this file.
1 #region License
2 
3 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
4 // This file is distributed under MIT License. See LICENSE.md for details.
5 //
6 // SLNTools
7 // Copyright (c) 2009
8 // by Christian Warren
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
11 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
13 // to permit persons to whom the Software is furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
16 // of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23 
24 #endregion
25 
26 using System;
27 using System.Collections.Generic;
28 using System.IO;
29 using System.Text;
30 
31 namespace SiliconStudio.Core.VisualStudio
32 {
33  internal class SolutionWriter : IDisposable
34  {
35  private StreamWriter writer;
36 
37  public SolutionWriter(string solutionFullPath) : this(new FileStream(solutionFullPath, FileMode.Create, FileAccess.Write))
38  {
39  }
40 
41  public SolutionWriter(Stream writer)
42  {
43  this.writer = new StreamWriter(writer, Encoding.UTF8);
44  }
45 
46  public void Dispose()
47  {
48  if (writer != null)
49  {
50  writer.Dispose();
51  writer = null;
52  }
53  }
54 
55  public void WriteSolutionFile(Solution solution)
56  {
57  lock (writer)
58  {
59  WriteHeader(solution);
60  WriteProjects(solution);
61  WriteGlobal(solution);
62  }
63  }
64 
65  private void WriteGlobal(Solution solution)
66  {
67  writer.WriteLine("Global");
68  WriteGlobalSections(solution);
69  writer.WriteLine("EndGlobal");
70  }
71 
72  private void WriteGlobalSections(Solution solution)
73  {
74  foreach (Section globalSection in solution.GlobalSections)
75  {
76  var propertyLines = new List<PropertyItem>(globalSection.Properties);
77  switch (globalSection.Name)
78  {
79  case "NestedProjects":
80  foreach (Project project in solution.Projects)
81  {
82  if (project.ParentGuid != Guid.Empty)
83  {
84  propertyLines.Add(new PropertyItem(project.Guid.ToString("B").ToUpperInvariant(), project.ParentGuid.ToString("B").ToUpperInvariant()));
85  }
86  }
87  break;
88 
89  case "ProjectConfigurationPlatforms":
90  foreach (Project project in solution.Projects)
91  {
92  foreach (PropertyItem propertyLine in project.PlatformProperties)
93  {
94  propertyLines.Add(
95  new PropertyItem(
96  string.Format("{0}.{1}", project.Guid.ToString("B").ToUpperInvariant(), propertyLine.Name),
97  propertyLine.Value));
98  }
99  }
100  break;
101 
102  default:
103  if (globalSection.Name.EndsWith("Control", StringComparison.InvariantCultureIgnoreCase))
104  {
105  int index = 1;
106  foreach (Project project in solution.Projects)
107  {
108  if (project.VersionControlProperties.Count > 0)
109  {
110  foreach (PropertyItem propertyLine in project.VersionControlProperties)
111  {
112  propertyLines.Add(
113  new PropertyItem(
114  string.Format("{0}{1}", propertyLine.Name, index),
115  propertyLine.Value));
116  }
117  index++;
118  }
119  }
120 
121  propertyLines.Insert(0, new PropertyItem("SccNumberOfProjects", index.ToString()));
122  }
123  break;
124  }
125 
126  WriteSection(globalSection, propertyLines);
127  }
128  }
129 
130  private void WriteHeader(Solution solution)
131  {
132  // If the header doesn't start with an empty line, add one
133  // (The first line of sln files saved as UTF-8 with BOM must be blank, otherwise Visual Studio Version Selector will not detect VS version correctly.)
134  if (solution.Headers.Count == 0 || solution.Headers[0].Trim().Length > 0)
135  {
136  writer.WriteLine();
137  }
138 
139  foreach (string line in solution.Headers)
140  {
141  writer.WriteLine(line);
142  }
143 
144  foreach (PropertyItem propertyLine in solution.Properties)
145  {
146  writer.WriteLine("{0} = {1}", propertyLine.Name, propertyLine.Value);
147  }
148  }
149 
150  private void WriteProjects(Solution solution)
151  {
152  foreach (Project project in solution.Projects)
153  {
154  writer.WriteLine("Project(\"{0}\") = \"{1}\", \"{2}\", \"{3}\"",
155  project.TypeGuid.ToString("B").ToUpperInvariant(),
156  project.Name,
157  project.RelativePath,
158  project.Guid.ToString("B").ToUpperInvariant());
159  foreach (Section projectSection in project.Sections)
160  {
161  WriteSection(projectSection, projectSection.Properties);
162  }
163  writer.WriteLine("EndProject");
164  }
165  }
166 
167  private void WriteSection(Section section, IEnumerable<PropertyItem> propertyLines)
168  {
169  writer.WriteLine("\t{0}({1}) = {2}", section.SectionType, section.Name, section.Step);
170  foreach (PropertyItem propertyLine in propertyLines)
171  {
172  writer.WriteLine("\t\t{0} = {1}", propertyLine.Name, propertyLine.Value);
173  }
174  writer.WriteLine("\tEnd{0}", section.SectionType);
175  }
176  }
177 }
System.Text.Encoding Encoding
System.IO.FileMode FileMode
Definition: ScriptSync.cs:33
EnvDTE.Project Project
Creates a new file, always. If a file exists, the function overwrites the file, clears the existing a...