diff --git a/ffs/main.cc b/ffs/main.cc index ab5175e..60e96ea 100644 --- a/ffs/main.cc +++ b/ffs/main.cc @@ -6,9 +6,12 @@ #include "starlark/token.h" #include "starlark/tokenizer.h" +#include +#include #include #include #include +#include #include #include #include @@ -27,15 +30,49 @@ std::string to_string(std::vector const &tokens) { return std::move(ss).str(); } -} // namespace +// Find the first directory w/ a MODULE.bazel above a given path. +std::optional project_root(std::filesystem::path from) { + assert(std::filesystem::is_directory(from)); -int main(int argc, char **argv) { - if (argc != 2) { - std::string_view name = argv[0] != nullptr ? argv[0] : ""; - std::cerr << "Usage: " << name << " \n"; - return 1; + do { + if (std::filesystem::exists(from / "MODULE.bazel")) { + return from; + } + + from = from.parent_path(); + std::cerr << from << std::endl; + } while (from != from.parent_path()); + + return std::nullopt; +} + +std::optional> +build_files_from_pattern(std::filesystem::path const &from, std::string_view pattern) { + if (pattern != "...") { + // TODO(robinlinden): Support patterns. + std::cerr << "Unsupported pattern '" << pattern << "'.\n"; + return std::nullopt; } + auto root_path = project_root(from); + if (!root_path) { + std::cerr << "Unable to find ffs project root for folder '" << from << "'.\n"; + return std::nullopt; + } + + std::vector found; + for (auto const &dir : std::filesystem::recursive_directory_iterator{from}) { + if (std::filesystem::exists(dir.path() / "BUILD.bazel")) { + found.push_back(dir.path() / "BUILD.bazel"); + } + } + + return found; +} + +int run_debug(int argc, char **argv) { + assert(argc == 2); + auto input = std::ifstream{argv[1]}; if (!input) { std::cerr << "Error: Could not open file " << argv[1] << "\n"; @@ -59,4 +96,40 @@ int main(int argc, char **argv) { std::cerr << "Error: Failed to parse input.\n"; return 1; } + + return 0; +} + +int run_query(int argc, char **argv) { + assert(argc == 3); + + auto cwd = std::filesystem::current_path(); + auto build_files = build_files_from_pattern(cwd, argv[2]); + if (!build_files) { + std::cerr << "Failed to find build files.\n"; + return 1; + } + + // TODO(robinlinden): Get targets from build files. + for (auto const &build_file : *build_files) { + std::cout << build_file << '\n'; + } + + return 0; +} + +} // namespace + +int main(int argc, char **argv) { + if (argc == 3 && argv[1] == std::string_view{"query"}) { + return run_query(argc, argv); + } + + if (argc != 2) { + std::string_view name = argv[0] != nullptr ? argv[0] : ""; + std::cerr << "Usage: " << name << " \n"; + return 1; + } + + return run_debug(argc, argv); }