Blame src/file.hpp

Packit bfcc33
#ifndef SASS_FILE_H
Packit bfcc33
#define SASS_FILE_H
Packit bfcc33
Packit bfcc33
#include <string>
Packit bfcc33
#include <vector>
Packit bfcc33
Packit bfcc33
#include "sass/context.h"
Packit bfcc33
#include "ast_fwd_decl.hpp"
Packit bfcc33
Packit bfcc33
namespace Sass {
Packit bfcc33
Packit bfcc33
  namespace File {
Packit bfcc33
Packit bfcc33
    // return the current directory
Packit bfcc33
    // always with forward slashes
Packit bfcc33
    std::string get_cwd();
Packit bfcc33
Packit bfcc33
    // test if path exists and is a file
Packit bfcc33
    bool file_exists(const std::string& file);
Packit bfcc33
Packit bfcc33
    // return if given path is absolute
Packit bfcc33
    // works with *nix and windows paths
Packit bfcc33
    bool is_absolute_path(const std::string& path);
Packit bfcc33
Packit bfcc33
    // return only the directory part of path
Packit bfcc33
    std::string dir_name(const std::string& path);
Packit bfcc33
Packit bfcc33
    // return only the filename part of path
Packit bfcc33
    std::string base_name(const std::string&);
Packit bfcc33
Packit bfcc33
    // do a locigal clean up of the path
Packit bfcc33
    // no physical check on the filesystem
Packit bfcc33
    std::string make_canonical_path (std::string path);
Packit bfcc33
Packit bfcc33
    // join two path segments cleanly together
Packit bfcc33
    // but only if right side is not absolute yet
Packit bfcc33
    std::string join_paths(std::string root, std::string name);
Packit bfcc33
Packit bfcc33
    // if the relative path is outside of the cwd we want want to
Packit bfcc33
    // show the absolute path in console messages
Packit bfcc33
    std::string path_for_console(const std::string& rel_path, const std::string& abs_path, const std::string& orig_path);
Packit bfcc33
Packit bfcc33
    // create an absolute path by resolving relative paths with cwd
Packit bfcc33
    std::string rel2abs(const std::string& path, const std::string& base = ".", const std::string& cwd = get_cwd());
Packit bfcc33
Packit bfcc33
    // create a path that is relative to the given base directory
Packit bfcc33
    // path and base will first be resolved against cwd to make them absolute
Packit bfcc33
    std::string abs2rel(const std::string& path, const std::string& base = ".", const std::string& cwd = get_cwd());
Packit bfcc33
Packit bfcc33
    // helper function to resolve a filename
Packit bfcc33
    // searching without variations in all paths
Packit bfcc33
    std::string find_file(const std::string& file, struct Sass_Compiler* options);
Packit bfcc33
    std::string find_file(const std::string& file, const std::vector<std::string> paths);
Packit bfcc33
Packit bfcc33
    // helper function to resolve a include filename
Packit bfcc33
    // this has the original resolve logic for sass include
Packit bfcc33
    std::string find_include(const std::string& file, const std::vector<std::string> paths);
Packit bfcc33
Packit bfcc33
    // split a path string delimited by semicolons or colons (OS dependent)
Packit bfcc33
    std::vector<std::string> split_path_list(const char* paths);
Packit bfcc33
Packit bfcc33
    // try to load the given filename
Packit bfcc33
    // returned memory must be freed
Packit bfcc33
    // will auto convert .sass files
Packit bfcc33
    char* read_file(const std::string& file);
Packit bfcc33
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // requested import
Packit bfcc33
  class Importer {
Packit bfcc33
    public:
Packit bfcc33
      // requested import path
Packit bfcc33
      std::string imp_path;
Packit bfcc33
      // parent context path
Packit bfcc33
      std::string ctx_path;
Packit bfcc33
      // base derived from context path
Packit bfcc33
      // this really just acts as a cache
Packit bfcc33
      std::string base_path;
Packit bfcc33
    public:
Packit bfcc33
      Importer(std::string imp_path, std::string ctx_path)
Packit bfcc33
      : imp_path(File::make_canonical_path(imp_path)),
Packit bfcc33
        ctx_path(File::make_canonical_path(ctx_path)),
Packit bfcc33
        base_path(File::dir_name(ctx_path))
Packit bfcc33
      { }
Packit bfcc33
  };
Packit bfcc33
Packit bfcc33
  // a resolved include (final import)
Packit bfcc33
  class Include : public Importer {
Packit bfcc33
    public:
Packit bfcc33
      // resolved absolute path
Packit bfcc33
      std::string abs_path;
Packit bfcc33
    public:
Packit bfcc33
      Include(const Importer& imp, std::string abs_path)
Packit bfcc33
      : Importer(imp), abs_path(abs_path)
Packit bfcc33
      { }
Packit bfcc33
  };
Packit bfcc33
Packit bfcc33
  // a loaded resource
Packit bfcc33
  class Resource {
Packit bfcc33
    public:
Packit bfcc33
      // the file contents
Packit bfcc33
      char* contents;
Packit bfcc33
      // conected sourcemap
Packit bfcc33
      char* srcmap;
Packit bfcc33
    public:
Packit bfcc33
      Resource(char* contents, char* srcmap)
Packit bfcc33
      : contents(contents), srcmap(srcmap)
Packit bfcc33
      { }
Packit bfcc33
  };
Packit bfcc33
Packit bfcc33
  // parsed stylesheet from loaded resource
Packit bfcc33
  class StyleSheet : public Resource {
Packit bfcc33
    public:
Packit bfcc33
      // parsed root block
Packit bfcc33
      Block_Obj root;
Packit bfcc33
    public:
Packit bfcc33
      StyleSheet(const Resource& res, Block_Obj root)
Packit bfcc33
      : Resource(res), root(root)
Packit bfcc33
      { }
Packit bfcc33
  };
Packit bfcc33
Packit bfcc33
  namespace File {
Packit bfcc33
Packit bfcc33
    static std::vector<std::string> defaultExtensions = { ".scss", ".sass", ".css" };
Packit bfcc33
Packit bfcc33
    std::vector<Include> resolve_includes(const std::string& root, const std::string& file,
Packit bfcc33
      const std::vector<std::string>& exts = defaultExtensions);
Packit bfcc33
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
}
Packit bfcc33
Packit bfcc33
#endif