Skip to content

Commit

Permalink
fix: resolve wrong project name resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
oclay1st committed Oct 9, 2024
1 parent 4d68a3d commit 760632d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lua/gradle/parsers/settings_gradle_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local open = context_manager.open

---@class SettingGradleParser
local SettingGradleParser = {}
local project_name_pattern = "rootProject%.name%s*=%s*[%'" .. '%"' .. "](%w+)[%'" .. '%"]'
local project_name_pattern = "rootProject%.name%s*=%s*[%'" .. '%"' .. "](.-)[%'" .. '%"]'
local sub_project_pattern = "include%s*%(?[%'" .. '%"' .. "](.+)[%'" .. '%"]'

SettingGradleParser.parse_file = function(settings_gradle_path)
Expand Down
9 changes: 5 additions & 4 deletions lua/gradle/sources/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ local function create_project_from_build_file(build_gradle_path)
local build_gradle_file = Path:new(build_gradle_path)
local build_gradle_parent = build_gradle_file:parent()
local project_path = build_gradle_parent:absolute()
local project_name = string.match(project_path, '%' .. Path.path.sep .. '(%w+)$')
local path_parts = Utils.split_path(project_path)
local project_name = path_parts[#path_parts]
local project = Project.new(project_path, project_name, build_gradle_path)
project:set_commands(custom_commands)
return project
Expand All @@ -43,7 +44,6 @@ local function create_project_from_settings_file(settings_gradle_path)
local settings_gradle_parent = settings_gradle_file:parent()
local project_path = settings_gradle_parent:absolute()
local setting = SettingsParser.parse_file(settings_gradle_file:absolute())
-- vim.print(setting)
local project = Project.new(project_path, setting.project_name, nil, settings_gradle_path)
project:set_commands(custom_commands)
for _, sub_project_name in ipairs(setting.sub_projects_names) do
Expand All @@ -67,8 +67,9 @@ end
---@param projects Project[]
---@return Project | nil
local function find_project(file_path, projects)
local _root_path = Path:new(file_path):parent():absolute()
for _, item in ipairs(projects) do
if item.build_gradle_path == file_path or item.settings_gradle_path == file_path then
if item.root_path == _root_path then
return item
end
end
Expand All @@ -84,7 +85,7 @@ M.scan_projects = function(base_path)
scan.scan_dir(base_path, {
search_pattern = { build_gradle_file_pattern, settings_gradle_file_pattern },
depth = 10,
on_insert = function(gradle_file_path, _)
on_insert = function(gradle_file_path)
if not vim.tbl_contains(scanned_path_list, gradle_file_path) then
local project = find_project(gradle_file_path, projects)
if string.match(gradle_file_path, build_gradle_file_pattern) then
Expand Down
10 changes: 10 additions & 0 deletions lua/gradle/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Path = require('plenary.path')
local random = math.random
local M = {}

Expand All @@ -6,6 +7,15 @@ M.SUCCEED_STATE = 'SUCCEED'
M.FAILED_STATE = 'FAILED'
M.PENDING_STATE = 'PENDING'

M.split_path = function(filepath)
local formatted = string.format('([^%s]+)', Path.path.sep)
local t = {}
for str in string.gmatch(filepath, formatted) do
table.insert(t, str)
end
return t
end

M.uuid = function()
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function(c)
Expand Down

0 comments on commit 760632d

Please sign in to comment.