{"version":3,"file":"get_swift_parser_dir.js","sourceRoot":"","sources":["../../src/parser_scripts/get_swift_parser_dir.ts"],"names":[],"mappings":";;;;;AAcA,8CAiNC;AA/ND,iDAAyC;AACzC,+CAAyD;AACzD,6DAAsD;AACtD,gDAAuB;AACvB,2BAA4C;AAE5C,8EAA8E;AAC9E,gFAAgF;AAChF,6EAA6E;AAC7E,oBAAoB;AACpB,EAAE;AACF,gFAAgF;AAChF,yEAAyE;AACzE,wEAAwE;AACjE,KAAK,UAAU,iBAAiB,CACrC,GAAW,EACX,aAAsB,EACtB,gBAAyB,EACzB,kBAA2B;IAE3B,IAAI,eAAmC,CAAA;IACvC,IAAI,aAAiC,CAAA;IACrC,IAAI,gBAAoC,CAAA;IAExC,wFAAwF;IACxF,IAAI,aAAa,EAAE,CAAC;QAClB,aAAa,GAAG,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IACrD,CAAC;SAAM,IAAI,gBAAgB,EAAE,CAAC;QAC5B,gBAAgB,GAAG,cAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IACzE,CAAC;SAAM,CAAC;QACN,aAAa,GAAG,IAAA,oCAAe,EAAC,GAAG,EAAE,aAAa,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QACzE,gBAAgB,GAAG,IAAA,oCAAe,EAAC,GAAG,EAAE,eAAe,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IAChF,CAAC;IAED,IAAI,CAAC,CAAC,aAAa,IAAI,gBAAgB,CAAC,EAAE,CAAC;QACzC,IAAA,uBAAa,EACX,mLAAmL,CACpL,CAAA;IACH,CAAC;IAED,IAAI,aAAa,EAAE,CAAC;QAClB,0EAA0E;QAC1E,qCAAqC;QACrC,MAAM,MAAM,GAAG,IAAA,yBAAS,EAAC,YAAY,EAAE,CAAC,UAAU,EAAE,aAA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,CAAC;QACX,gBAAM,CAAC,IAAI,CAAC,qDAAqD,CAAC,EAAE,CAAC,CAAA;IACvE,CAAC;IAED,gBAAM,CAAC,IAAI,CACT,uCAAuC,eAAe,0GAA0G,CACjK,CAAA;IAED,OAAO,eAAe,CAAA;AACxB,CAAC","sourcesContent":["import { spawnSync } from 'child_process'\nimport { exitWithError, logger } from '../common/logging'\nimport { getFileIfExists } from './get_file_if_exists'\nimport path from 'path'\nimport { readdirSync, existsSync } from 'fs'\n\n// Find the location of the Code Connect Swift package on disk, so that we can\n// call `swift run figma-swift` from the correct location. This requires parsing\n// the output of xcodebuild for .xcodeproj projects, or parsing Package.swift\n// for SPM projects.\n//\n// As this is a first party parser, we do this in TypeScript and call it as part\n// of our code. For third party parsers, logic like this would need to be\n// implemented in a script/binary which the user points Code Connect to.\nexport async function getSwiftParserDir(\n cwd: string,\n xcodeprojPath?: string,\n swiftPackagePath?: string,\n sourcePackagesPath?: string,\n) {\n let figmaPackageDir: string | undefined\n let xcodeprojFile: string | undefined\n let packageSwiftFile: string | undefined\n\n // // Check for the supported project types giving precedence top the user provided path\n if (xcodeprojPath) {\n xcodeprojFile = xcodeprojPath.replace(/\\s/g, '\\\\ ')\n } else if (swiftPackagePath) {\n packageSwiftFile = path.dirname(swiftPackagePath).replace(/\\s/g, '\\\\ ')\n } else {\n xcodeprojFile = getFileIfExists(cwd, '*.xcodeproj').replace(/\\s/g, '\\\\ ')\n packageSwiftFile = getFileIfExists(cwd, 'Package.swift').replace(/\\s/g, '\\\\ ')\n }\n\n if (!(xcodeprojFile || packageSwiftFile)) {\n exitWithError(\n 'No supported project found. Supported project types are .xcodeproj or Package.swift. You can specify the location of your .xcodeproj file with the `xcodeprojPath` config option.',\n )\n }\n\n if (xcodeprojFile) {\n // Use xcodebuild to get the build settings, so we can find where the Code\n // Connect Swift package is installed\n const result = spawnSync('xcodebuild', ['-project', xcodeprojFile, '-showBuildSettings'], {\n cwd,\n encoding: 'utf-8',\n })\n\n if (result.error) {\n throw result.error\n }\n\n const buildSettings = result.stdout\n\n // Extract the source and version of the Code Connect Swift package from the\n // xcodebuild output, which can be in any of the following formats depending\n // on how it is installed:\n // - Figma: https://github.com/figma/code-connect @ 0.1.2\n // - Figma: /path/to/code-connect @ local\n // - Figma: /path/to/code-connect\n const figmaPackageMatch = buildSettings.match(/\\s+Figma: ([^\\s]*)(?: @ (.*))?/)\n if (!figmaPackageMatch) {\n exitWithError(\n 'Code Connect Swift package not found. Please add a dependency to the Code Connect package at https://github.com/figma/code-connect to your project.',\n )\n }\n\n // Find the package's location on disk, to compile and run the parser binary from\n const [_, figmaPackageSource, figmaPackageVersion] = figmaPackageMatch\n // The package version is `local` if installed via the \"Add package\" dialog,\n // or undefined if installed via the \"Frameworks\" section directly (which we\n // do for our test project, because the dialog won't allow ancestors to be\n // added)\n const isLocalFigmaPackage = figmaPackageVersion === 'local' || figmaPackageVersion === undefined\n\n if (isLocalFigmaPackage) {\n // If the version is 'local', the package is installed from a local checkout,\n // and the path on disk is the source output by xcodebuild.\n figmaPackageDir = figmaPackageSource\n } else if (sourcePackagesPath) {\n logger.info(`Using custom DerivedData path: ${sourcePackagesPath}`)\n\n // If a custom path is supplied, use it.\n figmaPackageDir = `${sourcePackagesPath}/checkouts/code-connect`\n } else {\n // Otherwise the SourcePackages will typically be located in Xcode's DerivedData directory,\n // at ~/Users/{username}/Library/Developer/Xcode/DerivedData/{project}-{xxx}/SourcePackages, or in the Project's root\n // at {project}/DerivedData/{project}/SourcePackages (depending on how the user's project is configured).\n let hasFoundSourcePackagesDir = false\n\n // 1. First look in the USER_LIBRARY_DIR (e.g. /Users/{username}/Library)\n const userLibraryDirectoryMatch = buildSettings.match(/\\s+USER_LIBRARY_DIR = (.*)/)\n const userLibraryDirectory = userLibraryDirectoryMatch\n ? userLibraryDirectoryMatch[1]\n : undefined\n\n logger.info('Finding Code Connect Swift package')\n\n // Find the project's name using the build settings\n const projectNameMatch = buildSettings.match(/\\s+PROJECT_NAME = (.*)/)\n const projectName = projectNameMatch ? projectNameMatch[1] : undefined\n\n // We can't proceed without the project name (however, this should always exist)\n if (!projectName) {\n exitWithError('PROJECT_NAME not found in xcodebuild output')\n }\n\n // 1. From the folders in the user library directory use a regex to determine the folder name of the project defined by \"ProjectName-\" and a 28 character hash\n // e.g. project-fbybcbnivxfbfeefownexgukzwxd\n if (userLibraryDirectory) {\n // Default to Xcode's default Dervied Data location (e.g. ~/Users/{username}/Library/Developer/Xcode/DerivedData/project-fbybcbnivxfbfeefownexgukzwxd/SourcePackages)\n const root = `${userLibraryDirectory}/Developer/Xcode/DerivedData`\n\n const projectFolderRegex = new RegExp(`${projectName}-[a-zA-Z0-9]{28}`)\n const derivedDataFolders = readdirSync(root)\n const projectFolder = derivedDataFolders.find((folder: string) =>\n projectFolderRegex.test(folder),\n )\n\n // If the project folder is found, use it to find the Code Connect package\n if (projectFolder) {\n figmaPackageDir = `${root}/${projectFolder}/SourcePackages/checkouts/code-connect`\n hasFoundSourcePackagesDir = true\n } else {\n logger.warn('Package not found in user library directory')\n }\n }\n\n // 2. If SourcePackages couldn't be found in ~/Users/{username}/Library/Developer/Xcode/DerivedData/{project}-{xxx}/SourcePackages, attempt\n // to find it in the project root {project}/DerivedData/{project}/SourcePackages\n if (!hasFoundSourcePackagesDir) {\n const rootDir = buildSettings.match(/\\s+LOCROOT = (.*)/)\n\n if (rootDir) {\n figmaPackageDir = `${rootDir[1]}/DerivedData/${projectName}/SourcePackages/checkouts/code-connect`\n hasFoundSourcePackagesDir = true\n } else {\n logger.warn('Package not found in project root')\n }\n }\n\n // 3. Otherwise, the package may be installed to\n // /SourcePackages/checkouts/code-connect. We find the\n // DerivedData location from the BUILD_DIR (which points to\n // /Build/Products).\n if (!hasFoundSourcePackagesDir) {\n const buildDir = buildSettings.match(/\\s+BUILD_DIR = (.*)/)\n\n if (buildDir) {\n figmaPackageDir = `${buildDir[1]}/../../SourcePackages/checkouts/code-connect`\n } else {\n logger.warn('Package not found in build directory')\n }\n }\n }\n } else if (packageSwiftFile) {\n const swiftPackageDir = swiftPackagePath ? path.dirname(swiftPackagePath) : undefined\n const packageDir = swiftPackageDir || cwd\n // Use the Swift command to determine if the package is installed locally or from Git\n try {\n const result = spawnSync(\n 'swift',\n ['package', '--package-path', packageDir, 'describe', '--type', 'json'],\n {\n cwd,\n encoding: 'utf-8',\n },\n )\n\n if (result.error) {\n throw result.error\n }\n\n const packageInfo = JSON.parse(result.stdout)\n\n const codeConnectPackage =\n packageInfo.dependencies.find((p: any) => p.identity === 'code-connect') ??\n // Our local directory is called figmadoc, so this is what swift outputs as the identity\n packageInfo.dependencies.find((p: any) => p.identity === 'figmadoc')\n\n if (!codeConnectPackage) {\n exitWithError(\n 'Code Connect Swift package not found in Package.swift. Please add a dependency to https://github.com/figma/code-connect to your Package.swift file.',\n )\n }\n\n // We can run directly from the directory of the package swift file\n figmaPackageDir = packageDir\n } catch (e) {\n exitWithError(`Error calling Swift command: ${e}`)\n }\n }\n\n if (!figmaPackageDir) {\n exitWithError('Figma package could not be found')\n }\n\n // Check if the figmaPackageDir exists, otherwise we can't proceed,\n // as we require the code-connect package to continue\n if (!existsSync(figmaPackageDir)) {\n exitWithError(`Figma package directory not found at ${figmaPackageDir}`)\n }\n\n // We need to ensure that the directory is writable, so we can run the swift run command\n // otherwise an \"invalid access\" error will be thrown by swift.\n try {\n const packageFile = path.join(figmaPackageDir, 'Package.resolved')\n\n const accessCheck = spawnSync('test', ['-w', packageFile])\n if (accessCheck.status !== 0) {\n // Directory is not writable, attempt to make it writable\n spawnSync('chmod', ['-R', '755', packageFile])\n }\n logger.info(`Directory enabled for swift run command`)\n } catch (e) {\n logger.warn(`Unable to verify or modify directory permissions: ${e}`)\n }\n\n logger.info(\n `Found Code Connect Swift package at ${figmaPackageDir}, building parser binary. This may take a few minutes if this is the first time you've run Code Connect.`,\n )\n\n return figmaPackageDir\n}\n"]}