GitHub user erisu edited a comment on the discussion: Plugin: Set minimum iOS 
deployment target

If you simply want to terminate the build and notify the app user that their 
`deployment-target` is too low, you can use a hook script, as suggested by 
@breautek.

**Example Script:**

```javascript
const path = require('node:path');

const semver = require('semver');
const xcode = require('xcode');
const { ConfigParser } = require('cordova-common');
const CordovaPlatform = require('cordova-ios');

const MINIMUM_DEPLOYMENT_TARGET = '15.0';

module.exports = context => {
    const projectRoot = context.opts.projectRoot;
    const platformPath = path.join(projectRoot, 'platforms', 'ios');
    const iosProject = new CordovaPlatform('ios', platformPath);
    const cfg = new ConfigParser(iosProject.locations.configXml);

    // Get Deployment Target from config.xml.
    let deploymentTarget = cfg.getPreference('deployment-target', 'ios');

    if (!deploymentTarget) {
        const target = path.basename(iosProject.locations.xcodeCordovaProj);
        const xcodeProject = xcode.project(iosProject.locations.pbxproj);
        xcodeProject.parseSync();

        // Get Deployment Target from pbxproj
        deploymentTarget = 
xcodeProject.getBuildProperty('IPHONEOS_DEPLOYMENT_TARGET', undefined, target);
    }

    if (compareDeploymentTarget(deploymentTarget)) {
        throw new Error(`The project's deployment-target "${deploymentTarget}" 
is too low. Should be greater then ${MINIMUM_DEPLOYMENT_TARGET}`);
    }
}

const compareDeploymentTarget = (current) => semver.lt(
    // Converting the current deployment target to a semver format to compare 
with.
    semver.coerce(current)?.version || '0.0.0',
    semver.coerce(MINIMUM_DEPLOYMENT_TARGET)?.version
);
```

**Required NPM Packages:**

You'll need to install the following dependencies:

- `semver`
- `xcode`
- `cordova-common`

These packages are included with `cordova-ios`, but it's recommended to install 
them separately to avoid potential issues if `cordova-ios` updates or removes 
them.

Then set the script to run before the build (`before_build`).

The prepare step should have already been completed and if the user sets a 
`deployment-target` in their `config.xml`, it should be available in the 
platform's `config.xml`.

If it's missing, the script will retrieve the default value from `pbxproj`.

GitHub link: 
https://github.com/apache/cordova/discussions/529#discussioncomment-12601774

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscr...@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to