VSGenericTarget.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. #region BSD License
  2. /*
  3. Copyright (c) 2008 Matthew Holmes ([email protected]), John Anderson ([email protected])
  4. Redistribution and use in source and binary forms, with or without modification, are permitted
  5. provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice, this list of conditions
  7. and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  9. and the following disclaimer in the documentation and/or other materials provided with the
  10. distribution.
  11. * The name of the author may not be used to endorse or promote products derived from this software
  12. without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  14. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  17. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  18. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  19. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. #endregion
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Collections.Specialized;
  25. using System.IO;
  26. using Prebuild.Core.Interfaces;
  27. using Prebuild.Core.Nodes;
  28. using Prebuild.Core.Utilities;
  29. using System.CodeDom.Compiler;
  30. namespace Prebuild.Core.Targets
  31. {
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. public abstract class VSGenericTarget : ITarget
  36. {
  37. #region Fields
  38. readonly Dictionary<string, ToolInfo> tools = new Dictionary<string, ToolInfo>();
  39. // NameValueCollection CopyFiles = new NameValueCollection();
  40. Kernel kernel;
  41. #endregion
  42. #region Properties
  43. /// <summary>
  44. /// Gets or sets the solution version.
  45. /// </summary>
  46. /// <value>The solution version.</value>
  47. public abstract string SolutionVersion { get; }
  48. /// <summary>
  49. /// Gets or sets the product version.
  50. /// </summary>
  51. /// <value>The product version.</value>
  52. public abstract string ProductVersion { get; }
  53. /// <summary>
  54. /// Gets or sets the schema version.
  55. /// </summary>
  56. /// <value>The schema version.</value>
  57. public abstract string SchemaVersion { get; }
  58. /// <summary>
  59. /// Gets or sets the name of the version.
  60. /// </summary>
  61. /// <value>The name of the version.</value>
  62. public abstract string VersionName { get; }
  63. /// <summary>
  64. /// Gets or sets the version.
  65. /// </summary>
  66. /// <value>The version.</value>
  67. public abstract VSVersion Version { get; }
  68. /// <summary>
  69. /// Gets the name.
  70. /// </summary>
  71. /// <value>The name.</value>
  72. public abstract string Name { get; }
  73. protected abstract string GetToolsVersionXml(FrameworkVersion version);
  74. public abstract string SolutionTag { get; }
  75. #endregion
  76. #region Constructors
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="VSGenericTarget"/> class.
  79. /// </summary>
  80. protected VSGenericTarget()
  81. {
  82. tools["C#"] = new ToolInfo("C#", "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}", "csproj", "CSHARP", "$(MSBuildBinPath)\\Microsoft.CSHARP.Targets");
  83. tools["Database"] = new ToolInfo("Database", "{4F174C21-8C12-11D0-8340-0000F80270F8}", "dbp", "UNKNOWN");
  84. tools["Boo"] = new ToolInfo("Boo", "{45CEA7DC-C2ED-48A6-ACE0-E16144C02365}", "booproj", "Boo", "$(BooBinPath)\\Boo.Microsoft.Build.targets");
  85. tools["VisualBasic"] = new ToolInfo("VisualBasic", "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}", "vbproj", "VisualBasic", "$(MSBuildBinPath)\\Microsoft.VisualBasic.Targets");
  86. tools["Folder"] = new ToolInfo("Folder", "{2150E333-8FDC-42A3-9474-1A3956D46DE8}", null, null);
  87. }
  88. #endregion
  89. #region Private Methods
  90. private string MakeRefPath(ProjectNode project)
  91. {
  92. string ret = "";
  93. foreach (ReferencePathNode node in project.ReferencePaths)
  94. {
  95. try
  96. {
  97. string fullPath = Helper.ResolvePath(node.Path);
  98. if (ret.Length < 1)
  99. {
  100. ret = fullPath;
  101. }
  102. else
  103. {
  104. ret += ";" + fullPath;
  105. }
  106. }
  107. catch (ArgumentException)
  108. {
  109. kernel.Log.Write(LogType.Warning, "Could not resolve reference path: {0}", node.Path);
  110. }
  111. }
  112. return ret;
  113. }
  114. private static ProjectNode FindProjectInSolution(string name, SolutionNode solution)
  115. {
  116. SolutionNode node = solution;
  117. while (node.Parent is SolutionNode)
  118. node = node.Parent as SolutionNode;
  119. return FindProjectInSolutionRecursively(name, node);
  120. }
  121. private static ProjectNode FindProjectInSolutionRecursively(string name, SolutionNode solution)
  122. {
  123. if (solution.ProjectsTable.ContainsKey(name))
  124. return solution.ProjectsTable[name];
  125. foreach (SolutionNode child in solution.Solutions)
  126. {
  127. ProjectNode node = FindProjectInSolutionRecursively(name, child);
  128. if (node != null)
  129. return node;
  130. }
  131. return null;
  132. }
  133. private void WriteProject(SolutionNode solution, ProjectNode project)
  134. {
  135. if (!tools.ContainsKey(project.Language))
  136. {
  137. throw new UnknownLanguageException("Unknown .NET language: " + project.Language);
  138. }
  139. ToolInfo toolInfo = tools[project.Language];
  140. string projectFile = Helper.MakeFilePath(project.FullPath, project.Name, toolInfo.FileExtension);
  141. StreamWriter ps = new StreamWriter(projectFile);
  142. kernel.CurrentWorkingDirectory.Push();
  143. Helper.SetCurrentDir(Path.GetDirectoryName(projectFile));
  144. #region Project File
  145. using (ps)
  146. {
  147. string targets = "";
  148. if(project.Files.CopyFiles > 0)
  149. targets = "Build;CopyFiles";
  150. else
  151. targets = "Build";
  152. ps.WriteLine("<Project DefaultTargets=\"{0}\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" {1}>", targets, GetToolsVersionXml(project.FrameworkVersion));
  153. ps.WriteLine(" <PropertyGroup>");
  154. ps.WriteLine(" <ProjectType>Local</ProjectType>");
  155. ps.WriteLine(" <ProductVersion>{0}</ProductVersion>", ProductVersion);
  156. ps.WriteLine(" <SchemaVersion>{0}</SchemaVersion>", SchemaVersion);
  157. ps.WriteLine(" <ProjectGuid>{{{0}}}</ProjectGuid>", project.Guid.ToString().ToUpper());
  158. // Visual Studio has a hard coded guid for the project type
  159. if (project.Type == ProjectType.Web)
  160. ps.WriteLine(" <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>");
  161. ps.WriteLine(" <Configuration Condition=\" '$(Configuration)' == '' \">Debug</Configuration>");
  162. ps.WriteLine(" <ApplicationIcon>{0}</ApplicationIcon>", project.AppIcon);
  163. ps.WriteLine(" <AssemblyKeyContainerName>");
  164. ps.WriteLine(" </AssemblyKeyContainerName>");
  165. ps.WriteLine(" <AssemblyName>{0}</AssemblyName>", project.AssemblyName);
  166. foreach (ConfigurationNode conf in project.Configurations)
  167. {
  168. if (conf.Options.KeyFile != "")
  169. {
  170. ps.WriteLine(" <AssemblyOriginatorKeyFile>{0}</AssemblyOriginatorKeyFile>", conf.Options.KeyFile);
  171. ps.WriteLine(" <SignAssembly>true</SignAssembly>");
  172. break;
  173. }
  174. }
  175. ps.WriteLine(" <DefaultClientScript>JScript</DefaultClientScript>");
  176. ps.WriteLine(" <DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout>");
  177. ps.WriteLine(" <DefaultTargetSchema>IE50</DefaultTargetSchema>");
  178. ps.WriteLine(" <DelaySign>false</DelaySign>");
  179. ps.WriteLine(" <TargetFrameworkVersion>{0}</TargetFrameworkVersion>", project.FrameworkVersion.ToString().Replace("_", "."));
  180. ps.WriteLine(" <OutputType>{0}</OutputType>", project.Type == ProjectType.Web ? ProjectType.Library.ToString() : project.Type.ToString());
  181. ps.WriteLine(" <AppDesignerFolder>{0}</AppDesignerFolder>", project.DesignerFolder);
  182. ps.WriteLine(" <RootNamespace>{0}</RootNamespace>", project.RootNamespace);
  183. ps.WriteLine(" <StartupObject>{0}</StartupObject>", project.StartupObject);
  184. if (string.IsNullOrEmpty(project.DebugStartParameters))
  185. {
  186. ps.WriteLine(" <StartArguments>{0}</StartArguments>", project.DebugStartParameters);
  187. }
  188. ps.WriteLine(" <FileUpgradeFlags>");
  189. ps.WriteLine(" </FileUpgradeFlags>");
  190. ps.WriteLine(" </PropertyGroup>");
  191. foreach (ConfigurationNode conf in project.Configurations)
  192. {
  193. ps.Write(" <PropertyGroup ");
  194. ps.WriteLine("Condition=\" '$(Configuration)|$(Platform)' == '{0}|{1}' \">", conf.Name, conf.Platform);
  195. ps.WriteLine(" <AllowUnsafeBlocks>{0}</AllowUnsafeBlocks>", conf.Options["AllowUnsafe"]);
  196. ps.WriteLine(" <BaseAddress>{0}</BaseAddress>", conf.Options["BaseAddress"]);
  197. ps.WriteLine(" <CheckForOverflowUnderflow>{0}</CheckForOverflowUnderflow>", conf.Options["CheckUnderflowOverflow"]);
  198. ps.WriteLine(" <ConfigurationOverrideFile>");
  199. ps.WriteLine(" </ConfigurationOverrideFile>");
  200. ps.WriteLine(" <DefineConstants>{0}</DefineConstants>", conf.Options["CompilerDefines"]);
  201. ps.WriteLine(" <DocumentationFile>{0}</DocumentationFile>", Helper.NormalizePath(conf.Options["XmlDocFile"].ToString()));
  202. ps.WriteLine(" <DebugSymbols>{0}</DebugSymbols>", conf.Options["DebugInformation"]);
  203. ps.WriteLine(" <FileAlignment>{0}</FileAlignment>", conf.Options["FileAlignment"]);
  204. ps.WriteLine(" <Optimize>{0}</Optimize>", conf.Options["OptimizeCode"]);
  205. if (project.Type != ProjectType.Web)
  206. ps.WriteLine(" <OutputPath>{0}</OutputPath>",
  207. Helper.EndPath(Helper.NormalizePath(conf.Options["OutputPath"].ToString())));
  208. else
  209. ps.WriteLine(" <OutputPath>{0}</OutputPath>",
  210. Helper.EndPath(Helper.NormalizePath("bin\\")));
  211. ps.WriteLine(" <RegisterForComInterop>{0}</RegisterForComInterop>", conf.Options["RegisterComInterop"]);
  212. ps.WriteLine(" <RemoveIntegerChecks>{0}</RemoveIntegerChecks>", conf.Options["RemoveIntegerChecks"]);
  213. ps.WriteLine(" <TreatWarningsAsErrors>{0}</TreatWarningsAsErrors>", conf.Options["WarningsAsErrors"]);
  214. ps.WriteLine(" <WarningLevel>{0}</WarningLevel>", conf.Options["WarningLevel"]);
  215. ps.WriteLine(" <NoStdLib>{0}</NoStdLib>", conf.Options["NoStdLib"]);
  216. ps.WriteLine(" <NoWarn>{0}</NoWarn>", conf.Options["SuppressWarnings"]);
  217. ps.WriteLine(" <PlatformTarget>{0}</PlatformTarget>", conf.Platform);
  218. ps.WriteLine(" </PropertyGroup>");
  219. }
  220. //ps.WriteLine(" </Settings>");
  221. Dictionary<ReferenceNode, ProjectNode> projectReferences = new Dictionary<ReferenceNode, ProjectNode>();
  222. List<ReferenceNode> otherReferences = new List<ReferenceNode>();
  223. foreach (ReferenceNode refr in project.References)
  224. {
  225. ProjectNode projectNode = FindProjectInSolution(refr.Name, solution);
  226. if (projectNode == null)
  227. otherReferences.Add(refr);
  228. else
  229. projectReferences.Add(refr, projectNode);
  230. }
  231. // Assembly References
  232. ps.WriteLine(" <ItemGroup>");
  233. foreach (ReferenceNode refr in otherReferences)
  234. {
  235. ps.Write(" <Reference");
  236. ps.Write(" Include=\"");
  237. ps.Write(refr.Name);
  238. ps.WriteLine("\" >");
  239. ps.Write(" <Name>");
  240. ps.Write(refr.Name);
  241. ps.WriteLine("</Name>");
  242. if(!String.IsNullOrEmpty(refr.Path))
  243. {
  244. // Use absolute path to assembly (for determining assembly type)
  245. string absolutePath = Path.Combine(project.FullPath, refr.Path);
  246. if(File.Exists(Helper.MakeFilePath(absolutePath, refr.Name, "exe"))) {
  247. // Assembly is an executable (exe)
  248. ps.WriteLine(" <HintPath>{0}</HintPath>", Helper.MakeFilePath(refr.Path, refr.Name, "exe"));
  249. } else if(File.Exists(Helper.MakeFilePath(absolutePath, refr.Name, "dll"))) {
  250. // Assembly is an library (dll)
  251. ps.WriteLine(" <HintPath>{0}</HintPath>", Helper.MakeFilePath(refr.Path, refr.Name, "dll"));
  252. } else {
  253. string referencePath = Helper.MakeFilePath(refr.Path, refr.Name, "dll");
  254. kernel.Log.Write(LogType.Warning, "Reference \"{0}\": The specified file doesn't exist.", referencePath);
  255. ps.WriteLine(" <HintPath>{0}</HintPath>", Helper.MakeFilePath(refr.Path, refr.Name, "dll"));
  256. }
  257. }
  258. ps.WriteLine(" <Private>{0}</Private>", refr.LocalCopy);
  259. ps.WriteLine(" </Reference>");
  260. }
  261. ps.WriteLine(" </ItemGroup>");
  262. //Project References
  263. ps.WriteLine(" <ItemGroup>");
  264. foreach (KeyValuePair<ReferenceNode, ProjectNode> pair in projectReferences)
  265. {
  266. ToolInfo tool = tools[pair.Value.Language];
  267. if (tools == null)
  268. throw new UnknownLanguageException();
  269. string path =
  270. Helper.MakePathRelativeTo(project.FullPath,
  271. Helper.MakeFilePath(pair.Value.FullPath, pair.Value.Name, tool.FileExtension));
  272. ps.WriteLine(" <ProjectReference Include=\"{0}\">", path);
  273. // TODO: Allow reference to visual basic projects
  274. ps.WriteLine(" <Name>{0}</Name>", pair.Value.Name);
  275. ps.WriteLine(" <Project>{0}</Project>", pair.Value.Guid.ToString("B").ToUpper());
  276. ps.WriteLine(" <Package>{0}</Package>", tool.Guid.ToUpper());
  277. //This is the Copy Local flag in VS
  278. ps.WriteLine(" <Private>{0}</Private>", pair.Key.LocalCopy);
  279. ps.WriteLine(" </ProjectReference>");
  280. }
  281. ps.WriteLine(" </ItemGroup>");
  282. // ps.WriteLine(" </Build>");
  283. ps.WriteLine(" <ItemGroup>");
  284. // ps.WriteLine(" <Include>");
  285. List<string> list = new List<string>();
  286. foreach (string path in project.Files)
  287. {
  288. string lower = path.ToLower();
  289. if (lower.EndsWith(".resx"))
  290. {
  291. string codebehind = String.Format("{0}.Designer{1}", path.Substring(0, path.LastIndexOf('.')), toolInfo.LanguageExtension);
  292. if (!list.Contains(codebehind))
  293. list.Add(codebehind);
  294. }
  295. }
  296. foreach (string filePath in project.Files)
  297. {
  298. // Add the filePath with the destination as the key
  299. // will use it later to form the copy parameters with Include lists
  300. // for each destination
  301. if (project.Files.GetBuildAction(filePath) == BuildAction.Copy)
  302. continue;
  303. // if (file == "Properties\\Bind.Designer.cs")
  304. // {
  305. // Console.WriteLine("Wait a minute!");
  306. // Console.WriteLine(project.Files.GetSubType(file).ToString());
  307. // }
  308. SubType subType = project.Files.GetSubType(filePath);
  309. // Visual Studio chokes on file names if forward slash is used as a path separator
  310. // instead of backslash. So we must make sure that all file paths written to the
  311. // project file use \ as a path separator.
  312. string file = filePath.Replace(@"/", @"\");
  313. if (subType != SubType.Code && subType != SubType.Settings && subType != SubType.Designer
  314. && subType != SubType.CodeBehind)
  315. {
  316. ps.WriteLine(" <EmbeddedResource Include=\"{0}\">", file.Substring(0, file.LastIndexOf('.')) + ".resx");
  317. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(file));
  318. ps.WriteLine(" <SubType>Designer</SubType>");
  319. ps.WriteLine(" </EmbeddedResource>");
  320. //
  321. }
  322. if (subType == SubType.Designer)
  323. {
  324. ps.WriteLine(" <EmbeddedResource Include=\"{0}\">", file);
  325. string autogen_name = file.Substring(0, file.LastIndexOf('.')) + ".Designer.cs";
  326. string dependent_name = filePath.Substring(0, file.LastIndexOf('.')) + ".cs";
  327. // Check for a parent .cs file with the same name as this designer file
  328. if (File.Exists(Helper.NormalizePath(dependent_name)))
  329. {
  330. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(dependent_name));
  331. }
  332. else
  333. {
  334. ps.WriteLine(" <Generator>ResXFileCodeGenerator</Generator>");
  335. ps.WriteLine(" <LastGenOutput>{0}</LastGenOutput>", Path.GetFileName(autogen_name));
  336. ps.WriteLine(" <SubType>" + subType + "</SubType>");
  337. }
  338. ps.WriteLine(" </EmbeddedResource>");
  339. if (File.Exists(Helper.NormalizePath(autogen_name)))
  340. {
  341. ps.WriteLine(" <Compile Include=\"{0}\">", autogen_name);
  342. //ps.WriteLine(" <DesignTime>True</DesignTime>");
  343. // If a parent .cs file exists, link this autogen file to it. Otherwise link
  344. // to the designer file
  345. if (File.Exists(dependent_name))
  346. {
  347. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(dependent_name));
  348. }
  349. else
  350. {
  351. ps.WriteLine(" <AutoGen>True</AutoGen>");
  352. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(filePath));
  353. }
  354. ps.WriteLine(" </Compile>");
  355. }
  356. list.Add(autogen_name);
  357. }
  358. if (subType == SubType.Settings)
  359. {
  360. ps.Write(" <{0} ", project.Files.GetBuildAction(filePath));
  361. ps.WriteLine("Include=\"{0}\">", file);
  362. string fileName = Path.GetFileName(filePath);
  363. if (project.Files.GetBuildAction(filePath) == BuildAction.None)
  364. {
  365. ps.WriteLine(" <Generator>SettingsSingleFileGenerator</Generator>");
  366. ps.WriteLine(" <LastGenOutput>{0}</LastGenOutput>", fileName.Substring(0, fileName.LastIndexOf('.')) + ".Designer.cs");
  367. }
  368. else
  369. {
  370. ps.WriteLine(" <SubType>Code</SubType>");
  371. ps.WriteLine(" <AutoGen>True</AutoGen>");
  372. ps.WriteLine(" <DesignTimeSharedInput>True</DesignTimeSharedInput>");
  373. string fileNameShort = fileName.Substring(0, fileName.LastIndexOf('.'));
  374. string fileNameShorter = fileNameShort.Substring(0, fileNameShort.LastIndexOf('.'));
  375. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(fileNameShorter + ".settings"));
  376. }
  377. ps.WriteLine(" </{0}>", project.Files.GetBuildAction(filePath));
  378. }
  379. else if (subType != SubType.Designer)
  380. {
  381. string path = Helper.NormalizePath(file);
  382. string path_lower = path.ToLower();
  383. if (!list.Contains(filePath))
  384. {
  385. ps.Write(" <{0} ", project.Files.GetBuildAction(filePath));
  386. int startPos = 0;
  387. if (project.Files.GetPreservePath(filePath))
  388. {
  389. while ((@"./\").IndexOf(file.Substring(startPos, 1)) != -1)
  390. startPos++;
  391. }
  392. else
  393. {
  394. startPos = file.LastIndexOf(Path.GetFileName(path));
  395. }
  396. // be sure to write out the path with backslashes so VS recognizes
  397. // the file properly.
  398. ps.WriteLine("Include=\"{0}\">", file);
  399. int last_period_index = file.LastIndexOf('.');
  400. string short_file_name = (last_period_index >= 0)
  401. ? file.Substring(0, last_period_index)
  402. : file;
  403. string extension = Path.GetExtension(path);
  404. // make this upper case, so that when File.Exists tests for the
  405. // existence of a designer file on a case-sensitive platform,
  406. // it is correctly identified.
  407. string designer_format = string.Format(".Designer{0}", extension);
  408. if (path_lower.EndsWith(designer_format.ToLowerInvariant()))
  409. {
  410. int designer_index = path.IndexOf(designer_format);
  411. string file_name = path.Substring(0, designer_index);
  412. // There are two corrections to the next lines:
  413. // 1. Fix the connection between a designer file and a form
  414. // or usercontrol that don't have an associated resx file.
  415. // 2. Connect settings files to associated designer files.
  416. if (File.Exists(file_name + extension))
  417. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(file_name + extension));
  418. else if (File.Exists(file_name + ".resx"))
  419. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(file_name + ".resx"));
  420. else if (File.Exists(file_name + ".settings"))
  421. {
  422. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(file_name + ".settings"));
  423. ps.WriteLine(" <AutoGen>True</AutoGen>");
  424. ps.WriteLine(" <DesignTimeSharedInput>True</DesignTimeSharedInput>");
  425. }
  426. }
  427. else if (subType == SubType.CodeBehind)
  428. {
  429. ps.WriteLine(" <DependentUpon>{0}</DependentUpon>", Path.GetFileName(short_file_name));
  430. }
  431. if (project.Files.GetIsLink(filePath))
  432. {
  433. string alias = project.Files.GetLinkPath(filePath);
  434. alias += file.Substring(startPos);
  435. alias = Helper.NormalizePath(alias);
  436. ps.WriteLine(" <Link>{0}</Link>", alias);
  437. }
  438. else if (project.Files.GetBuildAction(filePath) != BuildAction.None)
  439. {
  440. if (project.Files.GetBuildAction(filePath) != BuildAction.EmbeddedResource)
  441. {
  442. ps.WriteLine(" <SubType>{0}</SubType>", subType);
  443. }
  444. }
  445. if (project.Files.GetCopyToOutput(filePath) != CopyToOutput.Never)
  446. {
  447. ps.WriteLine(" <CopyToOutputDirectory>{0}</CopyToOutputDirectory>", project.Files.GetCopyToOutput(filePath));
  448. }
  449. ps.WriteLine(" </{0}>", project.Files.GetBuildAction(filePath));
  450. }
  451. }
  452. }
  453. ps.WriteLine(" </ItemGroup>");
  454. /*
  455. * Copy Task
  456. *
  457. */
  458. if ( project.Files.CopyFiles > 0 ) {
  459. Dictionary<string, string> IncludeTags = new Dictionary<string, string>();
  460. int TagCount = 0;
  461. // Handle Copy tasks
  462. ps.WriteLine(" <ItemGroup>");
  463. foreach (string destPath in project.Files.Destinations)
  464. {
  465. string tag = "FilesToCopy_" + TagCount.ToString("0000");
  466. ps.WriteLine(" <{0} Include=\"{1}\" />", tag, String.Join(";", project.Files.SourceFiles(destPath)));
  467. IncludeTags.Add(destPath, tag);
  468. TagCount++;
  469. }
  470. ps.WriteLine(" </ItemGroup>");
  471. ps.WriteLine(" <Target Name=\"CopyFiles\">");
  472. foreach (string destPath in project.Files.Destinations)
  473. {
  474. ps.WriteLine(" <Copy SourceFiles=\"@({0})\" DestinationFolder=\"{1}\" />",
  475. IncludeTags[destPath], destPath);
  476. }
  477. ps.WriteLine(" </Target>");
  478. }
  479. ps.WriteLine(" <Import Project=\"" + toolInfo.ImportProject + "\" />");
  480. ps.WriteLine(" <PropertyGroup>");
  481. ps.WriteLine(" <PreBuildEvent>");
  482. ps.WriteLine(" </PreBuildEvent>");
  483. ps.WriteLine(" <PostBuildEvent>");
  484. ps.WriteLine(" </PostBuildEvent>");
  485. ps.WriteLine(" </PropertyGroup>");
  486. ps.WriteLine("</Project>");
  487. }
  488. #endregion
  489. #region User File
  490. ps = new StreamWriter(projectFile + ".user");
  491. using (ps)
  492. {
  493. // Get the first configuration from the project.
  494. ConfigurationNode firstConfiguration = null;
  495. if (project.Configurations.Count > 0)
  496. {
  497. firstConfiguration = project.Configurations[0];
  498. }
  499. ps.WriteLine("<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">");
  500. //ps.WriteLine( "<VisualStudioProject>" );
  501. //ps.WriteLine(" <{0}>", toolInfo.XMLTag);
  502. //ps.WriteLine(" <Build>");
  503. ps.WriteLine(" <PropertyGroup>");
  504. //ps.WriteLine(" <Settings ReferencePath=\"{0}\">", MakeRefPath(project));
  505. if (firstConfiguration != null)
  506. {
  507. ps.WriteLine(" <Configuration Condition=\" '$(Configuration)' == '' \">{0}</Configuration>", firstConfiguration.Name);
  508. ps.WriteLine(" <Platform Condition=\" '$(Platform)' == '' \">{0}</Platform>", firstConfiguration.Platform);
  509. }
  510. ps.WriteLine(" <ReferencePath>{0}</ReferencePath>", MakeRefPath(project));
  511. ps.WriteLine(" <LastOpenVersion>{0}</LastOpenVersion>", ProductVersion);
  512. ps.WriteLine(" <ProjectView>ProjectFiles</ProjectView>");
  513. ps.WriteLine(" <ProjectTrust>0</ProjectTrust>");
  514. ps.WriteLine(" </PropertyGroup>");
  515. foreach (ConfigurationNode conf in project.Configurations)
  516. {
  517. ps.Write(" <PropertyGroup");
  518. ps.Write(" Condition = \" '$(Configuration)|$(Platform)' == '{0}|{1}' \"", conf.Name, conf.Platform);
  519. ps.WriteLine(" />");
  520. }
  521. ps.WriteLine("</Project>");
  522. }
  523. #endregion
  524. kernel.CurrentWorkingDirectory.Pop();
  525. }
  526. private void WriteSolution(SolutionNode solution, bool writeSolutionToDisk)
  527. {
  528. kernel.Log.Write("Creating {0} solution and project files", VersionName);
  529. foreach (SolutionNode child in solution.Solutions)
  530. {
  531. kernel.Log.Write("...Creating folder: {0}", child.Name);
  532. WriteSolution(child, false);
  533. }
  534. foreach (ProjectNode project in solution.Projects)
  535. {
  536. kernel.Log.Write("...Creating project: {0}", project.Name);
  537. WriteProject(solution, project);
  538. }
  539. foreach (DatabaseProjectNode project in solution.DatabaseProjects)
  540. {
  541. kernel.Log.Write("...Creating database project: {0}", project.Name);
  542. WriteDatabaseProject(solution, project);
  543. }
  544. if (writeSolutionToDisk) // only write main solution
  545. {
  546. kernel.Log.Write("");
  547. string solutionFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "sln");
  548. using (StreamWriter ss = new StreamWriter(solutionFile))
  549. {
  550. kernel.CurrentWorkingDirectory.Push();
  551. Helper.SetCurrentDir(Path.GetDirectoryName(solutionFile));
  552. ss.WriteLine("Microsoft Visual Studio Solution File, Format Version {0}", SolutionVersion);
  553. ss.WriteLine(SolutionTag);
  554. WriteProjectDeclarations(ss, solution, solution);
  555. ss.WriteLine("Global");
  556. ss.WriteLine("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution");
  557. foreach (ConfigurationNode conf in solution.Configurations)
  558. {
  559. ss.WriteLine("\t\t{0} = {0}", conf.NameAndPlatform);
  560. }
  561. ss.WriteLine("\tEndGlobalSection");
  562. ss.WriteLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution");
  563. WriteConfigurationLines(solution.Configurations, solution, ss);
  564. ss.WriteLine("\tEndGlobalSection");
  565. if (solution.Solutions.Count > 0)
  566. {
  567. ss.WriteLine("\tGlobalSection(NestedProjects) = preSolution");
  568. foreach (SolutionNode embeddedSolution in solution.Solutions)
  569. {
  570. WriteNestedProjectMap(ss, embeddedSolution);
  571. }
  572. ss.WriteLine("\tEndGlobalSection");
  573. }
  574. ss.WriteLine("EndGlobal");
  575. }
  576. kernel.CurrentWorkingDirectory.Pop();
  577. }
  578. }
  579. private void WriteProjectDeclarations(TextWriter writer, SolutionNode actualSolution, SolutionNode embeddedSolution)
  580. {
  581. foreach (SolutionNode childSolution in embeddedSolution.Solutions)
  582. {
  583. WriteEmbeddedSolution(writer, childSolution);
  584. WriteProjectDeclarations(writer, actualSolution, childSolution);
  585. }
  586. foreach (ProjectNode project in embeddedSolution.Projects)
  587. {
  588. WriteProject(actualSolution, writer, project);
  589. }
  590. foreach (DatabaseProjectNode dbProject in embeddedSolution.DatabaseProjects)
  591. {
  592. WriteProject(actualSolution, writer, dbProject);
  593. }
  594. if (actualSolution.Guid == embeddedSolution.Guid)
  595. {
  596. WriteSolutionFiles(actualSolution, writer);
  597. }
  598. }
  599. private static void WriteNestedProjectMap(TextWriter writer, SolutionNode embeddedSolution)
  600. {
  601. foreach (ProjectNode project in embeddedSolution.Projects)
  602. {
  603. WriteNestedProject(writer, embeddedSolution, project.Guid);
  604. }
  605. foreach (DatabaseProjectNode dbProject in embeddedSolution.DatabaseProjects)
  606. {
  607. WriteNestedProject(writer, embeddedSolution, dbProject.Guid);
  608. }
  609. foreach (SolutionNode child in embeddedSolution.Solutions)
  610. {
  611. WriteNestedProject(writer, embeddedSolution, child.Guid);
  612. WriteNestedProjectMap(writer, child);
  613. }
  614. }
  615. private static void WriteNestedProject(TextWriter writer, SolutionNode solution, Guid projectGuid)
  616. {
  617. WriteNestedFolder(writer, solution.Guid, projectGuid);
  618. }
  619. private static void WriteNestedFolder(TextWriter writer, Guid parentGuid, Guid childGuid)
  620. {
  621. writer.WriteLine("\t\t{0} = {1}",
  622. childGuid.ToString("B").ToUpper(),
  623. parentGuid.ToString("B").ToUpper());
  624. }
  625. private static void WriteConfigurationLines(IEnumerable<ConfigurationNode> configurations, SolutionNode solution, TextWriter ss)
  626. {
  627. foreach (ProjectNode project in solution.Projects)
  628. {
  629. foreach (ConfigurationNode conf in configurations)
  630. {
  631. ss.WriteLine("\t\t{0}.{1}.ActiveCfg = {1}",
  632. project.Guid.ToString("B").ToUpper(),
  633. conf.NameAndPlatform);
  634. ss.WriteLine("\t\t{0}.{1}.Build.0 = {1}",
  635. project.Guid.ToString("B").ToUpper(),
  636. conf.NameAndPlatform);
  637. }
  638. }
  639. foreach (SolutionNode child in solution.Solutions)
  640. {
  641. WriteConfigurationLines(configurations, child, ss);
  642. }
  643. }
  644. private void WriteSolutionFiles(SolutionNode solution, TextWriter ss)
  645. {
  646. if(solution.Files != null && solution.Files.Count > 0)
  647. WriteProject(ss, "Folder", solution.Guid, "Solution Files", "Solution Files", solution.Files);
  648. }
  649. private void WriteEmbeddedSolution(TextWriter writer, SolutionNode embeddedSolution)
  650. {
  651. WriteProject(writer, "Folder", embeddedSolution.Guid, embeddedSolution.Name, embeddedSolution.Name, embeddedSolution.Files);
  652. }
  653. private void WriteProject(SolutionNode solution, TextWriter ss, ProjectNode project)
  654. {
  655. WriteProject(ss, solution, project.Language, project.Guid, project.Name, project.FullPath);
  656. }
  657. private void WriteProject(SolutionNode solution, TextWriter ss, DatabaseProjectNode dbProject)
  658. {
  659. if (solution.Files != null && solution.Files.Count > 0)
  660. WriteProject(ss, solution, "Database", dbProject.Guid, dbProject.Name, dbProject.FullPath);
  661. }
  662. const string ProjectDeclarationBeginFormat = "Project(\"{0}\") = \"{1}\", \"{2}\", \"{3}\"";
  663. const string ProjectDeclarationEndFormat = "EndProject";
  664. private void WriteProject(TextWriter ss, SolutionNode solution, string language, Guid guid, string name, string projectFullPath)
  665. {
  666. if (!tools.ContainsKey(language))
  667. throw new UnknownLanguageException("Unknown .NET language: " + language);
  668. ToolInfo toolInfo = tools[language];
  669. string path = Helper.MakePathRelativeTo(solution.FullPath, projectFullPath);
  670. path = Helper.MakeFilePath(path, name, toolInfo.FileExtension);
  671. WriteProject(ss, language, guid, name, path);
  672. }
  673. private void WriteProject(TextWriter writer, string language, Guid projectGuid, string name, string location)
  674. {
  675. WriteProject(writer, language, projectGuid, name, location, null);
  676. }
  677. private void WriteProject(TextWriter writer, string language, Guid projectGuid, string name, string location, FilesNode files)
  678. {
  679. if (!tools.ContainsKey(language))
  680. throw new UnknownLanguageException("Unknown .NET language: " + language);
  681. ToolInfo toolInfo = tools[language];
  682. writer.WriteLine(ProjectDeclarationBeginFormat,
  683. toolInfo.Guid,
  684. name,
  685. location,
  686. projectGuid.ToString("B").ToUpper());
  687. if (files != null)
  688. {
  689. writer.WriteLine("\tProjectSection(SolutionItems) = preProject");
  690. foreach (string file in files)
  691. writer.WriteLine("\t\t{0} = {0}", file);
  692. writer.WriteLine("\tEndProjectSection");
  693. }
  694. writer.WriteLine(ProjectDeclarationEndFormat);
  695. }
  696. private void WriteDatabaseProject(SolutionNode solution, DatabaseProjectNode project)
  697. {
  698. string projectFile = Helper.MakeFilePath(project.FullPath, project.Name, "dbp");
  699. IndentedTextWriter ps = new IndentedTextWriter(new StreamWriter(projectFile), " ");
  700. kernel.CurrentWorkingDirectory.Push();
  701. Helper.SetCurrentDir(Path.GetDirectoryName(projectFile));
  702. using (ps)
  703. {
  704. ps.WriteLine("# Microsoft Developer Studio Project File - Database Project");
  705. ps.WriteLine("Begin DataProject = \"{0}\"", project.Name);
  706. ps.Indent++;
  707. ps.WriteLine("MSDTVersion = \"80\"");
  708. // TODO: Use the project.Files property
  709. if (ContainsSqlFiles(Path.GetDirectoryName(projectFile)))
  710. WriteDatabaseFoldersAndFiles(ps, Path.GetDirectoryName(projectFile));
  711. ps.WriteLine("Begin DBRefFolder = \"Database References\"");
  712. ps.Indent++;
  713. foreach (DatabaseReferenceNode reference in project.References)
  714. {
  715. ps.WriteLine("Begin DBRefNode = \"{0}\"", reference.Name);
  716. ps.Indent++;
  717. ps.WriteLine("ConnectStr = \"{0}\"", reference.ConnectionString);
  718. ps.WriteLine("Provider = \"{0}\"", reference.ProviderId.ToString("B").ToUpper());
  719. //ps.WriteLine("Colorizer = 5");
  720. ps.Indent--;
  721. ps.WriteLine("End");
  722. }
  723. ps.Indent--;
  724. ps.WriteLine("End");
  725. ps.Indent--;
  726. ps.WriteLine("End");
  727. ps.Flush();
  728. }
  729. kernel.CurrentWorkingDirectory.Pop();
  730. }
  731. private static bool ContainsSqlFiles(string folder)
  732. {
  733. if(Directory.GetFiles(folder, "*.sql").Length > 0)
  734. return true; // if the folder contains 1 .sql file, that's good enough
  735. foreach (string child in Directory.GetDirectories(folder))
  736. {
  737. if (ContainsSqlFiles(child))
  738. return true; // if 1 child folder contains a .sql file, still good enough
  739. }
  740. return false;
  741. }
  742. private static void WriteDatabaseFoldersAndFiles(IndentedTextWriter writer, string folder)
  743. {
  744. foreach (string child in Directory.GetDirectories(folder))
  745. {
  746. if (ContainsSqlFiles(child))
  747. {
  748. writer.WriteLine("Begin Folder = \"{0}\"", Path.GetFileName(child));
  749. writer.Indent++;
  750. WriteDatabaseFoldersAndFiles(writer, child);
  751. writer.Indent--;
  752. writer.WriteLine("End");
  753. }
  754. }
  755. foreach (string file in Directory.GetFiles(folder, "*.sql"))
  756. {
  757. writer.WriteLine("Script = \"{0}\"", Path.GetFileName(file));
  758. }
  759. }
  760. private void CleanProject(ProjectNode project)
  761. {
  762. kernel.Log.Write("...Cleaning project: {0}", project.Name);
  763. ToolInfo toolInfo = tools[project.Language];
  764. string projectFile = Helper.MakeFilePath(project.FullPath, project.Name, toolInfo.FileExtension);
  765. string userFile = projectFile + ".user";
  766. Helper.DeleteIfExists(projectFile);
  767. Helper.DeleteIfExists(userFile);
  768. }
  769. private void CleanSolution(SolutionNode solution)
  770. {
  771. kernel.Log.Write("Cleaning {0} solution and project files", VersionName, solution.Name);
  772. string slnFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "sln");
  773. string suoFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "suo");
  774. Helper.DeleteIfExists(slnFile);
  775. Helper.DeleteIfExists(suoFile);
  776. foreach (ProjectNode project in solution.Projects)
  777. {
  778. CleanProject(project);
  779. }
  780. kernel.Log.Write("");
  781. }
  782. #endregion
  783. #region ITarget Members
  784. /// <summary>
  785. /// Writes the specified kern.
  786. /// </summary>
  787. /// <param name="kern">The kern.</param>
  788. public virtual void Write(Kernel kern)
  789. {
  790. if (kern == null)
  791. {
  792. throw new ArgumentNullException("kern");
  793. }
  794. kernel = kern;
  795. foreach (SolutionNode sol in kernel.Solutions)
  796. {
  797. WriteSolution(sol, true);
  798. }
  799. kernel = null;
  800. }
  801. /// <summary>
  802. /// Cleans the specified kern.
  803. /// </summary>
  804. /// <param name="kern">The kern.</param>
  805. public virtual void Clean(Kernel kern)
  806. {
  807. if (kern == null)
  808. {
  809. throw new ArgumentNullException("kern");
  810. }
  811. kernel = kern;
  812. foreach (SolutionNode sol in kernel.Solutions)
  813. {
  814. CleanSolution(sol);
  815. }
  816. kernel = null;
  817. }
  818. #endregion
  819. }
  820. }