From a5094e0acc7ccd4ae9306618dc697f630902ea66 Mon Sep 17 00:00:00 2001 From: David Richardson Date: Tue, 2 Jul 2024 16:39:17 -0600 Subject: [PATCH 1/2] Update nib2cib's Converter.j to handle xib files with deployment targets of Xcode 11 or later --- Tools/nib2cib/Converter.j | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Tools/nib2cib/Converter.j b/Tools/nib2cib/Converter.j index eeed7d66c..0b18044b4 100644 --- a/Tools/nib2cib/Converter.j +++ b/Tools/nib2cib/Converter.j @@ -34,11 +34,12 @@ @global java -var path = require("path"); -var fs = require("fs"); -var os = require("os"); -var child_process = require("child_process"); -var utilsFile = ObjectiveJ.utils.file; +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const { execSync, spawnSync } = require('child_process'); +const child_process = require("child_process"); +const utilsFile = ObjectiveJ.utils.file; /* var FILE = require("file"), OS = require("os"), */ @@ -147,11 +148,24 @@ ConverterConversionException = @"ConverterConversionException"; { // Compile xib or nib to make sure we have a non-new format nib. temporaryNibFilePath = path.join(tmpdir, path.basename(aFilePath) + ".tmp.nib"); - + temporaryXibFilePath = path.join(tmpdir, path.basename(aFilePath)); + + // Ensure that source xib's tag targets Xcode version 10.10 + var xibContent = fs.readFileSync(aFilePath, { encoding: 'utf8' }); + // Perform the in-memory modification of the deployment tag + if (xibContent.includes(' tag, if present + xibContent = xibContent.replace(//g, ''); + } + var insertPosition = xibContent.indexOf("") + "".length; + var deploymentTag = '\n '; + var updatedXIBContent = xibContent.slice(0, insertPosition) + deploymentTag + xibContent.slice(insertPosition); + // console.log(updatedXIBContent); + fs.writeFileSync(temporaryXibFilePath, updatedXIBContent, 'utf8'); try { - child_process.execSync("/usr/bin/ibtool" + " '" + aFilePath + "' " + "--compile" + " '" + temporaryNibFilePath + "'", {stdio: 'inherit'}); + child_process.execSync("/usr/bin/ibtool" + " '" + temporaryXibFilePath + "' " + "--compile" + " '" + temporaryNibFilePath + "'", {stdio: 'inherit'}); } catch(err) { - [CPException raise:ConverterConversionException reason:@"Could not compile file: " + aFilePath]; + [CPException raise:ConverterConversionException reason:@"Could not compile file: " + temporaryXibFilePath]; } } else From 24052e636940099e2307472b56330f2078437928 Mon Sep 17 00:00:00 2001 From: David Richardson Date: Wed, 3 Jul 2024 13:05:07 -0600 Subject: [PATCH 2/2] Improve inline documentation, code formatting --- Tools/nib2cib/Converter.j | 88 ++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/Tools/nib2cib/Converter.j b/Tools/nib2cib/Converter.j index 0b18044b4..e1dc57897 100644 --- a/Tools/nib2cib/Converter.j +++ b/Tools/nib2cib/Converter.j @@ -34,26 +34,23 @@ @global java -const fs = require('fs'); -const os = require('os'); -const path = require('path'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); const { execSync, spawnSync } = require('child_process'); -const child_process = require("child_process"); -const utilsFile = ObjectiveJ.utils.file; +const child_process = require("child_process"); +const utilsFile = ObjectiveJ.utils.file; -/* var FILE = require("file"), - OS = require("os"), */ +var SharedConverter = nil; -var SharedConverter = nil; +NibFormatUndetermined = 0; +NibFormatMac = 1; +NibFormatIPhone = 2; -NibFormatUndetermined = 0; -NibFormatMac = 1; -NibFormatIPhone = 2; +ConverterModeLegacy = 0; +ConverterModeNew = 1; -ConverterModeLegacy = 0; -ConverterModeNew = 1; - -ConverterConversionException = @"ConverterConversionException"; +ConverterConversionException = @"ConverterConversionException"; @implementation Converter : CPObject { @@ -90,7 +87,7 @@ ConverterConversionException = @"ConverterConversionException"; // Some .xibs are iPhone nibs, check the actual contents in this case. if (path.extname(inputPath) !== ".nib" && fs.lstatSync(inputPath).isFile() && fs.readFileSync(inputPath, { encoding: "utf8" }).indexOf(" tag targets Xcode version 10.10 + + // Ensure that source xib's tag targets deployment version 10.10 var xibContent = fs.readFileSync(aFilePath, { encoding: 'utf8' }); + // Perform the in-memory modification of the deployment tag - if (xibContent.includes(' tag, if present + // + // Remove existing tag, if present + // xib's with Xcode 14 or later do not have a element at all. + // It is simpler to ensure no element is present, + // then add what we want. + // + // Add a new element as the first child of the element. + // This is the placement expected when Xcode adds the element. + // Target version 10.10 as the deployment version. + if (xibContent.includes('/g, ''); } - var insertPosition = xibContent.indexOf("") + "".length; - var deploymentTag = '\n '; + // Where will the element be inserted in the string? + var deploymentTag = '\n '; + var insertPosition = xibContent.indexOf("") + "".length; + + // Slice the string representing the source xib and insert the new element. var updatedXIBContent = xibContent.slice(0, insertPosition) + deploymentTag + xibContent.slice(insertPosition); - // console.log(updatedXIBContent); + fs.writeFileSync(temporaryXibFilePath, updatedXIBContent, 'utf8'); - try { + try + { child_process.execSync("/usr/bin/ibtool" + " '" + temporaryXibFilePath + "' " + "--compile" + " '" + temporaryNibFilePath + "'", {stdio: 'inherit'}); - } catch(err) { + } + catch(err) + { [CPException raise:ConverterConversionException reason:@"Could not compile file: " + temporaryXibFilePath]; } } @@ -183,30 +198,19 @@ ConverterConversionException = @"ConverterConversionException"; // Convert from binary plist to XML plist var temporaryPlistFilePath = path.join(tmpdir, path.basename(aFilePath) + ".tmp.plist"); - try { + try + { child_process.execSync("/usr/bin/plutil" + " " + "-convert" + " " + "xml1" + " '" + (temporaryNibFilePathInDirectoryFile || temporaryNibFilePath) + "' " + "-o" + " '" + temporaryPlistFilePath + "'", {stdio: 'inherit'}); - } catch(err) { + } + catch(err) + { [CPException raise:ConverterConversionException reason:@"Could not convert to xml plist for file: " + aFilePath]; } -/* try - { - var p = OS.popen(["/usr/bin/plutil", "-convert", "xml1", temporaryNibFilePath, "-o", temporaryPlistFilePath]); - if (p.wait() === 1) - [CPException raise:ConverterConversionException reason:@"Could not convert to xml plist for file: " + aFilePath]; - } - finally - { - p.stdin.close(); - p.stdout.close(); - p.stderr.close(); - } */ - if (!isReadable(temporaryPlistFilePath)) [CPException raise:ConverterConversionException reason:@"Unable to convert nib file."]; var plistContents = fs.readFileSync(temporaryPlistFilePath, { encoding: "utf8" }); - //var plistContents = FILE.read(temporaryPlistFilePath, { charset: "UTF-8" }); // Minor NS keyed archive to CP keyed archive conversion. // Use Java directly because rhino's string.replace is *so slow*. 4 seconds vs. 1 millisecond.