@appshuttle.io/turing
Version:
Code Generation Library used in Shuttle
138 lines (98 loc) • 4.81 kB
JavaScript
const fs = require('fs')
const xmlbuilder = require('xmlbuilder')
const TRStringManagement = require('../../../../Mixins/StringManagement/StringManagement')
const TRDeviceManagement = require('../../../../Mixins/DeviceManagement/DeviceManagement')
const IDManagement = require('../../../../Mixins/IDManagement/IDManagement')
const SceneMotor = require('./Scene-Motor')
class StoryboardMotor {
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.StringManagement = new TRStringManagement()
this.DeviceManagement = new TRDeviceManagement()
this.IDManagement = IDManagement
this.SceneMotor = new SceneMotor(this.path, this.project, this.screenPointers, this.screens)
this.generateMainStoryboard = function () {
this.generateMainStoryboardStructure()
this.generateMainStoryboardDevice()
this.generateMainStoryboardDependencies()
this.generateMainStoryboardScenes()
this.closeMainStoryboardStructure()
const context = this
return new Promise((resolve, reject) => {
try {
fs.writeFileSync(context.path, context.storyboardContent)
resolve()
} catch (error) {
reject(error)
}
})
}
/*
GENERATION METHODS
*/
this.generateMainStoryboardStructure = function() {
this.storyboardContent = xmlbuilder.create('document',
{version: '1.0', encoding: 'UTF-8', standalone: true},
{pubID: null, sysID: null},
{skipNullNodes: false, skipNullAttributes: false,
headless: false, ignoreDecorators: false,
separateArrayItems: false, noDoubleEncoding: false,
stringify: {}})
.att('type','com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB')
.att('version', '3.0')
.att('toolsVersion', '13122.16')
.att('targetRuntime', 'iOS.CocoaTouch')
.att('propertyAccessControl', 'none')
.att('useAutolayout', 'YES')
.att('useTraitCollections', 'YES')
.att('useSafeAreas', 'YES')
.att('colorMatched', 'YES')
for (const screen of this.screens) {
if (screen.isInitialScreen && screen.isInitialScreen === true) {
this.mainScreen = screen
}
}
this.storyboardContent.att('initialViewController', this.IDManagement.getIDForSceneOrControllerWithID(this.mainScreen.id))
}
this.generateMainStoryboardDevice = function() {
const devices = this.storyboardContent.ele('device')
.att('id', this.DeviceManagement.iPhoneXStoryboardDevice.id)
.att('orientation', this.DeviceManagement.DeviceOrientations.Portrait)
devices.ele('adaptation')
.att('id', this.DeviceManagement.DeviceAdaptations.Fullscreen)
.end({ pretty: true })
devices.end({ pretty: true })
}
this.generateMainStoryboardDependencies = function() {
var dependencies = this.storyboardContent.ele('dependencies')
dependencies.ele('plugIn')
.att('identifier','com.apple.InterfaceBuilder.IBCocoaTouchPlugin')
.att('version', '13104.12')
dependencies.ele('capability')
.att('name', 'Safe area layout guides')
.att('minToolsVersion', '9.0')
dependencies.ele('capability')
.att('name', 'documents saved in the Xcode 8 format')
.att('minToolsVersion', '8.0')
dependencies.end({ pretty: true })
}
this.generateMainStoryboardScenes = function() {
var scenes = this.storyboardContent.ele('scenes')
for (const screen of this.screens) {
this.SceneMotor.generateXMLForScene(scenes.ele('scene'), screen)
}
scenes.end({ pretty: true })
}
this.closeMainStoryboardStructure = function() {
this.storyboardContent.end({ allowEmpty: true })
}
}
}
module.exports = StoryboardMotor