private static string AddFileReference(ref string projectContent, ProjectFileInformation file) { string fileName = file.FileName; //Debug.Log(fileName); string pattern = "/\\* Begin PBXFileReference section \\*/\n((?:.|\n)+?)/\\* End PBXFileReference section \\*/"; Match match = Regex.Match(projectContent, pattern); if (!match.Success) { Debug.Log("There is no file reference section"); return(null); } else { string referenceContent = match.Groups[1].Value; string filePath = file.FilePath; string referencePattern = "([A-Z0-9]+) /\\* " + Regex.Escape(fileName) + " \\*/ = {isa = PBXFileReference;"; Match m = Regex.Match(referenceContent, referencePattern); if (m.Success) { Debug.Log("The file is already added"); return(m.Groups[1].Value); } else { string guid = GenerateGUID(); string addLine = null; if (file.IsNeedAddNameToReference) { addLine = "\t\t" + guid + " /* " + fileName + " */ = {isa = PBXFileReference; lastKnownFileType = " + file.FileKnownType + "; name = " + fileName + "; path = " + filePath + "; sourceTree = " + file.SourceTree + "; };\n"; } else { addLine = "\t\t" + guid + " /* " + fileName + " */ = {isa = PBXFileReference; lastKnownFileType = " + file.FileKnownType + "; path = " + filePath + "; sourceTree = " + file.SourceTree + "; };\n"; } Debug.Log(addLine); projectContent = projectContent.Substring(0, match.Groups[1].Index) + addLine + projectContent.Substring(match.Groups[1].Index); return(guid); } } }
private static List <ProjectFileInformation> GetPlugInFilesInformation(string projectGroupPath, string projectFilePath) { string plugInFolderPath = System.IO.Path.Combine(Application.dataPath, PLUG_IN_FOLDER_RELATIVE_PATH); List <string> plugInFiles = FileOperateHelper.GetFiles(plugInFolderPath, new List <string>() { ".h", ".m" }); FileOperateHelper.CopyFiles(plugInFiles, plugInFolderPath, projectGroupPath); List <ProjectFileInformation> files = new List <ProjectFileInformation>(); foreach (string filePath in plugInFiles) { ProjectFileInformation information = new ProjectFileInformation(); information.FileName = System.IO.Path.GetFileName(filePath); information.FilePath = filePath; information.FileType = ProjectFileType.Source; } return(files); //XcodeModifyHelper.ModifyXcodeProject(plugInFiles, projectFilePath, ProjectFileType.Source); }
private static string AddBuildFile(ref string projectContent, ProjectFileInformation fileInfo, string refFileGuid) { string pattern = "/\\* Begin PBXBuildFile section \\*/\n((?:.|\n)+?)/\\* End PBXBuildFile section \\*/"; Match match = Regex.Match(projectContent, pattern); if (!match.Success) { Debug.Log("Couldn't find PBXBuildFile section."); return(null); } else { string buildFileContent = match.Groups[1].Value; string buildFilePattern = "([A-Z0-9]+).+?fileRef = " + Regex.Escape(refFileGuid); Match m = Regex.Match(buildFileContent, buildFilePattern); if (m.Success) { Debug.Log("This build file already exists: " + fileInfo.FileName); return(m.Groups[1].Value); } else { string guid = GenerateGUID(); string addLine = "\t\t" + guid + " /* " + fileInfo.FileName + " in " + fileInfo.FileType + " */ = {isa = PBXBuildFile; fileRef = " + refFileGuid + " /* " + fileInfo.FileName + " */; "; FrameworkFileInformation frameworkInfo = fileInfo as FrameworkFileInformation; if (frameworkInfo != null && frameworkInfo.IsWeak) { addLine = addLine + "settings = {ATTRIBUTES = (Weak, ); }; "; } addLine = addLine + "};\n"; projectContent = projectContent.Substring(0, match.Groups[1].Index) + addLine + projectContent.Substring(match.Groups[1].Index); return(guid); } } }
public static void ModifyXcodeProject(List <ProjectItemInformation> items, string projectFilePath) { string projectSettingFileName = GetXcodeProjectFilePath(projectFilePath); FileStream stream = File.Open(projectSettingFileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream); string content = reader.ReadToEnd(); reader.Close(); projectPath = projectFilePath; projectContent = content; targetName = DEPLOY_TARGET_NAME; buildPhaseDict.Clear(); InitializeConfigurationGuid(); foreach (ProjectItemInformation item in items) //(string fileName in files) { if (item is ProjectGroupInformation) { ProjectGroupInformation info = item as ProjectGroupInformation; string referenceGroupGuid = AddGroup(ref content, info); AddFileToGroup(ref content, info.ParentGroupName, referenceGroupGuid, info.GroupName); } else { ProjectFileInformation info = item as ProjectFileInformation; info.Initialize(); if (info is FrameworkFileInformation) { FrameworkFileInformation frameworkInfo = info as FrameworkFileInformation; if (frameworkInfo.FrameworkType == FrameworkType.Custom) { AddFrameworkConfiguration(ref content, frameworkInfo.FilePath); frameworkInfo.FilePath = frameworkInfo.FileName; } } if (info is ResourceFileInformation) { info.FilePath = FileOperateHelper.GetRelativePathFromAbsolutePath(projectPath, info.FilePath); } else if (info is SourceFileInformation) { info.FilePath = FileOperateHelper.GetRelativePathFromAbsolutePath(System.IO.Path.Combine(projectPath, "Classes"), info.FilePath); } string referenceFileGuid = AddFileReference(ref content, info); AddFileToGroup(ref content, info.ParentGroupName, referenceFileGuid, info.FileName); if (info.Phase != null) { string buildFileGuid = AddBuildFile(ref content, info, referenceFileGuid); AddBuildFileToPhase(ref content, buildFileGuid, info.FileName, info.Phase); } } } stream = File.Open(projectSettingFileName, FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(stream); writer.Write(content); writer.Close(); Debug.Log("Success!"); }