From bafc1e740dc84855f96779ef9a92977a83a66a16 Mon Sep 17 00:00:00 2001 From: lnpotter Date: Wed, 9 Oct 2024 12:20:15 -0300 Subject: [PATCH 1/2] feat: implemented init command #31 --- src/cli/commands/init.lua | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/cli/commands/init.lua diff --git a/src/cli/commands/init.lua b/src/cli/commands/init.lua new file mode 100644 index 0000000..558fd62 --- /dev/null +++ b/src/cli/commands/init.lua @@ -0,0 +1,62 @@ +local os = require('os') + +local function create_file(filepath, content) + local file = io.open(filepath, "w") + if file then + file:write(content) + file:close() + else + print("Error while creating file: " .. filepath) + end +end + +local function create_directory(path) + local success = os.execute("mkdir " .. path) + if not success then + print("Error while creating directory: " .. path) + end +end + +local function init_project(project_name) + create_directory(project_name) + create_file(project_name .. "/.gitignore", ".DS_Store\nThumbs.db\n") + + create_directory(project_name .. "/dist") + create_directory(project_name .. "/vendor") + + create_file(project_name .. "/README.md", "# " .. project_name .. "\n\n * **use:** `./cli.sh run src/game.lua`\n") + + create_directory(project_name .. "/src") + + local game_lua_content = 'local function init(std, game)\n\nend\n\n' + ..'local function loop(std, game)\n\nend\n\n' + ..'local function draw(std, game)\n' + ..' std.draw.clear(std.color.black)\n' + ..' std.draw.color(std.color.white)\n' + ..' std.draw.text(8, 8, "hello world")\n' + ..'end\n\n' + ..'local function exit(std, game)\n\nend\n\n' + ..'local P = {\n' + ..' meta={\n' + ..' title="' .. project_name .. '",\n' + ..' author="YourName",\n' + ..' description="description about the game",\n' + ..' version="1.0.0"\n' + ..' },\n' + ..' callbacks={\n' + ..' init=init,\n' + ..' loop=loop,\n' + ..' draw=draw,\n' + ..' exit=exit\n' + ..' }\n' + ..'}\n\n' + ..'return P\n' + + create_file(project_name .. "/src/game.lua", game_lua_content) + + print("Project " .. project_name .. " created with success!") +end + +return { + init_project = init_project +} From 20deb1cb067108071f1a45a3d2b7482ec7612ec7 Mon Sep 17 00:00:00 2001 From: lnpotter Date: Wed, 9 Oct 2024 12:21:29 -0300 Subject: [PATCH 2/2] refactor: integrate init.lua in cli.lua streamlining the project init command --- src/cli/commands/cli.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cli/commands/cli.lua b/src/cli/commands/cli.lua index 9fba693..e068a1e 100644 --- a/src/cli/commands/cli.lua +++ b/src/cli/commands/cli.lua @@ -33,10 +33,16 @@ local function cli_dump(args) return zeebo_bootstrap.dump(dist) end +local function cli_init(args) + local project_name = args[1] or 'my_project' + init.init_project(project_name) +end + local P = { ['cli-build'] = cli_build, ['cli-test'] = cli_test, - ['cli-dump'] = cli_dump + ['cli-dump'] = cli_dump, + ['cli-init'] = cli_init } return P