Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cmd init #107

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/cli/commands/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 53 additions & 0 deletions src/cli/commands/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local os = require('os')
local ok = true

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)
ok = false
end
end

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(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\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:** `lua cli.lua build src/game.lua`\n")

create_directory(project_name .. "/src")

create_file(project_name .. "/src/game.lua", game_lua_content)

return ok, ok and "Project " .. project_name .. " created with success!"
end

return {
init = init_project
}
7 changes: 4 additions & 3 deletions src/cli/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cli/argparse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/lib/util/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions tests/test_lib_util_http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Loading