Created
September 8, 2014 19:23
-
-
Save juliomarcos/e3a2e91136a9fe237a7e to your computer and use it in GitHub Desktop.
Fragment Generator CLI tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var program = require('commander'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var conf = {}; | |
process.chdir("C:\\Users\\Julio\\Desktop\\visitavirtual-android\\automation-tools"); | |
require('rc')('gen', conf); | |
program | |
.version('0.0.1') | |
.option('-c, --clazz <filename>', 'Java class of the new fragment') | |
.option('-n, --namespace <filename>', 'Java class namespace of the new fragment') | |
.option('-l, --layout <filename>', 'Layout of the new fragment') | |
.option('-t, --title <name>', 'ActionBar title of the new fragment') | |
.option('-s, --smart <title>', 'Title of the ActionBar. The generator guesses the other parameters') | |
.option('-p, --path <path>', 'Android project path') | |
.parse(process.argv); | |
var quit = false; | |
var genFragmentConf = conf['gen-fragment'] | |
if (!program.path && genFragmentConf['path']) program.path = genFragmentConf['path']; | |
if (!program.namespace && genFragmentConf['namespace']) program.namespace = genFragmentConf['namespace']; | |
if (program.smart) { | |
var smart = program.smart; | |
program.clazz = smart.replace(/\s/g, ''); | |
program.layout = smart.replace(/\s/g, '_').toLowerCase(); | |
program.title = smart; | |
console.log('\n>>> Guesses <<<'); | |
console.log('Class Name:', program.clazz+'Fragment.java'); | |
console.log('Layout filename:', 'fragment_'+program.layout+'.xml'); | |
console.log('>>> ------- <<<\n'); | |
} else { | |
if (!program.clazz) { console.log('Please inform the java class using -c or --clazz'); quit = true; } | |
if (!program.layout) { console.log('Please inform the layout using -l or --layout'); quit = true; } | |
if (!program.title) { console.log('Please inform the title using -t or --title'); quit = true; } | |
} | |
if (!program.path) { console.log('Please inform the android project path using -p or --path'); quit = true; } | |
if (!program.namespace) { console.log('Please inform the java project namespace of the class using -n or --namespace'); quit = true; } | |
if (quit && !program.smart) { console.log('You may also try the smart option using -s or --smart'); } | |
if (quit) process.exit(); | |
function genStringTitleXml(name, title) { | |
console.log('\n<string name="title_fragment_'+name+'">'+title+'</string>'); | |
} | |
function genJavaClass(layout, clazz, namespace, androidProjectPath) { | |
var javaClassTemplate = fs.readFileSync('./templates/TemplateFragment.java', 'utf8'); | |
javaClassTemplate = javaClassTemplate.replace('public class TemplateFragment', 'public class ' + clazz + 'Fragment'); | |
javaClassTemplate = javaClassTemplate.replace('R.layout.fragment_template', 'R.layout.fragment_'+layout); | |
javaClassTemplate = javaClassTemplate.replace('R.string.title_fragment_template', 'R.string.title_fragment_'+layout); | |
namespace = namespace.replace(/\./g, path.sep); | |
var javaClassPath = path.join(androidProjectPath, 'src', namespace, 'fragment', clazz+'Fragment.java'); | |
if (!fs.existsSync(javaClassPath)) { | |
fs.writeFileSync(javaClassPath, javaClassTemplate); | |
} | |
} | |
function genLayoutFile(layout, androidProjectPath) { | |
var layoutTemplate = fs.readFileSync('./templates/fragment_template.xml', 'utf8'); | |
var layoutPath = path.join(androidProjectPath, 'res', 'layout', 'fragment_'+layout+'.xml'); | |
if (!fs.existsSync(layoutPath)) { | |
fs.writeFileSync(layoutPath, layoutTemplate); | |
} | |
} | |
genStringTitleXml(program.layout, program.title); | |
genJavaClass(program.layout, program.clazz, program.namespace, program.path); | |
genLayoutFile(program.layout, program.path); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment