NETProjectGen.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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::CreateCustomCommands(XMLElement &propertyGroup, const String& cfg)
  216. {
  217. Project* atomicProject = projectGen_->GetAtomicProject();
  218. if (!atomicProject)
  219. return;
  220. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  221. XMLElement customCommands = propertyGroup.CreateChild("CustomCommands").CreateChild("CustomCommands");
  222. XMLElement xcommand = customCommands.CreateChild("Command");
  223. xcommand.SetAttribute("type", "Execute");
  224. String startArguments;
  225. #ifdef ATOMIC_DEV_BUILD
  226. String playerBin = tenv->GetAtomicNETRootDir() + cfg + "/AtomicIPCPlayer.exe";
  227. #else
  228. String playerBin = tenv->GetAtomicNETRootDir() + "Release/AtomicIPCPlayer.exe";
  229. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  230. #endif
  231. startArguments += ToString("--project \"%s\"", atomicProject->GetProjectPath().CString());
  232. String command = ToString("\"%s\"", playerBin.CString()) + " " + startArguments;
  233. xcommand.SetAttribute("command", command);
  234. }
  235. void NETCSProject::CreateReleasePropertyGroup(XMLElement &projectRoot)
  236. {
  237. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  238. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
  239. pgroup.CreateChild("DebugType").SetValue("full");
  240. pgroup.CreateChild("Optimize").SetValue("true");
  241. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_ + "Release\\");
  242. pgroup.CreateChild("DefineConstants").SetValue("TRACE");
  243. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  244. pgroup.CreateChild("WarningLevel").SetValue("4");
  245. pgroup.CreateChild("ConsolePause").SetValue("false");
  246. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  247. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  248. #ifndef ATOMIC_PLATFORM_WINDOWS
  249. CreateCustomCommands(pgroup, "Debug");
  250. #endif
  251. }
  252. void NETCSProject::CreateDebugPropertyGroup(XMLElement &projectRoot)
  253. {
  254. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  255. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
  256. pgroup.CreateChild("DebugSymbols").SetValue("true");
  257. pgroup.CreateChild("DebugType").SetValue("full");
  258. pgroup.CreateChild("Optimize").SetValue("false");
  259. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_ + "Debug\\");
  260. pgroup.CreateChild("DefineConstants").SetValue("DEBUG;TRACE");
  261. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  262. pgroup.CreateChild("WarningLevel").SetValue("4");
  263. pgroup.CreateChild("ConsolePause").SetValue("false");
  264. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  265. pgroup.CreateChild("PlatformTarget").SetValue("x64");
  266. #ifndef ATOMIC_PLATFORM_WINDOWS
  267. CreateCustomCommands(pgroup, "Release");
  268. #endif
  269. }
  270. void NETCSProject::CreateAssemblyInfo()
  271. {
  272. String info = "using System.Reflection;\nusing System.Runtime.CompilerServices;\nusing System.Runtime.InteropServices;\n\n\n";
  273. info += ToString("[assembly:AssemblyTitle(\"%s\")]\n", name_.CString());
  274. info += "[assembly:AssemblyDescription(\"\")]\n";
  275. info += "[assembly:AssemblyConfiguration(\"\")]\n";
  276. info += "[assembly:AssemblyCompany(\"\")]\n";
  277. info += ToString("[assembly:AssemblyProduct(\"%s\")]\n", name_.CString());
  278. info += "\n\n\n";
  279. info += "[assembly:ComVisible(false)]\n";
  280. info += "\n\n";
  281. info += ToString("[assembly:Guid(\"%s\")]\n", projectGuid_.CString());
  282. info += "\n\n";
  283. info += "[assembly:AssemblyVersion(\"1.0.0.0\")]\n";
  284. info += "[assembly:AssemblyFileVersion(\"1.0.0.0\")]\n";
  285. SharedPtr<File> output(new File(context_, projectPath_ + "Properties/AssemblyInfo.cs", FILE_WRITE));
  286. output->Write(info.CString(), info.Length());
  287. }
  288. void NETCSProject::CreateMainPropertyGroup(XMLElement& projectRoot)
  289. {
  290. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  291. // Configuration
  292. XMLElement config = pgroup.CreateChild("Configuration");
  293. config.SetAttribute("Condition", " '$(Configuration)' == '' ");
  294. config.SetValue("Debug");
  295. // Platform
  296. XMLElement platform = pgroup.CreateChild("Platform");
  297. platform.SetAttribute("Condition", " '$(Platform)' == '' ");
  298. platform.SetValue("AnyCPU");
  299. // ProjectGuid
  300. XMLElement guid = pgroup.CreateChild("ProjectGuid");
  301. guid.SetValue("{" + projectGuid_ + "}");
  302. // OutputType
  303. XMLElement outputType = pgroup.CreateChild("OutputType");
  304. outputType.SetValue(outputType_);
  305. pgroup.CreateChild("AppDesignerFolder").SetValue("Properties");
  306. // RootNamespace
  307. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  308. rootNamespace.SetValue(rootNamespace_);
  309. // AssemblyName
  310. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  311. assemblyName.SetValue(assemblyName_);
  312. // TargetFrameworkVersion
  313. XMLElement targetFrameWork = pgroup.CreateChild("TargetFrameworkVersion");
  314. targetFrameWork.SetValue("v4.6");
  315. pgroup.CreateChild("FileAlignment").SetValue("512");
  316. }
  317. bool NETCSProject::Generate()
  318. {
  319. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  320. NETSolution* solution = projectGen_->GetSolution();
  321. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  322. projectPath_ = solution->GetOutputPath() + name_ + "/";
  323. if (!CreateProjectFolder(projectPath_))
  324. return false;
  325. if (!CreateProjectFolder(projectPath_ + "Properties"))
  326. return false;
  327. XMLElement project = xmlFile_->CreateRoot("Project");
  328. project.SetAttribute("DefaultTargets", "Build");
  329. project.SetAttribute("ToolsVersion", "14.0");
  330. project.SetAttribute("DefaultTargets", "Build");
  331. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  332. XMLElement import = project.CreateChild("Import");
  333. import.SetAttribute("Project", "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props");
  334. import.SetAttribute("Condition", "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')");
  335. CreateMainPropertyGroup(project);
  336. CreateDebugPropertyGroup(project);
  337. CreateReleasePropertyGroup(project);
  338. CreateReferencesItemGroup(project);
  339. CreateProjectReferencesItemGroup(project);
  340. CreateCompileItemGroup(project);
  341. CreatePackagesItemGroup(project);
  342. CreateAssemblyInfo();
  343. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
  344. Project* atomicProject = projectGen_->GetAtomicProject();
  345. if (atomicProject)
  346. {
  347. XMLElement afterBuild = project.CreateChild("Target");
  348. afterBuild.SetAttribute("Name", "AfterBuild");
  349. XMLElement copy = afterBuild.CreateChild("Copy");
  350. copy.SetAttribute("SourceFiles", "$(TargetPath)");
  351. String destPath = projectPath_ + "../../../Resources/";
  352. String relativePath;
  353. if (GetRelativePath(projectPath_, atomicProject->GetResourcePath(), relativePath))
  354. {
  355. destPath = AddTrailingSlash(relativePath);
  356. }
  357. copy.SetAttribute("DestinationFolder", destPath);
  358. #ifndef ATOMIC_PLATFORM_WINDOWS
  359. copy = afterBuild.CreateChild("Copy");
  360. copy.SetAttribute("SourceFiles", "$(TargetPath).mdb");
  361. copy.SetAttribute("DestinationFolder", destPath);
  362. #endif
  363. // Create the AtomicProject.csproj.user file if it doesn't exist
  364. String userSettingsFilename = projectPath_ + name_ + ".csproj.user";
  365. if (!fileSystem->FileExists(userSettingsFilename))
  366. {
  367. SharedPtr<XMLFile> userSettings(new XMLFile(context_));
  368. XMLElement project = userSettings->CreateRoot("Project");
  369. //XMLElement xml = userRoot.CreateChild("?xml");
  370. //xml.SetAttribute("version", "1.0");
  371. //xml.SetAttribute("encoding", "utf-8");
  372. project.SetAttribute("ToolsVersion", "14.0");
  373. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  374. StringVector configs;
  375. configs.Push("Debug");
  376. configs.Push("Release");
  377. for (unsigned i = 0; i < configs.Size(); i++)
  378. {
  379. String cfg = configs[i];
  380. XMLElement propertyGroup = project.CreateChild("PropertyGroup");
  381. propertyGroup.SetAttribute("Condition", ToString("'$(Configuration)|$(Platform)' == '%s|AnyCPU'", cfg.CString()));
  382. String startArguments;
  383. #ifdef ATOMIC_DEV_BUILD
  384. String playerBin = tenv->GetAtomicNETRootDir() + cfg + "/AtomicIPCPlayer.exe";
  385. #else
  386. String playerBin = tenv->GetAtomicNETRootDir() + "Release/AtomicIPCPlayer.exe";
  387. startArguments += ToString("--resourcePrefix \"%s\" ", (fileSystem->GetProgramDir() + "Resources/").CString());
  388. #endif
  389. propertyGroup.CreateChild("StartAction").SetValue("Program");
  390. propertyGroup.CreateChild("StartProgram").SetValue(playerBin );
  391. startArguments += ToString("--project \"%s\"", atomicProject->GetProjectPath().CString());
  392. propertyGroup.CreateChild("StartArguments").SetValue(startArguments);
  393. }
  394. String userSettingsSource = userSettings->ToString();
  395. SharedPtr<File> output(new File(context_, userSettingsFilename, FILE_WRITE));
  396. output->Write(userSettingsSource.CString(), userSettingsSource.Length());
  397. output->Close();
  398. }
  399. }
  400. String projectSource = xmlFile_->ToString();
  401. SharedPtr<File> output(new File(context_, projectPath_ + name_ + ".csproj", FILE_WRITE));
  402. output->Write(projectSource.CString(), projectSource.Length());
  403. return true;
  404. }
  405. bool NETCSProject::Load(const JSONValue& root)
  406. {
  407. name_ = root["name"].GetString();
  408. projectGuid_ = projectGen_->GenerateUUID();
  409. outputType_ = root["outputType"].GetString();
  410. rootNamespace_ = root["rootNamespace"].GetString();
  411. assemblyName_ = root["assemblyName"].GetString();
  412. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  413. ReplacePathStrings(assemblyOutputPath_);
  414. assemblySearchPaths_ = root["assemblySearchPaths"].GetString();
  415. ReplacePathStrings(assemblySearchPaths_);
  416. const JSONArray& references = root["references"].GetArray();
  417. for (unsigned i = 0; i < references.Size(); i++)
  418. {
  419. String reference = references[i].GetString();
  420. ReplacePathStrings(reference);
  421. references_.Push(reference);
  422. }
  423. const JSONArray& packages = root["packages"].GetArray();
  424. for (unsigned i = 0; i < packages.Size(); i++)
  425. {
  426. String package = packages[i].GetString();
  427. if (packages_.Find(package) != packages_.End())
  428. {
  429. ATOMIC_LOGERRORF("Duplicate package found %s", package.CString());
  430. continue;
  431. }
  432. projectGen_->GetSolution()->RegisterPackage(package);
  433. packages_.Push(package);
  434. }
  435. const JSONArray& sources = root["sources"].GetArray();
  436. for (unsigned i = 0; i < sources.Size(); i++)
  437. {
  438. String source = sources[i].GetString();
  439. ReplacePathStrings(source);
  440. sourceFolders_.Push(AddTrailingSlash(source));
  441. }
  442. return true;
  443. }
  444. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen, bool rewrite) : NETProjectBase(context, projectGen),
  445. rewriteSolution_(rewrite)
  446. {
  447. }
  448. NETSolution::~NETSolution()
  449. {
  450. }
  451. bool NETSolution::Generate()
  452. {
  453. String slnPath = outputPath_ + name_ + ".sln";
  454. GenerateSolution(slnPath);
  455. return true;
  456. }
  457. void NETSolution::GenerateSolution(const String &slnPath)
  458. {
  459. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  460. source += "# Visual Studio 14\n";
  461. source += "VisualStudioVersion = 14.0.25420.1\n";
  462. source += "MinimumVisualStudioVersion = 10.0.40219.1\n";
  463. solutionGUID_ = projectGen_->GenerateUUID();
  464. PODVector<NETCSProject*> depends;
  465. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  466. for (unsigned i = 0; i < projects.Size(); i++)
  467. {
  468. NETCSProject* p = projects.At(i);
  469. const String& projectName = p->GetName();
  470. const String& projectGUID = p->GetProjectGUID();
  471. const String CSharpProjectGUID = "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  472. source += ToString("Project(\"{%s}\") = \"%s\", \"%s\\%s.csproj\", \"{%s}\"\n",
  473. CSharpProjectGUID.CString(), projectName.CString(), projectName.CString(),
  474. projectName.CString(), projectGUID.CString());
  475. projectGen_->GetCSProjectDependencies(p, depends);
  476. if (depends.Size())
  477. {
  478. source += "\tProjectSection(ProjectDependencies) = postProject\n";
  479. for (unsigned j = 0; j < depends.Size(); j++)
  480. {
  481. source += ToString("\t{%s} = {%s}\n",
  482. depends[j]->GetProjectGUID().CString(), depends[j]->GetProjectGUID().CString());
  483. }
  484. source += "\tEndProjectSection\n";
  485. }
  486. source += "EndProject\n";
  487. }
  488. source += "Global\n";
  489. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  490. source += " Debug|Any CPU = Debug|Any CPU\n";
  491. source += " Release|Any CPU = Release|Any CPU\n";
  492. source += " EndGlobalSection\n";
  493. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  494. for (unsigned i = 0; i < projects.Size(); i++)
  495. {
  496. NETCSProject* p = projects.At(i);
  497. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  498. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  499. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  500. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  501. }
  502. source += " EndGlobalSection\n";
  503. source += "EndGlobal\n";
  504. if (!rewriteSolution_)
  505. {
  506. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  507. if (fileSystem->Exists(slnPath))
  508. return;
  509. }
  510. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  511. output->Write(source.CString(), source.Length());
  512. output->Close();
  513. }
  514. bool NETSolution::Load(const JSONValue& root)
  515. {
  516. FileSystem* fs = GetSubsystem<FileSystem>();
  517. name_ = root["name"].GetString();
  518. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  519. ReplacePathStrings(outputPath_);
  520. // TODO: use poco mkdirs
  521. if (!fs->DirExists(outputPath_))
  522. fs->CreateDirsRecursive(outputPath_);
  523. return true;
  524. }
  525. bool NETSolution::RegisterPackage(const String& package)
  526. {
  527. if (packages_.Find(package) != packages_.End())
  528. return false;
  529. packages_.Push(package);
  530. return true;
  531. }
  532. NETProjectGen::NETProjectGen(Context* context) : Object(context),
  533. rewriteSolution_(false)
  534. {
  535. }
  536. NETProjectGen::~NETProjectGen()
  537. {
  538. }
  539. NETCSProject* NETProjectGen::GetCSProjectByName(const String & name)
  540. {
  541. for (unsigned i = 0; i < projects_.Size(); i++)
  542. {
  543. if (projects_[i]->GetName() == name)
  544. return projects_[i];
  545. }
  546. return nullptr;
  547. }
  548. bool NETProjectGen::GetCSProjectDependencies(NETCSProject* source, PODVector<NETCSProject*>& depends) const
  549. {
  550. depends.Clear();
  551. const Vector<String>& references = source->GetReferences();
  552. for (unsigned i = 0; i < projects_.Size(); i++)
  553. {
  554. NETCSProject* pdepend = projects_.At(i);
  555. if (source == pdepend)
  556. continue;
  557. for (unsigned j = 0; j < references.Size(); j++)
  558. {
  559. if (pdepend->GetName() == references[j])
  560. {
  561. depends.Push(pdepend);
  562. }
  563. }
  564. }
  565. return depends.Size() != 0;
  566. }
  567. bool NETProjectGen::Generate()
  568. {
  569. solution_->Generate();
  570. for (unsigned i = 0; i < projects_.Size(); i++)
  571. {
  572. if (!projects_[i]->Generate())
  573. return false;
  574. }
  575. return true;
  576. }
  577. void NETProjectGen::SetRewriteSolution(bool rewrite)
  578. {
  579. rewriteSolution_ = rewrite;
  580. if (solution_.NotNull())
  581. solution_->SetRewriteSolution(rewrite);
  582. }
  583. bool NETProjectGen::LoadProject(const JSONValue &root)
  584. {
  585. solution_ = new NETSolution(context_, this, rewriteSolution_);
  586. solution_->Load(root["solution"]);
  587. const JSONValue& jprojects = root["projects"];
  588. if (!jprojects.IsArray() || !jprojects.Size())
  589. return false;
  590. for (unsigned i = 0; i < jprojects.Size(); i++)
  591. {
  592. const JSONValue& jproject = jprojects[i];
  593. if (!jproject.IsObject())
  594. return false;
  595. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  596. if (!csProject->Load(jproject))
  597. return false;
  598. projects_.Push(csProject);
  599. }
  600. return true;
  601. }
  602. bool NETProjectGen::LoadProject(const String& projectPath)
  603. {
  604. SharedPtr<File> file(new File(context_));
  605. if (!file->Open(projectPath))
  606. return false;
  607. String json;
  608. file->ReadText(json);
  609. JSONValue jvalue;
  610. if (!JSONFile::ParseJSON(json, jvalue))
  611. return false;
  612. return LoadProject(jvalue);
  613. }
  614. bool NETProjectGen::LoadProject(Project* project)
  615. {
  616. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  617. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  618. atomicProject_ = project;
  619. JSONValue root;
  620. JSONValue solution;
  621. solution["name"] = "AtomicProject";
  622. solution["outputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Solution/";
  623. JSONArray projects;
  624. JSONObject jproject;
  625. jproject["name"] = "AtomicProject";
  626. jproject["outputType"] = "Library";
  627. jproject["assemblyName"] = "AtomicProject";
  628. jproject["assemblyOutputPath"] = AddTrailingSlash(project->GetProjectPath()) + "AtomicNET/Bin/";
  629. JSONArray references;
  630. references.Push(JSONValue("System"));
  631. references.Push(JSONValue("System.Core"));
  632. references.Push(JSONValue("System.Xml.Linq"));
  633. references.Push(JSONValue("System.XML"));
  634. String atomicNETAssembly = tenv->GetAtomicNETCoreAssemblyDir() + "AtomicNET.dll";
  635. if (!fileSystem->FileExists(atomicNETAssembly))
  636. {
  637. ATOMIC_LOGERRORF("NETProjectGen::LoadProject - AtomicNET assembly does not exist: %s", atomicNETAssembly.CString());
  638. return false;
  639. }
  640. references.Push(JSONValue(atomicNETAssembly));
  641. jproject["references"] = references;
  642. JSONArray sources;
  643. sources.Push(JSONValue(ToString("%s", project->GetResourcePath().CString())));
  644. jproject["sources"] = sources;
  645. projects.Push(jproject);
  646. root["projects"] = projects;
  647. root["solution"] = solution;
  648. return LoadProject(root);
  649. }
  650. bool NETProjectGen::GetRequiresNuGet()
  651. {
  652. if (solution_.Null())
  653. {
  654. ATOMIC_LOGERROR("NETProjectGen::GetRequiresNuGet() - called without a solution loaded");
  655. return false;
  656. }
  657. return solution_->GetPackages().Size() != 0;
  658. }
  659. String NETProjectGen::GenerateUUID()
  660. {
  661. Poco::UUIDGenerator& generator = Poco::UUIDGenerator::defaultGenerator();
  662. Poco::UUID uuid(generator.create()); // time based
  663. return String(uuid.toString().c_str()).ToUpper();
  664. }
  665. }