dart_xcodeproj 0.1.0 copy "dart_xcodeproj: ^0.1.0" to clipboard
dart_xcodeproj: ^0.1.0 copied to clipboard

Dart library for parsing and working with Xcode projects. Manipulates .xcodeproj, .xcworkspace, .xcscheme, and .xcconfig files. Pure Dart, cross-platform (macOS, Windows, Linux), zero native code.

example/main.dart

// ignore_for_file: avoid_print
// example/main.dart
// : demonstrates the dart_xcodeproj public API end-to-end.
// Run with:
// dart run example/main.dart
// This will create a temporary .xcodeproj in the system temp dir,
// add a native target and a file reference, then save to disk.

import 'dart:io';

import 'package:dart_xcodeproj/dart_xcodeproj.dart';
import 'package:path/path.dart' as p;

Future<void> main() async {
  // Use a scoped temp directory (never writes outside temp).
  final outDir = Directory.systemTemp.createTempSync('dart_xcodeproj_example_');
  final projectPath = p.join(outDir.path, 'Example.xcodeproj');

  // 1. Create a new empty project on disk.
  final project = await XcodeProject.create(projectPath);
  print('Created project at $projectPath');

  // 2. Add a file reference (Sources/main.swift) into the main group.
  final mainSwift = project.newFile('Sources/main.swift');
  print('Added file: ${mainSwift.path}');

  // 3. Add a native target via ProjectHelper.
  // ProductHelper expects the products group for the product file reference.
  final productsGroup = project.productsGroup;
  final target = ProjectHelper.newTarget(
    project,
    'application',
    'ExampleApp',
    'ios',
    '16.0',
    productsGroup,
    'swift',
    'ExampleApp',
  );
  print('Added target: ${target.name}');

  // 4. Wire the file reference into the target's Sources build phase.
  // ProjectHelper.newTarget creates a PBXSourcesBuildPhase for 'application'.
  final sourcesPhase = target.buildPhases
      .whereType<PBXSourcesBuildPhase>()
      .firstOrNull;
  if (sourcesPhase != null) {
    sourcesPhase.addFileReference(mainSwift);
    print('Wired ${mainSwift.path} into ${target.name} sources build phase.');
  }

  // 5. Save to disk.
  await project.save();
  print(
    'Saved. Targets: ${project.targets.length}, '
    'mainGroup children: ${project.mainGroup.children.length}',
  );

  // Verify the file was written.
  final pbxproj = File(p.join(projectPath, 'project.pbxproj'));
  print('project.pbxproj size: ${pbxproj.lengthSync()} bytes');

  // Clean up temp directory.
  outDir.deleteSync(recursive: true);
  print('Done — temp dir cleaned up.');
}
1
likes
0
points
15.6k
downloads

Publisher

verified publisherangelocassano.it

Weekly Downloads

Dart library for parsing and working with Xcode projects. Manipulates .xcodeproj, .xcworkspace, .xcscheme, and .xcconfig files. Pure Dart, cross-platform (macOS, Windows, Linux), zero native code.

Repository (GitHub)
View/report issues

Topics

#xcode #ios #macos #xcodeproj

License

unknown (license)

Dependencies

args, crypto, mason_logger, path, propertylistserialization, xml, yaml_writer

More

Packages that depend on dart_xcodeproj