Skip to content

Commit 5fe1073

Browse files
committed
Add new flag --write-includes for tracking imported dependencies
1 parent 43c4000 commit 5fe1073

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

Diff for: sassc.c

+52-14
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,30 @@ int output(int error_status, const char* error_message, const char* output_strin
8888
}
8989
}
9090

91-
int compile_stdin(struct Sass_Options* options, char* outfile) {
91+
int write_depsfile(struct Sass_Context* ctx_out, char* deps_outfile) {
92+
size_t i;
93+
94+
FILE* fp = fopen(deps_outfile, "wb");
95+
if(!fp) {
96+
perror("Error opening deps output file");
97+
return 1;
98+
}
99+
100+
char** included_files = sass_context_get_included_files(ctx_out);
101+
size_t num_included_files = sass_context_get_included_files_size(ctx_out);
102+
for(i = 0; i < num_included_files; ++i) {
103+
if(fprintf(fp, "%s\n", included_files[i]) < 0) {
104+
perror("Error writing to deps output file");
105+
fclose(fp);
106+
return 1;
107+
}
108+
}
109+
110+
fclose(fp);
111+
return 0;
112+
}
113+
114+
int compile_stdin(struct Sass_Options* options, char* outfile, char* deps_outfile) {
92115
int ret;
93116
struct Sass_Data_Context* ctx;
94117
char buffer[BUFSIZE];
@@ -142,11 +165,16 @@ int compile_stdin(struct Sass_Options* options, char* outfile) {
142165
sass_context_get_output_string(ctx_out),
143166
outfile
144167
);
168+
169+
if (ret == 0 && deps_outfile) {
170+
ret = write_depsfile(ctx_out, deps_outfile);
171+
}
172+
145173
sass_delete_data_context(ctx);
146174
return ret;
147175
}
148176

149-
int compile_file(struct Sass_Options* options, char* input_path, char* outfile) {
177+
int compile_file(struct Sass_Options* options, char* input_path, char* outfile, char* deps_outfile) {
150178
int ret;
151179
struct Sass_File_Context* ctx = sass_make_file_context(input_path);
152180
struct Sass_Context* ctx_out = sass_file_context_get_context(ctx);
@@ -173,6 +201,10 @@ int compile_file(struct Sass_Options* options, char* input_path, char* outfile)
173201
);
174202
}
175203

204+
if (ret == 0 && deps_outfile) {
205+
ret = write_depsfile(ctx_out, deps_outfile);
206+
}
207+
176208
sass_delete_file_context(ctx);
177209
return ret;
178210
}
@@ -208,16 +240,17 @@ void print_usage(char* argv0) {
208240
printf(" %s", style_option_strings[i].style_string);
209241
printf(i == 0 ? ".\n" : ",");
210242
}
211-
printf(" -l, --line-numbers Emit comments showing original line numbers.\n");
243+
printf(" -l, --line-numbers Emit comments showing original line numbers.\n");
212244
printf(" --line-comments\n");
213-
printf(" -I, --load-path PATH Set Sass import path.\n");
214-
printf(" -P, --plugin-path PATH Set path to autoload plugins.\n");
215-
printf(" -m, --sourcemap[=TYPE] Emit source map (auto or inline).\n");
216-
printf(" -M, --omit-map-comment Omits the source map url comment.\n");
217-
printf(" -p, --precision Set the precision for numbers.\n");
218-
printf(" -a, --sass Treat input as indented syntax.\n");
219-
printf(" -v, --version Display compiled versions.\n");
220-
printf(" -h, --help Display this help message.\n");
245+
printf(" -I, --load-path PATH Set Sass import path.\n");
246+
printf(" -D, --write-includes FILE Write a file containing the dependencies that were imported.\n");
247+
printf(" -P, --plugin-path PATH Set path to autoload plugins.\n");
248+
printf(" -m, --sourcemap[=TYPE] Emit source map (auto or inline).\n");
249+
printf(" -M, --omit-map-comment Omits the source map url comment.\n");
250+
printf(" -p, --precision Set the precision for numbers.\n");
251+
printf(" -a, --sass Treat input as indented syntax.\n");
252+
printf(" -v, --version Display compiled versions.\n");
253+
printf(" -h, --help Display this help message.\n");
221254
printf("\n");
222255
}
223256

@@ -245,6 +278,7 @@ int main(int argc, char** argv) {
245278
}
246279

247280
char *outfile = 0;
281+
char *deps_outfile = 0;
248282
int from_stdin = 0;
249283
bool auto_source_map = false;
250284
bool generate_source_map = false;
@@ -259,6 +293,7 @@ int main(int argc, char** argv) {
259293
{
260294
{ "stdin", no_argument, 0, 's' },
261295
{ "load-path", required_argument, 0, 'I' },
296+
{ "write-includes", required_argument, 0, 'D' },
262297
{ "plugin-path", required_argument, 0, 'P' },
263298
{ "style", required_argument, 0, 't' },
264299
{ "line-numbers", no_argument, 0, 'l' },
@@ -271,14 +306,17 @@ int main(int argc, char** argv) {
271306
{ "help", no_argument, 0, 'h' },
272307
{ NULL, 0, NULL, 0}
273308
};
274-
while ((c = getopt_long(argc, argv, "vhslm::Map:t:I:P:", long_options, &long_index)) != -1) {
309+
while ((c = getopt_long(argc, argv, "vhslm::Map:t:I:D:P:", long_options, &long_index)) != -1) {
275310
switch (c) {
276311
case 's':
277312
from_stdin = 1;
278313
break;
279314
case 'I':
280315
sass_option_push_include_path(options, optarg);
281316
break;
317+
case 'D':
318+
deps_outfile = optarg;
319+
break;
282320
case 'P':
283321
sass_option_push_plugin_path(options, optarg);
284322
break;
@@ -367,12 +405,12 @@ int main(int argc, char** argv) {
367405
} else if (auto_source_map) {
368406
sass_option_set_source_map_embed(options, true);
369407
}
370-
result = compile_file(options, argv[optind], outfile);
408+
result = compile_file(options, argv[optind], outfile, deps_outfile);
371409
} else {
372410
if (optind < argc) {
373411
outfile = argv[optind];
374412
}
375-
result = compile_stdin(options, outfile);
413+
result = compile_stdin(options, outfile, deps_outfile);
376414
}
377415

378416
sass_delete_options(options);

0 commit comments

Comments
 (0)