Closes #57. Customization in .jsbeautifyrc file now generates the config

(.cfg) file for Uncrustify beautification.
This commit is contained in:
Glavin Wiechert 2014-08-08 17:42:23 -04:00
parent 8fb1c8d769
commit 325ada7851
12 changed files with 159 additions and 57 deletions

View File

@ -35,3 +35,7 @@
#max_line_length: 79
#ignore:
# - "E24"
java:
indent_class: true
indent_with_tabs: 0
indent_size: 1

View File

@ -8,6 +8,5 @@
"max_preserve_newlines": 2,
"jslint_happy": true,
"indent_handlebars": true,
"object": {},
"configPath": "uncrustify.cfg"
"object": {}
}

View File

@ -21,6 +21,7 @@ module.exports = function (getCmd, isStdout) {
// Create temp output file
var outputPath = temp.path();
var deleteOutputFile = function () {
temp.cleanup();
// Delete the output path
fs.unlink(outputPath, function (err) {
if (err) {
@ -28,53 +29,64 @@ module.exports = function (getCmd, isStdout) {
}
});
};
// Get the command
var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line
if (cmd) {
var config = {
env: process.env
};
// Process the command
var processCmd = function (cmd) {
if (cmd) {
var isWin = /^win/.test(process.platform);
if (!isWin) {
// We need the $PATH to be correct when executing the command.
// This should normalize the $PATH
// by calling the external files that would usually
// change the $PATH variable on user login.
var $path =
'[ -f ~/.bash_profile ] && source ~/.bash_profile;';
$path += '[ -f ~/.bash_rc ] && source ~/.bash_rc;';
// See http://stackoverflow.com/a/638980/2578205
// for checking if file exists in Bash
cmd = $path + cmd;
}
var config = {
env: process.env
};
// Execute and beautify!
exec(cmd, config, function (err, stdout, stderr) {
console.log(stderr);
if (!err) {
// Beautification has completed
if (isStdout) {
// Execute callback with resulting output text
callback(stdout);
deleteOutputFile();
} else {
// Read contents of output file
fs.readFile(outputPath, 'utf8', function (err,
newText) {
// Execute callback with resulting output text
callback(newText);
deleteOutputFile();
});
}
} else {
console.log('Beautifcation Error: ', err);
deleteOutputFile();
var isWin = /^win/.test(process.platform);
if (!isWin) {
// We need the $PATH to be correct when executing the command.
// This should normalize the $PATH
// by calling the external files that would usually
// change the $PATH variable on user login.
var $path =
'[ -f ~/.bash_profile ] && source ~/.bash_profile;';
$path +=
'[ -f ~/.bash_rc ] && source ~/.bash_rc;';
// See http://stackoverflow.com/a/638980/2578205
// for checking if file exists in Bash
cmd = $path + cmd;
}
});
} else {
console.log('Beautify CLI command not valid.');
// Execute and beautify!
exec(cmd, config, function (err, stdout, stderr) {
// console.log(stderr);
if (!err) {
// Beautification has completed
if (isStdout) {
// Execute callback with resulting output text
callback(stdout);
deleteOutputFile();
} else {
// Read contents of output file
fs.readFile(outputPath, 'utf8', function (
err,
newText) {
// Execute callback with resulting output text
callback(newText);
deleteOutputFile();
});
}
} else {
console.log('Beautifcation Error: ', err);
deleteOutputFile();
}
});
} else {
console.log('Beautify CLI command not valid.');
}
};
// Get the command
var cmd = getCmd(info.path, outputPath, options,
processCmd); // jshint ignore: line
if (typeof cmd === 'string') {
processCmd(cmd);
}
}
});

View File

@ -0,0 +1,76 @@
/**
Requires http://pear.php.net/package/PHP_Beautifier
*/
'use strict';
var fs = require('fs');
var temp = require('temp').track();
module.exports = function (options, cb) {
var text = '';
// Apply indent_size to output_tab_size
options.output_tab_size = options.output_tab_size || options.indent_size; // jshint ignore: line
options.input_tab_size = options.input_tab_size || options.indent_size; // jshint ignore: line
delete options.indent_size; // jshint ignore: line
// Indent with Tabs?
// How to use tabs when indenting code
// 0=spaces only
// 1=indent with tabs to brace level, align with spaces
// 2=indent and align with tabs, using spaces when not on a tabstop
// jshint ignore: start
var ic = options.indent_char;
if (options.indent_with_tabs === 0 ||
options.indent_with_tabs === 1 ||
options.indent_with_tabs === 2) {
// Ignore indent_char option
} else if (ic === ' ') {
options.indent_with_tabs = 0; // Spaces only
} else if (ic === '\t') {
options.indent_with_tabs = 2; // indent and align with tabs, using spaces when not on a tabstop
} else {
options.indent_with_tabs = 1; // indent with tabs to brace level, align with spaces
}
delete options.indent_char;
// jshint ignore: end
// Remove misc
delete options.languageOverride;
delete options.configPath;
// Iterate over each property and write to configuration file
for (var k in options) {
var v = options[k];
var vs = v;
if (typeof vs === 'boolean') {
if (vs === true) {
vs = 'True';
} else {
vs = 'False';
}
}
text += k + ' = ' + vs + '\n';
}
// Create temp input file
temp.open({
suffix: '.cfg'
}, function (err, info) {
if (!err) {
// Save current text to input file
fs.write(info.fd, text || '', function (err) {
// console.log(err);
if (err) {
return cb(err);
}
fs.close(info.fd, function (err) {
// console.log(err);
if (err) {
return cb(err);
}
return cb(null, info.path);
});
});
} else {
return cb(err);
}
});
};

View File

@ -5,29 +5,40 @@ Requires http://pear.php.net/package/PHP_Beautifier
'use strict';
var cliBeautify = require('../cli-beautify');
var cfg = require('./cfg');
var path = require('path');
function getCmd(inputPath, outputPath, options) {
function getCmd(inputPath, outputPath, options, cb) {
// console.log('Uncrustify options:', options);
var configPath = options.configPath;
var lang = options.languageOverride || 'C';
function done(configPath) {
// console.log(configPath);
// Use command available in $PATH
var cmd = 'uncrustify -c "' + configPath +
'" -f "' + inputPath +
'" -o "' + outputPath +
'" -l "' + lang + '"';
// console.log(cmd);
return cb(cmd);
}
if (!configPath) {
// No custom config path
// Use Default config
configPath = path.resolve(__dirname, 'default.cfg');
cfg(options, function (error, path) {
if (error) {
throw error;
}
return done(path);
});
} else {
// Has custom config path
var editor = atom.workspace.getActiveEditor();
var basePath = path.dirname(editor.getPath());
console.log(basePath);
// console.log(basePath);
configPath = path.resolve(basePath, configPath);
done(configPath);
}
console.log(configPath);
// Use command available in $PATH
var cmd = 'uncrustify -c "' + configPath +
'" -f "' + inputPath +
'" -o "' + outputPath +
'" -l "' + lang + '"';
console.log(cmd);
return cmd;
return;
}
module.exports = cliBeautify(getCmd);