项目文件读取pbxproj格式的文件

在本文中,我们主要介绍项目文件的内容,自我感觉有个不错的建议和大家分享下

    ios项目,会都有一个名为project.pbxproj的文件,这里头保存了项目的基本配置,例如你的项目有几个target,用什么证书之类。

    笔者之前因项目须要,须要从这个文件中读取target、configuration等息信,本来想自己写程序现实。后来发当初github下面,经已有人现实了。用起来较比便方,不敢独享,在此分享。

    一、途用(示例码代)

    每日一道理
生命,是一场漫长的棋局。这盘棋没有猎猎西风,没有四起狼烟,只有在取舍和进退中抉择。只有像棋中的小卒那样,勇往直前,毫不退缩沿着沟沟坎坎的人生之路,艰难而执着的求索,前进,才会谱写人生最壮丽的强者之歌。
/*
 * #%L
 * xcode-project-reader
 * %%
 * Copyright (C) 2012 SAP AG
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.sap.prd.mobile.ios.mios.xcodeprojreader;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.sap.prd.mobile.ios.mios.xcodeprojreader.buildphases.BuildPhase;
import com.sap.prd.mobile.ios.mios.xcodeprojreader.buildphases.PBXShellScriptBuildPhase;
import com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlistParser;
import com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlistParserTest;

public class UsageExamples
{
  @Test
  public void usage() throws Exception
  {
    JAXBPlistParser parser = new JAXBPlistParser();

    // Optional step, if project file is not already in XML format.
    // parser.convert(JAXBPlistParserTest.fileName, JAXBPlistParserTest.fileName)
    Plist plist = parser.load(JAXBPlistParserTest.fileName);

    ProjectFile projectFile = new ProjectFile(plist);
    assertEquals("46", projectFile.getObjectVersion());

    Project project = projectFile.getProject();

    Target target = project.getTargets().get(0);
    assertEquals("MyTest", target.getName());

    target = project.getTargets().getByName("MyTest");
    assertEquals("MyTest", target.getName());

    BuildConfiguration config =
          project.getBuildConfigurationList().getBuildConfigurations().get(0);
    assertEquals("Debug",
          config.getName());

    config = project.getBuildConfigurationList().getBuildConfigurations().getByName("Release");
    assertEquals("Release", config.getName());

    BuildSettings buildSettings = config.getBuildSettings();
    assertEquals("5.1",
          buildSettings.getDict().getString("IPHONEOS_DEPLOYMENT_TARGET"));
    assertEquals("YES",
          buildSettings.getDict().getString("VALIDATE_PRODUCT"));

    // LOW LEVEL (not recommended)
    Array buildPhaseRefs = project.getTargets().get(0).getDict().getArray("buildPhases");
    String ref = projectFile.generateReference();
    buildPhaseRefs.add(ref);
    Dict phase = projectFile.createDict();
    phase.put("isa", "PBXShellScriptBuildPhase");
    phase.put("files", projectFile.createArray());
    phase.put("inputPaths", projectFile.createArray());
    phase.put("outputPaths", projectFile.createArray());
    phase.put("runOnlyForDeploymentPostprocessing", "0");
    phase.put("shellPath", "/bin/sh");
    phase.put("shellScript", "env > test.txt");
    projectFile.setObjectByReference(ref, phase);

    // HIGH LEVEL
    ReferenceArray<BuildPhase> buildPhases = project.getTargets().get(0).getBuildPhases();
    PBXShellScriptBuildPhase phase2 = new PBXShellScriptBuildPhase(projectFile);
    phase2.setDefaultValues();
    phase2.setShellScript("env > test.txt");
    buildPhases.add(phase2);

    /*
     * Note: Collections are created on the fly.
     * 
     * project.getTargets().get(0).getBuildPhases().size() would create a collection of targets and
     * a collection of build phases if they don't exist. This is important to know, if you intend to
     * save the property list later. If you don't want this behavior, you have to use the low level
     * APIs.
     */

    // Save back to disk
    // parser.save(plist, JAXBPlistParserTest.fileName);
  }
}

    二、如何获得

    项目的github网址:https://github.com/sap-production/XcodeProjectJavaAPI

文章结束给大家分享下程序员的一些笑话语录: 问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?  
  答:因为李艳红这种败类,所以我没女友!

posted @ 2013-05-03 19:32  坚固66  阅读(379)  评论(0编辑  收藏  举报