What is xcode?
The xcode npm package is a library for parsing, modifying, and generating Xcode project files programmatically. It allows developers to manipulate Xcode project settings and configurations without manually editing the project files, which can be error-prone and time-consuming.
What are xcode's main functionalities?
Parsing Xcode project files
This feature allows you to parse an existing Xcode project file and work with its contents programmatically.
const xcode = require('xcode');
const fs = require('fs');
const projectPath = 'MyApp.xcodeproj/project.pbxproj';
const project = xcode.project(projectPath);
project.parseSync();
console.log(project);
Adding files to a project
This feature allows you to add new source files to an Xcode project programmatically.
const xcode = require('xcode');
const fs = require('fs');
const projectPath = 'MyApp.xcodeproj/project.pbxproj';
const mySourceFile = 'MyClass.swift';
const project = xcode.project(projectPath);
project.parseSync();
project.addSourceFile(mySourceFile);
fs.writeFileSync(projectPath, project.writeSync());
Adding a framework to a project
This feature allows you to add a framework to an Xcode project programmatically.
const xcode = require('xcode');
const fs = require('fs');
const projectPath = 'MyApp.xcodeproj/project.pbxproj';
const frameworkPath = 'Frameworks/MyFramework.framework';
const project = xcode.project(projectPath);
project.parseSync();
project.addFramework(frameworkPath);
fs.writeFileSync(projectPath, project.writeSync());
Creating a new Xcode project
This feature allows you to create a new Xcode project from scratch programmatically.
const xcode = require('xcode');
const fs = require('fs');
const projectPath = 'MyNewApp.xcodeproj/project.pbxproj';
const project = xcode.project(projectPath);
project.parseSync();
// Add configurations and settings here
fs.writeFileSync(projectPath, project.writeSync());
Other packages similar to xcode
pbxproj
The pbxproj package is similar to xcode in that it allows for manipulation of Xcode project files. It provides a different API and may have different features or limitations compared to xcode.
node-xcode
parser/toolkit for xcodeproj project files
Allows you to edit xcodeproject files and write them back out.
Example
// API is a bit wonky right now
var xcode = require('xcode'),
fs = require('fs'),
projectPath = 'myproject.xcodeproj/project.pbxproj',
myProj = xcode.project(projectPath);
// parsing is async, in a different process
myProj.parse(function (err) {
myProj.addHeaderFile('foo.h');
myProj.addSourceFile('foo.m');
myProj.addFramework('FooKit.framework');
fs.writeFileSync(projectPath, myProj.writeSync());
console.log('new project written');
});
Working on the parser
If there's a problem parsing, you will want to edit the grammar under
lib/parser/pbxproj.pegjs
. You can test it online with the PEGjs online thingy
at http://pegjs.majda.cz/online - I have had some mixed results though.
Tests under the test/parser
directory will compile the parser from the
grammar. Other tests will use the prebuilt parser (lib/parser/pbxproj.js
).
To rebuild the parser js file after editing the grammar, run:
./node_modules/.bin/pegjs lib/parser/pbxproj.pegjs
(easier if ./node_modules/.bin
is in your path)
License
MIT