NETProjectGen.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/IO/File.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include "../ToolEnvironment.h"
  11. #include "NETProjectGen.h"
  12. namespace ToolCore
  13. {
  14. NETProjectBase::NETProjectBase(Context* context, NETProjectGen* projectGen):
  15. Object(context), xmlFile_(new XMLFile(context)), projectGen_(projectGen)
  16. {
  17. }
  18. NETProjectBase::~NETProjectBase()
  19. {
  20. }
  21. void NETProjectBase::ReplacePathStrings(String& path)
  22. {
  23. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  24. const String& atomicRoot = tenv->GetRootSourceDir();
  25. const String& scriptPlatform = projectGen_->GetScriptPlatform();
  26. path.Replace("$ATOMIC_ROOT$", atomicRoot, false);
  27. path.Replace("$SCRIPT_PLATFORM$", scriptPlatform, false);
  28. }
  29. NETCSProject::NETCSProject(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  30. {
  31. }
  32. NETCSProject::~NETCSProject()
  33. {
  34. }
  35. void NETCSProject::CreateCompileItemGroup(XMLElement &projectRoot)
  36. {
  37. FileSystem* fs = GetSubsystem<FileSystem>();
  38. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  39. for (unsigned i = 0; i < sourceFolders_.Size(); i++)
  40. {
  41. const String& sourceFolder = sourceFolders_[i];
  42. Vector<String> result;
  43. fs->ScanDir(result, sourceFolder, "*.cs", SCAN_FILES, true);
  44. for (unsigned j = 0; j < result.Size(); j++)
  45. {
  46. igroup.CreateChild("Compile").SetAttribute("Include", sourceFolder + result[j]);
  47. }
  48. }
  49. }
  50. void NETCSProject::CreateReferencesItemGroup(XMLElement &projectRoot)
  51. {
  52. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  53. FileSystem* fs = GetSubsystem<FileSystem>();
  54. const String& coreCLRAbsPath = tenv->GetNETCoreCLRAbsPath();
  55. XMLElement igroup = projectRoot.CreateChild("ItemGroup");
  56. XMLElement mscorlibref = igroup.CreateChild("Reference");
  57. mscorlibref.SetAttribute("Include", "mscorlib");
  58. mscorlibref.CreateChild("HintPath").SetValue(coreCLRAbsPath + "mscorlib.dll");
  59. mscorlibref.CreateChild("Private").SetValue("False");
  60. for (unsigned i = 0; i < references_.Size(); i++)
  61. {
  62. String ref = references_[i];
  63. String refpath = ref + ".dll";
  64. // we explicitly add mscorlib
  65. if (ref == "mscorlib")
  66. continue;
  67. XMLElement xref = igroup.CreateChild("Reference");
  68. xref.SetAttribute("Include", ref);
  69. // if we're a coreclr assembly, qualify it
  70. if (fs->FileExists(coreCLRAbsPath + refpath))
  71. {
  72. refpath = coreCLRAbsPath + refpath;
  73. }
  74. xref.CreateChild("HintPath").SetValue(refpath);
  75. xref.CreateChild("Private").SetValue("False");
  76. }
  77. }
  78. void NETCSProject::CreateReleasePropertyGroup(XMLElement &projectRoot)
  79. {
  80. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  81. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
  82. pgroup.CreateChild("DebugType").SetValue("full");
  83. pgroup.CreateChild("Optimize").SetValue("true");
  84. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_);
  85. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  86. pgroup.CreateChild("WarningLevel").SetValue("4");
  87. pgroup.CreateChild("ConsolePause").SetValue("false");
  88. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  89. pgroup.CreateChild("NoStdLib").SetValue("true");
  90. }
  91. void NETCSProject::CreateDebugPropertyGroup(XMLElement &projectRoot)
  92. {
  93. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  94. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
  95. pgroup.CreateChild("DebugSymbols").SetValue("true");
  96. pgroup.CreateChild("DebugType").SetValue("full");
  97. pgroup.CreateChild("Optimize").SetValue("false");
  98. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_);
  99. pgroup.CreateChild("DefineConstants").SetValue("DEBUG;");
  100. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  101. pgroup.CreateChild("WarningLevel").SetValue("4");
  102. pgroup.CreateChild("ConsolePause").SetValue("false");
  103. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  104. pgroup.CreateChild("NoStdLib").SetValue("true");
  105. }
  106. void NETCSProject::CreateMainPropertyGroup(XMLElement& projectRoot)
  107. {
  108. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  109. // Configuration
  110. XMLElement config = pgroup.CreateChild("Configuration");
  111. config.SetAttribute("Condition", " '$(Configuration)' == '' ");
  112. config.SetValue("Debug");
  113. // Platform
  114. XMLElement platform = pgroup.CreateChild("Platform");
  115. platform.SetAttribute("Condition", " '$(Platform)' == '' ");
  116. platform.SetValue("AnyCPU");
  117. // ProjectGuid
  118. XMLElement guid = pgroup.CreateChild("ProjectGuid");
  119. guid.SetValue(projectGuid_);
  120. // OutputType
  121. XMLElement outputType = pgroup.CreateChild("OutputType");
  122. outputType.SetValue(outputType_);
  123. // RootNamespace
  124. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  125. rootNamespace.SetValue(rootNamespace_);
  126. // AssemblyName
  127. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  128. assemblyName.SetValue(assemblyName_);
  129. // TargetFrameworkVersion
  130. XMLElement targetFrameWork = pgroup.CreateChild("TargetFrameworkVersion");
  131. targetFrameWork.SetValue("4.5");
  132. }
  133. bool NETCSProject::Generate()
  134. {
  135. XMLElement project = xmlFile_->CreateRoot("Project");
  136. project.SetAttribute("DefaultTargets", "Build");
  137. project.SetAttribute("ToolsVersion", "4.0");
  138. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  139. CreateMainPropertyGroup(project);
  140. CreateDebugPropertyGroup(project);
  141. CreateReleasePropertyGroup(project);
  142. CreateReferencesItemGroup(project);
  143. CreateCompileItemGroup(project);
  144. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildBinPath)\\Microsoft.CSharp.targets");
  145. String projectSource = xmlFile_->ToString();
  146. NETSolution* solution = projectGen_->GetSolution();
  147. String projectPath = solution->GetOutputPath() + name_ + ".csproj";
  148. SharedPtr<File> output(new File(context_, projectPath, FILE_WRITE));
  149. output->Write(projectSource.CString(), projectSource.Length());
  150. return true;
  151. }
  152. bool NETCSProject::Load(const JSONValue& root)
  153. {
  154. name_ = root["name"].GetString();
  155. projectGuid_ = root["projectGuid"].GetString();
  156. outputType_ = root["outputType"].GetString();
  157. rootNamespace_ = root["rootNamespace"].GetString();
  158. assemblyName_ = root["assemblyName"].GetString();
  159. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  160. ReplacePathStrings(assemblyOutputPath_);
  161. const JSONArray& references = root["references"].GetArray();
  162. for (unsigned i = 0; i < references.Size(); i++)
  163. {
  164. String reference = references[i].GetString();
  165. ReplacePathStrings(reference);
  166. references_.Push(reference);
  167. }
  168. const JSONArray& sources = root["sources"].GetArray();
  169. for (unsigned i = 0; i < sources.Size(); i++)
  170. {
  171. String source = sources[i].GetString();
  172. ReplacePathStrings(source);
  173. sourceFolders_.Push(AddTrailingSlash(source));
  174. }
  175. return true;
  176. }
  177. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  178. {
  179. }
  180. NETSolution::~NETSolution()
  181. {
  182. }
  183. bool NETSolution::Generate()
  184. {
  185. String slnPath = outputPath_ + name_ + ".sln";
  186. GenerateXamarinStudio(slnPath);
  187. return true;
  188. }
  189. void NETSolution::GenerateXamarinStudio(const String &slnPath)
  190. {
  191. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  192. source += "# Visual Studio 2012\n";
  193. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  194. for (unsigned i = 0; i < projects.Size(); i++)
  195. {
  196. NETCSProject* p = projects.At(i);
  197. source += ToString("Project(\"{%s}\") = \"%s\", \"%s.csproj\", \"{%s}\"\n",
  198. p->GetProjectGUID().CString(), p->GetName().CString(), p->GetName().CString(),
  199. p->GetProjectGUID().CString());
  200. source += "EndProject\n";
  201. }
  202. source += "Global\n";
  203. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  204. source += " Debug|Any CPU = Debug|Any CPU\n";
  205. source += " Release|Any CPU = Release|Any CPU\n";
  206. source += " EndGlobalSection\n";
  207. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  208. for (unsigned i = 0; i < projects.Size(); i++)
  209. {
  210. NETCSProject* p = projects.At(i);
  211. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  212. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  213. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  214. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  215. }
  216. source += " EndGlobalSection\n";
  217. source += "EndGlobal\n";
  218. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  219. output->Write(source.CString(), source.Length());
  220. output->Close();
  221. }
  222. bool NETSolution::Load(const JSONValue& root)
  223. {
  224. FileSystem* fs = GetSubsystem<FileSystem>();
  225. name_ = root["name"].GetString();
  226. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  227. ReplacePathStrings(outputPath_);
  228. // TODO: use poco mkdirs
  229. if (!fs->DirExists(outputPath_))
  230. fs->CreateDir(outputPath_);
  231. return true;
  232. }
  233. NETProjectGen::NETProjectGen(Context* context) : Object(context)
  234. {
  235. }
  236. NETProjectGen::~NETProjectGen()
  237. {
  238. }
  239. bool NETProjectGen::Generate()
  240. {
  241. solution_->Generate();
  242. for (unsigned i = 0; i < projects_.Size(); i++)
  243. {
  244. if (!projects_[i]->Generate())
  245. return false;
  246. }
  247. return true;
  248. }
  249. bool NETProjectGen::LoadProject(const JSONValue &root)
  250. {
  251. solution_ = new NETSolution(context_, this);
  252. solution_->Load(root["solution"]);
  253. const JSONValue& jprojects = root["projects"];
  254. if (!jprojects.IsArray() || ! jprojects.Size())
  255. return false;
  256. for (unsigned i = 0; i < jprojects.Size(); i++)
  257. {
  258. const JSONValue& jproject = jprojects[i];
  259. if (!jproject.IsObject())
  260. return false;
  261. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  262. if (!csProject->Load(jproject))
  263. return false;
  264. projects_.Push(csProject);
  265. }
  266. return true;
  267. }
  268. bool NETProjectGen::LoadProject(const String& projectPath)
  269. {
  270. SharedPtr<File> file(new File(context_));
  271. if (!file->Open(projectPath))
  272. return false;
  273. String json;
  274. file->ReadText(json);
  275. JSONValue jvalue;
  276. if (!JSONFile::ParseJSON(json, jvalue))
  277. return false;
  278. return LoadProject(jvalue);
  279. }
  280. }