Skip to content

Instantly share code, notes, and snippets.

@sclark39
Last active May 11, 2021 14:08
Show Gist options
  • Save sclark39/65efebab0c3edd0117e5a5bea9b8b5c6 to your computer and use it in GitHub Desktop.
Save sclark39/65efebab0c3edd0117e5a5bea9b8b5c6 to your computer and use it in GitHub Desktop.
Quickly rename all assets and files in GenericGraph plugin and ready it for inclusion in another plugin
'use strict';
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function listFiles(dir, filelist)
{
filelist = filelist || []
let files = fs.readdirSync(dir);
for ( let file of files )
{
let src = path.join(dir, file)
if (fs.statSync(src).isDirectory())
{
listFiles(src, filelist )
continue
}
filelist.push( src )
}
return filelist
}
function removeEmptyDir(dir)
{
try
{
fs.rmdirSync( dir )
let parentdir = dir.substring(0,dir.lastIndexOf("\\", dir.length - 2)+1)
if ( parentdir !== "" )
{
removeEmptyDir(parentdir)
}
}
catch (err)
{
return
}
}
function renameFiles(dir, map )
{
let files = listFiles(dir)
for ( let m of map )
{
m.r = new RegExp(m.old, 'g')
}
let filelist = []
for ( let file of files )
{
if (/(\.cpp|\.h|\.cs)$/.test(file))
{
let txt = fs.readFileSync( file, 'utf8' )
for ( let m of map )
{
if ( m.edit )
txt = txt.replaceAll(m.r, m.new)
}
fs.writeFileSync( file, txt )
}
let newfile = file
for ( let m of map )
{
if ( m.rename && m.r.test(newfile) )
{
newfile = newfile.replaceAll(m.r, m.new)
}
}
if ( file !== newfile )
{
let olddir = file.substring(0,file.lastIndexOf("\\")+1)
let newdir = newfile.substring(0,newfile.lastIndexOf("\\")+1)
mkdirp.sync( newdir )
fs.renameSync( file, newfile )
removeEmptyDir(olddir)
}
}
}
readline.question('What prefix should be applied?', NewPrefix =>
{
let map = [
{ old : 'GenericGraph', new : `${NewPrefix}GenericGraph`, rename : true, edit : true },
{ old : 'GENERICGRAPH', new : `${NewPrefix.toUpperCase()}GENERICGRAPH`, rename : false, edit : true},
{ old : 'AutoLayoutStrategy', new : `${NewPrefix}AutoLayoutStrategy`, rename : true, edit : true },
{ old : 'TreeLayoutStrategy', new : `${NewPrefix}TreeLayoutStrategy`, rename : true, edit : true },
{ old : 'ForceDirectedLayoutStrategy', new : `${NewPrefix}ForceDirectedLayoutStrategy`, rename : true, edit : true },
{ old : 'Blueprintable', new : ``, edit : true },
]
renameFiles( 'Source', map )
readline.close()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment