commit d0a7efd3a0d314a257fcffaaac4f197c4270d7ee
parent 7ca0614929668e933e2feb2e5beb8c57fe66a1bc
Author: Remy Noulin <loader2x@gmail.com>
Date: Thu, 26 Dec 2019 16:37:48 +0100
add add command in spm to add packages in shpPackages to dependency list in package.yml
install packages automatically when there is package,yml and
dependencies but no shpPackages folder
src/common.h | 12 ++++++------
src/sheepy.c | 36 +++++++++++++++++++++++++++++++++---
src/spm.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
src/spm.h | 3 ++-
4 files changed, 93 insertions(+), 13 deletions(-)
Diffstat:
4 files changed, 93 insertions(+), 13 deletions(-)
diff --git a/src/common.h b/src/common.h
@@ -26,13 +26,15 @@
#define HOME_DIR "~/.sheepy/"
// HOME_DIR/build is the folder for building
+#define PACKAGE_DIR "shpPackages"
+
#define CONFIG_NAME "config.yml"
#define CONFIG_YML "---\n"\
" tcc: false\n"\
" print_compile_commands: false\n"\
" clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
- " system: /usr/local # spm -g install the packages in this folder under shpPackages and bin\n"\
+ " system: /usr/local # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
" cflags: -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
" lflags: -rdynamic # these flags are overriden by package.yml\n"\
" linkWithGold: true\n"\
@@ -57,7 +59,7 @@
" tcc: false\n"\
" print_compile_commands: false\n"\
" clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
- " system: /usr/local # spm -g install the packages in this folder under shpPackages and bin\n"\
+ " system: /usr/local # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
" cflags: -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
" lflags: -rdynamic # these flags are overriden by package.yml\n"\
" linkWithGold: false\n"\
@@ -82,7 +84,7 @@
" tcc: false\n"\
" print_compile_commands: false\n"\
" clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
- " system: /data/data/com.termux/files/usr # spm -g install the packages in this folder under shpPackages and bin\n"\
+ " system: /data/data/com.termux/files/usr # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
" cflags: -D__arm__=1 -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
" lflags: -rdynamic # these flags are overriden by package.yml\n"\
" linkWithGold: false\n"\
@@ -107,7 +109,7 @@
" tcc: false\n"\
" print_compile_commands: false\n"\
" clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
- " system: /boot/home/config/non-packaged # spm -g install the packages in this folder under shpPackages and bin\n"\
+ " system: /boot/home/config/non-packaged # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
" cflags: -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
" lflags: # these flags are overriden by package.yml\n"\
" linkWithGold: false\n"\
@@ -130,8 +132,6 @@
extern char *defaultSpC;
-#define PACKAGE_DIR "shpPackages"
-
// package information
#define PACKAGE "package.yml"
diff --git a/src/sheepy.c b/src/sheepy.c
@@ -639,9 +639,39 @@ int MAIN(int ARGC, char** ARGV) {
char *rootMain = rootMain_l[0];
// main source folder (dir)
- // Ex: dir = ../bin
+ // Ex: dir = ../bin or ./ when there is no folder in mainFilename
char *dir = shDirname(mainFilename);
+ // if there is package.yml in dir, check if there are package dependencies and the shpPackages folder
+ // install the packages when shpPackages is not found
+ dum = catS(dir, "/"PACKAGE_DIR);
+ normalizePathG(&dum);
+ if (!sheepyParams.clean && not isDir(dum)) {
+ free(dum);
+ dum = catS(dir, "/", PACKAGE);
+ normalizePathG(&dum);
+ if (fileExists(dum)) {
+ createSmallJson(pkgInfo);
+ readFileG(&pkgInfo, dum);
+ smallDictt *pkgDeps = getG(&pkgInfo, rtSmallDictt, "dependencies");
+ if (pkgDeps) {
+ if (lenG(pkgDeps)) {
+ char *c = formatS("cd %s ; spm install", dir);
+ if (getG(cfgD, rtBool, "print_compile_commands")) {
+ printf("%s\n", c);
+ }
+ programResult += commandNFree(c);;
+ }
+ finishG(pkgDeps);
+ }
+ freeG(&pkgInfo);
+ }
+ free(dum);
+ }
+ else {
+ free(dum);
+ }
+
// find out real path for main source
char actualpath[PATH_MAX+1];
char *actualDir;
@@ -829,7 +859,7 @@ int MAIN(int ARGC, char** ARGV) {
// remove sub directories in packages, the a file is located at the package root
// shpPackages/ini/src/ini.h
// shpPackages/ini/ini.a
- smallArrayt *spl = splitG(L, "shpPackages/");
+ smallArrayt *spl = splitG(L, PACKAGE_DIR"/");
smallStringt *last = getG(spl, rtSmallStringt, -1);
setG(last, -1, 'a');
// last is "ini/src/ini.a"
@@ -837,7 +867,7 @@ int MAIN(int ARGC, char** ARGV) {
finishG(last);
char *lib = catS(getG(sslash, rtChar, 0), "/", getG(sslash, rtChar, -1));
setNFreeG(spl, -1, lib);
- lib = joinSG(spl, "shpPackages/");
+ lib = joinSG(spl, PACKAGE_DIR"/");
terminateManyG(spl,sslash);
if (fileExists(lib)) {
// link library only when it exists, there is no library in header only packages
diff --git a/src/spm.c b/src/spm.c
@@ -173,7 +173,8 @@ int MAIN(int ARGC, char** ARGV) {
put
putsG("For sheepy help, run: sheepy -h");
put
- putsG(" new (PKG1) (PKG2) - create a new empty package, without parameters print package.yml");
+ putsG(" new (PKG1) (PKG2) - create a new empty package, without parameters print default package.yml");
+ putsG(" add - add packages in "PACKAGE_DIR" folder to dependencies list in package.yml");
putsG(" publish - upload package to spm registry");
putsG(" install (PKG1) (PKG2) - install package from spm registry, without parameters install packages listed in package.yml");
putsG(" -g install PKG1 (PKG2) - install package globally in the system");
@@ -202,7 +203,6 @@ int MAIN(int ARGC, char** ARGV) {
// set parameters
// global is an option associated with install to install a package in the system
- memset(¶ms, 0, sizeof(params));
createAllocateSmallArray(options);
@@ -217,6 +217,10 @@ int MAIN(int ARGC, char** ARGV) {
params.new += 1;
bum = true;
}
+ else if (eqG(argv[i], "add")) {
+ params.add += 1;
+ bum = true;
+ }
else if (eqG(argv[i], "install")) {
params.install += 1;
bum = true;
@@ -301,7 +305,7 @@ int MAIN(int ARGC, char** ARGV) {
XFAILURE
}
- int paramsSum = params.install + params.publish + params.show + params.new + params.update + params.uninstall + params.find + params.top + params.hot + params.latest + params.whoami + params.profile + params.unpublish + params.info + params.adduser + params.apikey + params.test + params.memcheck + params.showmcheck + params.doc;
+ int paramsSum = params.install + params.publish + params.show + params.new + params.update + params.uninstall + params.find + params.top + params.hot + params.latest + params.whoami + params.profile + params.unpublish + params.info + params.adduser + params.apikey + params.test + params.memcheck + params.showmcheck + params.doc + params.add;
if (paramsSum > 1) {
putsG(BLD RED "Conflicting commands." RST);
XFAILURE
@@ -707,6 +711,51 @@ int MAIN(int ARGC, char** ARGV) {
XSUCCESS
}
+ if (params.add) {
+ if (not isDir(PACKAGE_DIR)) {
+ puts("'"PACKAGE_DIR"' folder not found in current path. Nothing to do.");
+ }
+ else {
+ if (!fileExists(PACKAGE)) {
+ // command line is spm install and there is no package.yml
+ puts(BLD RED "'package.yml' in current path!" RST);
+ XFAILURE
+ }
+ char **installedPackages = readDirDir(PACKAGE_DIR);
+ if (not isEmptyG(installedPackages)) {
+ createSmallJson(pkgInfo);
+ readFileG(&pkgInfo, PACKAGE);
+ smallDictt *pkgDeps = getG(&pkgInfo, rtSmallDictt, "dependencies");
+ if (not pkgDeps) {
+ // there are no dependencies in package.yml
+ initiateG(&pkgDeps);
+ forEachS(installedPackages, pkg) {
+ // add pkg to dependency list in package.yml
+ setG(pkgDeps, pkg, "");
+ printf("Added %s to dependencies", pkg);
+ printf("\n");
+ }
+ setNFreeG(&pkgInfo, "dependencies", pkgDeps);
+ }
+ else {
+ forEachS(installedPackages, pkg) {
+ if (not hasG(pkgDeps, pkg)) {
+ // add pkg to dependency list in package.yml
+ setG(pkgDeps, pkg, "");
+ printf("Added %s to dependencies", pkg);
+ printf("\n");
+ }
+ }
+ setNFreePG(&pkgInfo, "dependencies", pkgDeps);
+ }
+ writeFileG(&pkgInfo, PACKAGE);
+ freeG(&pkgInfo);
+ }
+ listFreeS(installedPackages);
+ }
+ XSUCCESS
+ }
+
// TODO check that package name is valid: not -g, publish, install
// publish package to registry
if (params.publish) {
diff --git a/src/spm.h b/src/spm.h
@@ -24,6 +24,7 @@
typedef struct {
int new;
+ int add;
int global;
int publish;
int install;
@@ -47,7 +48,7 @@ typedef struct {
int generatedPackageList;
} paramst;
-paramst params;
+paramst params = init0Var;
typedef struct {
bool isGlobal;