NETProjectGen.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. pgroup.CreateChild("NoConfig").SetValue("true");
  91. pgroup.CreateChild("NoCompilerStandardLib").SetValue("true");
  92. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  93. const String& coreCLRAbsPath = tenv->GetNETCoreCLRAbsPath();
  94. pgroup.CreateChild("AssemblySearchPaths").SetValue(coreCLRAbsPath);
  95. }
  96. void NETCSProject::CreateDebugPropertyGroup(XMLElement &projectRoot)
  97. {
  98. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  99. pgroup.SetAttribute("Condition", " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
  100. pgroup.CreateChild("DebugSymbols").SetValue("true");
  101. pgroup.CreateChild("DebugType").SetValue("full");
  102. pgroup.CreateChild("Optimize").SetValue("false");
  103. pgroup.CreateChild("OutputPath").SetValue(assemblyOutputPath_);
  104. pgroup.CreateChild("DefineConstants").SetValue("DEBUG;");
  105. pgroup.CreateChild("ErrorReport").SetValue("prompt");
  106. pgroup.CreateChild("WarningLevel").SetValue("4");
  107. pgroup.CreateChild("ConsolePause").SetValue("false");
  108. pgroup.CreateChild("AllowUnsafeBlocks").SetValue("true");
  109. pgroup.CreateChild("NoStdLib").SetValue("true");
  110. pgroup.CreateChild("NoConfig").SetValue("true");
  111. pgroup.CreateChild("NoCompilerStandardLib").SetValue("true");
  112. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  113. const String& coreCLRAbsPath = tenv->GetNETCoreCLRAbsPath();
  114. pgroup.CreateChild("AssemblySearchPaths").SetValue(coreCLRAbsPath);
  115. }
  116. void NETCSProject::CreateMainPropertyGroup(XMLElement& projectRoot)
  117. {
  118. XMLElement pgroup = projectRoot.CreateChild("PropertyGroup");
  119. // Configuration
  120. XMLElement config = pgroup.CreateChild("Configuration");
  121. config.SetAttribute("Condition", " '$(Configuration)' == '' ");
  122. config.SetValue("Debug");
  123. // Platform
  124. XMLElement platform = pgroup.CreateChild("Platform");
  125. platform.SetAttribute("Condition", " '$(Platform)' == '' ");
  126. platform.SetValue("AnyCPU");
  127. // ProjectGuid
  128. XMLElement guid = pgroup.CreateChild("ProjectGuid");
  129. guid.SetValue(projectGuid_);
  130. // OutputType
  131. XMLElement outputType = pgroup.CreateChild("OutputType");
  132. outputType.SetValue(outputType_);
  133. // RootNamespace
  134. XMLElement rootNamespace = pgroup.CreateChild("RootNamespace");
  135. rootNamespace.SetValue(rootNamespace_);
  136. // AssemblyName
  137. XMLElement assemblyName = pgroup.CreateChild("AssemblyName");
  138. assemblyName.SetValue(assemblyName_);
  139. // TargetFrameworkVersion
  140. XMLElement targetFrameWork = pgroup.CreateChild("TargetFrameworkVersion");
  141. targetFrameWork.SetValue("4.5");
  142. }
  143. bool NETCSProject::Generate()
  144. {
  145. XMLElement project = xmlFile_->CreateRoot("Project");
  146. project.SetAttribute("DefaultTargets", "Build");
  147. project.SetAttribute("ToolsVersion", "4.0");
  148. project.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  149. CreateMainPropertyGroup(project);
  150. CreateDebugPropertyGroup(project);
  151. CreateReleasePropertyGroup(project);
  152. CreateReferencesItemGroup(project);
  153. CreateCompileItemGroup(project);
  154. project.CreateChild("Import").SetAttribute("Project", "$(MSBuildBinPath)\\Microsoft.CSharp.targets");
  155. String projectSource = xmlFile_->ToString();
  156. NETSolution* solution = projectGen_->GetSolution();
  157. String projectPath = solution->GetOutputPath() + name_ + ".csproj";
  158. SharedPtr<File> output(new File(context_, projectPath, FILE_WRITE));
  159. output->Write(projectSource.CString(), projectSource.Length());
  160. return true;
  161. }
  162. bool NETCSProject::Load(const JSONValue& root)
  163. {
  164. name_ = root["name"].GetString();
  165. projectGuid_ = root["projectGuid"].GetString();
  166. outputType_ = root["outputType"].GetString();
  167. rootNamespace_ = root["rootNamespace"].GetString();
  168. assemblyName_ = root["assemblyName"].GetString();
  169. assemblyOutputPath_ = root["assemblyOutputPath"].GetString();
  170. ReplacePathStrings(assemblyOutputPath_);
  171. const JSONArray& references = root["references"].GetArray();
  172. for (unsigned i = 0; i < references.Size(); i++)
  173. {
  174. String reference = references[i].GetString();
  175. ReplacePathStrings(reference);
  176. references_.Push(reference);
  177. }
  178. const JSONArray& sources = root["sources"].GetArray();
  179. for (unsigned i = 0; i < sources.Size(); i++)
  180. {
  181. String source = sources[i].GetString();
  182. ReplacePathStrings(source);
  183. sourceFolders_.Push(AddTrailingSlash(source));
  184. }
  185. return true;
  186. }
  187. NETSolution::NETSolution(Context* context, NETProjectGen* projectGen) : NETProjectBase(context, projectGen)
  188. {
  189. }
  190. NETSolution::~NETSolution()
  191. {
  192. }
  193. bool NETSolution::Generate()
  194. {
  195. String slnPath = outputPath_ + name_ + ".sln";
  196. GenerateXamarinStudio(slnPath);
  197. return true;
  198. }
  199. void NETSolution::GenerateXamarinStudio(const String &slnPath)
  200. {
  201. String source = "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  202. source += "# Visual Studio 2012\n";
  203. const Vector<SharedPtr<NETCSProject>>& projects = projectGen_->GetCSProjects();
  204. for (unsigned i = 0; i < projects.Size(); i++)
  205. {
  206. NETCSProject* p = projects.At(i);
  207. source += ToString("Project(\"{%s}\") = \"%s\", \"%s.csproj\", \"{%s}\"\n",
  208. p->GetProjectGUID().CString(), p->GetName().CString(), p->GetName().CString(),
  209. p->GetProjectGUID().CString());
  210. source += "EndProject\n";
  211. }
  212. source += "Global\n";
  213. source += " GlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  214. source += " Debug|Any CPU = Debug|Any CPU\n";
  215. source += " Release|Any CPU = Release|Any CPU\n";
  216. source += " EndGlobalSection\n";
  217. source += " GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  218. for (unsigned i = 0; i < projects.Size(); i++)
  219. {
  220. NETCSProject* p = projects.At(i);
  221. source += ToString(" {%s}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n", p->GetProjectGUID().CString());
  222. source += ToString(" {%s}.Debug|Any CPU.Build.0 = Debug|Any CPU\n", p->GetProjectGUID().CString());
  223. source += ToString(" {%s}.Release|Any CPU.ActiveCfg = Release|Any CPU\n", p->GetProjectGUID().CString());
  224. source += ToString(" {%s}.Release|Any CPU.Build.0 = Release|Any CPU\n", p->GetProjectGUID().CString());
  225. }
  226. source += " EndGlobalSection\n";
  227. source += "EndGlobal\n";
  228. SharedPtr<File> output(new File(context_, slnPath, FILE_WRITE));
  229. output->Write(source.CString(), source.Length());
  230. output->Close();
  231. }
  232. bool NETSolution::Load(const JSONValue& root)
  233. {
  234. FileSystem* fs = GetSubsystem<FileSystem>();
  235. name_ = root["name"].GetString();
  236. outputPath_ = AddTrailingSlash(root["outputPath"].GetString());
  237. ReplacePathStrings(outputPath_);
  238. // TODO: use poco mkdirs
  239. if (!fs->DirExists(outputPath_))
  240. fs->CreateDir(outputPath_);
  241. return true;
  242. }
  243. NETProjectGen::NETProjectGen(Context* context) : Object(context)
  244. {
  245. }
  246. NETProjectGen::~NETProjectGen()
  247. {
  248. }
  249. bool NETProjectGen::Generate()
  250. {
  251. solution_->Generate();
  252. for (unsigned i = 0; i < projects_.Size(); i++)
  253. {
  254. if (!projects_[i]->Generate())
  255. return false;
  256. }
  257. return true;
  258. }
  259. bool NETProjectGen::LoadProject(const JSONValue &root)
  260. {
  261. solution_ = new NETSolution(context_, this);
  262. solution_->Load(root["solution"]);
  263. const JSONValue& jprojects = root["projects"];
  264. if (!jprojects.IsArray() || ! jprojects.Size())
  265. return false;
  266. for (unsigned i = 0; i < jprojects.Size(); i++)
  267. {
  268. const JSONValue& jproject = jprojects[i];
  269. if (!jproject.IsObject())
  270. return false;
  271. SharedPtr<NETCSProject> csProject(new NETCSProject(context_, this));
  272. if (!csProject->Load(jproject))
  273. return false;
  274. projects_.Push(csProject);
  275. }
  276. return true;
  277. }
  278. bool NETProjectGen::LoadProject(const String& projectPath)
  279. {
  280. SharedPtr<File> file(new File(context_));
  281. if (!file->Open(projectPath))
  282. return false;
  283. String json;
  284. file->ReadText(json);
  285. JSONValue jvalue;
  286. if (!JSONFile::ParseJSON(json, jvalue))
  287. return false;
  288. return LoadProject(jvalue);
  289. }
  290. }