Skip to content

Commit

Permalink
Updates to '.' code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Oct 30, 2024
1 parent 45f90c9 commit 82cf5a8
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/build/project_creation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 82cf5a8

Please sign in to comment.