Gerrel commented on issue #53:
URL: https://github.com/apache/cordova-node-xcode/issues/53#issuecomment-1609444164
@Thanaen indeed old project that I don't use anymore.... do know it was working...
Solution must be somewhere it this snippet
`
```typescript
// Configure localized folders
for (const item of self.lang.targets) {
let locale = groupForPath(`${item}.lproj`);
if (locale === undefined) {
// create locale if not exist
proj.addPbxGroup([], `${item}.lproj`, `${item}.lproj`)
locale = groupForPath(`${item}.lproj`);
if (locale === undefined) {
throw new Error(`Unable to configure localized folder for ${item}`);
}
// Add to parent group
const main = parentPBXGroup();
proj.addToPbxGroupType(locale.key, main.key, 'PBXGroup');
}
const rDirs = (dir: string): string[] => {
var walkSync = function (dir: string, filelist: string[]) {
let files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function (file) {
if (fs.statSync(dir + '/' + file).isDirectory()) {
filelist.push(file);
filelist = walkSync(dir + '/' + file, filelist);
}
else {
// filelist.push(file);
}
});
return filelist;
};
const result = walkSync(dir, []).filter(item => !item.endsWith('.lproj') && item !== 'Localizable');
return result.filter(function (elem, pos) {
return result.indexOf(elem) == pos;
}).map(f => f.toLowerCase());
}
const flavors = rDirs(source);
const syncFolder = (group: { key: string, object: PBXGroup }, path: string) => {
// Add / Update the root items in the root of localization folder
const isBaseDirectory = fs.existsSync(path) && fs.lstatSync(path).isDirectory();
let files: string[];
if (isBaseDirectory) {
files = fs.readdirSync(path);
} else {
const name = Path.basename(path);
files = [name];
path = path.replace(new RegExp(`/${name}$`), '');
// console.log(path, files);
}
if (group.object.children === undefined) {
group.object.children = [];
}
for (const file of files) {
const filePath = `${path}/${file}`;
const relativePath = filePath
.replace(new RegExp(`^${source}/[^/]+\.lproj/`, 'g'), '')
.replace(new RegExp(`^${group.object.name}/`, 'g'), '');
let existing = group.object.children.find((item) => item.comment === file);
// console.log(path);
const isDirectory = fs.existsSync(filePath) && fs.lstatSync(filePath).isDirectory();
// console.log(filePath);
if (isDirectory) {
if (file.endsWith('.lproj')) {
syncFolder(group, `${path}/${file}`);
continue;
} else {
// console.log(`[\nname: ${file}\npath: ${filePath}\nrpath: ${relativePath}\n]`, group.object)
if (existing === undefined) {
const newGroup: { uuid: string, pbxGroup: PBXGroup } = proj.addPbxGroup([], file, relativePath);
// console.log(newGroup);
proj.addToPbxGroupType(newGroup.uuid, group.key, 'PBXGroup');
existing = group.object.children.find((item) => item.comment === file);
if (existing === undefined) {
throw new Error(`Unable to create group: ${file}`);
}
}
}
// Recursive
syncFolder(
{
key: existing!.value,
object: proj.getPBXGroupByKey(existing!.value)
},
`${path}/${file}`);
} else {
if (file.endsWith('.swift')) {
const relativePath = filePath
.replace(new RegExp(`^${source}/`, 'g'), '')
.replace(new RegExp(`^${group.object.name}/`, 'g'), '');
// console.log(source, group.object.name);
// console.log(filePath, relativePath);
if (existing === undefined) {
proj.addFile(relativePath, group.key);
existing = group.object.children.find((item) => item.comment === file);
if (existing === undefined) {
throw new Error(`Unable to create source: ${file}`);
}
}
} else {
// Helper function to get variantGroup by name and localeTarget
const variantGroupByRef = (ref: string): { key: string, object: PBXGroup } | undefined => {
const variants = proj.getPBXObject('PBXVariantGroup');
// console.log(variants);
for (const key in variants) {
const variant: PBXGroup = variants[key];
if (variant === undefined) {
continue;
}
if (key === ref) {
return { key: key, object: variant };
}
}
return undefined;
};
const createLocalizationReference = (reference: string) => {
// create localization reference
const localeRefId = proj.generateUuid();
const localeRef = { fileRef: localeRefId, basename: item };
proj.addToPbxGroupType(localeRef, reference, 'PBXVariantGroup');
const ref = variantGroupByRef(reference);
if (ref === undefined) {
throw new Error(`Unable to retrieve file ref: ${file}`);
}
return ref
}
let ref;
if (existing !== undefined) {
// console.log(group);
ref = variantGroupByRef(existing.value);
// console.log('existing ref', ref);
}
// console.log(ref, existing, '\n--\n');
if (ref === undefined || existing === undefined) {
// create localization group
const key = proj.pbxCreateVariantGroup(file);
proj.addToPbxGroupType(key, group.key, 'PBXGroup');
existing = group.object.children.find((item) => item.comment === file);
if (existing === undefined) {
throw new Error(`Unable to create file ref: ${file}`);
}
ref = createLocalizationReference(existing.value);
// console.log('created ref', ref);
}
// console.log(ref, existing);
// Make sure there is a localization reference
let localeRef = ref.object.children!.find((child) => child.comment === item);
let localeRefId;
if (localeRef !== undefined) {
localeRefId = localeRef.value;
} else {
ref = createLocalizationReference(existing.value);
ref.object.children!.find((child) => child.comment === item)!.value;
}
// Add to file section reference, duplicate is not possible
const refRelativePath = filePath
.replace(new RegExp(`^${source}/`, 'g'), '')
.replace(/(\/.+(\.lproj)\/)/g, '/');
// console.log(relativePath);
const newFileRef = {
basename: item,
lastKnownFileType: 'text.plist.strings',
path: refRelativePath,
sourceTree: '"<group>"',
fileRef: localeRefId,
defaultEncoding: 4,
fileEncoding: 4,
}
// console.log(newFileRef);
proj.addToPbxFileReferenceSection(newFileRef);
}
// Check if fileRef is already in resources and create if necessary
const pbxBuildFileReference = (fileRef: string) => {
const refs = proj.pbxBuildFileSection();
for (const buildFilekey in refs) {
const ref = refs[buildFilekey];
if (ref === undefined) {
continue;
}
if (ref.fileRef === fileRef) {
return {
uuid: buildFilekey,
basename: file,
fileRef: fileRef,
group: file.endsWith('.swift') ? 'Sources':'Resources'
}
}
}
return undefined;
};
// Create buildFile reference if it does not already exist
let buildFileReference = pbxBuildFileReference(existing.value);
if (buildFileReference === undefined) {
const resourceKey = proj.generateUuid();
const newBuildFileRef = {
uuid: resourceKey,
basename: file,
fileRef: existing.value,
group: file.endsWith('.swift') ? 'Sources':'Resources',
defaultEncoding: 4,
fileEncoding: 4,
}
proj.addToPbxBuildFileSection(newBuildFileRef);
buildFileReference = newBuildFileRef;
}
// Assign file references to targets
const allTargets = (): { key: string, object: PBXNativeTarget }[] => {
const items = [];
const nativeList = proj.pbxNativeTargetSection();
for (const key in nativeList) {
const native: PBXNativeTarget | string = nativeList[key];
if (typeof native === 'string') {
continue;
}
items.push({ key: key, object: native });
}
return items;
}
for (const target of allTargets()) {
const resourcesTargetObj = (): PBXResourcesBuildPhase => proj.pbxResourcesBuildPhaseObj(target.key);
const sourcesTargetObj = (): PBXResourcesBuildPhase => proj.pbxSourcesBuildPhaseObj(target.key);
const targetName = target.object.name.toLowerCase();
const isFlavorTarget = flavors.includes(targetName);
const isCurrentFlavorFile = relativePath.includes(`/${targetName}/`)
const isFlavorFile = flavors.findIndex(item => relativePath.includes(`/${item}/`)) !== -1
// console.log(targetName, isFlavorTarget, isCurrentFlavorFile, isFlavorFile);
const ref = Object.assign(buildFileReference, { target: target.key });
const isMainLocale = `${self.lang.main}.lproj` === locale!.object.name;
const isResourceFile = !ref.basename.endsWith('.swift');
if (isResourceFile) {
// Remove duplicates
while (resourcesTargetObj().files.findIndex(item => item.value === buildFileReference!.uuid) !== -1) {
proj.removeFromPbxResourcesBuildPhase(ref);
}
} else {
while (sourcesTargetObj().files.findIndex(item => item.value === buildFileReference!.uuid) !== -1) {
proj.removeFromPbxSourcesBuildPhase(ref);
}
}
// console.log('--')
// console.log(relativePath, ref)
// console.log(isMainLocale, isResourceFile);
// console.log('--')
if (isResourceFile) {
if (ref.basename === 'InfoPlist.strings') {
if (isCurrentFlavorFile && isFlavorTarget) {
// console.log(`adding isFlavorFile isFlavorTarget`, relativePath, targetName);
proj.addToPbxResourcesBuildPhase(ref);
} else if (!isFlavorFile && !isFlavorTarget) {
// console.log(`adding !isFlavorFile !isFlavorTarget`, relativePath, targetName);
proj.addToPbxResourcesBuildPhase(ref);
}
} else {
// console.log(`adding Normal`, ref.basename, targetName);
proj.addToPbxResourcesBuildPhase(ref);
}
} else if (!isResourceFile) {
// console.log('TODO: Need to add source file', ref);
proj.addToPbxSourcesBuildPhase(ref);
} else {
// console.log('File ignored for target processing');
}
}
}
}
};
// const syncSourceFile = (group: { key: string, object: PBXGroup }, path: string) => {
// proj.addFile(path, group.key);
// }
// Does its shit recursive.
syncFolder(locale, `${source}/${item}.lproj`);
const parent = parentPBXGroup();
syncFolder(parent, `${source}/Localizable.swift`);
}
```
`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org