Esempio n. 1
0
// Compile scss file to css
func (c *Compiler) CompileFile(path string) (string, error) {
	ctx, err := C._sass_new_file_context()
	if err != nil {
		errStr := "failed to alloc file ctx: " + err.Error()
		return "", errors.New(errStr)
	}
	defer C._sass_free_file_context(ctx)

	if err := c.fillOptions((*C.sass_options_t)(&ctx.options)); err != nil {
		return "", err
	}

	ctx.input_path = C.CString(path)

	C._sass_compile_file(ctx)
	if ctx.error_status != 0 {
		errStr := fmt.Sprintf("failed to compile file: %d: %s",
			ctx.error_status, C.GoString(ctx.error_message))
		return "", errors.New(errStr)
	}

	return C.GoString(ctx.output_string), nil
}
Esempio n. 2
0
// Compile Sass files in in srcPath to CSS in outPath
func (c *Compiler) CompileFolder(srcPath, outPath string) error {
	ctx, err := C._sass_new_file_context()
	if err != nil {
		errStr := "failed to alloc file ctx: " + err.Error()
		return errors.New(errStr)
	}
	defer C._sass_free_file_context(ctx)

	if err := c.fillOptions((*C.sass_options_t)(&ctx.options)); err != nil {
		return err
	}

	walkF := func(p string, f os.FileInfo, e error) error {
		if f.IsDir() {
			return nil
		}

		if !strings.HasSuffix(p, ".scss") {
			return nil
		}

		base := filepath.Base(p)
		if strings.HasPrefix(base, "_") {
			// skip "_*.scss". they will be imported by other.
			return nil
		}

		dir := filepath.Dir(p)
		outDir := strings.Replace(filepath.ToSlash(dir), filepath.ToSlash(srcPath), filepath.ToSlash(outPath), 1)
		if err := os.MkdirAll(outDir, 0770); err != nil {
			return err
		}

		ctx.input_path = C.CString(p)
		C._sass_compile_file(ctx)
		if ctx.error_status != 0 {
			errStr := fmt.Sprintf("failed to compile file: %d: %s",
				ctx.error_status, C.GoString(ctx.error_message))
			return errors.New(errStr)
		}

		base = strings.TrimRight(base, ".scss") + ".css"
		outPath := filepath.Join(outDir, base)
		wp, err := os.Create(outPath)
		if err != nil {
			return err
		}
		defer wp.Close()

		n, err := wp.Write([]byte(C.GoString(ctx.output_string)))
		if err != nil {
			return err
		}
		if n == 0 {
			return errors.New("nothing written to " + p)
		}

		return nil
	}

	return filepath.Walk(srcPath, walkF)
}