Skip to content

Instantly share code, notes, and snippets.

@bramblex
Created November 4, 2017 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bramblex/5256c1de3cf0f504cc458b8d0a0dd8d8 to your computer and use it in GitHub Desktop.
Save bramblex/5256c1de3cf0f504cc458b8d0a0dd8d8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const [, , ...argv] = process.argv
const create_body_flag = argv.filter(key => key === '-b').length > 0
const classes = argv.filter(key => key !== '-b')
const warpNamespace = (namespaces, content) => {
if (namespaces.length === 0)
return content
else
return (
`namespace ${namespaces[0]} {
${warpNamespace(namespaces.slice(1), content)}
}`)
}
classes.forEach(arg => {
const options = {
namespaces: null,
classname: null
}
const result = arg.split('::')
options.classname = result[result.length - 1]
options.namespaces = result.slice(0, -1);
const header_path = path.join('./', options.classname + '.h')
const body_path = path.join('./', options.classname + '.cpp')
const header_macro_name =
options.namespaces
.concat(options.classname)
.map(k => k.toUpperCase())
.join('_') + '_H'
const header_content =
`// ${options.classname}.h
#ifndef ${header_macro_name}
#define ${header_macro_name}
${warpNamespace(options.namespaces,
`class ${options.classname} {
${options.classname}();
};`
)}
#endif // ${header_macro_name}`
const body_content =
`// ${options.classname}.cpp
#include "${options.classname}.h"
${warpNamespace(options.namespaces,
`${options.classname}::${options.classname}(){
// do somethings
}`
)}`
fs.writeFileSync(header_path, header_content, 'utf-8')
if (create_body_flag) fs.writeFileSync(body_path, body_content, 'utf-8')
})
// classes.forEach(header => {
// const options = {
// namespace: null,
// classname: null,// }
// if (/::/.test(header)) {
// const [namespace, classname] = header.split('::');
// options.namespace = namespace
// options.classname = classname
// } else {
// options.classname = header
// }
// const header_path = path.join('./', options.classname + '.h')
// const body_path = path.join('./', options.classname + '.cpp')
// const micro_name = (
// (options.namespace ? options.namespace + '_' : '')
// + (options.classname) + '_H'
// ).toUpperCase()
// const namespace = options.namespace
// const classname = options.classname
// const header_content = `// ${header_path}
// #ifndef ${micro_name}
// #define ${micro_name}
// ${namespace ? `namespace ${namespace} {` : ''}
// class ${classname} {
// public:
// ${classname}();
// ~${classname}();
// };
// ${namespace ? `}` : ''}
// #endif // ${micro_name}
// `
// const body_content = `// ${body_path}
// #include "${header_path}"
// ${namespace ? `namespace ${namespace} {` : ''}
// ${classname}::${classname}(){
// // do somethings
// }
// ${namespace ? `}` : ''}
// `
// fs.writeFileSync(header_path, header_content, 'utf-8')
// if (create_body_flag) fs.writeFileSync(body_path, body_content, 'utf-8')
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment