NETProjectGen.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. 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. // IMPORTANT: / Slash direction breaks intellisense :/
  83. String path = sourceFolder + result[j];
  84. path.Replace('/', '\\');
  85. compile.SetAttribute("Include", path.CString());
  86. // put generated files into generated folder
  87. if (sourceFolder.Contains("Generated") && sourceFolder.Contains("CSharp") && sourceFolder.Contains("Packages"))
  88. {
  89. compile.CreateChild("Link").SetValue("Generated\\" + result[j]);
  90. }
  91. else
  92. {
  93. compile.CreateChild("Link").SetValue(result[j]);
  94. }
  95. }
  96. }
  97. }
  98. void NETProjectBase::CopyXMLElementRecursive(XMLElement source, XMLElement dest)
  99. {
  100. Vector<String> attrNames = source.GetAttributeNames();
  101. for (unsigned i = 0; i < attrNames.Size(); i++)
  102. {
  103. String value = source.GetAttribute(attrNames[i]);
  104. dest.SetAttribute(attrNames[i], value);
  105. }
  106. dest.SetValue(source.GetValue());
  107. XMLElement child = source.GetChild();
  108. while (child.NotNull() && child.GetName().Length())
  109. {
  110. XMLElement childDest = dest.CreateChild(child.GetName());
  111. CopyXMLElementRecursive(child, childDest);
  112. child = child.GetNext();
  113. }
  114. }
  115. void NETCSProject::CreateReferencesItemGroup(XMLElement &projectRoot)
  116. {
  117. XMLElement xref;
  118. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  119. for (unsigned i = 0; i < references_.Size(); i++)
  120. {
  121. String ref = references_[i];
  122. // project reference
  123. if (projectGen_->GetCSProjectByName(ref))
  124. continue;
  125. // NuGet project
  126. if (ref.StartsWith("<"))
  127. {
  128. XMLFile xmlFile(context_);
  129. if (!xmlFile.FromString(ref))
  130. {
  131. LOGERROR("NETCSProject::CreateReferencesItemGroup - Unable to parse reference XML");
  132. }
  133. xref = igroup.CreateChild("Reference");
  134. CopyXMLElementRecursive(xmlFile.GetRoot(), xref);
  135. continue;
  136. }
  137. String refpath = ref + ".dll";
  138. xref = igroup.CreateChild("Reference");
  139. xref.SetAttribute("Include", ref);
  140. }
  141. }
  142. void NETCSProject::CreateProjectReferencesItemGroup(XMLElement &projectRoot)
  143. {
  144. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  145. for (unsigned i = 0; i < references_.Size(); i++)
  146. {
  147. const String& ref = references_[i];
  148. NETCSProject* project = projectGen_->GetCSProjectByName(ref);
  149. if (!project)
  150. continue;
  151. XMLElement projectRef = igroup.CreateChild("ProjectReference");
  152. projectRef.SetAttribute("Include", ToString("..\\%s\\%s.csproj", ref.CString(), ref.CString()));
  153. XMLElement xproject = projectRef.CreateChild("Project");
  154. xproject.SetValue(ToString("{%s}", project->GetProjectGUID().ToLower().CString()));
  155. XMLElement xname = projectRef.CreateChild("Name");
  156. xname.SetValue(project->GetName());
  157. }
  158. }
  159. void NETCSProject::CreatePackagesItemGroup(XMLElement &projectRoot)
  160. {
  161. if (!packages_.Size())
  162. return;
  163. XMLElement xref;
  164. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  165. xref = igroup.CreateChild("None");
  166. xref.SetAttribute("Include", "packages.config");
  167. XMLFile packageConfig(context_);
  168. XMLElement packageRoot = packageConfig.CreateRoot("packages");
  169. for (unsigned i = 0; i < packages_.Size(); i++)
  170. {
  171. XMLFile xmlFile(context_);
  172. if (!xmlFile.FromString(packages_[i]))
  173. {
  174. LOGERROR("NETCSProject::CreatePackagesItemGroup - Unable to parse package xml");
  175. }
  176. xref = packageRoot.CreateChild("package");
  177. CopyXMLElementRecursive(xmlFile.GetRoot(), xref);
  178. }
  179. SharedPtr<File> output(new File(context_, projectPath_ + "packages.config", FILE_WRITE));
  180. String source = packageConfig.ToString();
  181. output->Write(source.CString(), source.Length());
  182. }
  183. void NETCSProject::GetAssemblySearchPaths(String& paths)
  184. {
  185. paths.Clear();
  186. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  187. Vector<String> searchPaths;
  188. if (assemblySearchPaths_.Length())
  189. searchPaths.Push(assemblySearchPaths_);
  190. paths.Join(searchPaths, ";");
  191. }
  192. void NETCSProject::CreateReleasePropertyGroup(XMLElement &projectRoot)
  193. {
  194. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  195. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
  196. pgroup.CreateChild("DebugType").SetValue("full");
  197. pgroup.CreateChild("Optimize").SetValue("true");
  198. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_ + "Release\\");
  199. pgroup.CreateChild("DefineConstants").SetValue("TRACE");
  200. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  201. pgroup.CreateChild("WarningLevel").SetValue("4");
  202. pgroup.CreateChild("ConsolePause").SetValue("false");
  203. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  204. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  205. }
  206. void NETCSProject::CreateDebugPropertyGroup(XMLElement &projectRoot)
  207. {
  208. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  209. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
  210. pgroup.CreateChild("DebugSymbols").SetValue("true");
  211. pgroup.CreateChild("DebugType").SetValue("full");
  212. pgroup.CreateChild("Optimize").SetValue("false");
  213. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_ + "Debug\\");
  214. pgroup.CreateChild("DefineConstants").SetValue("DEBUG;TRACE");
  215. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  216. pgroup.CreateChild("WarningLevel").SetValue("4");
  217. pgroup.CreateChild("ConsolePause").SetValue("false");
  218. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  219. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  220. }
  221. void NETCSProject::CreateAssemblyInfo()
  222. {
  223. String info = "using System.Reflection;\nusing System.Runtime.CompilerServices;\nusing System.Runtime.InteropServices;\n\n\n";
  224. info += ToString("[assembly:AssemblyTitle(\"%s\")]\n", name_.CString());
  225. info += "[assembly:AssemblyDescription(\"\")]\n";
  226. info += "[assembly:AssemblyConfiguration(\"\")]\n";
  227. info += "[assembly:AssemblyCompany(\"\")]\n";
  228. info += ToString("[assembly:AssemblyProduct(\"%s\")]\n", name_.CString());
  229. info += "\n\n\n";
  230. info += "[assembly:ComVisible(false)]\n";
  231. info += "\n\n";
  232. info += ToString("[assembly:Guid(\"%s\")]\n", projectGuid_.CString());
  233. info += "\n\n";
  234. info += "[assembly:AssemblyVersion(\"1.0.0.0\")]\n";
  235. info += "[assembly:AssemblyFileVersion(\"1.0.0.0\")]\n";
  236. SharedPtr<File> output(new File(context_, projectPath_ + "Properties/AssemblyInfo.cs", FILE_WRITE));
  237. output->Write(info.CString(), info.Length());
  238. }
  239. void NETCSProject::CreateMainPropertyGroup(XMLElement& projectRoot)
  240. {
  241. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  242. // Configuration
  243. XMLElement config = pgroup.CreateChild("Configuration");
  244. config.SetAttribute("Condition", " '$(Configuration)' == '' ");
  245. config.SetValue("Debug");
  246. // Platform
  247. XMLElement platform = pgroup.CreateChild("Platform");
  248. platform.SetAttribute("Condition", " '$(Platform)' == '' ");
  249. platform.SetValue("AnyCPU");
  250. // ProjectGuid
  251. XMLElement guid = pgroup.CreateChild("ProjectGuid");
  252. guid.SetValue("{" + projectGuid_ + "}");
  253. // OutputType
  254. XMLElement outputType = pgroup.CreateChild("OutputType");
  255. outputType.SetValue(outputType_);
  256. pgroup.CreateChild("AppDesignerFolder").SetValue("Properties");
  257. // RootNamespace
  258. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  259. rootNamespace.SetValue(rootNamespace_);
  260. // AssemblyName
  261. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  262. assemblyName.SetValue(assemblyName_);
  263. // TargetFrameworkVersion
  264. XMLElement targetFrameWork = pgroup.CreateChild("TargetFrameworkVersion");
  265. targetFrameWork.SetValue("v4.6");
  266. pgroup.CreateChild("FileAlignment").SetValue("512");
  267. }
  268. bool NETCSProject::Generate()
  269. {
  270. NETSolution* solution = projectGen_->GetSolution();
  271. projectPath_ = solution->GetOutputPath() + name_ + "/";
  272. if (!CreateProjectFolder(projectPath_))
  273. return false;
  274. if (!CreateProjectFolder(projectPath_ + "Properties"))
  275. return false;
  276. XMLElement project = xmlFile_->CreateRoot("Project");
  277. project.SetAttribute("DefaultTargets", "Build");
  278. project.SetAttribute("ToolsVersion", "14.0");
  279. project.SetAttribute("DefaultTargets", "Build");
  280. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  281. XMLElement import = project.CreateChild("Import");
  282. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  283. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  284. CreateMainPropertyGroup(project);
  285. CreateDebugPropertyGroup(project);
  286. CreateReleasePropertyGroup(project);
  287. CreateReferencesItemGroup(project);
  288. CreateProjectReferencesItemGroup(project);
  289. CreateCompileItemGroup(project);
  290. CreatePackagesItemGroup(project);
  291. CreateAssemblyInfo();
  292. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
  293. if (name_ == "AtomicProject")
  294. {
  295. XMLElement afterBuild = project.CreateChild("Target");
  296. afterBuild.SetAttribute("Name", "AfterBuild");
  297. XMLElement copy = afterBuild.CreateChild("Copy");
  298. copy.SetAttribute("SourceFiles", "$(TargetPath)");
  299. copy.SetAttribute("DestinationFolder", projectPath_ + "../../../Resources/");
  300. }
  301. String projectSource = xmlFile_->ToString();
  302. SharedPtr<File> output(new File(context_, projectPath_ + name_ + ".csproj", FILE_WRITE));
  303. output->Write(projectSource.CString(), projectSource.Length());
  304. return true;
  305. }
  306. bool NETCSProject::Load(const JSONValue& root)
  307. {
  308. name_ = root["name"].GetString();
  309. projectGuid_ = projectGen_->GenerateUUID();
  310. outputType_ = root["outputType"].GetString();
  311. rootNamespace_ = root["rootNamespace"].GetString();
  312. assemblyName_ = root["assemblyName"].GetString();
  313. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  314. ReplacePathStrings(assemblyOutputPath_);
  315. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  316. ReplacePathStrings(assemblySearchPaths_);
  317. const JSONArray& references = root["references"].GetArray();
  318. for (unsigned i = 0; i < references.Size(); i++)
  319. {
  320. String reference = references[i].GetString();
  321. ReplacePathStrings(reference);
  322. references_.Push(reference);
  323. }
  324. const JSONArray& packages = root["packages"].GetArray();
  325. for (unsigned i = 0; i < packages.Size(); i++)
  326. {
  327. String package = packages[i].GetString();
  328. if (packages_.Find(package) != packages_.End())
  329. {
  330. LOGERRORF("Duplicate package found %s", package.CString());
  331. continue;
  332. }
  333. projectGen_->GetSolution()->RegisterPackage(package);
  334. packages_.Push(package);
  335. }
  336. const JSONArray& sources = root["sources"].GetArray();
  337. for (unsigned i = 0; i < sources.Size(); i++)
  338. {
  339. String source = sources[i].GetString();
  340. ReplacePathStrings(source);
  341. sourceFolders_.Push(AddTrailingSlash(source));
  342. }
  343. return true;
  344. }
  345. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  346. {
  347. }
  348. NETSolution::~NETSolution()
  349. {
  350. }
  351. bool NETSolution::Generate()
  352. {
  353. String slnPath = outputPath_ + name_ + ".sln";
  354. GenerateSolution(slnPath);
  355. return true;
  356. }
  357. void NETSolution::GenerateSolution(const String &slnPath)
  358. {
  359. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  360. source += "# Visual Studio 14\n";
  361. source += "VisualStudioVersion = 14.0.25420.1\n";
  362. source += "MinimumVisualStudioVersion = 10.0.40219.1\n";
  363. solutionGUID_ = projectGen_->GenerateUUID();
  364. PODVector<NETCSProject*> depends;
  365. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  366. for (unsigned i = 0; i < projects.Size(); i++)
  367. {
  368. NETCSProject* p = projects.At(i);
  369. const String& projectName = p->GetName();
  370. const String& projectGUID = p->GetProjectGUID();
  371. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.csproj\", \"{%s}\"\n",
  372. solutionGUID_.CString(), projectName.CString(), projectName.CString(),
  373. projectName.CString(), projectGUID.CString());
  374. projectGen_->GetCSProjectDependencies(p, depends);
  375. if (depends.Size())
  376. {
  377. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  378. for (unsigned j = 0; j < depends.Size(); j++)
  379. {
  380. source += ToString("\t{%s} = {%s}\n",
  381. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  382. }
  383. source += "\tEndProjectSection\n";
  384. }
  385. source += "\tEndProject\n";
  386. }
  387. source += "Global\n";
  388. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  389. source += " Debug|Any CPU = Debug|Any CPU\n";
  390. source += " Release|Any CPU = Release|Any CPU\n";
  391. source += " EndGlobalSection\n";
  392. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  393. for (unsigned i = 0; i < projects.Size(); i++)
  394. {
  395. NETCSProject* p = projects.At(i);
  396. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  397. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  398. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  399. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  400. }
  401. source += " EndGlobalSection\n";
  402. source += "EndGlobal\n";
  403. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  404. output->Write(source.CString(), source.Length());
  405. output->Close();
  406. }
  407. bool NETSolution::Load(const JSONValue& root)
  408. {
  409. FileSystem* fs = GetSubsystem<FileSystem>();
  410. name_ = root["name"].GetString();
  411. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  412. ReplacePathStrings(outputPath_);
  413. // TODO: use poco mkdirs
  414. if (!fs->DirExists(outputPath_))
  415. fs->CreateDirsRecursive(outputPath_);
  416. return true;
  417. }
  418. bool NETSolution::RegisterPackage(const String& package)
  419. {
  420. if (packages_.Find(package) != packages_.End())
  421. return false;
  422. packages_.Push(package);
  423. return true;
  424. }
  425. NETProjectGen::NETProjectGen(Context* context) : Object(context)
  426. {
  427. }
  428. NETProjectGen::~NETProjectGen()
  429. {
  430. }
  431. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  432. {
  433. for (unsigned i = 0; i < projects_.Size(); i++)
  434. {
  435. if (projects_[i]->GetName() == name)
  436. return projects_[i];
  437. }
  438. return nullptr;
  439. }
  440. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  441. {
  442. depends.Clear();
  443. const Vector<String>& references = source->GetReferences();
  444. for (unsigned i = 0; i < projects_.Size(); i++)
  445. {
  446. NETCSProject* pdepend = projects_.At(i);
  447. if (source == pdepend)
  448. continue;
  449. for (unsigned j = 0; j < references.Size(); j++)
  450. {
  451. if (pdepend->GetName() == references[j])
  452. {
  453. depends.Push(pdepend);
  454. }
  455. }
  456. }
  457. return depends.Size() != 0;
  458. }
  459. bool NETProjectGen::Generate()
  460. {
  461. solution_->Generate();
  462. for (unsigned i = 0; i < projects_.Size(); i++)
  463. {
  464. if (!projects_[i]->Generate())
  465. return false;
  466. }
  467. return true;
  468. }
  469. bool NETProjectGen::LoadProject(const JSONValue &root)
  470. {
  471. solution_ = new NETSolution(context_, this);
  472. solution_->Load(root["solution"]);
  473. const JSONValue& jprojects = root["projects"];
  474. if (!jprojects.IsArray() || !jprojects.Size())
  475. return false;
  476. for (unsigned i = 0; i < jprojects.Size(); i++)
  477. {
  478. const JSONValue& jproject = jprojects[i];
  479. if (!jproject.IsObject())
  480. return false;
  481. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  482. if (!csProject->Load(jproject))
  483. return false;
  484. projects_.Push(csProject);
  485. }
  486. return true;
  487. }
  488. bool NETProjectGen::LoadProject(const String& projectPath)
  489. {
  490. SharedPtr<File> file(new File(context_));
  491. if (!file->Open(projectPath))
  492. return false;
  493. String json;
  494. file->ReadText(json);
  495. JSONValue jvalue;
  496. if (!JSONFile::ParseJSON(json, jvalue))
  497. return false;
  498. return LoadProject(jvalue);
  499. }
  500. bool NETProjectGen::LoadProject(Project* project)
  501. {
  502. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  503. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  504. JSONValue root;
  505. JSONValue solution;
  506. solution["name"] = "AtomicProject";
  507. solution["outputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Solution/";
  508. JSONArray projects;
  509. JSONObject jproject;
  510. jproject["name"] = "AtomicProject";
  511. jproject["outputType"] = "Library";
  512. jproject["assemblyName"] = "AtomicProject";
  513. jproject["assemblyOutputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Bin/";
  514. JSONArray references;
  515. references.Push(JSONValue("System"));
  516. references.Push(JSONValue("System.Core"));
  517. references.Push(JSONValue("System.Xml.Linq"));
  518. references.Push(JSONValue("System.XML"));
  519. String atomicNETAssembly = tenv->GetAtomicNETCoreAssemblyDir() + "AtomicNET.dll";
  520. if (!fileSystem->FileExists(atomicNETAssembly))
  521. {
  522. LOGERRORF("NETProjectGen::LoadProject - AtomicNET assembly does not exist: %s", atomicNETAssembly.CString());
  523. return false;
  524. }
  525. references.Push(JSONValue(atomicNETAssembly));
  526. jproject["references"] = references;
  527. JSONArray sources;
  528. sources.Push(JSONValue(ToString("%s", project->GetResourcePath().CString())));
  529. jproject["sources"] = sources;
  530. projects.Push(jproject);
  531. root["projects"] = projects;
  532. root["solution"] = solution;
  533. return LoadProject(root);
  534. }
  535. bool NETProjectGen::GetRequiresNuGet()
  536. {
  537. if (solution_.Null())
  538. {
  539. LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  540. return false;
  541. }
  542. return solution_->GetPackages().Size() != 0;
  543. }
  544. String NETProjectGen::GenerateUUID()
  545. {
  546. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  547. Poco::UUID uuid(generator.create()); // time based
  548. return String(uuid.toString().c_str()).ToUpper();
  549. }
  550. }