donnieparka
Can't run IO.inspect(args) in the first main(args) line
defmodule Createproj do
@moduledoc """
Documentation for `Createproj`.
"""
def print_help do
IO.puts(
"Usage: newP" <>
" -f filepath: creates a new project\n" <>
"-b: adds boilerplate\n"
)
end
def write_boilerplate(project_name) do
file_name = project_name <> ".ex"
file_path = Path.join([project_name, file_name])
boilerplate = "defmodule #{project_name} do\nend"
File.write(file_path, boilerplate, [:append])
end
def get_filename(args) do
Enum.with_index(args)
|> Enum.find(fn {arg, index} -> arg == "-f" and index < length(args) - 1 end)
|> case do
{_, index} ->
Enum.at(args, index + 1)
{nil} ->
nil
end
end
def create_folder(project_name, file_name \\ "") do
folder_path = Path.join([project_name, file_name])
File.mkdir(folder_path)
|> case do
:ok -> IO.puts("created#{folder_path}")
{_, _} -> IO.puts("failed to make #{file_name}")
end
end
def main(args) do
IO.inspect(args)
new_file = Enum.member?(args, "-f")
boilerplate = Enum.member?(args, "-b")
cond do
new_file and not boilerplate ->
project_name = get_filename(args)
create_folder(project_name)
create_folder(project_name, "/lib")
create_folder(project_name, "/test")
new_file and boilerplate ->
project_name = get_filename(args)
create_folder(project_name)
create_folder(project_name, "/lib")
create_folder(project_name, "/test")
write_boilerplate(project_name)
true ->
print_help()
end
end
end
compilation works great but when I run:
~/projects ./createproj -f dio -b
** (MatchError) no match of right hand side value: "dio"
(createproj 0.1.0) lib/createproj.ex:33: Createproj.main/1
(elixir 1.18.1) lib/kernel/cli.ex:137: anonymous fn/3 in Kernel.CLI.exec_fun/2
If I run it from the iex like this:
Createproj.main(["-f", "cane", "-b"])
["-f", "cane", "-b"]
createdcane
createdcane/lib
createdcane/test
:ok
as you can see everything works… any idea what’s going on?
Marked As Solved
benwilson512
If this is an escript, you need to rebuild the escript after each change you make.
Also Liked
al2o3cr
Line 33 of what you posted isn’t in main and couldn’t raise a MatchError anyways:
folder_path = Path.join([project_name, file_name])
My guess is that a compiled version of Createproj is running, confusing the situation.
Eiji
I have tried this in newly generated project. I have only added escript: [main_module: Createproj] to Keyword returned in project function inside mix.exs and it works as expected. We would need something more to see where is the problem as for now I cannot reproduce it.
al2o3cr
The BEAM caches compiled bytecode to disk when it reads .ex files, so rebooting will not make a difference. Usually the advice is to remove the _build folder, but I’m not sure where that lives for escripts.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









