From 707f55124aaf19e490c8fbc853f132d79fd487f6 Mon Sep 17 00:00:00 2001 From: ygor Date: Sun, 27 Oct 2024 20:00:23 -0300 Subject: [PATCH 1/4] allow current directory as project --- src/build/project_creation.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/build/project_creation.c b/src/build/project_creation.c index d90232c42..424c758d6 100644 --- a/src/build/project_creation.c +++ b/src/build/project_creation.c @@ -182,6 +182,8 @@ const char* DEFAULT_TARGETS[] = { const char *LIB_README = "Welcome to the %s library.\n"; static bool check_name(const char *name); +static char* get_proj_name(); +static inline bool current_dir(const char *name); static void exit_fail(const char *fmt, ...); static void delete_dir_and_exit(BuildOptions *build_options, const char *fmt, ...); static void mkdir_or_fail(BuildOptions *build_options, const char *name); @@ -250,7 +252,7 @@ void create_project(BuildOptions *build_options) size_t len; template = file_read_all(build_options->template, &len); } - if (!check_name(build_options->project_name)) + if (!current_dir(build_options->project_name) && !check_name(build_options->project_name)) { error_exit("'%s' is not a valid project name.", build_options->project_name); } @@ -260,12 +262,19 @@ void create_project(BuildOptions *build_options) error_exit("Can't open path '%s'.", build_options->path); } - if (!dir_make(build_options->project_name)) + if (current_dir(build_options->project_name)) + { + build_options->project_name = get_proj_name(); + } + else { - error_exit("Could not create directory '%s'.", build_options->project_name); + if (!dir_make(build_options->project_name)) + { + error_exit("Could not create directory '%s'.", build_options->project_name); + } + chdir_or_fail(build_options, build_options->project_name); } - chdir_or_fail(build_options, build_options->project_name); create_file_or_fail(build_options, "LICENSE", NULL); create_file_or_fail(build_options, "README.md", NULL); create_file_or_fail(build_options, "project.json", template, build_options->project_name); @@ -349,6 +358,21 @@ static bool check_name(const char *name) return true; } +static inline bool current_dir(const char *name) +{ + return (name[0] == '.' && name[1] == '\0'); +} + +static char* get_proj_name() +{ + char* full_path = getcwd(NULL, 0); + int end = 0; + while (full_path[end] != '\0') end++; + int begin = end; + while (begin > 0 && full_path[begin] != '/') begin--; + return full_path+begin+1; +} + static void chdir_or_fail(BuildOptions *build_options, const char *name) { if (!dir_change(name)) From 8e273d314b4aa40441fe8b14fe6f71adac375261 Mon Sep 17 00:00:00 2001 From: ygor Date: Sun, 27 Oct 2024 20:16:59 -0300 Subject: [PATCH 2/4] name fix --- src/build/project_creation.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/build/project_creation.c b/src/build/project_creation.c index 424c758d6..23d2cfb7e 100644 --- a/src/build/project_creation.c +++ b/src/build/project_creation.c @@ -370,7 +370,8 @@ static char* get_proj_name() while (full_path[end] != '\0') end++; int begin = end; while (begin > 0 && full_path[begin] != '/') begin--; - return full_path+begin+1; + if (full_path[begin] == '/') begin++; + return full_path+begin; } static void chdir_or_fail(BuildOptions *build_options, const char *name) From 45f90c98cf44c0e81dedb7720b1aa48368033e63 Mon Sep 17 00:00:00 2001 From: ygor Date: Sun, 27 Oct 2024 20:47:14 -0300 Subject: [PATCH 3/4] avoid multiple underscores in module name --- src/build/project_creation.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/build/project_creation.c b/src/build/project_creation.c index 23d2cfb7e..03bd260bd 100644 --- a/src/build/project_creation.c +++ b/src/build/project_creation.c @@ -301,27 +301,35 @@ static const char *module_name(BuildOptions *build_options) scratch_buffer_clear(); size_t len = strlen(build_options->project_name); bool has_char = false; + bool appended_underscore = false; for (size_t i = 0; i < len; i++) { char c = build_options->project_name[i]; if (c >= '0' && c <= '9') { if (!has_char) scratch_buffer_append("m_"); - has_char = true; scratch_buffer_append_char(c); + has_char = true; + appended_underscore=false; continue; } if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { scratch_buffer_append_char(c | 0x20); has_char = true; + appended_underscore=false; continue; } - scratch_buffer_append_char('_'); + if (!appended_underscore) + { + scratch_buffer_append_char('_'); + appended_underscore = true; + } } if (!has_char) scratch_buffer_append("module"); return scratch_buffer_to_string(); } + static void create_file_or_fail(BuildOptions *build_options, const char *filename, const char *fmt, ...) { if (!fmt) From 82cf5a823988d1a8dc9af0fffb14a51cea3dee7f Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 30 Oct 2024 21:21:39 +0100 Subject: [PATCH 4/4] Updates to '.' code. --- src/build/project_creation.c | 63 ++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/src/build/project_creation.c b/src/build/project_creation.c index 03bd260bd..ff37d8997 100644 --- a/src/build/project_creation.c +++ b/src/build/project_creation.c @@ -182,8 +182,7 @@ const char* DEFAULT_TARGETS[] = { const char *LIB_README = "Welcome to the %s library.\n"; static bool check_name(const char *name); -static char* get_proj_name(); -static inline bool current_dir(const char *name); +static char* get_cwd_project_name(); static void exit_fail(const char *fmt, ...); static void delete_dir_and_exit(BuildOptions *build_options, const char *fmt, ...); static void mkdir_or_fail(BuildOptions *build_options, const char *name); @@ -252,7 +251,19 @@ void create_project(BuildOptions *build_options) size_t len; template = file_read_all(build_options->template, &len); } - if (!current_dir(build_options->project_name) && !check_name(build_options->project_name)) + + // Special case, a '.' is given + if (str_eq(build_options->project_name, ".")) + { + build_options->project_name = get_cwd_project_name(); + if (!check_name(build_options->project_name)) + { + error_exit("The parent directory (which is '%s') is not a valid project name.", build_options->project_name); + } + goto CREATE; + } + + if (!check_name(build_options->project_name)) { error_exit("'%s' is not a valid project name.", build_options->project_name); } @@ -262,19 +273,13 @@ void create_project(BuildOptions *build_options) error_exit("Can't open path '%s'.", build_options->path); } - if (current_dir(build_options->project_name)) + if (!dir_make(build_options->project_name)) { - build_options->project_name = get_proj_name(); - } - else - { - if (!dir_make(build_options->project_name)) - { - error_exit("Could not create directory '%s'.", build_options->project_name); - } - chdir_or_fail(build_options, build_options->project_name); + error_exit("Could not create directory '%s'.", build_options->project_name); } + chdir_or_fail(build_options, build_options->project_name); +CREATE: create_file_or_fail(build_options, "LICENSE", NULL); create_file_or_fail(build_options, "README.md", NULL); create_file_or_fail(build_options, "project.json", template, build_options->project_name); @@ -310,14 +315,14 @@ static const char *module_name(BuildOptions *build_options) if (!has_char) scratch_buffer_append("m_"); scratch_buffer_append_char(c); has_char = true; - appended_underscore=false; + appended_underscore = false; continue; } if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { scratch_buffer_append_char(c | 0x20); has_char = true; - appended_underscore=false; + appended_underscore = false; continue; } if (!appended_underscore) @@ -366,20 +371,22 @@ static bool check_name(const char *name) return true; } -static inline bool current_dir(const char *name) -{ - return (name[0] == '.' && name[1] == '\0'); -} - -static char* get_proj_name() +static char* get_cwd_project_name() { - char* full_path = getcwd(NULL, 0); - int end = 0; - while (full_path[end] != '\0') end++; - int begin = end; - while (begin > 0 && full_path[begin] != '/') begin--; - if (full_path[begin] == '/') begin++; - return full_path+begin; + char *full_path = getcwd(NULL, 0); + size_t len = strlen(full_path); + for (size_t i = len; i > 0; i--) + { + switch (full_path[i]) + { + case '/': +#if PLATFORM_WINDOWS + case '\\': +#endif + return &full_path[i + 1]; + } + } + return full_path; } static void chdir_or_fail(BuildOptions *build_options, const char *name)