Skip to content

addBuildPhase corrupting project #48

@jacobg

Description

@jacobg

I want to automate Crashlytics initialization by adding a build phase, as documented here:
https://firebase.google.com/docs/crashlytics/get-started#initialize

I added a cordova after_prepare hook to run the following script:

const xcode = require('xcode')
const path = require('path')
const fs = require('fs')

module.exports = function (context) {

  const projectDir = path.resolve(context.opts.projectRoot + '/platforms/ios')
  const dirContent = fs.readdirSync(projectDir)
  const matchingProjectFiles = dirContent.filter(filePath => /.*\.xcodeproj/gi.test(filePath) );
  const projectPath = projectDir + '/' + matchingProjectFiles[0] + '/project.pbxproj'

  const project = xcode.project(projectPath)

  project.parse(error => {
    if (error) console.error('failed to parse project', error)
    const options = {
      shellPath: '/bin/sh',
      shellScript: '${PODS_ROOT}/Fabric/run',
      // TODO: setting inputPaths corrupts the project file
      // inputPaths: ['$(BUILT_PRODUCTS_DIR)/$(INFOPLIST_PATH)']
    }
    // TODO: Only add if not there yet
    project.addBuildPhase(
      [],
      'PBXShellScriptBuildPhase',
      'Initialize Crashlytics',
      project.getFirstTarget().uuid,
      options)
    fs.writeFileSync(projectPath, project.writeSync());
  })
}

The script as-is will create the build phase, but note the TODO comment above the commented-out configuration to set inputPaths. If I uncomment inputPaths, then the project file is corrupted, and I need to wipe out the platforms directory and start over.

Am I doing something wrong?

P.S. How can I check whether a build phase exists before adding it, in order to prevent duplicates?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions