save
This commit is contained in:
2
Makefile
2
Makefile
@@ -15,7 +15,7 @@ else
|
||||
all: build $(YAIT) $(YAIT_DOC)
|
||||
|
||||
build:
|
||||
mkdir bin
|
||||
mkdir -p bin
|
||||
|
||||
$(YAIT): $(YAIT_SRCS)
|
||||
$(CC) $(CFLAGS) -Iinclude -DCOMMIT=$(shell git rev-list --count --all) $^ -o $@
|
||||
|
||||
4
config.mak
Normal file
4
config.mak
Normal file
@@ -0,0 +1,4 @@
|
||||
PREFIX=/usr/bin/
|
||||
CFLAGS=-Wall -Wextra -O2 --std=c23
|
||||
LDFLAGS=
|
||||
CC=gcc
|
||||
2
configure
vendored
2
configure
vendored
@@ -19,7 +19,7 @@ cmdexists () { type "$1" >/dev/null 2>&1 ; }
|
||||
trycc () { test -z "$CC" && cmdexists "$1" && CC=$1 ; }
|
||||
|
||||
prefix=/usr/bin/
|
||||
CFLAGS="-Wall -Wextra -O2"
|
||||
CFLAGS="-Wall -Wextra -O2 --std=c23"
|
||||
LDFLAGS=
|
||||
CC=
|
||||
|
||||
|
||||
0
doc/sample_project/Makefile
Normal file
0
doc/sample_project/Makefile
Normal file
0
doc/sample_project/README
Normal file
0
doc/sample_project/README
Normal file
0
doc/sample_project/bin/bin
Normal file
0
doc/sample_project/bin/bin
Normal file
0
doc/sample_project/configure
vendored
Normal file
0
doc/sample_project/configure
vendored
Normal file
0
doc/sample_project/doc/information
Normal file
0
doc/sample_project/doc/information
Normal file
0
doc/sample_project/include/project.h
Normal file
0
doc/sample_project/include/project.h
Normal file
0
doc/sample_project/man/project.1
Normal file
0
doc/sample_project/man/project.1
Normal file
0
doc/sample_project/src/main.c
Normal file
0
doc/sample_project/src/main.c
Normal file
@@ -9,14 +9,12 @@
|
||||
#ifndef YAIT_H
|
||||
#define YAIT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
typedef enum {
|
||||
BSD3,
|
||||
GPLv3,
|
||||
MIT,
|
||||
UNLICENCE,
|
||||
LICENCE_HELP,
|
||||
BSD3,
|
||||
GPLv3,
|
||||
MIT,
|
||||
UNLICENCE,
|
||||
LICENCE_HELP,
|
||||
} licence_t;
|
||||
|
||||
/* A bit field is used so that we can accomplish two things. (a) store lots of
|
||||
@@ -46,29 +44,12 @@ typedef struct {
|
||||
flags_t flag;
|
||||
} manifest_t;
|
||||
|
||||
#define DEFAULT_CLANG_FORMAT true
|
||||
#define DEFAULT_GIT_INIT true
|
||||
#define DEFAULT_GNU false
|
||||
#define DEFAULT_LIBRARIES LIB_NONE
|
||||
#define DEFAULT_LICENCE BSD3
|
||||
|
||||
#define HAS_LIBRARY(libs, lib) ((libs) & (lib))
|
||||
#define ADD_LIBRARY(libs, lib) ((libs) |= (lib))
|
||||
#define REMOVE_LIBRARY(libs, lib) ((libs) &= ~(lib))
|
||||
|
||||
[[maybe_unused]] static lib_flags_t TOLibrary(char *src)
|
||||
{
|
||||
if (strcmp(src, "raylib"))
|
||||
return LIB_RAYLIB;
|
||||
if (strcmp(src, "ncurse"))
|
||||
return LIB_NCURSES;
|
||||
if (strcmp(src, "ncurses"))
|
||||
return LIB_NCURSES;
|
||||
if (strcmp(src, "curl"))
|
||||
return LIB_CURL;
|
||||
if (strcmp(src, "help"))
|
||||
return LIB_HELP;
|
||||
return LIB_COUNT_; /* bad case */
|
||||
}
|
||||
int create_project(manifest_t manifest);
|
||||
int mkdir_p(const char *path);
|
||||
int create_file_with_content(const char *path, const char *format, ...);
|
||||
|
||||
#endif // YAIT_H
|
||||
|
||||
73
man/yait.1
Normal file
73
man/yait.1
Normal file
@@ -0,0 +1,73 @@
|
||||
.\" yait.1 — Manual page for yait
|
||||
.TH YAIT 1 "August 2025" "yait 1.0" "User Commands"
|
||||
.SH NAME
|
||||
yait \- yet another init tool for creating C/C++ projects
|
||||
.SH SYNOPSIS
|
||||
.B yait
|
||||
.RI [ options ] " project-name"
|
||||
.SH DESCRIPTION
|
||||
.B yait
|
||||
(yet another init tool) is a project scaffolding utility that helps you start
|
||||
new C or C++ projects quickly. It creates a new directory containing a portable
|
||||
build system, header and source files, and optional libraries to provide a
|
||||
kick start for development.
|
||||
|
||||
The intent of
|
||||
.B yait
|
||||
is to reduce setup overhead, letting you focus on writing code instead of
|
||||
boilerplate.
|
||||
|
||||
For more detailed documentation, visit:
|
||||
<https://vx-clutch.github.io/vxserver.dev/docs/yait.html>
|
||||
.SH OPTIONS
|
||||
.TP
|
||||
.BR \-h ", " \-\-help
|
||||
Display usage information and exit.
|
||||
.TP
|
||||
.BR \-v ", " \-\-version
|
||||
Show version information and exit.
|
||||
.TP
|
||||
.BR \-l " " libname
|
||||
Include optional library support (for example, a testing library).
|
||||
.TP
|
||||
.BR \-L
|
||||
List available optional libraries that can be added to a new project.
|
||||
.TP
|
||||
.BR \-c
|
||||
Generate a C project (default).
|
||||
.TP
|
||||
.BR \-C++
|
||||
Generate a C++ project.
|
||||
.SH EXAMPLES
|
||||
Initialize a new C project in a directory named
|
||||
.I foo
|
||||
:
|
||||
.PP
|
||||
.nf
|
||||
.RS
|
||||
yait foo
|
||||
.RE
|
||||
.fi
|
||||
.PP
|
||||
Initialize a new C++ project with an optional library:
|
||||
.PP
|
||||
.nf
|
||||
.RS
|
||||
yait -C++ -l examplelib bar
|
||||
.RE
|
||||
.fi
|
||||
.SH FILES
|
||||
.TP
|
||||
.I include/
|
||||
Public headers for the new project.
|
||||
.TP
|
||||
.I src/
|
||||
Source files for the new project.
|
||||
.TP
|
||||
.I Makefile
|
||||
Portable POSIX makefile.
|
||||
.SH SEE ALSO
|
||||
.BR make (1),
|
||||
.BR sh (1)
|
||||
.SH AUTHOR
|
||||
Written by vx-clutch.
|
||||
@@ -6,335 +6,9 @@
|
||||
* <https://opensource.org/licence/bsd-3-clause>
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "manifest.h"
|
||||
#include "contents.h"
|
||||
#include "yait.h"
|
||||
|
||||
#define DEFAULT_USER_NAME "unknown"
|
||||
#define DEFAULT_PROJECT_NAME "Project"
|
||||
#define DEFAULT_LICENCE BSD3
|
||||
#define DEFAULT_GIT_INIT true
|
||||
#define DEFAULT_CLANG_FORMAT true
|
||||
#define DEFAULT_GNU false
|
||||
|
||||
int program_exists(const char *prog)
|
||||
{
|
||||
char *path = getenv("PATH");
|
||||
if (!path)
|
||||
return -1;
|
||||
|
||||
char *copy = strdup(path);
|
||||
if (!copy)
|
||||
return -1;
|
||||
|
||||
char *dir = strtok(copy, ":");
|
||||
while (dir) {
|
||||
char buf[4096];
|
||||
snprintf(buf, sizeof(buf), "%s/%s", dir, prog);
|
||||
if (access(buf, X_OK) == 0) {
|
||||
free(copy);
|
||||
return 0;
|
||||
}
|
||||
dir = strtok(NULL, ":");
|
||||
}
|
||||
|
||||
free(copy);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sanitize(manifest_t *m)
|
||||
{
|
||||
int status;
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
|
||||
status = program_exists("git");
|
||||
if (status != 0)
|
||||
return fprintf(stderr, "git binary not present\n"),
|
||||
EXIT_FAILURE;
|
||||
|
||||
if (!m->project)
|
||||
m->project = DEFAULT_PROJECT_NAME;
|
||||
|
||||
if (!m->name)
|
||||
m->name = (pw && pw->pw_name) ? pw->pw_name : DEFAULT_USER_NAME;
|
||||
|
||||
if (m->licence == UNLICENCE)
|
||||
m->licence = DEFAULT_LICENCE;
|
||||
|
||||
if (!m->flag.git)
|
||||
m->flag.git = DEFAULT_GIT_INIT;
|
||||
|
||||
if (!m->flag.clang_format)
|
||||
m->flag.clang_format = DEFAULT_CLANG_FORMAT;
|
||||
|
||||
if (!m->flag.GNU)
|
||||
m->flag.GNU = DEFAULT_GNU;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int create_libraries(manifest_t manifest)
|
||||
{
|
||||
int status = 0;
|
||||
|
||||
if (!manifest.libraries) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (HAS_LIBRARY(manifest.libraries, LIB_RAYLIB)) {
|
||||
fputs("Pulling raylib", stderr);
|
||||
REMOVE_LIBRARY(manifest.libraries, LIB_RAYLIB);
|
||||
status = system(
|
||||
"git submodule add --quiet https://github.com/raysan5/raylib");
|
||||
if (status != 0)
|
||||
return status;
|
||||
fputs(", done.\n", stderr);
|
||||
}
|
||||
|
||||
if (HAS_LIBRARY(manifest.libraries, LIB_NCURSES)) {
|
||||
fputs("Pulling ncurses", stderr);
|
||||
REMOVE_LIBRARY(manifest.libraries, LIB_NCURSES);
|
||||
status = system(
|
||||
"git submodule add --quiet https://github.com/mirror/ncurses");
|
||||
if (status != 0)
|
||||
return status;
|
||||
fputs(", done.\n", stderr);
|
||||
}
|
||||
|
||||
if (HAS_LIBRARY(manifest.libraries, LIB_CURL)) {
|
||||
fputs("Pulling curl", stderr);
|
||||
REMOVE_LIBRARY(manifest.libraries, LIB_CURL);
|
||||
status = system(
|
||||
"git submodule add --quiet https://github.com/curl/curl");
|
||||
if (status != 0)
|
||||
return status;
|
||||
fputs(", done.\n", stderr);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int create_licence(manifest_t manifest, char **licence_line_buffer)
|
||||
{
|
||||
if (manifest.licence == UNLICENCE)
|
||||
return 0;
|
||||
|
||||
if (!licence_line_buffer)
|
||||
return EINVAL;
|
||||
|
||||
switch (manifest.licence) {
|
||||
case BSD3:
|
||||
*licence_line_buffer = "Bsd";
|
||||
fprintf(stderr,
|
||||
"create_licence: BSD3 license generation not implemented\n");
|
||||
return ENOSYS;
|
||||
case GPLv3:
|
||||
fprintf(stderr,
|
||||
"create_licence: GPLv3 license generation not implemented\n");
|
||||
return ENOSYS;
|
||||
case MIT:
|
||||
fprintf(stderr,
|
||||
"create_licence: MIT license generation not implemented\n");
|
||||
return ENOSYS;
|
||||
default:
|
||||
fprintf(stderr, "create_licence: unknown license type\n");
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
int maybe_create_clang_format(manifest_t manifest)
|
||||
{
|
||||
if (!manifest.flag.clang_format)
|
||||
return 0;
|
||||
|
||||
int status = create_file_with_content(".clang-format",
|
||||
clang_format_template);
|
||||
return status;
|
||||
}
|
||||
|
||||
int setup_git(manifest_t manifest)
|
||||
{
|
||||
if (!manifest.flag.git)
|
||||
return 0;
|
||||
|
||||
int status = system("git init --quiet");
|
||||
if (status == -1) {
|
||||
fprintf(stderr, "...: %s\n", strerror(errno));
|
||||
} else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
|
||||
fprintf(stderr, "...: exited with status %d\n",
|
||||
WEXITSTATUS(status));
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int create_makefile(manifest_t manifest)
|
||||
{
|
||||
char *m = strdup(manifest.project);
|
||||
char *M = strdup(manifest.project);
|
||||
if (!M) {
|
||||
fputs("create_makefile: fatal: out of memory\n", stderr);
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
for (char *p = M; *p; ++p)
|
||||
if (*p >= 'a' && *p <= 'z')
|
||||
*p -= 'a' - 'A';
|
||||
|
||||
int status = create_file_with_content(strcat(m, "Makefile"),
|
||||
makefile_template, M, m, M, m, M,
|
||||
M, m, M, m, M, m);
|
||||
|
||||
free(m);
|
||||
free(M);
|
||||
return status;
|
||||
}
|
||||
|
||||
int create_configure(manifest_t manifest)
|
||||
{
|
||||
const char *cc;
|
||||
if (manifest.flag.use_cpp) {
|
||||
cc = "trycc g++\ntrycc CC\ntrycc clang++\n";
|
||||
} else {
|
||||
cc = "trycc gcc\ntrycc cc\ntrycc clang\n";
|
||||
}
|
||||
|
||||
int status =
|
||||
create_file_with_content("configure", configure_template, cc);
|
||||
if (status != 0)
|
||||
return status;
|
||||
|
||||
status = system("chmod +x configure");
|
||||
if (status != 0)
|
||||
fprintf(stderr, "create_configure: chmod failed: %s\n",
|
||||
strerror(status));
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int generate_source_code(manifest_t manifest, char *licence_line)
|
||||
{
|
||||
int status = 0;
|
||||
|
||||
time_t t = time(NULL);
|
||||
struct tm tm = *localtime(&t);
|
||||
int year = tm.tm_year + 1900;
|
||||
|
||||
status = create_file_with_content("config.h", config_h_template,
|
||||
manifest.project, licence_line, year);
|
||||
if (status != 0) {
|
||||
fprintf(stderr,
|
||||
"generate_source_code: failed to create config.h: %s\n",
|
||||
strerror(status));
|
||||
return status;
|
||||
}
|
||||
|
||||
if (status != 0) {
|
||||
fprintf(stderr,
|
||||
"generate_source_code: failed to create or enter directory: %s\n",
|
||||
strerror(status));
|
||||
return status;
|
||||
}
|
||||
|
||||
if (manifest.flag.GNU) {
|
||||
status = create_file_with_content(
|
||||
strcat(manifest.project, "main.c"), main_c_gnu_template,
|
||||
manifest.project, manifest.name);
|
||||
} else {
|
||||
status = create_file_with_content(
|
||||
strcat(manifest.project, "main.c"), main_c_template,
|
||||
manifest.project, manifest.name);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
#include "../include/yait.h"
|
||||
|
||||
int create_project(manifest_t manifest)
|
||||
{
|
||||
int status;
|
||||
|
||||
sanitize(&manifest);
|
||||
|
||||
// TODO(vx-clutch): take dir.
|
||||
|
||||
// status = create_makefile(manifest);
|
||||
// if (status != 0) {
|
||||
// fprintf(stderr,
|
||||
// "create_project: failed to create Makefile: %s\n",
|
||||
// strerror(status));
|
||||
// return status;
|
||||
// }
|
||||
//
|
||||
// status = create_configure(manifest);
|
||||
// if (status != 0) {
|
||||
// fprintf(stderr,
|
||||
// "create_project: failed to create configure: %s\n",
|
||||
// strerror(status));
|
||||
// return status;
|
||||
// }
|
||||
//
|
||||
// status = maybe_create_clang_format(manifest);
|
||||
// if (status != 0) {
|
||||
// fprintf(stderr,
|
||||
// "create_project: warning: clang-format setup failed: %s\n",
|
||||
// strerror(status));
|
||||
// }
|
||||
//
|
||||
// char *licence_line = malloc(1024);
|
||||
// if (!licence_line) {
|
||||
// fputs("create_project: failed to allocate memory for licence line\n",
|
||||
// stderr);
|
||||
// return ENOMEM;
|
||||
// }
|
||||
//
|
||||
// status = create_licence(manifest, &licence_line);
|
||||
// if (status != 0) {
|
||||
// fprintf(stderr,
|
||||
// "create_project: failed to create licence: %s\n",
|
||||
// strerror(status));
|
||||
// free(licence_line);
|
||||
// return status;
|
||||
// }
|
||||
//
|
||||
// status = generate_source_code(manifest, licence_line);
|
||||
// if (status != 0) {
|
||||
// fprintf(stderr,
|
||||
// "create_project: failed to generate source code: %s\n",
|
||||
// strerror(status));
|
||||
// free(licence_line);
|
||||
// return status;
|
||||
// }
|
||||
//
|
||||
// free(licence_line);
|
||||
//
|
||||
// status = create_libraries(manifest);
|
||||
// if (status != 0) {
|
||||
// printfn("failed to get libraries: %s", strerror(status));
|
||||
// return status;
|
||||
// }
|
||||
//
|
||||
// status = setup_git(manifest);
|
||||
// if (status != 0) {
|
||||
// printfn("warning: git initialization failed: %s",
|
||||
// strerror(status));
|
||||
// }
|
||||
|
||||
char *cwd = getcwd(NULL, 0);
|
||||
if (!cwd) {
|
||||
printfn("could not get current working directory");
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "Created %s at\n %s\n", manifest.project, cwd);
|
||||
free(cwd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -15,11 +15,9 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "yait.h"
|
||||
#include "../include/yait.h"
|
||||
|
||||
int files_made = 0;
|
||||
|
||||
static int mkdir_p(const char *path)
|
||||
int mkdir_p(const char *path)
|
||||
{
|
||||
char *copypath = strdup(path);
|
||||
char *p = copypath;
|
||||
@@ -80,9 +78,6 @@ int create_file_with_content(const char *path, const char *format, ...)
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
++files_made;
|
||||
emit_progress("Creating files", files_made);
|
||||
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
18
src/main.c
18
src/main.c
@@ -6,7 +6,7 @@
|
||||
* <https://opensource.org/licence/bsd-3-clause>
|
||||
*/
|
||||
|
||||
// Usage: yait [OPTION]... <PATH>
|
||||
// Usage: yait [OPTION]... <PROJECT>
|
||||
|
||||
#include <getopt.h>
|
||||
#include <pwd.h>
|
||||
@@ -16,8 +16,8 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../config.h"
|
||||
#include "../core/yait.h"
|
||||
#include "../core/manifest.h"
|
||||
#include "../include/yait.h"
|
||||
#include "standard.c"
|
||||
|
||||
#define print_option(option, description) \
|
||||
printf(" %-20s %-20s\n", option, description)
|
||||
@@ -84,12 +84,12 @@ static int parse_arguments(manifest_t *conf, int argc, char **argv)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
break;
|
||||
case 'L':
|
||||
if (strcmp(optarg, "help"))
|
||||
fprintf(stderr, "raylib\nncurses\ncurl\n");
|
||||
else
|
||||
ADD_LIBRARY(conf->libraries, TOLibrary(optarg));
|
||||
break;
|
||||
// case 'L':
|
||||
// if (strcmp(optarg, "help"))
|
||||
// fprintf(stderr, "raylib\nncurses\ncurl\n");
|
||||
// else
|
||||
// ADD_LIBRARY(conf->libraries, TOLibrary(optarg));
|
||||
// break;
|
||||
case 'h':
|
||||
usage(0);
|
||||
exit(0);
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#define COMMIT 0
|
||||
#endif
|
||||
|
||||
#include "yait.h"
|
||||
#include "../config.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
29
src/yait.h
29
src/yait.h
@@ -1,29 +0,0 @@
|
||||
/* Copyright (C) vx_clutch
|
||||
*
|
||||
* This file is part of yait
|
||||
*
|
||||
* This project and file is licenced under the BSD-3-Clause licence.
|
||||
* <https://opensource.org/license/bsd-3-clause>
|
||||
*/
|
||||
|
||||
#ifndef YAIT_H
|
||||
#define YAIT_H
|
||||
|
||||
/* Constants for file operations */
|
||||
#define DEFAULT_DIR_PERMISSIONS 0755
|
||||
#define MAX_PATH_LENGTH 1024
|
||||
|
||||
int printfn(char *format, ...);
|
||||
|
||||
int create_file_with_content(const char *path, const char *format, ...);
|
||||
|
||||
int parse_standard_options(void (*usage_func)(), int argc, char **argv);
|
||||
|
||||
int program_exists(const char *prog);
|
||||
|
||||
#include "manifest.h"
|
||||
int create_project(manifest_t manifest);
|
||||
|
||||
void emit_progress(const char *action, int count);
|
||||
|
||||
#endif // YAIT_H
|
||||
Reference in New Issue
Block a user