public static void OnPostProcessUpdateProjectDeps(
            BuildTarget buildTarget, string pathToBuiltProject) {
        if (!InjectDependencies()) return;

        // If the Pods directory does not exist, the pod download step
        // failed.
        var podsDir = Path.Combine(pathToBuiltProject, "Pods");
        if (!Directory.Exists(podsDir)) return;

        Directory.CreateDirectory(Path.Combine(pathToBuiltProject,
                                               "Frameworks"));
        Directory.CreateDirectory(Path.Combine(pathToBuiltProject,
                                               "Resources"));

        string pbxprojPath = GetProjectPath(pathToBuiltProject);
        var project = new UnityEditor.iOS.Xcode.PBXProject();
        project.ReadFromString(File.ReadAllText(pbxprojPath));
        string target = project.TargetGuidByName(TARGET_NAME);

        HashSet<string> frameworks = new HashSet<string>();
        HashSet<string> linkFlags = new HashSet<string>();
        foreach (var frameworkFullPath in
                 Directory.GetDirectories(podsDir, "*.framework",
                                          SearchOption.AllDirectories)) {
            string frameworkName = new DirectoryInfo(frameworkFullPath).Name;
            string destFrameworkPath = Path.Combine("Frameworks",
                                                    frameworkName);
            string destFrameworkFullPath = Path.Combine(pathToBuiltProject,
                                                        destFrameworkPath);
            // Only move this framework if it contains a library.
            // Skip frameworks that consist of just resources, they're handled
            // in a separate import step.
            if (!File.Exists(Path.Combine(
                    frameworkFullPath,
                    Path.GetFileName(frameworkFullPath)
                        .Replace(".framework", "")))) {
                continue;
            }

            PlayServicesSupport.DeleteExistingFileOrDirectory(
                destFrameworkFullPath);
            Directory.Move(frameworkFullPath, destFrameworkFullPath);
            project.AddFileToBuild(
                target,
                project.AddFile(destFrameworkPath,
                                destFrameworkPath,
                                UnityEditor.iOS.Xcode.PBXSourceTree.Source));

            string moduleMapPath =
                Path.Combine(Path.Combine(destFrameworkFullPath, "Modules"),
                             "module.modulemap");

            if (File.Exists(moduleMapPath)) {
                // Parse the modulemap, format spec here:
                // http://clang.llvm.org/docs/Modules.html#module-map-language
                using (StreamReader moduleMapFile =
                       new StreamReader(moduleMapPath)) {
                    string line;
                    char[] delim = {' '};
                    while ((line = moduleMapFile.ReadLine()) != null) {
                        string[] items = line.TrimStart(delim).Split(delim, 2);
                        if (items.Length > 1) {
                            if (items[0] == "link") {
                                if (items[1].StartsWith("framework")) {
                                    items = items[1].Split(delim, 2);
                                    frameworks.Add(items[1].Trim(
                                        new char[] {'\"'}) + ".framework");
                                } else {
                                    linkFlags.Add("-l" + items[1]);
                                }
                            }
                        }
                    }
                }
            }

            string resourcesFolder = Path.Combine(destFrameworkFullPath,
                                                  "Resources");
            if (Directory.Exists(resourcesFolder)) {
                string[] resFiles = Directory.GetFiles(resourcesFolder);
                string[] resFolders =
                    Directory.GetDirectories(resourcesFolder);
                foreach (var resFile in resFiles) {
                    string destFile = Path.Combine("Resources",
                                                   Path.GetFileName(resFile));
                    File.Copy(resFile, Path.Combine(pathToBuiltProject,
                                                    destFile), true);
                    project.AddFileToBuild(
                        target, project.AddFile(
                            destFile, destFile,
                            UnityEditor.iOS.Xcode.PBXSourceTree.Source));
                }
                foreach (var resFolder in resFolders) {
                    string destFolder =
                        Path.Combine("Resources",
                                     new DirectoryInfo(resFolder).Name);
                    string destFolderFullPath =
                        Path.Combine(pathToBuiltProject, destFolder);
                    PlayServicesSupport.DeleteExistingFileOrDirectory(
                        destFolderFullPath);
                    Directory.Move(resFolder, destFolderFullPath);
                    project.AddFileToBuild(
                        target, project.AddFile(
                            destFolder, destFolder,
                            UnityEditor.iOS.Xcode.PBXSourceTree.Source));
                }
            }
        }

        foreach (var framework in frameworks) {
            project.AddFrameworkToProject(target, framework, false);
        }
        foreach (var linkFlag in linkFlags) {
            project.AddBuildProperty(target, "OTHER_LDFLAGS", linkFlag);
        }
        File.WriteAllText(pbxprojPath, project.WriteToString());
    }
        public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath)
        {
            if (buildTarget != BuildTarget.iOS)
            {
                return;
            }
#if UNITY_IOS
            var    proj     = new UnityEditor.iOS.Xcode.PBXProject();
            string projPath = UnityEditor.iOS.Xcode.PBXProject.GetPBXProjectPath(buildPath);
            proj.ReadFromFile(projPath);
            string target    = proj.TargetGuidByName("Unity-iPhone");
            string plistPath = buildPath + "/Info.plist";
            var    plist     = new UnityEditor.iOS.Xcode.PlistDocument();
            plist.ReadFromFile(plistPath);

            //获取所有的配置文件
            string CONFIG_PATH            = Application.dataPath + "/../SDK/iOS/";
            List <Unity_Xcode_Json> jsons = new List <Unity_Xcode_Json>();
            string[] files = Directory.GetFiles(CONFIG_PATH, "*.json", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < files.Length; i++)
            {
                jsons.Add(JsonUtility.FromJson <Unity_Xcode_Json>(File.ReadAllText(files[i])));
            }

            //创建资源目录
            string frameworkPath = buildPath + "/Frameworks/Plugins/iOS/";
            if (!Directory.Exists(frameworkPath))
            {
                Directory.CreateDirectory(frameworkPath);
            }
            string aPath = buildPath + "/Libraries/Plugins/iOS/";
            if (!Directory.Exists(aPath))
            {
                Directory.CreateDirectory(aPath);
            }

            proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)/Frameworks/Plugins/iOS");
            proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");

            proj.SetBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries/Plugins/iOS");
            proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries");
            proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)");
            proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(inherited)");

            for (int i = 0; i < jsons.Count; i++)
            {
                Unity_Xcode_Json json = jsons[i];
                //系统静态库引用
                if (json.internal_frameworks != null)
                {
                    for (int j = 0; j < json.internal_frameworks.Length; j++)
                    {
                        proj.AddFrameworkToProject(target, json.internal_frameworks[j], false);
                    }
                }

                //系统动态库引用
                if (json.internal_dynamiclibs != null)
                {
                    for (int j = 0; j < json.internal_dynamiclibs.Length; j++)
                    {
                        proj.AddFileToBuild(target, proj.AddFile("usr/lib/" + json.internal_dynamiclibs[j], "Frameworks/" + json.internal_dynamiclibs[j], UnityEditor.iOS.Xcode.PBXSourceTree.Sdk));
                    }
                }

                //外部静态库引用
                if (json.external_frameworks != null)
                {
                    for (int j = 0; j < json.external_frameworks.Length; j++)
                    {
                        EditorUtils_Common.copy_files(CONFIG_PATH + json.external_frameworks[j], frameworkPath);
                        string fileName = Path.GetFileName(json.external_frameworks[j]);
                        proj.AddFileToBuild(target, proj.AddFile("Frameworks/Plugins/iOS/" + fileName, "Frameworks/" + fileName, UnityEditor.iOS.Xcode.PBXSourceTree.Source));
                    }
                }

                //外部静态库引用
                if (json.external_staticlibs != null)
                {
                    for (int j = 0; j < json.external_staticlibs.Length; j++)
                    {
                        EditorUtils_Common.copy_files(CONFIG_PATH + json.external_staticlibs[j], aPath);
                        string fileName = Path.GetFileName(json.external_staticlibs[j]);
                        proj.AddFileToBuild(target, proj.AddFile("Libraries/Plugins/iOS/" + fileName, "Libraries/" + fileName, UnityEditor.iOS.Xcode.PBXSourceTree.Source));
                    }
                }

                //外部文件引用
                if (json.external_files != null)
                {
                    for (int j = 0; j < json.external_files.Length; j++)
                    {
                        EditorUtils_Common.copy_files(CONFIG_PATH + json.external_files[j], aPath);
                        string fileName = Path.GetFileName(json.external_files[j]);
                        proj.AddFileToBuild(target, proj.AddFile("Libraries/Plugins/iOS/" + fileName, "Libraries/" + fileName, UnityEditor.iOS.Xcode.PBXSourceTree.Source));
                    }
                }

                if (json.lib_searchpath != null)
                {
                    for (int j = 0; j < json.lib_searchpath.Length; j++)
                    {
                        proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries/Plugins/iOS/" + json.lib_searchpath[j]);
                    }
                }

                if (json.framework_searchpath != null)
                {
                    for (int j = 0; j < json.framework_searchpath.Length; j++)
                    {
                        proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)/Frameworks/Plugins/iOS/" + json.framework_searchpath[j]);
                    }
                }

                //BuildSetting
                if (json.buildset_set != null)
                {
                    for (int j = 0; j < json.buildset_set.Length; j++)
                    {
                        proj.SetBuildProperty(target, json.buildset_set[j].key, json.buildset_set[j].value);
                    }
                }
                if (json.buildset_add != null)
                {
                    for (int j = 0; j < json.buildset_add.Length; j++)
                    {
                        proj.UpdateBuildProperty(target, json.buildset_add[j].key, new List <string>()
                        {
                            json.buildset_add[j].value
                        }, new List <string>());
                    }
                }

                //Info.plist
                if (json.plistset != null)
                {
                    var dict = plist.root.AsDict();
                    for (int j = 0; j < json.plistset.Length; j++)
                    {
                        dict.SetString(json.plistset[j].key, json.plistset[j].value);
                    }
                }
                if (json.plistarray != null)
                {
                    var dict = plist.root.AsDict();
                    for (int j = 0; j < json.plistarray.Length; j++)
                    {
                        var array = dict.CreateArray(json.plistarray[j].key);
                        for (int k = 0; k < json.plistarray[j].value.Length; k++)
                        {
                            array.AddString(json.plistarray[j].value[k]);
                        }
                    }
                }
                if (json.plistarraydict != null)
                {
                    var dict = plist.root.AsDict();
                    for (int j = 0; j < json.plistarraydict.Length; j++)
                    {
                        var arrayDict = dict.CreateDict(json.plistarraydict[j].key);
                        var array     = arrayDict.CreateArray(json.plistarraydict[j].value.key);
                        for (int k = 0; k < json.plistarraydict[j].value.value.Length; k++)
                        {
                            array.AddString(json.plistarraydict[j].value.value[k]);
                        }
                    }
                }
            }

            proj.WriteToFile(projPath);
            plist.WriteToFile(plistPath);
