Skip to content

Commit 1dc03c1

Browse files
committed
Add initial implementation (and test case)
1 parent 190e175 commit 1dc03c1

File tree

13 files changed

+633
-15
lines changed

13 files changed

+633
-15
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ set(LIBSASS_LIBRARY_DIR "${LIBSASS_DIR}/lib" CACHE FILEPATH "Path to libsass lib
1818
set(LIBSASS_INCLUDE_DIR "${LIBSASS_DIR}/include" CACHE FILEPATH "Path to libsass includes")
1919

2020
link_directories("${LIBSASS_LIBRARY_DIR}" "${LIBSASS_DIR}")
21-
include_directories("${LIBSASS_INCLUDE_DIR}" "${LIBSASS_DIR}")
21+
include_directories("${LIBSASS_INCLUDE_DIR}" "${LIBSASS_DIR}" "vendor")
2222

23-
add_library(glob SHARED src/glob.cpp)
23+
add_library(glob SHARED vendor/FS.cpp src/glob.cpp)
2424

2525
find_library(SASS_LIBRARY NAMES sass
2626
HINTS "${CMAKE_CURRENT_SOURCE_DIR}/../lib"
2727
)
2828

29-
target_include_directories(glob PUBLIC ../include)
29+
target_include_directories(glob PUBLIC ../include ../boost)
3030
target_link_libraries(glob LINK_PUBLIC sass)

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
This is no implementation and only demo functionality!
2-
This serves only as a base for such an implementation!
3-
41
# Libsass Glob Plugin
52

63
Native libsass plugin for glob-based imports

src/cli.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include "FS.hpp"
4+
5+
// implement simple command line tool
6+
int main(int argc, char** argv)
7+
{
8+
9+
// unquote all arguments
10+
for (size_t i = 1; i < argc; i += 1) {
11+
// very dumb unquote function
12+
size_t len = strlen(argv[i]) - 1;
13+
// check if start and end matches single/double quote
14+
bool quoted = (argv[i][0] == '"' && argv[i][len] == '"')
15+
|| (argv[i][0] == '\'' && argv[i][len] == '\'');
16+
// and remove quotes from both sides
17+
if (quoted) { argv[i][len] = 0; argv[i] += 1; }
18+
// check if start and end matches double quote
19+
if (argv[i][0] == '"' && argv[i][len] == '"') {
20+
// remove from both sides
21+
argv[i][len] = 0; argv[i] += 1;
22+
}
23+
}
24+
25+
// get the only supported argument
26+
// this is the pattern to match against
27+
// mostly quoted to avoid shell expansion
28+
const char* arg = argc == 1 ? "*" : argv[1];
29+
// std::cerr << "search for " << arg << std::endl;
30+
31+
// instantiate the matcher instance
32+
FS::Match* matcher = new FS::Match(arg);
33+
34+
// get vector of matches (results are cached)
35+
const std::vector<FS::Entry*> matches = matcher->getMatches();
36+
37+
// iterate over the list and print out the results
38+
std::vector<FS::Entry*>::const_iterator it = matches.begin();
39+
std::vector<FS::Entry*>::const_iterator end = matches.end();
40+
while (it != end) std::cout << (*it++)->path() << std::endl;
41+
42+
}

src/glob.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,60 @@
11
#include <cstring>
2+
#include <cstdlib>
23
#include <iostream>
34
#include <stdint.h>
5+
#include <string>
6+
#include "FS.hpp"
47
#include "sass.h"
8+
#include <libgen.h>
59

610
// return version of libsass we are linked against
711
extern "C" const char* ADDCALL libsass_get_version() {
812
return libsass_version();
913
}
1014

11-
// create a custom header to define to variables
15+
// create a custom importer to resolve glob-based includes
1216
Sass_Import_List glob_importer(const char* cur_path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
1317
{
14-
std::cerr << "import globs [" << cur_path << "]\n";
18+
19+
// get the base directory from previous import
20+
Sass_Import_Entry imp = sass_compiler_get_last_import(comp);
21+
char* prev = strdup(sass_import_get_abs_path(imp));
22+
std::string pattern(dirname(prev)); std::free(prev);
23+
pattern += std::string("/") + cur_path;
24+
25+
// instantiate the matcher instance
26+
FS::Match* matcher = new FS::Match(pattern);
27+
// get vector of matches (results are cached)
28+
const std::vector<FS::Entry*> matches = matcher->getMatches();
29+
1530
// get the cookie from importer descriptor
1631
// void* cookie = sass_importer_get_cookie(cb);
1732
// create a list to hold our import entries
18-
Sass_Import_List incs = sass_make_import_list(3);
19-
// create the resolved import entries (paths to be loaded)
20-
incs[0] = sass_make_import("foo.scss", "foo.scss", 0, 0);
21-
incs[1] = sass_make_import("bar.scss", "bar.scss", 0, 0);
22-
incs[2] = sass_make_import("baz.scss", "baz.scss", 0, 0);
33+
Sass_Import_List incs = sass_make_import_list(matches.size());
34+
35+
// iterate over the list and print out the results
36+
std::vector<FS::Entry*>::const_iterator it = matches.begin();
37+
std::vector<FS::Entry*>::const_iterator end = matches.end();
38+
39+
// attach import entry for each match
40+
size_t i = 0; while (i < matches.size()) {
41+
// create intermediate string object
42+
std::string path(matches[i]->path());
43+
// create the resolved import entries (paths to be loaded)
44+
incs[i ++ ] = sass_make_import(path.c_str(), path.c_str(), 0, 0);
45+
}
46+
2347
// return imports
2448
return incs;
49+
2550
}
2651

27-
// entry point for libsass to request custom headers from plugin
52+
// entry point for libsass to request custom importers from plugin
2853
extern "C" Sass_Importer_List ADDCALL libsass_load_importers()
2954
{
3055
// allocate a custom function caller
3156
Sass_Importer_Entry c_header =
32-
sass_make_importer(glob_importer, 3000, (void*) 0);
57+
sass_make_importer(glob_importer, 3000, (void*) 0);
3358
// create list of all custom functions
3459
Sass_Importer_List imp_list = sass_make_importer_list(1);
3560
// put the only function in this plugin to the list

test/lib1.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "lib1/**/*.scss"

test/lib1/footer.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.lib1 { footer: true; }

test/lib1/header.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.lib1 { header: true; }

test/lib2.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "lib2/**/*.scss"

test/lib2/footer.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.lib2 { footer: true; }

test/lib2/header.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.lib2 { header: true; }

0 commit comments

Comments
 (0)