Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

4.6 KB
Raw Blame
const fs = require('node:fs');
const path = require('node:path');
const { withDangerousMod, withPodfile, withSettingsGradle } = require('@expo/config-plugins');
const { EXPO_UPDATES_VERSION, nativeSourceHash } = require('./selfhost-ota/contract');
const { patchSources, replaceChecked } = require('./selfhost-ota/patches');

const DIRECTORY = 'cindy-selfhost-expo-updates';
const OWNER_FILE = '.cindy-selfhost-ota-adapter';
const MARKER = 'cindy-selfhost-native-ota-v1';

function resolveSources(projectRoot) {
  const packageFile = require.resolve('expo-updates/package.json', { paths: [projectRoot] });
  const packageRoot = fs.realpathSync(path.dirname(packageFile));
  const version = JSON.parse(fs.readFileSync(packageFile, 'utf8')).version;
  if (version !== EXPO_UPDATES_VERSION) {
    throw new Error('Self-host OTA adapter requires expo-updates@' + EXPO_UPDATES_VERSION + '; review the native hooks before upgrading');
  }
  const patched = patchSources((file) => fs.readFileSync(path.join(packageRoot, file), 'utf8'));
  return { packageRoot, patched };
}

function prepareAdapter(projectRoot, platformRoot) {
  // Validate all upstream hooks before touching any generated output. The
  // original dependency is read-only, even across simultaneous EAS/self-host builds.
  const { packageRoot, patched } = resolveSources(projectRoot);
  const parent = fs.realpathSync(platformRoot);
  const destination = path.join(parent, DIRECTORY);
  if (fs.existsSync(destination)) {
    if (fs.lstatSync(destination).isSymbolicLink() ||
      fs.readFileSync(path.join(destination, OWNER_FILE), 'utf8') !== MARKER) {
      throw new Error('Refusing to replace an unowned self-host OTA adapter directory');
    }
    // This fixed child is exclusively generated by this plugin; it never holds
    // credentials, user data or shared dependencies.
    fs.rmSync(destination, { recursive: true });
  }
  fs.cpSync(packageRoot, destination, {
    recursive: true,
    dereference: true,
    filter: (entry) => {
      const parts = path.relative(packageRoot, entry).split(path.sep);
      return !parts.some((part) => part === 'node_modules' || part === '.git' ||
        part === '.gradle' || part.endsWith('.xcframework')) &&
        !(parts[0] === 'android' && parts[1] === 'build');
    },
  });
  for (const [file, source] of patched) fs.writeFileSync(path.join(destination, file), source);
  fs.copyFileSync(path.join(__dirname, 'selfhost-ota/android/CindyOtaJournal.kt'),
    path.join(destination, 'android/src/main/java/expo/modules/updates/CindyOtaJournal.kt'));
  fs.copyFileSync(path.join(__dirname, 'selfhost-ota/ios/CindyOtaJournal.swift'),
    path.join(destination, 'ios/EXUpdates/CindyOtaJournal.swift'));
  fs.writeFileSync(path.join(destination, OWNER_FILE), MARKER);
  return destination;
}

function patchPodfile(source) {
  if (source.includes('# ' + MARKER)) return source;
  return replaceChecked(source, '  use_expo_modules!',
    '  # ' + MARKER + '\n' +
    "  pod 'EXUpdates', :path => './" + DIRECTORY + "/ios', :modular_headers => true\n" +
    '  use_expo_modules!', 'Podfile');
}

function patchSettingsGradle(source) {
  if (source.includes('// ' + MARKER)) return source;
  if (!source.includes('expoAutolinking.useExpoModules()')) {
    throw new Error('Self-host OTA adapter requires the Expo settings autolinking hook');
  }
  return source + '\n// ' + MARKER + '\n' +
    "project(':expo-updates').projectDir = new File(rootDir, '" + DIRECTORY + "/android')\n";
}

function withSelfhostOta(config, options = {}) {
  if (process.env.EXPO_PUBLIC_XDT_OTA_SELFHOST !== '1' || process.env.CINDY_MOBILE_OTA_NATIVE !== '1') {
    throw new Error('Native OTA adapter may only be assembled by an explicitly enabled self-host build');
  }
  if (options.sourceHash !== nativeSourceHash()) {
    throw new Error('Native OTA adapter sources do not match the resolved runtime fingerprint inputs');
  }
  for (const platform of ['android', 'ios']) {
    config = withDangerousMod(config, [platform, async (mod) => {
      prepareAdapter(mod.modRequest.projectRoot, mod.modRequest.platformProjectRoot);
      return mod;
    }]);
  }
  config = withPodfile(config, (mod) => {
    mod.modResults.contents = patchPodfile(mod.modResults.contents);
    return mod;
  });
  config = withSettingsGradle(config, (mod) => {
    if (mod.modResults.language !== 'groovy') throw new Error('Unsupported Android settings language for self-host OTA');
    mod.modResults.contents = patchSettingsGradle(mod.modResults.contents);
    return mod;
  });
  return config;
}

module.exports = withSelfhostOta;
module.exports.__testing = { resolveSources, prepareAdapter, patchPodfile, patchSettingsGradle };