@appshuttle.io/turing
Version:
Code Generation Library used in Shuttle
345 lines (249 loc) • 23 kB
JavaScript
const fs = require('fs')
const crypto = require('crypto')
const TRStringManagement = require('../../../../Mixins/StringManagement/StringManagement')
const PBXPROJTemplates = require('../TemplateContent/PBXPROJ-Templates')
class PBXPROJMotor {
constructor(path, project, screenPointers, screens) {
if (!path) throw new Error('No pbxproj path is specified')
if (!project) throw new Error('No project is specified')
if (!screenPointers) throw new Error('No screen pointers were specified')
if (!screens) throw new Error('No screens were specified')
this.path = path
this.project = project
this.screenPointers = screenPointers
this.screens = screens
this.pbxprojContent = ''
this.templates = new PBXPROJTemplates()
this.StringManagement = new TRStringManagement()
this.generatePBXPROJ = function () {
this.generatePBXStructure()
this.generatePBXBuildFileSection()
this.generatePBXFileReferenceSection()
this.generatePBXFrameworksBuildPhaseSection()
this.generatePBXGroupSection()
this.generatePBXNativeTargetSection()
this.generatePBXProjectSection()
this.generatePBXResourcesBuildPhaseSection()
this.generatePBXSourcesBuildPhaseSection()
this.generatePBXVariantGroupSection()
this.generateXCBuildConfigurationSection()
this.generateXCConfigurationListSection()
const context = this
return new Promise((resolve, reject) => {
try {
fs.writeFileSync(context.path, context.pbxprojContent)
resolve()
} catch (error) {
reject(error)
}
})
}
/*
GENERATION METHODS
*/
this.generatePBXStructure = function () {
this.pbxprojContent = this.templates.getPBXPROJStructure()
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.ARCHIVE_VERSION_HOLDER, 'g'), this.getPBXPROJArchiveVersion())
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.CLASSES_HOLDER, 'g'), this.getPBXPROJClasses())
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.OBJECT_VERSION_HOLDER, 'g'), this.getPBXPROJObjectVersion())
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.ROOT_OBJECT_HOLDER, 'g'), this.getIDForItemWithID(this.templates.ROOT_OBJECT_HOLDER))
}
this.generatePBXBuildFileSection = function () {
this.buildFiles = [
{ fileName: 'AppDelegate.swift', id: this.getIDForItemWithID('AppDelegate.swift'), fileReference: this.getIDForItemWithID('AppDelegate.swift-FileRef'), baseFileReference: this.getIDForItemWithID('AppDelegate.swift-BaseRef'), fileType: this.templates.SWIFT_FILE_TYPE },
{ fileName: 'Main.storyboard', id: this.getIDForItemWithID('Main.storyboard'), fileReference: this.getIDForItemWithID('Main.storyboard-FileRef'), baseFileReference: this.getIDForItemWithID('Main.storyboard-BaseRef'), fileType: this.templates.STORYBOARD_FILE_TYPE },
{ fileName: 'Assets.xcassets', id: this.getIDForItemWithID('Assets.xcassets'), fileReference: this.getIDForItemWithID('Assets.xcassets-FileRef'), baseFileReference: this.getIDForItemWithID('Assets.xcassets-BaseRef'), fileType: this.templates.XCASSETS_FILE_TYPE },
{ fileName: 'LaunchScreen.storyboard', id: this.getIDForItemWithID('LaunchScreen.storyboard'), fileReference: this.getIDForItemWithID('LaunchScreen.storyboard-FileRef'), baseFileReference: this.getIDForItemWithID('LaunchScreen.storyboard-BaseRef'), fileType: this.templates.STORYBOARD_FILE_TYPE }
]
for (const screen of this.screens) {
const file = {
fileName: this.StringManagement.alfanumericString(screen.name) + 'ViewController.swift',
id: this.getIDForItemWithID(screen.id),
fileReference: this.getIDForItemWithID(screen.id + 'FILEREF'),
baseFileReference: this.getIDForItemWithID(screen.id + 'BASEREF'),
fileType: this.templates.SWIFT_FILE_TYPE
}
this.buildFiles.push(file)
}
var pbxbuildfileContent = ''
for (const file of this.buildFiles) {
var buildFileItemContent = this.templates.getPBXBuildFileItemStructure()
buildFileItemContent = buildFileItemContent.replace(new RegExp(this.templates.BUILD_FILE_ID, 'g'), file.id)
buildFileItemContent = buildFileItemContent.replace(new RegExp(this.templates.BUILD_FILE_REFERENCE_ID, 'g'), file.fileReference)
buildFileItemContent = buildFileItemContent.replace(new RegExp(this.templates.BUILD_FILE_NAME, 'g'), file.fileName)
pbxbuildfileContent = pbxbuildfileContent + buildFileItemContent
}
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXBUILDFILE_HOLDER, 'g'), pbxbuildfileContent)
}
this.generatePBXFileReferenceSection = function () {
var pbxfilereferenceContent = ''
var appFileReferenceContent = this.templates.getPBXFileReferenceAppItemStructure()
appFileReferenceContent = appFileReferenceContent.replace(new RegExp(this.templates.APP_FILE_ID, 'g'), this.getIDForItemWithID(this.templates.APP_FILE_ID))
appFileReferenceContent = appFileReferenceContent.replace(new RegExp(this.templates.APP_FILE_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName) + '.app')
pbxfilereferenceContent = pbxfilereferenceContent + appFileReferenceContent
this.buildFiles.push({
fileName: 'Info.plist', id: this.getIDForItemWithID('Info.plist'), fileReference: this.getIDForItemWithID('Info.plist-FileRef'), fileType: this.templates.PLIST_FILE_TYPE
})
for (const file of this.buildFiles) {
if (file.fileType === this.templates.STORYBOARD_FILE_TYPE) {
var fileReferenceItemContent = this.templates.getPBXFileReferenceStoryboardItemStructure()
fileReferenceItemContent = fileReferenceItemContent.replace(new RegExp(this.templates.BASE_BUILD_REFERENCE_ID, 'g'), file.baseFileReference)
fileReferenceItemContent = fileReferenceItemContent.replace(new RegExp(this.templates.BUILD_FILE_NAME, 'g'), file.fileName)
pbxfilereferenceContent = pbxfilereferenceContent + fileReferenceItemContent
} else {
var fileReferenceItemContent = this.templates.getPBXFileReferenceItemStructure()
fileReferenceItemContent = fileReferenceItemContent.replace(new RegExp(this.templates.BUILD_FILE_REFERENCE_ID, 'g'), file.fileReference)
fileReferenceItemContent = fileReferenceItemContent.replace(new RegExp(this.templates.BUILD_FILE_NAME, 'g'), file.fileName)
fileReferenceItemContent = fileReferenceItemContent.replace(new RegExp(this.templates.BUILD_FILE_TYPE, 'g'), file.fileType)
pbxfilereferenceContent = pbxfilereferenceContent + fileReferenceItemContent
}
}
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXFILEREFERENCE_HOLDER, 'g'), pbxfilereferenceContent)
}
this.generatePBXFrameworksBuildPhaseSection = function () {
var pbxframeworksContent = this.templates.getPBXFrameworksStructure()
pbxframeworksContent = pbxframeworksContent.replace(new RegExp(this.templates.FRAMEWORKS_SECTION_ID, 'g'), this.getIDForItemWithID(this.templates.FRAMEWORKS_SECTION_ID))
pbxframeworksContent = pbxframeworksContent.replace(new RegExp(this.templates.BUILD_ACTION_MASK, 'g'), this.templates.BUILD_ACTION_MASK_DEFAULT)
pbxframeworksContent = pbxframeworksContent.replace(new RegExp(this.templates.RUN_ONLY_POSTPROCC, 'g'), this.templates.RUN_ONLY_FOR_DEPLOYMENT_POSTPROCC_DEFAULT)
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXFRAMEWORKS_HOLDER, 'g'), pbxframeworksContent)
}
this.generatePBXGroupSection = function () {
var pbxgroupsmaingroupContent = this.templates.getPBXGroupsMainGroupStructure()
pbxgroupsmaingroupContent = pbxgroupsmaingroupContent.replace(new RegExp(this.templates.GROUPS_MAIN_ID, 'g'), this.getIDForItemWithID(this.templates.GROUPS_MAIN_ID))
pbxgroupsmaingroupContent = pbxgroupsmaingroupContent.replace(new RegExp(this.templates.GROUPS_APP_ID, 'g'), this.getIDForItemWithID(this.templates.GROUPS_APP_ID))
pbxgroupsmaingroupContent = pbxgroupsmaingroupContent.replace(new RegExp(this.templates.GROUPS_APP_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName))
pbxgroupsmaingroupContent = pbxgroupsmaingroupContent.replace(new RegExp(this.templates.GROUPS_PRODUCTS_ID, 'g'), this.getIDForItemWithID(this.templates.GROUPS_PRODUCTS_ID))
var pbxgroupsproductsgroupContent = this.templates.getPBXGroupsProductsGroupStructure()
pbxgroupsproductsgroupContent = pbxgroupsproductsgroupContent.replace(new RegExp(this.templates.GROUPS_PRODUCTS_ID, 'g'), this.getIDForItemWithID(this.templates.GROUPS_PRODUCTS_ID))
pbxgroupsproductsgroupContent = pbxgroupsproductsgroupContent.replace(new RegExp(this.templates.APP_FILE_ID, 'g'), this.getIDForItemWithID(this.templates.APP_FILE_ID))
pbxgroupsproductsgroupContent = pbxgroupsproductsgroupContent.replace(new RegExp(this.templates.APP_FILE_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName) + '.app')
var pbxgroupsappgroupContent = this.templates.getPBXGroupsAppGroupStructure()
pbxgroupsappgroupContent = pbxgroupsappgroupContent.replace(new RegExp(this.templates.GROUPS_APP_ID, 'g'), this.getIDForItemWithID(this.templates.GROUPS_APP_ID))
pbxgroupsappgroupContent = pbxgroupsappgroupContent.replace(new RegExp(this.templates.GROUPS_APP_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName))
var fileChildren = ''
for (const file of this.buildFiles) {
fileChildren = fileChildren + file.fileReference + ' /* ' + file.fileName + ' */,\n'
}
pbxgroupsappgroupContent = pbxgroupsappgroupContent.replace(new RegExp(this.templates.GROUPS_APP_CHILDREN, 'g'), fileChildren)
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXGROUP_HOLDER, 'g'), pbxgroupsmaingroupContent + pbxgroupsproductsgroupContent + pbxgroupsappgroupContent)
}
this.generatePBXNativeTargetSection = function () {
var pbxnativetargetContent = this.templates.getPBXNativeTargetStructure()
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.NATIVE_TARGET_ID, 'g'), this.getIDForItemWithID(this.templates.NATIVE_TARGET_ID))
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.GROUPS_APP_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName))
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.XCBUILD_TARGET_CONFIG_LIST_ID, 'g'), this.getIDForItemWithID(this.templates.XCBUILD_TARGET_CONFIG_LIST_ID))
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.SOURCES_BUILD_ID, 'g'), this.getIDForItemWithID(this.templates.SOURCES_BUILD_ID))
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.FRAMEWORKS_SECTION_ID, 'g'), this.getIDForItemWithID(this.templates.FRAMEWORKS_SECTION_ID))
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.RESOURCES_BUILD_ID, 'g'), this.getIDForItemWithID(this.templates.RESOURCES_BUILD_ID))
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.APP_FILE_ID, 'g'), this.getIDForItemWithID(this.templates.APP_FILE_ID))
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.APP_FILE_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName) + '.app')
pbxnativetargetContent = pbxnativetargetContent.replace(new RegExp(this.templates.NATIVE_TARGET_PRODUCT_TYPE, 'g'), this.templates.NATIVE_TARGET_PRODUCT_TYPE_DEFAULT)
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXNATIVETARGET_HOLDER, 'g'), pbxnativetargetContent)
}
this.generatePBXProjectSection = function () {
var pbxprojectContent = this.templates.getPBXProjectSectionStructure()
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.ROOT_OBJECT_HOLDER, 'g'), this.getIDForItemWithID(this.templates.ROOT_OBJECT_HOLDER))
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.LAST_SWIFT_UPDATE_CHECK, 'g'), this.templates.LAST_SWIFT_UPDATE_CHECK_DEFAULT)
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.LAST_UPGRADE_CHECK, 'g'), this.templates.LAST_UPGRADE_CHECK_DEFAULT)
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.ORGANIZATION_NAME, 'g'), this.project.creatorInformation.name)
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.NATIVE_TARGET_ID, 'g'), this.getIDForItemWithID(this.templates.NATIVE_TARGET_ID))
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.CREATED_ON_TOOLS_VERSION, 'g'), this.templates.CREATED_ON_TOOLS_VERSION_DEFAULT)
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.XCBUILD_PROJECT_CONFIG_LIST_ID, 'g'), this.getIDForItemWithID(this.templates.XCBUILD_PROJECT_CONFIG_LIST_ID))
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.GROUPS_APP_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName))
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.COMPATIBILITY_VERSION, 'g'), this.templates.COMPATIBILITY_VERSION_DEFAULT)
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.DEVELOPMENT_REGION, 'g'), this.templates.DEVELOPMENT_REGION_DEFAULT)
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.HAS_SCANNED_ENCODINGS, 'g'), this.templates.HAS_SCANNED_ENCODINGS_DEFAULT)
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.GROUPS_MAIN_ID, 'g'), this.getIDForItemWithID(this.templates.GROUPS_MAIN_ID))
pbxprojectContent = pbxprojectContent.replace(new RegExp(this.templates.GROUPS_PRODUCTS_ID, 'g'), this.getIDForItemWithID(this.templates.GROUPS_PRODUCTS_ID))
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXPROJECT_HOLDER, 'g'), pbxprojectContent)
}
this.generatePBXResourcesBuildPhaseSection = function () {
var pbxresourcesContent = this.templates.getPBXResourcesBuildPhaseStructure()
pbxresourcesContent = pbxresourcesContent.replace(new RegExp(this.templates.RESOURCES_BUILD_ID, 'g'), this.getIDForItemWithID(this.templates.RESOURCES_BUILD_ID))
pbxresourcesContent = pbxresourcesContent.replace(new RegExp(this.templates.BUILD_ACTION_MASK, 'g'), this.templates.BUILD_ACTION_MASK_DEFAULT)
pbxresourcesContent = pbxresourcesContent.replace(new RegExp(this.templates.RUN_ONLY_POSTPROCC, 'g'), this.templates.RUN_ONLY_FOR_DEPLOYMENT_POSTPROCC_DEFAULT)
var stringFiles = ''
for (const file of this.buildFiles) {
if (file.fileType === this.templates.STORYBOARD_FILE_TYPE || file.fileType === this.templates.XCASSETS_FILE_TYPE) {
stringFiles = stringFiles + file.id + ' /* ' + file.fileName + ' in Resources */,\n'
}
}
pbxresourcesContent = pbxresourcesContent.replace(new RegExp(this.templates.RESOURCES_FILES, 'g'), stringFiles)
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXRESOURCEBUILD_HOLDER, 'g'), pbxresourcesContent)
}
this.generatePBXSourcesBuildPhaseSection = function () {
var pbxsourcesContent = this.templates.getPBXSourcesBuildPhaseStructure()
pbxsourcesContent = pbxsourcesContent.replace(new RegExp(this.templates.SOURCES_BUILD_ID, 'g'), this.getIDForItemWithID(this.templates.SOURCES_BUILD_ID))
pbxsourcesContent = pbxsourcesContent.replace(new RegExp(this.templates.BUILD_ACTION_MASK, 'g'), this.templates.BUILD_ACTION_MASK_DEFAULT)
pbxsourcesContent = pbxsourcesContent.replace(new RegExp(this.templates.RUN_ONLY_POSTPROCC, 'g'), this.templates.RUN_ONLY_FOR_DEPLOYMENT_POSTPROCC_DEFAULT)
var stringFiles = ''
for (const file of this.buildFiles) {
if (file.fileType === this.templates.SWIFT_FILE_TYPE) {
stringFiles = stringFiles + file.id + ' /* ' + file.fileName + ' in Sources */,\n'
}
}
pbxsourcesContent = pbxsourcesContent.replace(new RegExp(this.templates.SOURCE_FILES, 'g'), stringFiles)
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXSOURCESBUILD_HOLDER, 'g'), pbxsourcesContent)
}
this.generatePBXVariantGroupSection = function () {
var pbxvariantgroupContent = ''
for (const file of this.buildFiles) {
if (file.fileType === this.templates.STORYBOARD_FILE_TYPE) {
var pbxvariantgroupItemContent = this.templates.getPBXVariantGroupItemStructure()
pbxvariantgroupItemContent = pbxvariantgroupItemContent.replace(new RegExp(this.templates.BUILD_FILE_NAME, 'g'), file.fileName)
pbxvariantgroupItemContent = pbxvariantgroupItemContent.replace(new RegExp(this.templates.BUILD_FILE_REFERENCE_ID, 'g'), file.fileReference)
pbxvariantgroupItemContent = pbxvariantgroupItemContent.replace(new RegExp(this.templates.BASE_BUILD_REFERENCE_ID, 'g'), file.baseFileReference)
pbxvariantgroupContent = pbxvariantgroupContent + pbxvariantgroupItemContent + '\n'
}
}
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXVARIANTGROUP_HOLDER, 'g'), pbxvariantgroupContent)
}
this.generateXCBuildConfigurationSection = function () {
var xcbuildconfigContent = this.templates.getPBXXCBuildConfigStructure()
xcbuildconfigContent = xcbuildconfigContent.replace(new RegExp(this.templates.PROJECT_DEBUG_ID, 'g'), this.getIDForItemWithID(this.templates.PROJECT_DEBUG_ID))
xcbuildconfigContent = xcbuildconfigContent.replace(new RegExp(this.templates.PROJECT_RELEASE_ID, 'g'), this.getIDForItemWithID(this.templates.PROJECT_RELEASE_ID))
xcbuildconfigContent = xcbuildconfigContent.replace(new RegExp(this.templates.TARGET_DEBUG_ID, 'g'), this.getIDForItemWithID(this.templates.TARGET_DEBUG_ID))
xcbuildconfigContent = xcbuildconfigContent.replace(new RegExp(this.templates.TARGET_RELEASE_ID, 'g'), this.getIDForItemWithID(this.templates.TARGET_RELEASE_ID))
xcbuildconfigContent = xcbuildconfigContent.replace(new RegExp(this.templates.GROUPS_APP_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName))
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXBUILDCONFIG_HOLDER, 'g'), xcbuildconfigContent)
}
this.generateXCConfigurationListSection = function () {
var xcconfiglistContent = this.templates.getXCConfigListStructure()
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.XCBUILD_PROJECT_CONFIG_LIST_ID, 'g'), this.getIDForItemWithID(this.templates.XCBUILD_PROJECT_CONFIG_LIST_ID))
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.GROUPS_APP_NAME, 'g'), this.StringManagement.alfanumericString(this.project.appInformation.appName))
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.PROJECT_DEBUG_ID, 'g'), this.getIDForItemWithID(this.templates.PROJECT_DEBUG_ID))
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.PROJECT_RELEASE_ID, 'g'), this.getIDForItemWithID(this.templates.PROJECT_RELEASE_ID))
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.XCBUILD_TARGET_CONFIG_LIST_ID, 'g'), this.getIDForItemWithID(this.templates.XCBUILD_TARGET_CONFIG_LIST_ID))
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.TARGET_DEBUG_ID, 'g'), this.getIDForItemWithID(this.templates.TARGET_DEBUG_ID))
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.TARGET_RELEASE_ID, 'g'), this.getIDForItemWithID(this.templates.TARGET_RELEASE_ID))
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.CONFIG_IS_VISIBLE, 'g'), this.templates.CONFIG_IS_VISIBLE_DEFAULT)
xcconfiglistContent = xcconfiglistContent.replace(new RegExp(this.templates.CONFIG_LIST_NAME, 'g'), this.templates.CONFIG_LIST_NAME_DEFAULT)
this.pbxprojContent = this.pbxprojContent.replace(new RegExp(this.templates.PBXXCCONFIGLIST_HOLDER, 'g'), xcconfiglistContent)
}
/*
CONTENT PRODUCERS
*/
this.getPBXPROJArchiveVersion = function () {
return this.templates.ARCHIVE_VERSION_DEFAULT
}
this.getPBXPROJClasses = function () {
return this.templates.CLASSES_DEFAULT
}
this.getPBXPROJObjectVersion = function () {
return this.templates.OBJECT_VERSION_DEFAULT
}
/*
ID PRODUCER
*/
this.getIDForItemWithID = function(itemID) {
if (!this.ids) {
this.ids = {}
}
if (!this.ids[itemID]) {
this.ids[itemID] = crypto.randomBytes(12).toString('hex').toUpperCase()
}
return this.ids[itemID]
}
}
}
module.exports = PBXPROJMotor