LANG: added javascript init

This commit is contained in:
Andrew Haynes
2026-03-29 00:27:54 -04:00
parent 62015919dd
commit d99dc30dcd
+159 -140
View File
@@ -3,9 +3,9 @@ local utils = require("Polydev.utils")
local M = {} local M = {}
local function extras(guard, file_type, file_name) local function extras(guard, file_type, file_name)
if not file_name then return print("Header file creation canceled") end if not file_name then return print("Header file creation canceled") end
local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. guard local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. guard
local content = string.format([[ local content = string.format([[
#ifndef %s #ifndef %s
#define %s #define %s
@@ -13,56 +13,67 @@ local function extras(guard, file_type, file_name)
#endif #endif
]], guard_macro, guard_macro) ]], guard_macro, guard_macro)
utils.write_file({ utils.get_project_root(), "include", file_name .. file_type }, content) utils.write_file({ utils.get_project_root(), "include", file_name .. file_type }, content)
end end
M.projects = { M.projects = {
lua = { javascript = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.mkdir(full_project_root .. "/lua/" .. project_name, "p") utils.write_file({ full_project_root, "main.js" }, [[
utils.write_file({ full_project_root, "lua", project_name, "init.lua" }, [[ console.log("Hello World!");
]])
local gitignore = { "" }
utils.init_git(full_project_root, gitignore)
vim.cmd("edit " .. full_project_root .. "main.js")
end
},
lua = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.mkdir(full_project_root .. "/lua/" .. project_name, "p")
utils.write_file({ full_project_root, "lua", project_name, "init.lua" }, [[
local M = {} local M = {}
return M return M
]]) ]])
local gitignore = { "" } local gitignore = { "" }
utils.init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.cmd("edit " .. full_project_root .. "/lua/" .. project_name .. "/init.lua") vim.cmd("edit " .. full_project_root .. "/lua/" .. project_name .. "/init.lua")
end end
}, },
java = { java = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(full_project_root .. path, "p") end for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(full_project_root .. path, "p") end
local content = [[ local content = [[
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello, World!"); System.out.println("Hello, World!");
} }
} }
]] ]]
local gitignore = { local gitignore = {
"build/" "build/"
} }
utils.init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
utils.write_file({ full_project_root, "src", "Main.java" }, content) utils.write_file({ full_project_root, "src", "Main.java" }, content)
end end
}, },
python = { python = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.mkdir(full_project_root .. "/include", "p") vim.fn.mkdir(full_project_root .. "/include", "p")
utils.write_file({ full_project_root, "main.py" }, [[ utils.write_file({ full_project_root, "main.py" }, [[
def main(): def main():
print("Hello, World!") print("Hello, World!")
if __name__ == "__main__": if __name__ == "__main__":
main() main()
]]) ]])
utils.write_file({ full_project_root, "include/__init__.py" }, "") utils.write_file({ full_project_root, "include/__init__.py" }, "")
utils.write_file({ full_project_root, "requirements.txt" }, "") utils.write_file({ full_project_root, "requirements.txt" }, "")
utils.write_file({ full_project_root, "setup.py" }, string.format([[ utils.write_file({ full_project_root, "setup.py" }, string.format([[
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup( setup(
@@ -73,24 +84,27 @@ setup(
python_requires=">=3.6", python_requires=">=3.6",
) )
]], project_name)) ]], project_name))
local gitignore = { local gitignore = {
"venv/" "venv/"
} }
utils.init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.cmd("edit " .. full_project_root .. "/main.py") vim.cmd("edit " .. full_project_root .. "/main.py")
local venv_path = full_project_root .. "/venv" local venv_path = full_project_root .. "/venv"
vim.fn.system("python3 -m venv " .. venv_path) vim.fn.system("python3 -m venv " .. venv_path)
vim.g.python3_host_prog = venv_path .. "/bin/python" vim.g.python3_host_prog = venv_path .. "/bin/python"
vim.fn.setenv("VIRTUAL_ENV", venv_path) vim.fn.setenv("VIRTUAL_ENV", venv_path)
vim.fn.setenv("PATH", venv_path .. "/bin:" .. vim.fn.getenv("PATH")) vim.fn.setenv("PATH", venv_path .. "/bin:" .. vim.fn.getenv("PATH"))
print("Virtual environment activated for " .. project_name) print("Virtual environment activated for " .. project_name)
end end
}, },
c = { c = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build", "/include" }) do vim.fn.mkdir(full_project_root .. path, "p") end for _, path in ipairs({ "/src", "/build", "/include" }) do
utils.write_file({ full_project_root, ".clang-format" }, [[ vim.fn.mkdir(
full_project_root .. path, "p")
end
utils.write_file({ full_project_root, ".clang-format" }, [[
--- ---
BasedOnStyle: Google BasedOnStyle: Google
Standard: Latest Standard: Latest
@@ -111,7 +125,7 @@ AllowShortLoopsOnASingleLine: true
IndentAccessModifiers: false # Indent access specifiers by one level IndentAccessModifiers: false # Indent access specifiers by one level
AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level
]]) ]])
utils.write_file({ full_project_root, "src/main.c" }, [[ utils.write_file({ full_project_root, "src/main.c" }, [[
#include <stdio.h> #include <stdio.h>
int main() { int main() {
@@ -120,7 +134,7 @@ int main() {
} }
]]) ]])
utils.write_file({ full_project_root, ".clang-tidy" }, string.format([[ utils.write_file({ full_project_root, ".clang-tidy" }, string.format([[
--- ---
Checks: > Checks: >
-*, -*,
@@ -143,7 +157,7 @@ CheckOptions:
value: lower_case value: lower_case
--- ---
]])) ]]))
utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[ utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)
@@ -157,19 +171,21 @@ set(SOURCES
add_executable(%s ${SOURCES}) add_executable(%s ${SOURCES})
]], project_name, project_name, project_name)) ]], project_name, project_name, project_name))
local gitignore = { "build/", ".cache/" } local gitignore = { "build/", ".cache/" }
utils.init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.fn.system(string.format("cd %s/build && cmake -S .. -B . && cmake --build . && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..", full_project_root)) vim.fn.system(string.format(
vim.cmd("edit " .. full_project_root .. "/src/main.c") "cd %s/build && cmake -S .. -B . && cmake --build . && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..",
end full_project_root))
}, vim.cmd("edit " .. full_project_root .. "/src/main.c")
cpp = { end
run = function (project_name, project_root) },
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name cpp = {
for _, path in ipairs({ "/src", "/build", "/include" }) do run = function(project_name, project_root)
vim.fn.mkdir(full_project_root .. path, "p") local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
end for _, path in ipairs({ "/src", "/build", "/include" }) do
utils.write_file({ full_project_root, ".clang-format" }, [[ vim.fn.mkdir(full_project_root .. path, "p")
end
utils.write_file({ full_project_root, ".clang-format" }, [[
--- ---
BasedOnStyle: Google BasedOnStyle: Google
Standard: Latest Standard: Latest
@@ -191,7 +207,7 @@ IndentAccessModifiers: false # Indent access specifiers by one level
AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level
]]) ]])
utils.write_file({ full_project_root, "src/main.cpp" }, [[ utils.write_file({ full_project_root, "src/main.cpp" }, [[
#include <iostream> #include <iostream>
int main() { int main() {
@@ -200,7 +216,7 @@ int main() {
} }
]]) ]])
utils.write_file({ full_project_root, ".clang-tidy" }, string.format([[ utils.write_file({ full_project_root, ".clang-tidy" }, string.format([[
--- ---
Checks: > Checks: >
-*, -*,
@@ -224,7 +240,7 @@ CheckOptions:
--- ---
]])) ]]))
utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[ utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -240,26 +256,28 @@ set(SOURCES
add_executable(%s ${SOURCES}) add_executable(%s ${SOURCES})
]], project_name, project_name)) ]], project_name, project_name))
local gitignore = { "build/", ".cache/" } local gitignore = { "build/", ".cache/" }
utils.init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.fn.system(string.format("cd %s/build && cmake -S .. -B . && cmake --build . && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..", full_project_root)) vim.fn.system(string.format(
vim.cmd("edit " .. full_project_root .. "/src/main.cpp") "cd %s/build && cmake -S .. -B . && cmake --build . && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..",
end full_project_root))
}, vim.cmd("edit " .. full_project_root .. "/src/main.cpp")
rust = { end
run = function (project_name, project_root) },
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name rust = {
vim.fn.system("cargo new " .. full_project_root) run = function(project_name, project_root)
vim.cmd("edit " .. full_project_root .. "/src/main.rs") local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
end vim.fn.system("cargo new " .. full_project_root)
}, vim.cmd("edit " .. full_project_root .. "/src/main.rs")
html = { end
run = function(project_name, project_root) },
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name html = {
if vim.fn.isdirectory(full_project_root) == 0 then run = function(project_name, project_root)
vim.fn.mkdir(full_project_root, "p") -- Create directory with parent directories local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
end if vim.fn.isdirectory(full_project_root) == 0 then
local main_html_content = [[ vim.fn.mkdir(full_project_root, "p") -- Create directory with parent directories
end
local main_html_content = [[
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@@ -273,50 +291,51 @@ add_executable(%s ${SOURCES})
</body> </body>
</html> </html>
]] ]]
utils.write_file({ full_project_root, "index.html" }, main_html_content) utils.write_file({ full_project_root, "index.html" }, main_html_content)
end end
}, },
} }
M.files = { M.files = {
lua = { lua = {
run = function (file_name) run = function(file_name)
utils.write_file({ utils.get_project_root(), "lua", utils.get_project_name(), file_name .. ".lua"}, "") utils.write_file(
end { utils.get_project_root(), "lua", utils.get_project_name(), file_name .. ".lua" }, "")
}, end
java = { },
run = function (file_name) java = {
utils.write_file({ utils.get_project_root(), "src", file_name .. ".java" }, string.format([[ run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".java" }, string.format([[
public class %s { public class %s {
// New File // New File
} }
]], file_name)) ]], file_name))
end end
}, },
python = { python = {
run = function (file_name) run = function(file_name)
utils.write_file({ utils.get_project_root(), "include", file_name .. ".py" }, "") utils.write_file({ utils.get_project_root(), "include", file_name .. ".py" }, "")
end end
}, },
c = { c = {
run = function (file_name) run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".c" }, "") utils.write_file({ utils.get_project_root(), "src", file_name .. ".c" }, "")
end end
}, },
cpp = { cpp = {
run = function (file_name) run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".cpp" }, "") utils.write_file({ utils.get_project_root(), "src", file_name .. ".cpp" }, "")
end end
}, },
rust = { rust = {
run = function (file_name) run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".rs" }, "") utils.write_file({ utils.get_project_root(), "src", file_name .. ".rs" }, "")
end end
}, },
html = { html = {
run = function (file_name) run = function(file_name)
utils.write_file({ ".", file_name .. ".html" }, string.format([[ utils.write_file({ ".", file_name .. ".html" }, string.format([[
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@@ -330,22 +349,22 @@ public class %s {
</body> </body>
</html> </html>
]])) ]]))
end end
} }
} }
M.extras = { M.extras = {
run = function (lang, file_name) run = function(lang, file_name)
if lang == "c" then if lang == "c" then
extras("_H", ".h", file_name) extras("_H", ".h", file_name)
print("Successful creation of " .. file_name .. ".h") print("Successful creation of " .. file_name .. ".h")
elseif lang == "cpp" then elseif lang == "cpp" then
extras("_HPP", ".hpp", file_name) extras("_HPP", ".hpp", file_name)
print("Successful creation of " .. file_name .. ".hpp") print("Successful creation of " .. file_name .. ".hpp")
else else
print("Extras language is not found: enter cpp or c") print("Extras language is not found: enter cpp or c")
end end
end end
} }
return M return M