Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

iosBeforePrepareHook.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Hook executed before the 'prepare' stage. Only for iOS project.
  3. It will check if project name has changed. If so - it will change the name of the .entitlements file to remove that file duplicates.
  4. If file name has no changed - hook will do nothing.
  5. */
  6. var path = require('path');
  7. var fs = require('fs');
  8. var ConfigXmlHelper = require('./lib/configXmlHelper.js');
  9. module.exports = function(ctx) {
  10. run(ctx);
  11. };
  12. /**
  13. * Run the hook logic.
  14. *
  15. * @param {Object} ctx - cordova context object
  16. */
  17. function run(ctx) {
  18. var projectRoot = ctx.opts.projectRoot;
  19. var iosProjectFilePath = path.join(projectRoot, 'platforms', 'ios');
  20. var configXmlHelper = new ConfigXmlHelper(ctx);
  21. var newProjectName = configXmlHelper.getProjectName();
  22. var oldProjectName = getOldProjectName(iosProjectFilePath);
  23. // if name has not changed - do nothing
  24. if (oldProjectName.length && oldProjectName === newProjectName) {
  25. return;
  26. }
  27. console.log('Project name has changed. Renaming .entitlements file.');
  28. // if it does - rename it
  29. var oldEntitlementsFilePath = path.join(iosProjectFilePath, oldProjectName, 'Resources', oldProjectName + '.entitlements');
  30. var newEntitlementsFilePath = path.join(iosProjectFilePath, oldProjectName, 'Resources', newProjectName + '.entitlements');
  31. try {
  32. fs.renameSync(oldEntitlementsFilePath, newEntitlementsFilePath);
  33. } catch (err) {
  34. console.warn('Failed to rename .entitlements file.');
  35. console.warn(err);
  36. }
  37. }
  38. // region Private API
  39. /**
  40. * Get old name of the project.
  41. * Name is detected by the name of the .xcodeproj file.
  42. *
  43. * @param {String} projectDir absolute path to ios project directory
  44. * @return {String} old project name
  45. */
  46. function getOldProjectName(projectDir) {
  47. var files = [];
  48. try {
  49. files = fs.readdirSync(projectDir);
  50. } catch (err) {
  51. return '';
  52. }
  53. var projectFile = '';
  54. files.forEach(function(fileName) {
  55. if (path.extname(fileName) === '.xcodeproj') {
  56. projectFile = path.basename(fileName, '.xcodeproj');
  57. }
  58. });
  59. return projectFile;
  60. }
  61. // endregion