NETProjectGen.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Poco/UUID.h>
  23. #include <Poco/UUIDGenerator.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/File.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include "../ToolEnvironment.h"
  28. #include "../ToolSystem.h"
  29. #include "../Project/Project.h"
  30. #include "NETProjectGen.h"
  31. namespace ToolCore
  32. {
  33. NETProjectBase::NETProjectBase(Context* context, NETProjectGen* projectGen) :
  34. Object(context), xmlFile_(new XMLFile(context)), projectGen_(projectGen)
  35. {
  36. }
  37. NETProjectBase::~NETProjectBase()
  38. {
  39. }
  40. void NETProjectBase::ReplacePathStrings(String& path)
  41. {
  42. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  43. String atomicRoot = tenv->GetRootSourceDir();
  44. atomicRoot = RemoveTrailingSlash(atomicRoot);
  45. const String& scriptPlatform = projectGen_->GetScriptPlatform();
  46. path.Replace("$ATOMIC_ROOT$", atomicRoot, false);
  47. path.Replace("$SCRIPT_PLATFORM$", scriptPlatform, false);
  48. }
  49. NETCSProject::NETCSProject(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  50. {
  51. }
  52. NETCSProject::~NETCSProject()
  53. {
  54. }
  55. bool NETCSProject::CreateProjectFolder(const String& path)
  56. {
  57. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  58. if (fileSystem->DirExists(path))
  59. return true;
  60. fileSystem->CreateDirsRecursive(path);
  61. if (!fileSystem->DirExists(path))
  62. {
  63. ATOMIC_LOGERRORF("Unable to create dir: %s", path.CString());
  64. return false;
  65. }
  66. return true;
  67. }
  68. void NETCSProject::CreateCompileItemGroup(XMLElement &projectRoot)
  69. {
  70. FileSystem* fs = GetSubsystem<FileSystem>();
  71. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  72. // Compile AssemblyInfo.cs
  73. igroup.CreateChild("Compile").SetAttribute("Include", "Properties\\AssemblyInfo.cs");
  74. for (unsigned i = 0; i < sourceFolders_.Size(); i++)
  75. {
  76. const String& sourceFolder = sourceFolders_[i];
  77. Vector<String> result;
  78. fs->ScanDir(result, sourceFolder, "*.cs", SCAN_FILES, true);
  79. for (unsigned j = 0; j < result.Size(); j++)
  80. {
  81. XMLElement compile = igroup.CreateChild("Compile");
  82. String path = sourceFolder + result[j];
  83. String relativePath;
  84. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  85. {
  86. path = relativePath + GetFileName(path) + GetExtension(path);
  87. }
  88. // IMPORTANT: / Slash direction breaks intellisense :/
  89. path.Replace('/', '\\');
  90. compile.SetAttribute("Include", path.CString());
  91. // put generated files into generated folder
  92. if (sourceFolder.Contains("Generated") && sourceFolder.Contains("CSharp") && sourceFolder.Contains("Packages"))
  93. {
  94. compile.CreateChild("Link").SetValue("Generated\\" + result[j]);
  95. }
  96. else
  97. {
  98. compile.CreateChild("Link").SetValue(result[j]);
  99. }
  100. }
  101. }
  102. }
  103. void NETProjectBase::CopyXMLElementRecursive(XMLElement source, XMLElement dest)
  104. {
  105. Vector<String> attrNames = source.GetAttributeNames();
  106. for (unsigned i = 0; i < attrNames.Size(); i++)
  107. {
  108. String value = source.GetAttribute(attrNames[i]);
  109. dest.SetAttribute(attrNames[i], value);
  110. }
  111. dest.SetValue(source.GetValue());
  112. XMLElement child = source.GetChild();
  113. while (child.NotNull() && child.GetName().Length())
  114. {
  115. XMLElement childDest = dest.CreateChild(child.GetName());
  116. CopyXMLElementRecursive(child, childDest);
  117. child = child.GetNext();
  118. }
  119. }
  120. void NETCSProject::CreateReferencesItemGroup(XMLElement &projectRoot)
  121. {
  122. XMLElement xref;
  123. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  124. for (unsigned i = 0; i < references_.Size(); i++)
  125. {
  126. String ref = references_[i];
  127. // project reference
  128. if (projectGen_->GetCSProjectByName(ref))
  129. continue;
  130. // NuGet project
  131. if (ref.StartsWith("<"))
  132. {
  133. XMLFile xmlFile(context_);
  134. if (!xmlFile.FromString(ref))
  135. {
  136. ATOMIC_LOGERROR("NETCSProject::CreateReferencesItemGroup - Unable to parse reference XML");
  137. }
  138. xref = igroup.CreateChild("Reference");
  139. CopyXMLElementRecursive(xmlFile.GetRoot(), xref);
  140. continue;
  141. }
  142. xref = igroup.CreateChild("Reference");
  143. xref.SetAttribute("Include", ref);
  144. }
  145. Project* project = projectGen_->GetAtomicProject();
  146. if (project)
  147. {
  148. Vector<String> result;
  149. GetSubsystem<FileSystem>()->ScanDir(result, project->GetResourcePath(), "*.dll", SCAN_FILES, true);
  150. for (unsigned j = 0; j < result.Size(); j++)
  151. {
  152. String path = project->GetResourcePath() + result[j];
  153. String relativePath;
  154. if (GetRelativePath(projectPath_, GetPath(path), relativePath))
  155. {
  156. if (projectGen_->GetCSProjectByName(GetFileName(path)))
  157. continue;
  158. path = relativePath + GetFileName(path) + GetExtension(path);
  159. }
  160. xref = igroup.CreateChild("Reference");
  161. xref.SetAttribute("Include", path);
  162. }
  163. }
  164. }
  165. void NETCSProject::CreateProjectReferencesItemGroup(XMLElement &projectRoot)
  166. {
  167. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  168. for (unsigned i = 0; i < references_.Size(); i++)
  169. {
  170. const String& ref = references_[i];
  171. NETCSProject* project = projectGen_->GetCSProjectByName(ref);
  172. if (!project)
  173. continue;
  174. XMLElement projectRef = igroup.CreateChild("ProjectReference");
  175. projectRef.SetAttribute("Include", ToString("..\\%s\\%s.csproj", ref.CString(), ref.CString()));
  176. XMLElement xproject = projectRef.CreateChild("Project");
  177. xproject.SetValue(ToString("{%s}", project->GetProjectGUID().ToLower().CString()));
  178. XMLElement xname = projectRef.CreateChild("Name");
  179. xname.SetValue(project->GetName());
  180. }
  181. }
  182. void NETCSProject::CreatePackagesItemGroup(XMLElement &projectRoot)
  183. {
  184. if (!packages_.Size())
  185. return;
  186. XMLElement xref;
  187. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  188. xref = igroup.CreateChild("None");
  189. xref.SetAttribute("Include", "packages.config");
  190. XMLFile packageConfig(context_);
  191. XMLElement packageRoot = packageConfig.CreateRoot("packages");
  192. for (unsigned i = 0; i < packages_.Size(); i++)
  193. {
  194. XMLFile xmlFile(context_);
  195. if (!xmlFile.FromString(packages_[i]))
  196. {
  197. ATOMIC_LOGERROR("NETCSProject::CreatePackagesItemGroup - Unable to parse package xml");
  198. }
  199. xref = packageRoot.CreateChild("package");
  200. CopyXMLElementRecursive(xmlFile.GetRoot(), xref);
  201. }
  202. SharedPtr<File> output(new File(context_, projectPath_ + "packages.config", FILE_WRITE));
  203. String source = packageConfig.ToString();
  204. output->Write(source.CString(), source.Length());
  205. }
  206. void NETCSProject::GetAssemblySearchPaths(String& paths)
  207. {
  208. paths.Clear();
  209. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  210. Vector<String> searchPaths;
  211. if (assemblySearchPaths_.Length())
  212. searchPaths.Push(assemblySearchPaths_);
  213. paths.Join(searchPaths, ";");
  214. }
  215. void NETCSProject::CreateReleasePropertyGroup(XMLElement &projectRoot)
  216. {
  217. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  218. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
  219. pgroup.CreateChild("DebugType").SetValue("full");
  220. pgroup.CreateChild("Optimize").SetValue("true");
  221. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_ + "Release\\");
  222. pgroup.CreateChild("DefineConstants").SetValue("TRACE");
  223. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  224. pgroup.CreateChild("WarningLevel").SetValue("4");
  225. pgroup.CreateChild("ConsolePause").SetValue("false");
  226. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  227. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  228. }
  229. void NETCSProject::CreateDebugPropertyGroup(XMLElement &projectRoot)
  230. {
  231. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  232. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
  233. pgroup.CreateChild("DebugSymbols").SetValue("true");
  234. pgroup.CreateChild("DebugType").SetValue("full");
  235. pgroup.CreateChild("Optimize").SetValue("false");
  236. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_ + "Debug\\");
  237. pgroup.CreateChild("DefineConstants").SetValue("DEBUG;TRACE");
  238. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  239. pgroup.CreateChild("WarningLevel").SetValue("4");
  240. pgroup.CreateChild("ConsolePause").SetValue("false");
  241. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  242. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  243. }
  244. void NETCSProject::CreateAssemblyInfo()
  245. {
  246. String info = "using System.Reflection;\nusing System.Runtime.CompilerServices;\nusing System.Runtime.InteropServices;\n\n\n";
  247. info += ToString("[assembly:AssemblyTitle(\"%s\")]\n", name_.CString());
  248. info += "[assembly:AssemblyDescription(\"\")]\n";
  249. info += "[assembly:AssemblyConfiguration(\"\")]\n";
  250. info += "[assembly:AssemblyCompany(\"\")]\n";
  251. info += ToString("[assembly:AssemblyProduct(\"%s\")]\n", name_.CString());
  252. info += "\n\n\n";
  253. info += "[assembly:ComVisible(false)]\n";
  254. info += "\n\n";
  255. info += ToString("[assembly:Guid(\"%s\")]\n", projectGuid_.CString());
  256. info += "\n\n";
  257. info += "[assembly:AssemblyVersion(\"1.0.0.0\")]\n";
  258. info += "[assembly:AssemblyFileVersion(\"1.0.0.0\")]\n";
  259. SharedPtr<File> output(new File(context_, projectPath_ + "Properties/AssemblyInfo.cs", FILE_WRITE));
  260. output->Write(info.CString(), info.Length());
  261. }
  262. void NETCSProject::CreateMainPropertyGroup(XMLElement& projectRoot)
  263. {
  264. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  265. // Configuration
  266. XMLElement config = pgroup.CreateChild("Configuration");
  267. config.SetAttribute("Condition", " '$(Configuration)' == '' ");
  268. config.SetValue("Debug");
  269. // Platform
  270. XMLElement platform = pgroup.CreateChild("Platform");
  271. platform.SetAttribute("Condition", " '$(Platform)' == '' ");
  272. platform.SetValue("AnyCPU");
  273. // ProjectGuid
  274. XMLElement guid = pgroup.CreateChild("ProjectGuid");
  275. guid.SetValue("{" + projectGuid_ + "}");
  276. // OutputType
  277. XMLElement outputType = pgroup.CreateChild("OutputType");
  278. outputType.SetValue(outputType_);
  279. pgroup.CreateChild("AppDesignerFolder").SetValue("Properties");
  280. // RootNamespace
  281. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  282. rootNamespace.SetValue(rootNamespace_);
  283. // AssemblyName
  284. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  285. assemblyName.SetValue(assemblyName_);
  286. // TargetFrameworkVersion
  287. XMLElement targetFrameWork = pgroup.CreateChild("TargetFrameworkVersion");
  288. targetFrameWork.SetValue("v4.6");
  289. pgroup.CreateChild("FileAlignment").SetValue("512");
  290. }
  291. bool NETCSProject::Generate()
  292. {
  293. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  294. NETSolution* solution = projectGen_->GetSolution();
  295. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  296. projectPath_ = solution->GetOutputPath() + name_ + "/";
  297. if (!CreateProjectFolder(projectPath_))
  298. return false;
  299. if (!CreateProjectFolder(projectPath_ + "Properties"))
  300. return false;
  301. XMLElement project = xmlFile_->CreateRoot("Project");
  302. project.SetAttribute("DefaultTargets", "Build");
  303. project.SetAttribute("ToolsVersion", "14.0");
  304. project.SetAttribute("DefaultTargets", "Build");
  305. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  306. XMLElement import = project.CreateChild("Import");
  307. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  308. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  309. CreateMainPropertyGroup(project);
  310. CreateDebugPropertyGroup(project);
  311. CreateReleasePropertyGroup(project);
  312. CreateReferencesItemGroup(project);
  313. CreateProjectReferencesItemGroup(project);
  314. CreateCompileItemGroup(project);
  315. CreatePackagesItemGroup(project);
  316. CreateAssemblyInfo();
  317. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
  318. Project* atomicProject = projectGen_->GetAtomicProject();
  319. if (atomicProject)
  320. {
  321. XMLElement afterBuild = project.CreateChild("Target");
  322. afterBuild.SetAttribute("Name", "AfterBuild");
  323. XMLElement copy = afterBuild.CreateChild("Copy");
  324. copy.SetAttribute("SourceFiles", "$(TargetPath)");
  325. String destPath = projectPath_ + "../../../Resources/";
  326. String relativePath;
  327. if (GetRelativePath(projectPath_, atomicProject->GetResourcePath(), relativePath))
  328. {
  329. destPath = AddTrailingSlash(relativePath);
  330. }
  331. copy.SetAttribute("DestinationFolder", destPath);
  332. // Create the AtomicProject.csproj.user file if it doesn't exist
  333. String userSettingsFilename = projectPath_ + name_ + ".csproj.user";
  334. if (!fileSystem->FileExists(userSettingsFilename))
  335. {
  336. SharedPtr<XMLFile> userSettings(new XMLFile(context_));
  337. XMLElement project = userSettings->CreateRoot("Project");
  338. //XMLElement xml = userRoot.CreateChild("?xml");
  339. //xml.SetAttribute("version", "1.0");
  340. //xml.SetAttribute("encoding", "utf-8");
  341. project.SetAttribute("ToolsVersion", "14.0");
  342. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  343. StringVector configs;
  344. configs.Push("Debug");
  345. configs.Push("Release");
  346. for (unsigned i = 0; i < configs.Size(); i++)
  347. {
  348. String cfg = configs[i];
  349. XMLElement propertyGroup = project.CreateChild("PropertyGroup");
  350. propertyGroup.SetAttribute("Condition", ToString("'$(Configuration)|$(Platform)' == '%s|AnyCPU'", cfg.CString()));
  351. String startArguments;
  352. #ifdef ATOMIC_DEV_BUILD
  353. String playerBin = tenv->GetAtomicNETRootDir() + cfg + "/AtomicIPCPlayer.exe";
  354. #else
  355. String playerBin = tenv->GetAtomicNETRootDir() + "Release/AtomicIPCPlayer.exe";
  356. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  357. #endif
  358. propertyGroup.CreateChild("StartAction").SetValue("Program");
  359. propertyGroup.CreateChild("StartProgram").SetValue(playerBin );
  360. startArguments += ToString("--project \"%s\"", atomicProject->GetProjectPath().CString());
  361. propertyGroup.CreateChild("StartArguments").SetValue(startArguments);
  362. }
  363. String userSettingsSource = userSettings->ToString();
  364. SharedPtr<File> output(new File(context_, userSettingsFilename, FILE_WRITE));
  365. output->Write(userSettingsSource.CString(), userSettingsSource.Length());
  366. output->Close();
  367. }
  368. }
  369. String projectSource = xmlFile_->ToString();
  370. SharedPtr<File> output(new File(context_, projectPath_ + name_ + ".csproj", FILE_WRITE));
  371. output->Write(projectSource.CString(), projectSource.Length());
  372. return true;
  373. }
  374. bool NETCSProject::Load(const JSONValue& root)
  375. {
  376. name_ = root["name"].GetString();
  377. projectGuid_ = projectGen_->GenerateUUID();
  378. outputType_ = root["outputType"].GetString();
  379. rootNamespace_ = root["rootNamespace"].GetString();
  380. assemblyName_ = root["assemblyName"].GetString();
  381. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  382. ReplacePathStrings(assemblyOutputPath_);
  383. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  384. ReplacePathStrings(assemblySearchPaths_);
  385. const JSONArray& references = root["references"].GetArray();
  386. for (unsigned i = 0; i < references.Size(); i++)
  387. {
  388. String reference = references[i].GetString();
  389. ReplacePathStrings(reference);
  390. references_.Push(reference);
  391. }
  392. const JSONArray& packages = root["packages"].GetArray();
  393. for (unsigned i = 0; i < packages.Size(); i++)
  394. {
  395. String package = packages[i].GetString();
  396. if (packages_.Find(package) != packages_.End())
  397. {
  398. ATOMIC_LOGERRORF("Duplicate package found %s", package.CString());
  399. continue;
  400. }
  401. projectGen_->GetSolution()->RegisterPackage(package);
  402. packages_.Push(package);
  403. }
  404. const JSONArray& sources = root["sources"].GetArray();
  405. for (unsigned i = 0; i < sources.Size(); i++)
  406. {
  407. String source = sources[i].GetString();
  408. ReplacePathStrings(source);
  409. sourceFolders_.Push(AddTrailingSlash(source));
  410. }
  411. return true;
  412. }
  413. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen, bool rewrite) : NETProjectBase(context, projectGen),
  414. rewriteSolution_(rewrite)
  415. {
  416. }
  417. NETSolution::~NETSolution()
  418. {
  419. }
  420. bool NETSolution::Generate()
  421. {
  422. String slnPath = outputPath_ + name_ + ".sln";
  423. GenerateSolution(slnPath);
  424. return true;
  425. }
  426. void NETSolution::GenerateSolution(const String &slnPath)
  427. {
  428. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  429. source += "# Visual Studio 14\n";
  430. source += "VisualStudioVersion = 14.0.25420.1\n";
  431. source += "MinimumVisualStudioVersion = 10.0.40219.1\n";
  432. solutionGUID_ = projectGen_->GenerateUUID();
  433. PODVector<NETCSProject*> depends;
  434. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  435. for (unsigned i = 0; i < projects.Size(); i++)
  436. {
  437. NETCSProject* p = projects.At(i);
  438. const String& projectName = p->GetName();
  439. const String& projectGUID = p->GetProjectGUID();
  440. const String CSharpProjectGUID = "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  441. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.csproj\", \"{%s}\"\n",
  442. CSharpProjectGUID.CString(), projectName.CString(), projectName.CString(),
  443. projectName.CString(), projectGUID.CString());
  444. projectGen_->GetCSProjectDependencies(p, depends);
  445. if (depends.Size())
  446. {
  447. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  448. for (unsigned j = 0; j < depends.Size(); j++)
  449. {
  450. source += ToString("\t{%s} = {%s}\n",
  451. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  452. }
  453. source += "\tEndProjectSection\n";
  454. }
  455. source += "EndProject\n";
  456. }
  457. source += "Global\n";
  458. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  459. source += " Debug|Any CPU = Debug|Any CPU\n";
  460. source += " Release|Any CPU = Release|Any CPU\n";
  461. source += " EndGlobalSection\n";
  462. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  463. for (unsigned i = 0; i < projects.Size(); i++)
  464. {
  465. NETCSProject* p = projects.At(i);
  466. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  467. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  468. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  469. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  470. }
  471. source += " EndGlobalSection\n";
  472. source += "EndGlobal\n";
  473. if (!rewriteSolution_)
  474. {
  475. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  476. if (fileSystem->Exists(slnPath))
  477. return;
  478. }
  479. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  480. output->Write(source.CString(), source.Length());
  481. output->Close();
  482. }
  483. bool NETSolution::Load(const JSONValue& root)
  484. {
  485. FileSystem* fs = GetSubsystem<FileSystem>();
  486. name_ = root["name"].GetString();
  487. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  488. ReplacePathStrings(outputPath_);
  489. // TODO: use poco mkdirs
  490. if (!fs->DirExists(outputPath_))
  491. fs->CreateDirsRecursive(outputPath_);
  492. return true;
  493. }
  494. bool NETSolution::RegisterPackage(const String& package)
  495. {
  496. if (packages_.Find(package) != packages_.End())
  497. return false;
  498. packages_.Push(package);
  499. return true;
  500. }
  501. NETProjectGen::NETProjectGen(Context* context) : Object(context),
  502. rewriteSolution_(false)
  503. {
  504. }
  505. NETProjectGen::~NETProjectGen()
  506. {
  507. }
  508. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  509. {
  510. for (unsigned i = 0; i < projects_.Size(); i++)
  511. {
  512. if (projects_[i]->GetName() == name)
  513. return projects_[i];
  514. }
  515. return nullptr;
  516. }
  517. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  518. {
  519. depends.Clear();
  520. const Vector<String>& references = source->GetReferences();
  521. for (unsigned i = 0; i < projects_.Size(); i++)
  522. {
  523. NETCSProject* pdepend = projects_.At(i);
  524. if (source == pdepend)
  525. continue;
  526. for (unsigned j = 0; j < references.Size(); j++)
  527. {
  528. if (pdepend->GetName() == references[j])
  529. {
  530. depends.Push(pdepend);
  531. }
  532. }
  533. }
  534. return depends.Size() != 0;
  535. }
  536. bool NETProjectGen::Generate()
  537. {
  538. solution_->Generate();
  539. for (unsigned i = 0; i < projects_.Size(); i++)
  540. {
  541. if (!projects_[i]->Generate())
  542. return false;
  543. }
  544. return true;
  545. }
  546. void NETProjectGen::SetRewriteSolution(bool rewrite)
  547. {
  548. rewriteSolution_ = rewrite;
  549. if (solution_.NotNull())
  550. solution_->SetRewriteSolution(rewrite);
  551. }
  552. bool NETProjectGen::LoadProject(const JSONValue &root)
  553. {
  554. solution_ = new NETSolution(context_, this, rewriteSolution_);
  555. solution_->Load(root["solution"]);
  556. const JSONValue& jprojects = root["projects"];
  557. if (!jprojects.IsArray() || !jprojects.Size())
  558. return false;
  559. for (unsigned i = 0; i < jprojects.Size(); i++)
  560. {
  561. const JSONValue& jproject = jprojects[i];
  562. if (!jproject.IsObject())
  563. return false;
  564. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  565. if (!csProject->Load(jproject))
  566. return false;
  567. projects_.Push(csProject);
  568. }
  569. return true;
  570. }
  571. bool NETProjectGen::LoadProject(const String& projectPath)
  572. {
  573. SharedPtr<File> file(new File(context_));
  574. if (!file->Open(projectPath))
  575. return false;
  576. String json;
  577. file->ReadText(json);
  578. JSONValue jvalue;
  579. if (!JSONFile::ParseJSON(json, jvalue))
  580. return false;
  581. return LoadProject(jvalue);
  582. }
  583. bool NETProjectGen::LoadProject(Project* project)
  584. {
  585. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  586. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  587. atomicProject_ = project;
  588. JSONValue root;
  589. JSONValue solution;
  590. solution["name"] = "AtomicProject";
  591. solution["outputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Solution/";
  592. JSONArray projects;
  593. JSONObject jproject;
  594. jproject["name"] = "AtomicProject";
  595. jproject["outputType"] = "Library";
  596. jproject["assemblyName"] = "AtomicProject";
  597. jproject["assemblyOutputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Bin/";
  598. JSONArray references;
  599. references.Push(JSONValue("System"));
  600. references.Push(JSONValue("System.Core"));
  601. references.Push(JSONValue("System.Xml.Linq"));
  602. references.Push(JSONValue("System.XML"));
  603. String atomicNETAssembly = tenv->GetAtomicNETCoreAssemblyDir() + "AtomicNET.dll";
  604. if (!fileSystem->FileExists(atomicNETAssembly))
  605. {
  606. ATOMIC_LOGERRORF("NETProjectGen::LoadProject - AtomicNET assembly does not exist: %s", atomicNETAssembly.CString());
  607. return false;
  608. }
  609. references.Push(JSONValue(atomicNETAssembly));
  610. jproject["references"] = references;
  611. JSONArray sources;
  612. sources.Push(JSONValue(ToString("%s", project->GetResourcePath().CString())));
  613. jproject["sources"] = sources;
  614. projects.Push(jproject);
  615. root["projects"] = projects;
  616. root["solution"] = solution;
  617. return LoadProject(root);
  618. }
  619. bool NETProjectGen::GetRequiresNuGet()
  620. {
  621. if (solution_.Null())
  622. {
  623. ATOMIC_LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  624. return false;
  625. }
  626. return solution_->GetPackages().Size() != 0;
  627. }
  628. String NETProjectGen::GenerateUUID()
  629. {
  630. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  631. Poco::UUID uuid(generator.create()); // time based
  632. return String(uuid.toString().c_str()).ToUpper();
  633. }
  634. }