Skip to content

Commit

Permalink
♻️refactor(fortran): Removed options that might cause trouble due to …
Browse files Browse the repository at this point in the history
…execution order. They have been replaced by the new option `Run this file`. It's still possible to run multi file projects with the option `FPM`.
  • Loading branch information
Zeioth committed May 26, 2024
1 parent ca41461 commit d0bc068
Showing 1 changed file with 20 additions and 121 deletions.
141 changes: 20 additions & 121 deletions lua/compiler/languages/fortran.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,165 +4,64 @@ local M = {}

-- Frontend - options displayed on telescope
M.options = {
{ text = "Build and run program", value = "option1" },
{ text = "Build program", value = "option2" },
{ text = "Run program", value = "option3" },
{ text = "Build solution", value = "option4" },
{ text = "", value = "separator" },
{ text = "fpm build and run", value = "option5" },
{ text = "fpm build", value = "option6" },
{ text = "fpm run", value = "option7" },
{ text = "Run this file", value = "option1" },
{ text = "FPM build and run", value = "option2" },
{ text = "FPM build", value = "option3" },
{ text = "FPM run", value = "option4" },
}

-- Backend - overseer tasks performed on option selected
function M.action(selected_option)
local utils = require("compiler.utils")
local overseer = require("overseer")
local entry_point = utils.os_path(vim.fn.getcwd() .. "/main.f90") -- working_directory/main.f90
local output_dir = utils.os_path(vim.fn.getcwd() .. "/bin/") -- working_directory/bin/
local output = utils.os_path(vim.fn.getcwd() .. "/bin/program") -- working_directory/bin/program
local arguments = "-D warnings -g" -- arguments can be overriden in .solution
local current_file = vim.fn.expand('%:p') -- current file
local output_dir = utils.os_path(vim.fn.stdpath("cache") .. "/compiler/fortran/") -- working_directory/bin/
local output = output_dir .. "program" -- working_directory/bin/program
local arguments = "" -- arguments can be overriden in .solution
local final_message = "--task finished--"

