4 using System.Collections.Generic;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Diagnostics;
9 using SiliconStudio.Core.IO;
10 using SiliconStudio.Core.VisualStudio;
12 namespace SiliconStudio.Assets
17 internal class PackageSessionHelper
19 private const string SiliconStudioPackage =
"SiliconStudioPackage";
20 private const string SolutionHeader =
@"Microsoft Visual Studio Solution File, Format Version 12.00
22 VisualStudioVersion = 12.0.30723.0
23 MinimumVisualStudioVersion = 10.0.40219.1";
25 public static bool IsSolutionFile(
string filePath)
27 return String.Compare(Path.GetExtension(filePath),
".sln", StringComparison.InvariantCultureIgnoreCase) == 0;
30 public static bool IsPackageFile(
string filePath)
32 return String.Compare(Path.GetExtension(filePath),
Package.PackageFileExtension, StringComparison.InvariantCultureIgnoreCase) == 0;
35 public static void LoadSolution(PackageSession session,
string filePath, List<string> packagePaths,
ILogger sessionResult)
37 var solutionDirectory = Path.GetDirectoryName(filePath);
38 if (solutionDirectory == null)
40 throw new ArgumentException(
"Must be absolute",
"filePath");
44 session.SolutionPath = filePath;
46 var solution = Solution.FromFile(filePath);
48 foreach (var project
in solution.Projects)
51 if (IsPackage(project, out packagePath))
53 var packageFullPath = Path.Combine(solutionDirectory, packagePath);
54 packagePaths.Add(packageFullPath);
59 private static bool IsPackage(
Project project)
62 return IsPackage(project, out packagePath);
65 private static bool IsPackage(
Project project, out
string packagePathRelative)
67 packagePathRelative = null;
70 var propertyItem = project.Sections[SiliconStudioPackage].Properties.FirstOrDefault();
71 if (propertyItem != null)
73 packagePathRelative = propertyItem.Name;
80 public static void SaveSolution(PackageSession session,
ILogger log)
83 if (session.SolutionPath == null)
88 var solutionPath = UPath.Combine(Environment.CurrentDirectory, session.SolutionPath);
94 var solutionDir = solutionPath.GetParent();
97 if (
File.Exists(solutionPath))
99 solution = Solution.FromFile(solutionPath);
103 solution =
new Solution { FullPath = solutionPath };
104 solution.Headers.Add(SolutionHeader);
108 if (!solution.
GlobalSections.Contains(
"SolutionConfigurationPlatforms"))
110 solution.GlobalSections.Add(
new Section(
"SolutionConfigurationPlatforms",
"GlobalSection",
"preSolution", Enumerable.Empty<
PropertyItem>()));
112 if (!solution.
GlobalSections.Contains(
"ProjectConfigurationPlatforms"))
114 solution.GlobalSections.Add(
new Section(
"ProjectConfigurationPlatforms",
"GlobalSection",
"postSolution", Enumerable.Empty<
PropertyItem>()));
118 solution.GlobalSections.Add(
new Section(
"NestedProjects",
"GlobalSection",
"preSolution", Enumerable.Empty<
PropertyItem>()));
124 var platformsUsedBySession =
new SolutionPlatformCollection();
125 platformsUsedBySession.AddRange(AssetRegistry.SupportedPlatforms.Where(platform => platform.Type == PlatformType.Windows));
127 foreach (var package
in session.LocalPackages)
129 foreach (var profile
in package.Profiles.Where(profile => profile.Platform !=
PlatformType.Shared && profile.ProjectReferences.Count > 0))
131 var platformType = profile.Platform;
132 if (!platformsUsedBySession.Contains(platformType))
134 platformsUsedBySession.AddRange(AssetRegistry.SupportedPlatforms.Where(platform => platform.Type == platformType));
142 var configs =
new List<Tuple<string, SolutionPlatform, SolutionPlatformPart>>();
143 foreach (var configName
in platformsUsedBySession.SelectMany(solutionPlatform => solutionPlatform.Configurations).Select(config => config.Name).Distinct())
145 foreach (var platform
in platformsUsedBySession)
147 foreach (var platformPart
in platform.GetParts())
150 if (!platformPart.IncludeInSolution)
153 configs.Add(
new Tuple<string, SolutionPlatform, SolutionPlatformPart>(configName, platform, platformPart));
159 configs = configs.OrderBy(part => part.Item1, StringComparer.InvariantCultureIgnoreCase).ThenBy(part => part.Item3.SafeSolutionName, StringComparer.InvariantCultureIgnoreCase).ToList();
162 var solutionPlatforms = solution.GlobalSections[
"SolutionConfigurationPlatforms"];
163 solutionPlatforms.Properties.Clear();
164 foreach (var config
in configs)
166 var solutionConfigPlatform = string.Format(
"{0}|{1}", config.Item1, config.Item3.SafeSolutionName);
167 if (!solutionPlatforms.Properties.Contains(solutionConfigPlatform))
169 solutionPlatforms.Properties.Add(
new PropertyItem(solutionConfigPlatform, solutionConfigPlatform));
174 var projectToRemove = solution.Projects.Where(project => !project.IsSolutionFolder && !File.Exists(project.FullPath)).ToList();
175 foreach (var project
in projectToRemove)
177 solution.Projects.Remove(project);
183 foreach (var package
in session.LocalPackages)
185 if (
string.IsNullOrWhiteSpace(package.Meta.Name))
187 log.Error(
"Error while saving solution [{0}]. Package [{1}] should have a Meta.Name", solutionPath, package.FullPath);
191 var packageFolder = solution.Projects.FindByGuid(package.Id);
194 if (packageFolder == null)
197 packageFolder =
new Project(solution,
208 solution.Projects.Add(packageFolder);
209 packageFolder = solution.Projects[package.Id];
213 packageFolder.Sections.Clear();
214 var relativeUrl = package.FullPath.MakeRelative(solutionDir);
215 packageFolder.Sections.Add(
new Section(SiliconStudioPackage,
"ProjectSection",
"preProject",
new[] {
new PropertyItem(relativeUrl, relativeUrl) }));
220 foreach (var profile
in package.Profiles.OrderBy(x => x.Platform ==
PlatformType.Windows ? 0 : 1))
222 foreach (var project
in profile.ProjectReferences)
224 var projectInSolution = solution.Projects.FindByGuid(project.Id);
226 if (projectInSolution == null)
228 var projectRelativePath = project.Location.MakeRelative(solutionDir);
231 projectInSolution =
new Project(solution,
234 project.Location.GetFileName(),
235 projectRelativePath.ToWindowsPath(),
241 solution.Projects.Add(projectInSolution);
244 projectInSolution = solution.Projects.FindByGuid(project.Id);
248 projectInSolution.ParentGuid = package.Id;
253 projectInSolution.PlatformProperties.Clear();
255 foreach (var config
in configs)
257 var configName = config.Item1;
258 var platform = config.Item2;
259 var platformPart = config.Item3;
262 if (project.Type ==
ProjectType.Executable && !platformPart.UseWithExecutables)
267 var platformName = platformPart.SafeSolutionName;
269 var solutionConfigPlatform = string.Format(
"{0}|{1}", configName, platformName);
271 var configNameInProject = configName;
272 var platformNameInProject = platformPart.GetProjectName(project.Type);
274 var platformTarget = platform;
277 platformTarget = platformsUsedBySession.First(plat => plat.Type == profile.Platform);
280 bool isPartOfBuild = platformTarget == platform;
282 if (!platformTarget.Configurations.Contains(configName))
284 configNameInProject = platformTarget.Configurations.FirstOrDefault().Name;
285 isPartOfBuild =
false;
289 if (platformTarget.GetParts().All(part => part.GetProjectName(project.Type) != platformNameInProject))
291 platformNameInProject = platformTarget.GetParts().FirstOrDefault(part => part.IsProjectHandled(project.Type)).SafeSolutionName;
292 isPartOfBuild =
false;
295 var projectConfigPlatform = string.Format(
"{0}|{1}", configNameInProject, platformNameInProject);
297 var propertyActive = solutionConfigPlatform +
".ActiveCfg";
298 var propertyBuild = solutionConfigPlatform +
".Build.0";
300 if (!projectInSolution.PlatformProperties.Contains(propertyActive))
302 projectInSolution.PlatformProperties.Remove(propertyActive);
303 projectInSolution.PlatformProperties.Add(
new PropertyItem(propertyActive, projectConfigPlatform));
309 projectInSolution.PlatformProperties.Remove(propertyBuild);
310 projectInSolution.PlatformProperties.Add(
new PropertyItem(propertyBuild, projectConfigPlatform));
315 var propertyDeploy = solutionConfigPlatform +
".Deploy.0";
316 projectInSolution.PlatformProperties.Remove(propertyDeploy);
317 projectInSolution.PlatformProperties.Add(
new PropertyItem(propertyDeploy, projectConfigPlatform));
327 for (
int i = solution.
Projects.Count - 1; i >=0; i--)
329 var project = solution.Projects[i];
330 if (IsPackage(project) && !session.Packages.ContainsById(project.Guid))
332 solution.Projects.RemoveAt(i);
341 log.Error(
"Error while saving solution [{0}]", ex, solutionPath);
PlatformType
Describes the platform operating system.
A project referenced by a VisualStudio solution.
bool IsSolutionFolder
Gets a value indicating whether this instance is solution folder.
A section defined in a Project
static readonly Guid SolutionFolder
ProjectCollection Projects
Gets all projects.
The template can be applied to an existing Assets.Package.
A key/value pair used by PropertyItemCollection
SectionCollection Sections
Gets the project sections.
ProjectType
Type of the project.
static readonly Guid CSharp
SectionCollection GlobalSections
Gets the global sections.