From 426f37a7218f3e9152fddd912a71eae777da473f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Noal=20P=C3=B6tter?= <47451974+lnpotter@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:33:20 -0300 Subject: [PATCH 1/3] feat: add wget to http util create request #90 --- src/lib/util/http.lua | 30 ++++++++++++++++++++++++++++++ tests/test_lib_util_http.lua | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/lib/util/http.lua b/src/lib/util/http.lua index d9e9a2f..e218ad9 100644 --- a/src/lib/util/http.lua +++ b/src/lib/util/http.lua @@ -123,6 +123,36 @@ local function create_request(method, uri) return request, function() end end + self.to_wget_cmd = function () + local parts = {'wget --quiet --output-document=-'} + + if method == 'HEAD' then + table.insert(parts, '--method=HEAD') + elseif method ~= 'GET' then + table.insert(parts, '--method=' .. method) + end + + for index, header in ipairs(self.header_list) do + local value = self.header_dict[header] + if value then + local escaped_value = value:gsub('"', '\\"') + table.insert(parts, '--header="' .. header .. ': ' .. escaped_value .. '"') + end + end + + if method ~= 'GET' and method ~= 'HEAD' and #self.body_content > 0 then + local escaped_body = self.body_content:gsub('"', '\\"') + table.insert(parts, '--body-data="' .. escaped_body .. '"') + end + + table.insert(parts, uri) + + local request = table.concat(parts, ' ') + + self = nil + return request, function() end + end + return self end diff --git a/tests/test_lib_util_http.lua b/tests/test_lib_util_http.lua index e1832ee..d336114 100644 --- a/tests/test_lib_util_http.lua +++ b/tests/test_lib_util_http.lua @@ -71,4 +71,38 @@ function test_create_request_no_body_in_get() luaunit.assertEquals(request, 'GET / HTTP/1.1\r\n\r\n') end +function test_create_request_wget_get() + local request = zeebo_util_http.create_request('GET', 'http://example.com') + .add_imutable_header('Accept', 'application/json') + .to_wget_cmd() + + luaunit.assertEquals(request, 'wget --quiet --output-document=- --header="Accept: application/json" http://example.com') +end + +function test_create_request_wget_post_with_body() + local request = zeebo_util_http.create_request('POST', 'http://example.com') + .add_imutable_header('Content-Type', 'application/json') + .add_body_content('{"key": "value"}') + .to_wget_cmd() + + luaunit.assertEquals(request, 'wget --quiet --output-document=- --method=POST --header="Content-Type: application/json" --body-data="{\\"key\\": \\"value\\"}" http://example.com') +end + +function test_create_request_wget_with_headers() + local request = zeebo_util_http.create_request('PUT', 'http://example.com') + .add_imutable_header('Authorization', 'Bearer token') + .add_imutable_header('Content-Type', 'application/json') + .add_body_content('{"name": "test"}') + .to_wget_cmd() + + luaunit.assertEquals(request, 'wget --quiet --output-document=- --method=PUT --header="Authorization: Bearer token" --header="Content-Type: application/json" --body-data="{\\"name\\": \\"test\\"}" http://example.com') +end + +function test_create_request_wget_head() + local request = zeebo_util_http.create_request('HEAD', 'http://example.com') + .to_wget_cmd() + + luaunit.assertEquals(request, 'wget --quiet --output-document=- --method=HEAD http://example.com') +end + os.exit(luaunit.LuaUnit.run()) From 5d63ada5bf5afbe7b8c655568085ca18db0124b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Noal=20P=C3=B6tter?= <47451974+lnpotter@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:08:06 -0300 Subject: [PATCH 2/3] Implementation of init command (#94) * feat: implemented init command #31 * refactor: integrate init.lua in cli.lua streamlining the project init command --- src/cli/commands/cli.lua | 8 ++++- src/cli/commands/init.lua | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/cli/commands/init.lua 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 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 6335b46ca4fc9cd3fa585b5bf6609274b1ae1efc Mon Sep 17 00:00:00 2001 From: RodrigoDornelles Date: Thu, 31 Oct 2024 10:36:36 -0300 Subject: [PATCH 3/3] chore: somes adjusts in #94 --- src/cli/commands/init.lua | 51 ++++++++++++++++----------------------- src/cli/main.lua | 7 +++--- src/lib/cli/argparse.lua | 2 +- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/cli/commands/init.lua b/src/cli/commands/init.lua index 558fd62..f9571fd 100644 --- a/src/cli/commands/init.lua +++ b/src/cli/commands/init.lua @@ -1,4 +1,5 @@ local os = require('os') +local ok = true local function create_file(filepath, content) local file = io.open(filepath, "w") @@ -7,6 +8,7 @@ local function create_file(filepath, content) file:close() else print("Error while creating file: " .. filepath) + ok = false end end @@ -14,49 +16,38 @@ local function create_directory(path) local success = os.execute("mkdir " .. path) if not success then print("Error while creating directory: " .. path) + ok = false end end -local function init_project(project_name) +local function init_project(args) + local project_name = args.project + local project_template = args.template + local project_gamefile, error_gamefile = io.open(project_template, 'r') + + ok = true + + if not project_gamefile then + return false, 'cannot open template: '..project_template + end + + local game_lua_content = project_gamefile:read('*a') + create_directory(project_name) - create_file(project_name .. "/.gitignore", ".DS_Store\nThumbs.db\n") + create_file(project_name .. "/.gitignore", ".DS_Store\nThumbs.db\nvendor\ndist\ncli.lua") 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_file(project_name .. "/README.md", "# " .. project_name .. "\n\n * **use:** `lua cli.lua build 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!") + + return ok, ok and "Project " .. project_name .. " created with success!" end return { - init_project = init_project + init = init_project } diff --git a/src/cli/main.lua b/src/cli/main.lua index 94b3ee7..10c9a50 100644 --- a/src/cli/main.lua +++ b/src/cli/main.lua @@ -10,12 +10,13 @@ local commands_cli = require('src/cli/commands/cli') local commands_fs = require('src/cli/commands/fs') local commands_game = require('src/cli/commands/game') local commands_info = require('src/cli/commands/info') +local commands_init = require('src/cli/commands/init') local commands_tools = require('src/cli/commands/tools') local command = zeebo_argparse.from(arg) - .add_subcommand('init', commands_game) - .add_next_value('game', {require=true, alias='@examples/{{game}}/game.lua'}) - .add_option_get('dist', {default='{game}'}) + .add_subcommand('init', commands_init) + .add_next_value('project', {required=true}) + .add_option_get('template', {alias='@examples/{{template}}/game.lua', default='./examples/helloworld/game.lua'}) -- .add_subcommand('build', commands_build) .add_next_value('game', {alias='@examples/{{game}}/game.lua'}) diff --git a/src/lib/cli/argparse.lua b/src/lib/cli/argparse.lua index e604b19..79fc724 100644 --- a/src/lib/cli/argparse.lua +++ b/src/lib/cli/argparse.lua @@ -77,7 +77,7 @@ local function run(self, host_args) local required = self.param_dict_option_get_required[command][param] local default_value = self.param_dict_option_get_default[command][param] if alias and (value or ''):sub(1, 1) == alias:sub(1, 1) then - value = self.param_dict_value_alias[command][param]:sub(2):gsub('{{'..param..'}}', value:sub(2)) + value = alias:sub(2):gsub('{{'..param..'}}', value:sub(2)) end if required and not value then command = self.error_usage