if selected_option == "option1" then
local task = overseer.new_task({
name = "- Fortran compiler",
strategy = { "orchestrator",
tasks = {{ "shell", name = "- Build & run program " .. entry_point,
tasks = {{ "shell", name = "- Run this file " .. current_file,
cmd = "rm -f " .. output .. " || true" .. -- clean
" && mkdir -p " .. output_dir .. -- mkdir
" && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile
" && gfortran " .. current_file .. " -o " .. output .. " " .. arguments .. -- compile
" && " .. output .. -- run
" && echo " .. entry_point .. -- echo
" && echo " .. current_file .. -- echo
" && echo '" .. final_message .. "'"
},},},})
task:start()
vim.cmd("OverseerOpen")
elseif selected_option == "option2" then
local task = overseer.new_task({
name = "- Fortran compiler",
strategy = { "orchestrator",
tasks = {{ "shell", name = "- Build program → " .. entry_point,
cmd = "rm -f " .. output .. " || true" .. -- clean
" && mkdir -p " .. output_dir .. -- mkdir
" && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile
" && echo " .. entry_point .. -- echo
" && echo '" .. final_message .. "'"
},},},})
task:start()
vim.cmd("OverseerOpen")
elseif selected_option == "option3" then
local task = overseer.new_task({
name = "- Fortran compiler",
strategy = { "orchestrator",
tasks = {{ "shell", name = "- Run program → " .. entry_point,
cmd = output .. -- run
" && echo " .. output .. -- echo
" && echo '" .. final_message .. "'"
},},},})
task:start()
vim.cmd("OverseerOpen")
elseif selected_option == "option4" then
local entry_points
local task = {}
local tasks = {}
local executables = {}

-- if .solution file exists in working dir
local solution_file = utils.get_solution_file()
if solution_file then
local config = utils.parse_solution_file(solution_file)

for entry, variables in pairs(config) do
if entry == "executables" then goto continue end
entry_point = utils.os_path(variables.entry_point)
output = utils.os_path(variables.output)
output_dir = utils.os_path(output:match("^(.-[/\\])[^/\\]*$"))
arguments = variables.arguments or arguments -- optional
task = { "shell", name = "- Build program → " .. entry_point,
cmd = "rm -f " .. output .. " || true" .. -- clean
" && mkdir -p " .. output_dir .. -- mkdir
" && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile
" && echo " .. entry_point .. -- echo
" && echo '" .. final_message .. "'"
}
table.insert(tasks, task) -- store all the tasks we've created
::continue::
end

local solution_executables = config["executables"]
if solution_executables then
for entry, executable in pairs(solution_executables) do
task = { "shell", name = "- Run program → " .. executable,
cmd = executable .. -- run
" && echo " .. executable .. -- echo
" && echo '" .. final_message .. "'"
}
table.insert(executables, task) -- store all the executables we've created
end
end

task = overseer.new_task({
name = "- Fortran compiler", strategy = { "orchestrator",
tasks = {
tasks, -- Build all the programs in the solution in parallel
executables -- Then run the solution executable(s)
}}})
task:start()
vim.cmd("OverseerOpen")

else -- If no .solution file
-- Create a list of all entry point files in the working directory
entry_points = utils.find_files(vim.fn.getcwd(), "main.f90")

for _, entry_point in ipairs(entry_points) do
entry_point = utils.os_path(entry_point)
output_dir = utils.os_path(entry_point:match("^(.-[/\\])[^/\\]*$") .. "bin") -- entry_point/bin
output = utils.os_path(output_dir .. "/program") -- entry_point/bin/program
task = { "shell", name = "- Build program → " .. entry_point,
cmd = "rm -f " .. output .. " || true" .. -- clean
" && mkdir -p " .. output_dir .. -- mkdir
" && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile
" && echo " .. entry_point .. -- echo
" && echo '" .. final_message .. "'"
}
table.insert(tasks, task) -- store all the tasks we've created
end

task = overseer.new_task({ -- run all tasks we've created in parallel
name = "- Fortran compiler", strategy = { "orchestrator", tasks = tasks }
})
task:start()
vim.cmd("OverseerOpen")
end
elseif selected_option == "option5" then
local task = overseer.new_task({
name = "- Fortran compiler",
strategy = { "orchestrator",
tasks = {{ "shell", name = "- fpm build & run → " .. "fpm.toml",
cmd = "fpm build " .. -- compile
" && fpm run" .. --run
" && echo '" .. final_message .. "'" -- echo
cmd = "fpm build " .. -- compile
" && fpm run" .. -- run
" && echo '" .. final_message .. "'" -- echo
},},},})
task:start()
vim.cmd("OverseerOpen")
elseif selected_option == "option6" then
elseif selected_option == "option3" then
local task = overseer.new_task({
name = "- Fortran compiler",
strategy = { "orchestrator",
tasks = {{ "shell", name = "- fpm build → " .. "fpm.toml",
cmd = "fpm build " .. -- compile
" && echo '" .. final_message .. "'" -- echo
cmd = "fpm build " .. -- compile
" && echo '" .. final_message .. "'" -- echo
},},},})
task:start()
vim.cmd("OverseerOpen")
elseif selected_option == "option7" then
elseif selected_option == "option4" then
local task = overseer.new_task({
name = "- Fortran compiler",
strategy = { "orchestrator",
tasks = {{ "shell", name = "- fpm run → " .. "fpm.toml",
cmd = "fpm run " .. -- run
" && echo '" .. final_message .. "'" -- echo
cmd = "fpm run " .. -- run
" && echo '" .. final_message .. "'" -- echo
},},},})
task:start()
vim.cmd("OverseerOpen")
Expand Down

0 comments on commit d0bc068

Please sign in to comment.