Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -MF -MP and -MT in line with gcc dependency tracking. #3087

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 89 additions & 24 deletions bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ function render() {
return;
}

if (options.depends) {
if (!output && (!options.dependsFile || !options.dependsTarget)) {
console.error("option --depends requires an output path to be specified, or --depends-file and --depends-target");
process.exit(1);
}
}

var ensureDirectory = function (filepath) {
var dir = path.dirname(filepath),
cmd,
Expand All @@ -155,15 +162,6 @@ function render() {
}
};

if (options.depends) {
if (!outputbase) {
console.error("option --depends requires an output path to be specified");
process.exitCode = 1;
return;
}
process.stdout.write(outputbase + ": ");
}

if (!sourceMapFileInline) {
var writeSourceMap = function(output, onDone) {
output = output || "";
Expand Down Expand Up @@ -196,40 +194,85 @@ function render() {
}
};

var logError = function(err) {
if (errno && errno.errno[err.errno]) {
console.error("Error: " + errno.errno[err.errno].description);
} else {
console.error("Error: " + err.code + " " + err.message);
}
}

var writeOutput = function(output, result, onSuccess) {
if (options.depends) {
if (options.noCss) {
onSuccess();
} else if (output) {
ensureDirectory(output);
fs.writeFile(output, result.css, {encoding: 'utf8'}, function (err) {
if (err) {
var description = "Error: ";
if (errno && errno.errno[err.errno]) {
description += errno.errno[err.errno].description;
} else {
description += err.code + " " + err.message;
}
console.error('lessc: failed to create file ' + output);
console.error(description);
logError(err);
process.exitCode = 1;
} else {
less.logger.info('lessc: wrote ' + output);
onSuccess();
}
});
} else if (!options.depends) {
} else {
process.stdout.write(result.css);
onSuccess();
}
};

var logDependencies = function(options, result) {
if (options.depends) {
var depends = "";
for (var i = 0; i < result.imports.length; i++) {
depends += result.imports[i] + " ";

var writeDependencies = function(stream, options, dependencies) {
var target = options.dependsTarget || output;

stream.write(target);
stream.write(':');
if (input) {
stream.write(' ');
stream.write(input);
}

for (var i = 0; i < dependencies.length; i++) {
stream.write(' ');
stream.write(dependencies[i]);
}
stream.write('\n');

if (options.dependsPhony) {
for (var i = 0; i < dependencies.length; i++) {
stream.write(dependencies[i]);
stream.write(':\n');
}
}
}

var logDependencies = function(options, result, onSuccess) {
if (!options.depends) {
return;
}

if (!options.dependsFile) {
writeDependencies(process.stdout, options, result.imports);
if (onSuccess) {
onSuccess();
}
console.log(depends);
} else {
var stream = fs.createWriteStream(options.dependsFile);
stream.on('error', function (error) {
console.error('lessc: failed to create file ' + options.dependsFile);
logError(error);
stream.close();
process.exitCode = 1;
});
stream.on('open', function() {
writeDependencies(stream, options, result.imports);
stream.end();
if (onSuccess) {
onSuccess();
}
});
}
};

Expand Down Expand Up @@ -392,6 +435,28 @@ function processPluginQueue() {
case 'depends':
options.depends = true;
break;
case 'MP':
case 'depends-phony':
options.depends = true;
options.dependsPhony = true;
break;
case 'MT':
case 'depends-target':
if (checkArgFunc(arg, match[2])) {
options.depends = true;
options.dependsTarget = match[2];
}
break;
case 'MF':
case 'depends-file':
if (checkArgFunc(arg, match[2])) {
options.depends = true;
options.dependsFile = match[2];
}
break;
case 'no-css':
options.noCss = true;
break;
case 'max-line-len':
if (checkArgFunc(arg, match[2])) {
options.maxLineLen = parseInt(match[2], 10);
Expand Down
5 changes: 5 additions & 0 deletions lib/less-node/lessc-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ var lessc_helper = {
console.log(" -h, --help Prints help (this message) and exit.");
console.log(" --include-path=PATHS Sets include paths. Separated by `:'. `;' also supported on windows.");
console.log(" -M, --depends Outputs a makefile import dependency list to stdout.");
console.log(" -MP --depends-phony Adds phony targets for imported files in the generated dependencies.");
console.log(" This avoids errors from make if imports are deleted. Implies --depends.");
console.log(" -MT --depends-target Changes the target of the generated make dependencies. Implies --depends.");
console.log(" -MF --depends-file Writes the makefile depdencies to a file instead of stdout. Implies --depends.");
console.log(" --no-css Disables normal CSS output (for example to generate --depends only).");
console.log(" --no-color Disables colorized output.");
console.log(" --ie-compat Enables IE8 compatibility checks.");
console.log(" --js Enables inline JavaScript in less files");
Expand Down
14 changes: 13 additions & 1 deletion lib/less/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ module.exports = function() {
/* Outputs a makefile import dependency list to stdout. */
depends: false,

/* Outputs phony targets for dependencies, like gcc -MP. */
dependsPhony: false,

/* Set the target of the rule in the generated dependencies, null means real output. */
dependsTarget: null,

/* Write generated dependency information to a file instead of stdout. */
dependsFile: null,

/* Suppress regular CSS output. */
noCss: false,

/* Compress using less built-in compression.
* This does an okay job but does not utilise all the tricks of
* dedicated css compression. */
Expand Down Expand Up @@ -62,4 +74,4 @@ module.exports = function() {
/* This option allows you to specify a argument to go on to every URL. */
urlArgs: ''
}
}
}