Skip to content

Commit

Permalink
Closes #57. Customization in .jsbeautifyrc file now generates the config
Browse files Browse the repository at this point in the history
(.cfg) file for Uncrustify beautification.
  • Loading branch information
Glavin001 committed Aug 8, 2014
1 parent 8fb1c8d commit 325ada7
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 55 deletions.
4 changes: 4 additions & 0 deletions examples/nested-jsbeautifyrc/.jsbeautifyrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
#max_line_length: 79
#ignore:
# - "E24"
java:
indent_class: true
indent_with_tabs: 0
indent_size: 1
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions examples/simple-jsbeautifyrc/.jsbeautifyrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
"max_preserve_newlines": 2,
"jslint_happy": true,
"indent_handlebars": true,
"object": {},
"configPath": "uncrustify.cfg"
"object": {}
}
94 changes: 53 additions & 41 deletions lib/langs/cli-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,72 @@ 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) {
console.log('Deleting output file', err);
}
});
};
// 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) {
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;
}

// 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(newText);
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('Beautifcation Error: ', err);
deleteOutputFile();
}
});
} else {
console.log('Beautify CLI command not valid.');
});
} 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);
}
}
});
Expand Down
76 changes: 76 additions & 0 deletions lib/langs/uncrustify/cfg.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
};
35 changes: 23 additions & 12 deletions lib/langs/uncrustify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 325ada7

Please sign in to comment.