Skip to content

Commit

Permalink
fix: use yaml parser for flutter dependency detection in pubspec.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
sidlatau committed Sep 23, 2024
1 parent 6610090 commit 87625d0
Show file tree
Hide file tree
Showing 2 changed files with 754 additions and 6 deletions.
41 changes: 35 additions & 6 deletions lua/flutter-tools/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local job_runner = lazy.require("flutter-tools.runners.job_runner") ---@module "
local debugger_runner = lazy.require("flutter-tools.runners.debugger_runner") ---@module "flutter-tools.runners.debugger_runner"
local path = lazy.require("flutter-tools.utils.path") ---@module "flutter-tools.utils.path"
local dev_log = lazy.require("flutter-tools.log") ---@module "flutter-tools.log"
local parser = lazy.require("flutter-tools.utils.yaml_parser")

local M = {}

Expand Down Expand Up @@ -196,15 +197,43 @@ local function get_cwd(project_conf)
return lsp.get_lsp_root_dir()
end

--@return table?
local function parse_yaml(str)
local ok, yaml = pcall(parser.parse, str)
if not ok then return nil end
return yaml
end

---@param cwd string
local function has_flutter_dependency_in_pubspec(cwd)
local pubspec = vim.fn.glob(path.join(cwd, "pubspec.yaml"))
if pubspec == "" then return false end
local pubspec_content = vim.fn.readfile(pubspec)
-- As this plugin is tailored for flutter projects,
-- we assume that the project is a flutter project.
local default_has_flutter_dependency = true
local pubspec_path = vim.fn.glob(path.join(cwd, "pubspec.yaml"))
if pubspec_path == "" then return default_has_flutter_dependency end
local pubspec_content = vim.fn.readfile(pubspec_path)
local joined_content = table.concat(pubspec_content, "\n")

local flutter_dependency = string.match(joined_content, "flutter:\n[%s\t]*sdk:[%s\t]*flutter")
return flutter_dependency ~= nil
local pubspec = parse_yaml(joined_content)
if not pubspec then return default_has_flutter_dependency end
--https://github.com/Dart-Code/Dart-Code/blob/43914cd2709d77668e19a4edf3500f996d5c307b/src/shared/utils/fs.ts#L183
return (
pubspec.dependencies
and (
pubspec.dependencies.flutter
or pubspec.dependencies.flutter_test
or pubspec.dependencies.sky_engine
or pubspec.dependencies.flutter_goldens
)
)
or (
pubspec.devDependencies
and (
pubspec.devDependencies.flutter
or pubspec.devDependencies.flutter_test
or pubspec.devDependencies.sky_engine
or pubspec.devDependencies.flutter_goldens
)
)
end

---@param opts RunOpts
Expand Down
Loading

0 comments on commit 87625d0

Please sign in to comment.