NETProjectGen.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.csproj\", \"{%s}\"\n",
  441. solutionGUID_.CString(), projectName.CString(), projectName.CString(),
  442. projectName.CString(), projectGUID.CString());
  443. projectGen_->GetCSProjectDependencies(p, depends);
  444. if (depends.Size())
  445. {
  446. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  447. for (unsigned j = 0; j < depends.Size(); j++)
  448. {
  449. source += ToString("\t{%s} = {%s}\n",
  450. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  451. }
  452. source += "\tEndProjectSection\n";
  453. }
  454. source += "\tEndProject\n";
  455. }
  456. source += "Global\n";
  457. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  458. source += " Debug|Any CPU = Debug|Any CPU\n";
  459. source += " Release|Any CPU = Release|Any CPU\n";
  460. source += " EndGlobalSection\n";
  461. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  462. for (unsigned i = 0; i < projects.Size(); i++)
  463. {
  464. NETCSProject* p = projects.At(i);
  465. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  466. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  467. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  468. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  469. }
  470. source += " EndGlobalSection\n";
  471. source += "EndGlobal\n";
  472. if (!rewriteSolution_)
  473. {
  474. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  475. if (fileSystem->Exists(slnPath))
  476. return;
  477. }
  478. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  479. output->Write(source.CString(), source.Length());
  480. output->Close();
  481. }
  482. bool NETSolution::Load(const JSONValue& root)
  483. {
  484. FileSystem* fs = GetSubsystem<FileSystem>();
  485. name_ = root["name"].GetString();
  486. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  487. ReplacePathStrings(outputPath_);
  488. // TODO: use poco mkdirs
  489. if (!fs->DirExists(outputPath_))
  490. fs->CreateDirsRecursive(outputPath_);
  491. return true;
  492. }
  493. bool NETSolution::RegisterPackage(const String& package)
  494. {
  495. if (packages_.Find(package) != packages_.End())
  496. return false;
  497. packages_.Push(package);
  498. return true;
  499. }
  500. NETProjectGen::NETProjectGen(Context* context) : Object(context),
  501. rewriteSolution_(false)
  502. {
  503. }
  504. NETProjectGen::~NETProjectGen()
  505. {
  506. }
  507. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  508. {
  509. for (unsigned i = 0; i < projects_.Size(); i++)
  510. {
  511. if (projects_[i]->GetName() == name)
  512. return projects_[i];
  513. }
  514. return nullptr;
  515. }
  516. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  517. {
  518. depends.Clear();
  519. const Vector<String>& references = source->GetReferences();
  520. for (unsigned i = 0; i < projects_.Size(); i++)
  521. {
  522. NETCSProject* pdepend = projects_.At(i);
  523. if (source == pdepend)
  524. continue;
  525. for (unsigned j = 0; j < references.Size(); j++)
  526. {
  527. if (pdepend->GetName() == references[j])
  528. {
  529. depends.Push(pdepend);
  530. }
  531. }
  532. }
  533. return depends.Size() != 0;
  534. }
  535. bool NETProjectGen::Generate()
  536. {
  537. solution_->Generate();
  538. for (unsigned i = 0; i < projects_.Size(); i++)
  539. {
  540. if (!projects_[i]->Generate())
  541. return false;
  542. }
  543. return true;
  544. }
  545. void NETProjectGen::SetRewriteSolution(bool rewrite)
  546. {
  547. rewriteSolution_ = rewrite;
  548. if (solution_.NotNull())
  549. solution_->SetRewriteSolution(rewrite);
  550. }
  551. bool NETProjectGen::LoadProject(const JSONValue &root)
  552. {
  553. solution_ = new NETSolution(context_, this, rewriteSolution_);
  554. solution_->Load(root["solution"]);
  555. const JSONValue& jprojects = root["projects"];
  556. if (!jprojects.IsArray() || !jprojects.Size())
  557. return false;
  558. for (unsigned i = 0; i < jprojects.Size(); i++)
  559. {
  560. const JSONValue& jproject = jprojects[i];
  561. if (!jproject.IsObject())
  562. return false;
  563. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  564. if (!csProject->Load(jproject))
  565. return false;
  566. projects_.Push(csProject);
  567. }
  568. return true;
  569. }
  570. bool NETProjectGen::LoadProject(const String& projectPath)
  571. {
  572. SharedPtr<File> file(new File(context_));
  573. if (!file->Open(projectPath))
  574. return false;
  575. String json;
  576. file->ReadText(json);
  577. JSONValue jvalue;
  578. if (!JSONFile::ParseJSON(json, jvalue))
  579. return false;
  580. return LoadProject(jvalue);
  581. }
  582. bool NETProjectGen::LoadProject(Project* project)
  583. {
  584. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  585. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  586. atomicProject_ = project;
  587. JSONValue root;
  588. JSONValue solution;
  589. solution["name"] = "AtomicProject";
  590. solution["outputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Solution/";
  591. JSONArray projects;
  592. JSONObject jproject;
  593. jproject["name"] = "AtomicProject";
  594. jproject["outputType"] = "Library";
  595. jproject["assemblyName"] = "AtomicProject";
  596. jproject["assemblyOutputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Bin/";
  597. JSONArray references;
  598. references.Push(JSONValue("System"));
  599. references.Push(JSONValue("System.Core"));
  600. references.Push(JSONValue("System.Xml.Linq"));
  601. references.Push(JSONValue("System.XML"));
  602. String atomicNETAssembly = tenv->GetAtomicNETCoreAssemblyDir() + "AtomicNET.dll";
  603. if (!fileSystem->FileExists(atomicNETAssembly))
  604. {
  605. ATOMIC_LOGERRORF("NETProjectGen::LoadProject - AtomicNET assembly does not exist: %s", atomicNETAssembly.CString());
  606. return false;
  607. }
  608. references.Push(JSONValue(atomicNETAssembly));
  609. jproject["references"] = references;
  610. JSONArray sources;
  611. sources.Push(JSONValue(ToString("%s", project->GetResourcePath().CString())));
  612. jproject["sources"] = sources;
  613. projects.Push(jproject);
  614. root["projects"] = projects;
  615. root["solution"] = solution;
  616. return LoadProject(root);
  617. }
  618. bool NETProjectGen::GetRequiresNuGet()
  619. {
  620. if (solution_.Null())
  621. {
  622. ATOMIC_LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  623. return false;
  624. }
  625. return solution_->GetPackages().Size() != 0;
  626. }
  627. String NETProjectGen::GenerateUUID()
  628. {
  629. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  630. Poco::UUID uuid(generator.create()); // time based
  631. return String(uuid.toString().c_str()).ToUpper();
  632. }
  633. }