UNPKG

generator-ios-suite

Version:
342 lines (306 loc) 11.3 kB
'use strict'; const Generator = require('yeoman-generator'); const chalk = require('chalk'); const yosay = require('yosay'); var removeLibraryDependencies = []; module.exports = class extends Generator { promptiwng() { this.log('prompting....'); // Have Yeoman greet the user. this.log( yosay(`Welcome to the AppSuite ${chalk.red('generator-ios-suite')} generator!`) ); const prompts = [ // { // type: 'input', // name: 'codeLanguage', // message: 'type objc or swift to select language -- (Objective-C/Swift)', // default: 'objc', // // store: true // }, { type: 'input', name: 'appName', message: 'Type your app name', default: 'EMASDemo', // store: true }, { type: 'input', name: 'bundleId', message: 'Type your bundleId', default: 'com.mycompany.myiosapp', // store: true }, // { // type: 'input', // name: 'commonDebug', // message: 'install commonDebug for your app', // default: 'yes', // // store: true // } ]; return this.prompt(prompts).then(props => { this.props = props; }); } writing() { this.log('writing'); let replacements = { appName: this.props.appName, bundleId: this.props.bundleId, bundleIdKey: this.props.appName.toLowerCase() }; let oldAppName = 'EMASDemo'; let appName = this.props.appName; //TODO: only support Objective-C this.props.codeLanguage = 'objc'; const templateFiles = [ ['Podfile'], ['README.md'], [`${oldAppName}\ Notification\ Extension\ Service`, `${appName}\ Notification\ Extension\ Service`], [`${oldAppName}Tests`, `${appName}Tests`], [`${oldAppName}UITests`, `${appName}UITests`], ['patch'], ['preload'], ['Resource'], ['_.gitignore', '.gitignore'], [`${oldAppName}`, `${appName}`], [`${oldAppName}.xcodeproj`, `${appName}.xcodeproj`], ['AccsReceiverTable.plist'], ['AliyunEmasServices-Info.plist'], ['build.sh'], ['gen_scaffold.sh'], ['PushSDKReceiverConfig.plist'], ['WeexContainer-Info.plist'], ]; // [`${oldAppName}.xcworkspace`, `${appName}.xcworkspace`], var languagetemplatePath = this.sourceRoot() + '/' + 'template-' + this.props.codeLanguage.toLowerCase() + '/'; templateFiles.forEach(([src, dest = src]) => { var srcPath = languagetemplatePath + `${src}`; this.fs.copyTpl(srcPath, `${dest}`, replacements); }); } install() { this.log('installing'); } end() { this.log( yosay(`Installed success ${chalk.red('generator-ios-suite')} generator!`) ); this.log('install end, enjoy your Suite APP.'); this.parseConfFileContent((err, data) => { // this.log(err); }); // if (this.props.commonDebug !== 'yes') { // this.replacePodfileModule('commonDebug #APPSUITETAG', '# commonDebug #APPSUITETAG', function (err) { // // console.log(err); // // console.log((err ? ('replacePodfileModule end, error is' + err) : "replacePodfileModule success")); // }) // } // this.replacePodfileModule('commonDebug #APPSUITETAG', '# commonDebug #APPSUITETAG', function (err) { // // console.log(err); // // console.log((err ? ('replacePodfileModule end, error is' + err) : "replacePodfileModule success")); // }); } readFileFromPath(filePath, callback) { if (!filePath || !callback) { (callback && typeof (callback) === "function") && callback(null, null); return; } var fs = require('fs'); fs.stat(filePath, function (err, stat) { if (!(stat && stat.isFile())) { let errorString = 'file not found'; //console.log(err); (callback && typeof (callback) === "function") && callback(err, null); return; } console.log('file exist'); let data = fs.readFileSync(filePath, 'utf8');// function (err, data) { let errcallback = "data null"; (callback && typeof (callback) === "function") && callback(data ? null : errcallback, data); // }); }); } remveModule(moduleDependenciesArray, callback) { let err = 'moduleDependenciesArray null'; if (!moduleDependenciesArray) { (callback && typeof (callback) === "function") && callback(err); return; } if (!moduleDependenciesArray.length) { (callback && typeof (callback) === "function") && callback(err); return; } var myStringArray = moduleDependenciesArray; var arrayLength = myStringArray.length; for (var index = 0; index < arrayLength; index++) { let moduleString = moduleDependenciesArray[index]; if (!moduleString.length) { continue; } // 'commonDebug #APPSUITETAG' --> '# commonDebug #APPSUITETAG' let moduleTag = " #APPSUITETAG"; let addOperaFullText = moduleString + moduleTag; let removeOperaFullText = "# " + moduleString + moduleTag; console.log(moduleTag); console.log(addOperaFullText); console.log(removeOperaFullText); this.replacePodfileModule(addOperaFullText, removeOperaFullText, function (err) { //console.log(err); if (err) { console.log("replacePodfileModule failed"); } else { console.log("replacePodfileModule success:" + addOperaFullText + " to " + removeOperaFullText); } (callback && typeof (callback) === "function") && callback(err); }); } } addLib(otherLibraryDependenciesDict, callback) { // otherLibraryDependenciesDict let err = 'addLib FAILED'; if (!(otherLibraryDependenciesDict)) { (callback && typeof (callback) === "function") && callback(err); return; } if (!(otherLibraryDependenciesDict.length)) { (callback && typeof (callback) === "function") && callback(err); return; } var obj = otherLibraryDependenciesDict; let libDependencies = ""; Object.keys(obj).forEach(function (key) { console.log("190" + key, obj[key]); let libName = key; let libVerson = obj[key]; //pod 'DynamicConfigurationAdaptor', '10.0.3' let libVersionWithFormat = ""; if (libVerson) { libVersionWithFormat = ", \'" + libVerson + "\'"; } let libDependencie = "pod \'" + libName + "\'" + libVersionWithFormat + "\n"; libDependencies = libDependencies + libDependencie; }) if (!libDependencies.length) { (callback && typeof (callback) === "function") && callback(err); return; } libDependencies = "\n" + libDependencies; let originOtherLibString = "def otherLibraryDependencies"; let libDependenciesFormat = originOtherLibString + libDependencies; let addOperaFullText = originOtherLibString; let removeOperaFullText = libDependenciesFormat; this.replacePodfileModule(addOperaFullText, removeOperaFullText, function (err) { //console.log(err); if (err) { console.log("replacePodfileModule libDependencies failed"); } else { console.log("replacePodfileModule libDependencies success:" + addOperaFullText + " to " + removeOperaFullText); } (callback && typeof (callback) === "function") && callback(err); }); } replacePodfileModule(originText, newText, callback) { if (!originText || !newText || !callback) { (callback && typeof (callback) === "function") && callback(null); return; } console.log("editing podfile"); /** * expect for languagetemplatePath with sourceRoot, we only change destinationPath. https://yeoman.io/authoring/file-system.html //readdir方法读取文件名 */ var fs = require('fs'); var filePath = this.destinationPath('Podfile'); //readFile方法读取文件内容 this.readFileFromPath(filePath, function (err, data) { if (err) { console.log('readFileFromPath failed'); (callback && typeof (callback) === "function") && callback(err); return; } console.log("242 data content:" + data) var result = data.replace(originText, newText); //----writeFile改写文件内容 fs.writeFileSync(filePath, result, 'utf8'); (callback && typeof (callback) === "function") && callback(null); // }); }); } parseConfFileContent(callback) { if (!callback) { (callback && typeof (callback) === "function") && callback(null); return; } this.log("begin parse ConfFile content"); var fs = require('fs'); var filePath = this.destinationPath('.suite-conf.json'); this.readFileFromPath(filePath, (err, data) => { if (err) { //console.log(err); console.log("parseConfFileContent failed"); (callback && typeof (callback) === "function") && callback(err); return; } var confs; try { confs = JSON.parse(data); } catch (e) { if (typeof data == 'object') { confs = data; } else { try { confs = JSON.parse(JSON.stringify(data)); } catch (error) { } } } console.log(Object.keys(confs).length); console.log('data content is' + typeof (data) + data + "parsed confs is" + typeof (confs) + confs); let libraryManagerString = confs.libraryManager; console.log('confs libraryManager' + confs['libraryManager']); console.log("libraryManagerString is" + libraryManagerString) //FIXME: this is libraryManagerString key is now set only for other manager beforehand, and we will upgrate someday. if (libraryManagerString !== "CocoaPods") { let err = 'sorry, only support CocoaPods now'; console.log(err); (callback && typeof (callback) === "function") && callback(err); return; } let moduleDependenciesJSON = confs.moduleDependencies; let otherLibraryDependenciesDict = confs.otherLibraryDependencies; /* just for test */ console.log("281" + JSON.stringify(libraryManagerString)); console.log("282" + JSON.stringify(moduleDependenciesJSON)); console.log("283" + JSON.stringify(otherLibraryDependenciesDict)); var obj = moduleDependenciesJSON; Object.keys(obj).forEach(function (key) { console.log("287" + key, obj[key]); let bool = obj[key]; console.log("290" + bool + "---" + key) if (!bool) { removeLibraryDependencies.push(key); } }); this.remveModule(removeLibraryDependencies, (err) => { // this.log(err); // if (err) { // console.log("remve libDependencies failed"); // } else { // console.log("remve libDependencies success"); // } }); this.addLib(otherLibraryDependenciesDict, (err) => { // this.log(err); // if (err) { // console.log("add libDependencies failed"); // } else { // console.log("add libDependencies success"); // } }); (callback && typeof (callback) === "function") && callback(null); return; }); } };