|
| 1 | +--- Fortran language actions |
| 2 | + |
| 3 | +local M = {} |
| 4 | + |
| 5 | +-- Frontend - options displayed on telescope |
| 6 | +M.options = { |
| 7 | + { text = "Build and run program", value = "option1" }, |
| 8 | + { text = "Build program", value = "option2" }, |
| 9 | + { text = "Run program", value = "option3" }, |
| 10 | + { text = "Build solution", value = "option4" }, |
| 11 | + { text = "", value = "separator" }, |
| 12 | + { text = "fpm build and run", value = "option5" }, |
| 13 | + { text = "fpm build", value = "option6" }, |
| 14 | + { text = "fpm run", value = "option7" }, |
| 15 | +} |
| 16 | + |
| 17 | +-- Backend - overseer tasks performed on option selected |
| 18 | +function M.action(selected_option) |
| 19 | + local utils = require("compiler.utils") |
| 20 | + local overseer = require("overseer") |
| 21 | + local entry_point = utils.os_path(vim.fn.getcwd() .. "/main.f90") -- working_directory/main.f90 |
| 22 | + local output_dir = utils.os_path(vim.fn.getcwd() .. "/bin/") -- working_directory/bin/ |
| 23 | + local output = utils.os_path(vim.fn.getcwd() .. "/bin/program") -- working_directory/bin/program |
| 24 | + local arguments = "-D warnings -g" -- arguments can be overriden in .solution |
| 25 | + local final_message = "--task finished--" |
| 26 | + |
| 27 | + if selected_option == "option1" then |
| 28 | + local task = overseer.new_task({ |
| 29 | + name = "- Fortran compiler", |
| 30 | + strategy = { "orchestrator", |
| 31 | + tasks = {{ "shell", name = "- Build & run program → " .. entry_point, |
| 32 | + cmd = "rm -f " .. output .. " || true" .. -- clean |
| 33 | + " && mkdir -p " .. output_dir .. -- mkdir |
| 34 | + " && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile |
| 35 | + " && " .. output .. -- run |
| 36 | + " && echo " .. entry_point .. -- echo |
| 37 | + " && echo '" .. final_message .. "'" |
| 38 | + },},},}) |
| 39 | + task:start() |
| 40 | + vim.cmd("OverseerOpen") |
| 41 | + elseif selected_option == "option2" then |
| 42 | + local task = overseer.new_task({ |
| 43 | + name = "- Fortran compiler", |
| 44 | + strategy = { "orchestrator", |
| 45 | + tasks = {{ "shell", name = "- Build program → " .. entry_point, |
| 46 | + cmd = "rm -f " .. output .. " || true" .. -- clean |
| 47 | + " && mkdir -p " .. output_dir .. -- mkdir |
| 48 | + " && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile |
| 49 | + " && echo " .. entry_point .. -- echo |
| 50 | + " && echo '" .. final_message .. "'" |
| 51 | + },},},}) |
| 52 | + task:start() |
| 53 | + vim.cmd("OverseerOpen") |
| 54 | + elseif selected_option == "option3" then |
| 55 | + local task = overseer.new_task({ |
| 56 | + name = "- Fortran compiler", |
| 57 | + strategy = { "orchestrator", |
| 58 | + tasks = {{ "shell", name = "- Run program → " .. entry_point, |
| 59 | + cmd = output .. -- run |
| 60 | + " && echo " .. output .. -- echo |
| 61 | + " && echo '" .. final_message .. "'" |
| 62 | + },},},}) |
| 63 | + task:start() |
| 64 | + vim.cmd("OverseerOpen") |
| 65 | + elseif selected_option == "option4" then |
| 66 | + local entry_points |
| 67 | + local task = {} |
| 68 | + local tasks = {} |
| 69 | + local executables = {} |
| 70 | + |
| 71 | + -- if .solution file exists in working dir |
| 72 | + local solution_file = utils.get_solution_file() |
| 73 | + if solution_file then |
| 74 | + local config = utils.parse_solution_file(solution_file) |
| 75 | + |
| 76 | + for entry, variables in pairs(config) do |
| 77 | + if entry == "executables" then goto continue end |
| 78 | + entry_point = utils.os_path(variables.entry_point) |
| 79 | + output = utils.os_path(variables.output) |
| 80 | + output_dir = utils.os_path(output:match("^(.-[/\\])[^/\\]*$")) |
| 81 | + arguments = variables.arguments or arguments -- optional |
| 82 | + task = { "shell", name = "- Build program → " .. entry_point, |
| 83 | + cmd = "rm -f " .. output .. " || true" .. -- clean |
| 84 | + " && mkdir -p " .. output_dir .. -- mkdir |
| 85 | + " && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile |
| 86 | + " && echo " .. entry_point .. -- echo |
| 87 | + " && echo '" .. final_message .. "'" |
| 88 | + } |
| 89 | + table.insert(tasks, task) -- store all the tasks we've created |
| 90 | + ::continue:: |
| 91 | + end |
| 92 | + |
| 93 | + local solution_executables = config["executables"] |
| 94 | + if solution_executables then |
| 95 | + for entry, executable in pairs(solution_executables) do |
| 96 | + task = { "shell", name = "- Run program → " .. executable, |
| 97 | + cmd = executable .. -- run |
| 98 | + " && echo " .. executable .. -- echo |
| 99 | + " && echo '" .. final_message .. "'" |
| 100 | + } |
| 101 | + table.insert(executables, task) -- store all the executables we've created |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + task = overseer.new_task({ |
| 106 | + name = "- Fortran compiler", strategy = { "orchestrator", |
| 107 | + tasks = { |
| 108 | + tasks, -- Build all the programs in the solution in parallel |
| 109 | + executables -- Then run the solution executable(s) |
| 110 | + }}}) |
| 111 | + task:start() |
| 112 | + vim.cmd("OverseerOpen") |
| 113 | + |
| 114 | + else -- If no .solution file |
| 115 | + -- Create a list of all entry point files in the working directory |
| 116 | + entry_points = utils.find_files(vim.fn.getcwd(), "main.f90") |
| 117 | + |
| 118 | + for _, entry_point in ipairs(entry_points) do |
| 119 | + entry_point = utils.os_path(entry_point) |
| 120 | + output_dir = utils.os_path(entry_point:match("^(.-[/\\])[^/\\]*$") .. "bin") -- entry_point/bin |
| 121 | + output = utils.os_path(output_dir .. "/program") -- entry_point/bin/program |
| 122 | + task = { "shell", name = "- Build program → " .. entry_point, |
| 123 | + cmd = "rm -f " .. output .. " || true" .. -- clean |
| 124 | + " && mkdir -p " .. output_dir .. -- mkdir |
| 125 | + " && gfortran " .. entry_point .. " -o " .. output .. " " .. arguments .. -- compile |
| 126 | + " && echo " .. entry_point .. -- echo |
| 127 | + " && echo '" .. final_message .. "'" |
| 128 | + } |
| 129 | + table.insert(tasks, task) -- store all the tasks we've created |
| 130 | + end |
| 131 | + |
| 132 | + task = overseer.new_task({ -- run all tasks we've created in parallel |
| 133 | + name = "- Fortran compiler", strategy = { "orchestrator", tasks = tasks } |
| 134 | + }) |
| 135 | + task:start() |
| 136 | + vim.cmd("OverseerOpen") |
| 137 | + end |
| 138 | + elseif selected_option == "option5" then |
| 139 | + local task = overseer.new_task({ |
| 140 | + name = "- Fortran compiler", |
| 141 | + strategy = { "orchestrator", |
| 142 | + tasks = {{ "shell", name = "- fpm build & run → " .. "fpm.toml", |
| 143 | + cmd = "fpm build " .. -- compile |
| 144 | + " && fpm run" .. --run |
| 145 | + " && echo '" .. final_message .. "'" -- echo |
| 146 | + },},},}) |
| 147 | + task:start() |
| 148 | + vim.cmd("OverseerOpen") |
| 149 | + elseif selected_option == "option6" then |
| 150 | + local task = overseer.new_task({ |
| 151 | + name = "- Fortran compiler", |
| 152 | + strategy = { "orchestrator", |
| 153 | + tasks = {{ "shell", name = "- fpm build → " .. "fpm.toml", |
| 154 | + cmd = "fpm build " .. -- compile |
| 155 | + " && echo '" .. final_message .. "'" -- echo |
| 156 | + },},},}) |
| 157 | + task:start() |
| 158 | + vim.cmd("OverseerOpen") |
| 159 | + elseif selected_option == "option7" then |
| 160 | + local task = overseer.new_task({ |
| 161 | + name = "- Fortran compiler", |
| 162 | + strategy = { "orchestrator", |
| 163 | + tasks = {{ "shell", name = "- fpm run → " .. "fpm.toml", |
| 164 | + cmd = "fpm run " .. -- run |
| 165 | + " && echo '" .. final_message .. "'" -- echo |
| 166 | + },},},}) |
| 167 | + task:start() |
| 168 | + vim.cmd("OverseerOpen") |
| 169 | + end |
| 170 | +end |
| 171 | + |
| 172 | +return M |
| 173 | + |
0 commit comments