// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Flax.Build.Projects { /// /// Project generator for XCode. /// public class XCodeProjectGenerator : ProjectGenerator { private Random _rand = new Random(1995); private byte[] _randBytes = new byte[12]; /// /// Initializes a new instance of the class. /// public XCodeProjectGenerator() { } /// public override string ProjectFileExtension => string.Empty; /// public override string SolutionFileExtension => "xcodeproj"; /// public override TargetType? Type => null; /// public override Project CreateProject() { return new Project { Generator = this, }; } /// public override void GenerateProject(Project project) { } /// public override void GenerateSolution(Solution solution) { // TOOD: use random IDs for project and configurations var groupId = "04FD6344277A0F15000EA5CA"; var projectId = "04FD6345277A0F15000EA5CA"; var projectName = solution.Name; var projectWorkspacePath = Path.Combine(solution.Path, projectName + ".xcworkspace"); // Generate folders Directory.CreateDirectory(solution.Path); Directory.CreateDirectory(projectWorkspacePath); // Generate XCode project { var contents = new StringBuilder(); contents.AppendLine("// !$*UTF8*$!"); contents.AppendLine("{"); contents.AppendLine("\tarchiveVersion = 1;"); contents.AppendLine("\tclasses = {"); contents.AppendLine("\t};"); contents.AppendLine("\tobjectVersion = 55;"); contents.AppendLine("\tobjects = {"); contents.AppendLine(""); contents.AppendLine("/* Begin PBXGroup section */"); contents.AppendLine("\t\t" + groupId + " = {"); contents.AppendLine("\t\t\tisa = PBXGroup;"); contents.AppendLine("\t\t\tchildren = ("); contents.AppendLine("\t\t\t);"); contents.AppendLine("\t\t\tsourceTree = \"\";"); contents.AppendLine("\t\t};"); contents.AppendLine("/* End PBXGroup section */"); contents.AppendLine(""); contents.AppendLine("/* Begin PBXProject section */"); contents.AppendLine("\t\t" + projectId + " /* Project object */ = {"); contents.AppendLine("\t\t\tisa = PBXProject;"); contents.AppendLine("\t\t\tattributes = {"); contents.AppendLine("\t\t\t\tBuildIndependentTargetsInParallel = 1;"); contents.AppendLine("\t\t\t\tLastUpgradeCheck = 1320;"); contents.AppendLine("\t\t\t};"); contents.AppendLine("\t\t\tbuildConfigurationList = 04FD6348277A0F15000EA5CA /* Build configuration list for PBXProject \"" + projectName + "\" */;"); contents.AppendLine("\t\t\tcompatibilityVersion = \"Xcode 13.0\";"); contents.AppendLine("\t\t\tdevelopmentRegion = en;"); contents.AppendLine("\t\t\thasScannedForEncodings = 0;"); contents.AppendLine("\t\t\tknownRegions = ("); contents.AppendLine("\t\t\t\ten,"); contents.AppendLine("\t\t\t\tBase,"); contents.AppendLine("\t\t\t);"); contents.AppendLine("\t\t\tmainGroup = " + groupId+ ";"); contents.AppendLine("\t\t\tprojectDirPath = \"\";"); contents.AppendLine("\t\t\tprojectRoot = \"\";"); contents.AppendLine("\t\t\ttargets = ("); contents.AppendLine("\t\t\t);"); contents.AppendLine("\t\t};"); contents.AppendLine("/* End PBXProject section */"); contents.AppendLine(""); contents.AppendLine("/* Begin XCBuildConfiguration section */"); contents.AppendLine("\t\t04FD6349277A0F15000EA5CA /* Debug */ = {"); contents.AppendLine("\t\t\tisa = XCBuildConfiguration;"); contents.AppendLine("\t\t\tbuildSettings = {"); contents.AppendLine("\t\t\t};"); contents.AppendLine("\t\t\tname = Debug;"); contents.AppendLine("\t\t};"); contents.AppendLine("\t\t04FD634A277A0F15000EA5CA /* Release */ = {"); contents.AppendLine("\t\t\tisa = XCBuildConfiguration;"); contents.AppendLine("\t\t\tbuildSettings = {"); contents.AppendLine("\t\t\t};"); contents.AppendLine("\t\t\tname = Release;"); contents.AppendLine("\t\t};"); contents.AppendLine("/* End XCBuildConfiguration section */"); contents.AppendLine(""); contents.AppendLine("/* Begin XCConfigurationList section */"); contents.AppendLine("\t\t04FD6348277A0F15000EA5CA /* Build configuration list for PBXProject \"" + projectName + "\" */ = {"); contents.AppendLine("\t\t\tisa = XCConfigurationList;"); contents.AppendLine("\t\t\tbuildConfigurations = ("); contents.AppendLine("\t\t\t\t04FD6349277A0F15000EA5CA /* Debug */,"); contents.AppendLine("\t\t\t\t04FD634A277A0F15000EA5CA /* Release */,"); contents.AppendLine("\t\t\t);"); contents.AppendLine("\t\t\tdefaultConfigurationIsVisible = 0;"); contents.AppendLine("\t\t\tdefaultConfigurationName = Release;"); contents.AppendLine("\t\t};"); contents.AppendLine("/* End XCConfigurationList section */"); contents.AppendLine("\t};"); contents.AppendLine("\trootObject = " + projectId + " /* Project object */;"); contents.AppendLine("}"); Utilities.WriteFileIfChanged(Path.Combine(solution.Path, "project.pbxproj"), contents.ToString()); } // Generate XCode workspace data { var contents = new StringBuilder(); contents.AppendLine(""); contents.AppendLine(""); contents.AppendLine(" "); contents.AppendLine(" "); contents.AppendLine(""); Utilities.WriteFileIfChanged(Path.Combine(projectWorkspacePath, "contents.xcworkspacedata"), contents.ToString()); } } private string GetRandomGuid() { _rand.NextBytes(_randBytes); string result = string.Empty; for (int i = 0; i < 12; i++) { result += _randBytes[i].ToString("X2"); } return result; } } }