#endif
        }
Exemple #3
0
        public static void OnPostProcessUpdateProjectDeps(
            BuildTarget buildTarget, string pathToBuiltProject)
        {
            if (!InjectDependencies())
            {
                return;
            }

            // If the Pods directory does not exist, the pod download step
            // failed.
            var podsDir = Path.Combine(pathToBuiltProject, "Pods");

            if (!Directory.Exists(podsDir))
            {
                return;
            }

            Directory.CreateDirectory(Path.Combine(pathToBuiltProject,
                                                   "Frameworks"));
            Directory.CreateDirectory(Path.Combine(pathToBuiltProject,
                                                   "Resources"));

            string pbxprojPath = GetProjectPath(pathToBuiltProject);
            var    project     = new UnityEditor.iOS.Xcode.PBXProject();

            project.ReadFromString(File.ReadAllText(pbxprojPath));
            string target = project.TargetGuidByName(TARGET_NAME);

            HashSet <string> frameworks = new HashSet <string>();
            HashSet <string> linkFlags  = new HashSet <string>();

            foreach (var frameworkFullPath in
                     Directory.GetDirectories(podsDir, "*.framework",
                                              SearchOption.AllDirectories))
            {
                string frameworkName     = new DirectoryInfo(frameworkFullPath).Name;
                string destFrameworkPath = Path.Combine("Frameworks",
                                                        frameworkName);
                string destFrameworkFullPath = Path.Combine(pathToBuiltProject,
                                                            destFrameworkPath);
                // Only move this framework if it contains a library.
                // Skip frameworks that consist of just resources, they're handled
                // in a separate import step.
                if (!File.Exists(Path.Combine(
                                     frameworkFullPath,
                                     Path.GetFileName(frameworkFullPath)
                                     .Replace(".framework", ""))))
                {
                    continue;
                }

                PlayServicesSupport.DeleteExistingFileOrDirectory(
                    destFrameworkFullPath);
                Directory.Move(frameworkFullPath, destFrameworkFullPath);
                project.AddFileToBuild(
                    target,
                    project.AddFile(destFrameworkPath,
                                    destFrameworkPath,
                                    UnityEditor.iOS.Xcode.PBXSourceTree.Source));

                string moduleMapPath =
                    Path.Combine(Path.Combine(destFrameworkFullPath, "Modules"),
                                 "module.modulemap");

                if (File.Exists(moduleMapPath))
                {
                    // Parse the modulemap, format spec here:
                    // http://clang.llvm.org/docs/Modules.html#module-map-language
                    using (StreamReader moduleMapFile =
                               new StreamReader(moduleMapPath)) {
                        string line;
                        char[] delim = { ' ' };
                        while ((line = moduleMapFile.ReadLine()) != null)
                        {
                            string[] items = line.TrimStart(delim).Split(delim, 2);
                            if (items.Length > 1)
                            {
                                if (items[0] == "link")
                                {
                                    if (items[1].StartsWith("framework"))
                                    {
                                        items = items[1].Split(delim, 2);
                                        frameworks.Add(items[1].Trim(
                                                           new char[] { '\"' }) + ".framework");
                                    }
                                    else
                                    {
                                        linkFlags.Add("-l" + items[1]);
                                    }
                                }
                            }
                        }
                    }
                }

                string resourcesFolder = Path.Combine(destFrameworkFullPath,
                                                      "Resources");
                if (Directory.Exists(resourcesFolder))
                {
                    string[] resFiles   = Directory.GetFiles(resourcesFolder);
                    string[] resFolders =
                        Directory.GetDirectories(resourcesFolder);
                    foreach (var resFile in resFiles)
                    {
                        string destFile = Path.Combine("Resources",
                                                       Path.GetFileName(resFile));
                        File.Copy(resFile, Path.Combine(pathToBuiltProject,
                                                        destFile), true);
                        project.AddFileToBuild(
                            target, project.AddFile(
                                destFile, destFile,
                                UnityEditor.iOS.Xcode.PBXSourceTree.Source));
                    }
                    foreach (var resFolder in resFolders)
                    {
                        string destFolder =
                            Path.Combine("Resources",
                                         new DirectoryInfo(resFolder).Name);
                        string destFolderFullPath =
                            Path.Combine(pathToBuiltProject, destFolder);
                        PlayServicesSupport.DeleteExistingFileOrDirectory(
                            destFolderFullPath);
                        Directory.Move(resFolder, destFolderFullPath);
                        project.AddFileToBuild(
                            target, project.AddFile(
                                destFolder, destFolder,
                                UnityEditor.iOS.Xcode.PBXSourceTree.Source));
                    }
                }
            }

            foreach (var framework in frameworks)
            {
                project.AddFrameworkToProject(target, framework, false);
            }
            foreach (var linkFlag in linkFlags)
            {
                project.AddBuildProperty(target, "OTHER_LDFLAGS", linkFlag);
            }
            File.WriteAllText(pbxprojPath, project.WriteToString());
        }