NETProjectGen.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  271. NETSolution* solution = projectGen_->GetSolution();
  272. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  273. projectPath_ = solution->GetOutputPath() + name_ + "/";
  274. if (!CreateProjectFolder(projectPath_))
  275. return false;
  276. if (!CreateProjectFolder(projectPath_ + "Properties"))
  277. return false;
  278. XMLElement project = xmlFile_->CreateRoot("Project");
  279. project.SetAttribute("DefaultTargets", "Build");
  280. project.SetAttribute("ToolsVersion", "14.0");
  281. project.SetAttribute("DefaultTargets", "Build");
  282. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  283. XMLElement import = project.CreateChild("Import");
  284. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  285. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  286. CreateMainPropertyGroup(project);
  287. CreateDebugPropertyGroup(project);
  288. CreateReleasePropertyGroup(project);
  289. CreateReferencesItemGroup(project);
  290. CreateProjectReferencesItemGroup(project);
  291. CreateCompileItemGroup(project);
  292. CreatePackagesItemGroup(project);
  293. CreateAssemblyInfo();
  294. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
  295. Project* atomicProject = projectGen_->GetAtomicProject();
  296. if (atomicProject)
  297. {
  298. XMLElement afterBuild = project.CreateChild("Target");
  299. afterBuild.SetAttribute("Name", "AfterBuild");
  300. XMLElement copy = afterBuild.CreateChild("Copy");
  301. copy.SetAttribute("SourceFiles", "$(TargetPath)");
  302. copy.SetAttribute("DestinationFolder", projectPath_ + "../../../Resources/");
  303. // Create the AtomicProject.csproj.user file if it doesn't exist
  304. String userSettingsFilename = projectPath_ + name_ + ".csproj.user";
  305. if (!fileSystem->FileExists(userSettingsFilename))
  306. {
  307. SharedPtr<XMLFile> userSettings(new XMLFile(context_));
  308. XMLElement project = userSettings->CreateRoot("Project");
  309. //XMLElement xml = userRoot.CreateChild("?xml");
  310. //xml.SetAttribute("version", "1.0");
  311. //xml.SetAttribute("encoding", "utf-8");
  312. project.SetAttribute("ToolsVersion", "14.0");
  313. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  314. StringVector configs;
  315. configs.Push("Debug");
  316. configs.Push("Release");
  317. for (unsigned i = 0; i < configs.Size(); i++)
  318. {
  319. String cfg = configs[i];
  320. XMLElement propertyGroup = project.CreateChild("PropertyGroup");
  321. propertyGroup.SetAttribute("Condition", ToString("'$(Configuration)|$(Platform)' == '%s|AnyCPU'", cfg.CString()));
  322. #ifdef ATOMIC_DEV_BUILD
  323. String playerBin = tenv->GetAtomicNETRootDir() + cfg + "/AtomicPlayer.exe";
  324. #else
  325. String playerBin = tenv->GetAtomicNETRootDir() + "Release/AtomicPlayer.exe";
  326. #endif
  327. propertyGroup.CreateChild("StartAction").SetValue("Program");
  328. propertyGroup.CreateChild("StartProgram").SetValue(playerBin );
  329. propertyGroup.CreateChild("StartArguments").SetValue(ToString("--project %s", atomicProject->GetProjectPath().CString()));
  330. }
  331. String userSettingsSource = userSettings->ToString();
  332. SharedPtr<File> output(new File(context_, userSettingsFilename, FILE_WRITE));
  333. output->Write(userSettingsSource.CString(), userSettingsSource.Length());
  334. output->Close();
  335. }
  336. }
  337. String projectSource = xmlFile_->ToString();
  338. SharedPtr<File> output(new File(context_, projectPath_ + name_ + ".csproj", FILE_WRITE));
  339. output->Write(projectSource.CString(), projectSource.Length());
  340. return true;
  341. }
  342. bool NETCSProject::Load(const JSONValue& root)
  343. {
  344. name_ = root["name"].GetString();
  345. projectGuid_ = projectGen_->GenerateUUID();
  346. outputType_ = root["outputType"].GetString();
  347. rootNamespace_ = root["rootNamespace"].GetString();
  348. assemblyName_ = root["assemblyName"].GetString();
  349. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  350. ReplacePathStrings(assemblyOutputPath_);
  351. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  352. ReplacePathStrings(assemblySearchPaths_);
  353. const JSONArray& references = root["references"].GetArray();
  354. for (unsigned i = 0; i < references.Size(); i++)
  355. {
  356. String reference = references[i].GetString();
  357. ReplacePathStrings(reference);
  358. references_.Push(reference);
  359. }
  360. const JSONArray& packages = root["packages"].GetArray();
  361. for (unsigned i = 0; i < packages.Size(); i++)
  362. {
  363. String package = packages[i].GetString();
  364. if (packages_.Find(package) != packages_.End())
  365. {
  366. LOGERRORF("Duplicate package found %s", package.CString());
  367. continue;
  368. }
  369. projectGen_->GetSolution()->RegisterPackage(package);
  370. packages_.Push(package);
  371. }
  372. const JSONArray& sources = root["sources"].GetArray();
  373. for (unsigned i = 0; i < sources.Size(); i++)
  374. {
  375. String source = sources[i].GetString();
  376. ReplacePathStrings(source);
  377. sourceFolders_.Push(AddTrailingSlash(source));
  378. }
  379. return true;
  380. }
  381. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  382. {
  383. }
  384. NETSolution::~NETSolution()
  385. {
  386. }
  387. bool NETSolution::Generate()
  388. {
  389. String slnPath = outputPath_ + name_ + ".sln";
  390. GenerateSolution(slnPath);
  391. return true;
  392. }
  393. void NETSolution::GenerateSolution(const String &slnPath)
  394. {
  395. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  396. source += "# Visual Studio 14\n";
  397. source += "VisualStudioVersion = 14.0.25420.1\n";
  398. source += "MinimumVisualStudioVersion = 10.0.40219.1\n";
  399. solutionGUID_ = projectGen_->GenerateUUID();
  400. PODVector<NETCSProject*> depends;
  401. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  402. for (unsigned i = 0; i < projects.Size(); i++)
  403. {
  404. NETCSProject* p = projects.At(i);
  405. const String& projectName = p->GetName();
  406. const String& projectGUID = p->GetProjectGUID();
  407. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.csproj\", \"{%s}\"\n",
  408. solutionGUID_.CString(), projectName.CString(), projectName.CString(),
  409. projectName.CString(), projectGUID.CString());
  410. projectGen_->GetCSProjectDependencies(p, depends);
  411. if (depends.Size())
  412. {
  413. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  414. for (unsigned j = 0; j < depends.Size(); j++)
  415. {
  416. source += ToString("\t{%s} = {%s}\n",
  417. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  418. }
  419. source += "\tEndProjectSection\n";
  420. }
  421. source += "\tEndProject\n";
  422. }
  423. source += "Global\n";
  424. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  425. source += " Debug|Any CPU = Debug|Any CPU\n";
  426. source += " Release|Any CPU = Release|Any CPU\n";
  427. source += " EndGlobalSection\n";
  428. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  429. for (unsigned i = 0; i < projects.Size(); i++)
  430. {
  431. NETCSProject* p = projects.At(i);
  432. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  433. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  434. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  435. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  436. }
  437. source += " EndGlobalSection\n";
  438. source += "EndGlobal\n";
  439. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  440. output->Write(source.CString(), source.Length());
  441. output->Close();
  442. }
  443. bool NETSolution::Load(const JSONValue& root)
  444. {
  445. FileSystem* fs = GetSubsystem<FileSystem>();
  446. name_ = root["name"].GetString();
  447. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  448. ReplacePathStrings(outputPath_);
  449. // TODO: use poco mkdirs
  450. if (!fs->DirExists(outputPath_))
  451. fs->CreateDirsRecursive(outputPath_);
  452. return true;
  453. }
  454. bool NETSolution::RegisterPackage(const String& package)
  455. {
  456. if (packages_.Find(package) != packages_.End())
  457. return false;
  458. packages_.Push(package);
  459. return true;
  460. }
  461. NETProjectGen::NETProjectGen(Context* context) : Object(context)
  462. {
  463. }
  464. NETProjectGen::~NETProjectGen()
  465. {
  466. }
  467. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  468. {
  469. for (unsigned i = 0; i < projects_.Size(); i++)
  470. {
  471. if (projects_[i]->GetName() == name)
  472. return projects_[i];
  473. }
  474. return nullptr;
  475. }
  476. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  477. {
  478. depends.Clear();
  479. const Vector<String>& references = source->GetReferences();
  480. for (unsigned i = 0; i < projects_.Size(); i++)
  481. {
  482. NETCSProject* pdepend = projects_.At(i);
  483. if (source == pdepend)
  484. continue;
  485. for (unsigned j = 0; j < references.Size(); j++)
  486. {
  487. if (pdepend->GetName() == references[j])
  488. {
  489. depends.Push(pdepend);
  490. }
  491. }
  492. }
  493. return depends.Size() != 0;
  494. }
  495. bool NETProjectGen::Generate()
  496. {
  497. solution_->Generate();
  498. for (unsigned i = 0; i < projects_.Size(); i++)
  499. {
  500. if (!projects_[i]->Generate())
  501. return false;
  502. }
  503. return true;
  504. }
  505. bool NETProjectGen::LoadProject(const JSONValue &root)
  506. {
  507. solution_ = new NETSolution(context_, this);
  508. solution_->Load(root["solution"]);
  509. const JSONValue& jprojects = root["projects"];
  510. if (!jprojects.IsArray() || !jprojects.Size())
  511. return false;
  512. for (unsigned i = 0; i < jprojects.Size(); i++)
  513. {
  514. const JSONValue& jproject = jprojects[i];
  515. if (!jproject.IsObject())
  516. return false;
  517. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  518. if (!csProject->Load(jproject))
  519. return false;
  520. projects_.Push(csProject);
  521. }
  522. return true;
  523. }
  524. bool NETProjectGen::LoadProject(const String& projectPath)
  525. {
  526. SharedPtr<File> file(new File(context_));
  527. if (!file->Open(projectPath))
  528. return false;
  529. String json;
  530. file->ReadText(json);
  531. JSONValue jvalue;
  532. if (!JSONFile::ParseJSON(json, jvalue))
  533. return false;
  534. return LoadProject(jvalue);
  535. }
  536. bool NETProjectGen::LoadProject(Project* project)
  537. {
  538. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  539. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  540. atomicProject_ = project;
  541. JSONValue root;
  542. JSONValue solution;
  543. solution["name"] = "AtomicProject";
  544. solution["outputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Solution/";
  545. JSONArray projects;
  546. JSONObject jproject;
  547. jproject["name"] = "AtomicProject";
  548. jproject["outputType"] = "Library";
  549. jproject["assemblyName"] = "AtomicProject";
  550. jproject["assemblyOutputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Bin/";
  551. JSONArray references;
  552. references.Push(JSONValue("System"));
  553. references.Push(JSONValue("System.Core"));
  554. references.Push(JSONValue("System.Xml.Linq"));
  555. references.Push(JSONValue("System.XML"));
  556. String atomicNETAssembly = tenv->GetAtomicNETCoreAssemblyDir() + "AtomicNET.dll";
  557. if (!fileSystem->FileExists(atomicNETAssembly))
  558. {
  559. LOGERRORF("NETProjectGen::LoadProject - AtomicNET assembly does not exist: %s", atomicNETAssembly.CString());
  560. return false;
  561. }
  562. references.Push(JSONValue(atomicNETAssembly));
  563. jproject["references"] = references;
  564. JSONArray sources;
  565. sources.Push(JSONValue(ToString("%s", project->GetResourcePath().CString())));
  566. jproject["sources"] = sources;
  567. projects.Push(jproject);
  568. root["projects"] = projects;
  569. root["solution"] = solution;
  570. return LoadProject(root);
  571. }
  572. bool NETProjectGen::GetRequiresNuGet()
  573. {
  574. if (solution_.Null())
  575. {
  576. LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  577. return false;
  578. }
  579. return solution_->GetPackages().Size() != 0;
  580. }
  581. String NETProjectGen::GenerateUUID()
  582. {
  583. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  584. Poco::UUID uuid(generator.create()); // time based
  585. return String(uuid.toString().c_str()).ToUpper();
  586. }
  587. }