diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5d0b38b --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: Chromium +IndentWidth: 2 +TabWidth: 2 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 89db399..37b0afc 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,4 @@ dkms.conf *.dwo build/ -wnpkg-build/ \ No newline at end of file +lnpkg-build/ \ No newline at end of file diff --git a/Makefile b/Makefile index d71ba48..e687329 100644 --- a/Makefile +++ b/Makefile @@ -1,48 +1,59 @@ CC = gcc -FLAGS = -Iinclude/ +FLAGS = -Iinclude/ -fsanitize=address -Wall BUILD_DIR = build -WNPKG_EXECUTABLE = wnpkg -WNPKG_EXECUTABLE_DIR = $(BUILD_DIR)/$(WNPKG_EXECUTABLE) +LNPKG_EXECUTABLE = lnpkg +LNPKG_EXECUTABLE_DIR = $(BUILD_DIR)/$(LNPKG_EXECUTABLE) # For termux -WNPKG_TEMP = $(HOME)/temp/c/wnpkg -WNPKG_BUILD_DIR = wnpkg-build +LNPKG_TEMP = $(HOME)/temp/c/lnpkg +LNPKG_BUILD_DIR = lnpkg-build -MAIN_SRC = src/main.c -FILE_SRC = src/file.c +MAIN_SRC = src/main.c +FILE_SRC = src/file.c +BUILDER_SRC = src/builder.c -FILE_O = $(BUILD_DIR)/file.o +FILE_O = $(BUILD_DIR)/file.o +BUILDER_O = $(BUILD_DIR)/builder.o -all: setup $(WNPKG_EXECUTABLE_DIR) +OBJS = $(FILE_O) $(NODE_O) $(BUILDER_O) + +all: setup $(LNPKG_EXECUTABLE_DIR) .PHONY: setup clean $(FILE_O): $(FILE_SRC) $(CC) -c $(FILE_SRC) -o $(FILE_O) $(FLAGS) -$(WNPKG_EXECUTABLE_DIR): $(MAIN_SRC) $(FILE_O) - $(CC) $(MAIN_SRC) $(FILE_O) -o $(WNPKG_EXECUTABLE_DIR) $(FLAGS) +$(BUILDER_O): $(BUILDER_SRC) + $(CC) -c $(BUILDER_SRC) -o $(BUILDER_O) $(FLAGS) + +$(LNPKG_EXECUTABLE_DIR): setup $(MAIN_SRC) $(OBJS) + $(CC) $(MAIN_SRC) $(OBJS) -o $(LNPKG_EXECUTABLE_DIR) $(FLAGS) setup: mkdir -p build clean: - rm -rf $(WNPKG_EXECUTABLE_DIR) - -run: $(WNPKG_EXECUTABLE_DIR) - rm -rf $(WNPKG_BUILD_DIR) - chmod +x $(WNPKG_EXECUTABLE_DIR) - ./$(WNPKG_EXECUTABLE_DIR) basic_nodejs_proj - cd $(WNPKG_BUILD_DIR) && chmod +x basic_nodejs_proj && ./basic_nodejs_proj - -termux_run: $(WNPKG_EXECUTABLE_DIR) - rm -rf $(WNPKG_BUILD_DIR) - mkdir -p $(WNPKG_TEMP) - cp $(WNPKG_EXECUTABLE_DIR) $(WNPKG_TEMP)/$(WNPKG_EXECUTABLE) - chmod +x $(WNPKG_TEMP)/$(WNPKG_EXECUTABLE) - $(WNPKG_TEMP)/$(WNPKG_EXECUTABLE) basic_nodejs_proj - rm -rf $(WNPKG_TEMP)/$(WNPKG_BUILD_DIR) - cp -rf $(WNPKG_BUILD_DIR) $(WNPKG_TEMP)/$(WNPKG_BUILD_DIR) - cd $(WNPKG_TEMP)/$(WNPKG_BUILD_DIR) && chmod +x basic_nodejs_proj && ./basic_nodejs_proj \ No newline at end of file + rm -rf $(LNPKG_EXECUTABLE_DIR) + +runtest: + cd $(LNPKG_BUILD_DIR) && chmod +x basic_nodejs_proj && ./basic_nodejs_proj + +run: $(LNPKG_EXECUTABLE_DIR) + chmod +x $(LNPKG_EXECUTABLE_DIR) + ./$(LNPKG_EXECUTABLE_DIR) basic_nodejs_proj + $(MAKE) runtest + +termux_runtest: + rm -rf $(LNPKG_TEMP)/$(LNPKG_BUILD_DIR) + cp -rf $(LNPKG_BUILD_DIR) $(LNPKG_TEMP)/$(LNPKG_BUILD_DIR) + cd $(LNPKG_TEMP)/$(LNPKG_BUILD_DIR) && chmod +x basic_nodejs_proj && ./basic_nodejs_proj + +termux_run: $(LNPKG_EXECUTABLE_DIR) + mkdir -p $(LNPKG_TEMP) + cp $(LNPKG_EXECUTABLE_DIR) $(LNPKG_TEMP)/$(LNPKG_EXECUTABLE) + chmod +x $(LNPKG_TEMP)/$(LNPKG_EXECUTABLE) + $(LNPKG_TEMP)/$(LNPKG_EXECUTABLE) basic_nodejs_proj + $(MAKE) termux_runtest \ No newline at end of file diff --git a/README.md b/README.md index 07eaeff..016f135 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ -# 📦 wnpkg +# 📦 lnpkg -**wnpkg** is a tiny utility that packages a Node.js application into a portable executable — works on **Windows** and **Linux**. +**lnpkg** is a tiny linux utility that packages a Node.js application into a linux executable. ## 🚀 Features - Automatically wraps your Node.js script into a standalone executable -- Includes the Node.js runtime (`node.exe` on Windows, `node` on Linux) -- Optional custom icon support (`icon.ico`) -- Generates a clean build structure in `wnpkg-build/` +- Includes the Node.js runtime `node` +- Generates a clean build structure in `lnpkg-build/` - Super lightweight — written in pure C using `gcc` --- @@ -15,34 +14,32 @@ ## 🛠️ Usage ``` -wnpkg +lnpkg ``` This command will: -1. Copy `node` (or `node.exe` on Windows) and your `index.js` into a build folder. +1. Copy `node` and your `index.js` into a build folder. 2. Compile a small C launcher using `gcc`. 3. Output a single executable: - - On **Windows**: `my-app.exe` - On **Linux**: `my-app` (ELF binary without file extension) ### Example ``` -wnpkg my-app +lnpkg my-app ``` This will create the following structure: ``` -wnpkg-build/ +lnpkg-build/ ├── source/ │ ├── index.js <-- Your Node.js app -│ └── node(.exe) <-- Node.js runtime -├── my-app(.exe) <-- Final executable +│ └── node <-- Node.js runtime +├── my-app <-- Final executable ``` -- On **Windows**, just double-click `my-app.exe` to run your app. - On **Linux**, run it with: ``` @@ -51,38 +48,22 @@ wnpkg-build/ --- -## 🎨 Optional: Custom Icon +## ⚙️ Optional: Configuration File (`lnpkg_config`) -To embed a custom icon in the Windows executable: - -1. Place your `icon.ico` file in the project folder. -2. `wnpkg` will automatically detect and include it in the `.exe`. - ---- - -## ⚙️ Optional: Configuration File (`wnpkg_config`) - -You can customize the app name and icon by creating a file named `wnpkg_config` in your project folder. This file should contain: ``` app-name; -icon.ico; ``` - Replace `app-name` with the desired output name. -- If you don't want to use an icon, simply use a `*` instead: - -``` -my-app; -*; -``` --- ## 📄 Requirements -- Windows or Linux (32 or 64 bit) -- [GCC / MinGW](https://www.mingw-w64.org/) installed and available in PATH +- Linux (32 or 64 bit) +- [Node](https://nodejs.org/pt) installed and available in PATH +- [GCC](https://gcc.gnu.org/) installed and available in PATH --- @@ -92,4 +73,18 @@ This project was built for fun and utility — feel free to hack it, improve it --- -## 🐧 Created by Leonardo Pinezi (a.k.a. Shinpi) +## 🐧 Created by Leonardo Pinezi (a.k.a. Shinpi) & Aquiles Trindade (a.k.a TrindadeDev13) + +#### Leonardo Pinezi : +- Make the original base code and created the project +- Started the project organization +- Windows 32/64bit port +- Fixed bugs + +#### Aquiles Trindade : +- Organized all the entire code +- Added makefile +- Added colours and file functions +- Linux 32/64bits port +- ELF export +- Fixed bugs diff --git a/basic_nodejs_proj/lnpkg_config b/basic_nodejs_proj/lnpkg_config new file mode 100644 index 0000000..712e239 --- /dev/null +++ b/basic_nodejs_proj/lnpkg_config @@ -0,0 +1 @@ +basic_nodejs_proj; \ No newline at end of file diff --git a/basic_nodejs_proj/wnpkg_config b/basic_nodejs_proj/wnpkg_config deleted file mode 100644 index 5ab6bdf..0000000 --- a/basic_nodejs_proj/wnpkg_config +++ /dev/null @@ -1,2 +0,0 @@ -basic_nodejs_proj; -*; \ No newline at end of file diff --git a/default_icon.ico b/default_icon.ico deleted file mode 100644 index e7da1c3..0000000 Binary files a/default_icon.ico and /dev/null differ diff --git a/include/lnpkg/builder.h b/include/lnpkg/builder.h new file mode 100644 index 0000000..e44fcf0 --- /dev/null +++ b/include/lnpkg/builder.h @@ -0,0 +1,9 @@ +#ifndef __BUILDER_H__ +#define __BUILDER_H__ + +void lnpkg_write_main(); +void lnpkg_write_node_s(); +void lnpkg_write_node(); +void lnpkg_write_lnpkg_h(); + +#endif \ No newline at end of file diff --git a/include/wnpkg/color.h b/include/lnpkg/color.h similarity index 81% rename from include/wnpkg/color.h rename to include/lnpkg/color.h index f8ede8d..b6ab143 100644 --- a/include/wnpkg/color.h +++ b/include/lnpkg/color.h @@ -8,7 +8,7 @@ #define RED_PRINT(fmt, ...) printf("%s" fmt "%s", RED, ##__VA_ARGS__, RESET) #define GREEN_PRINT(fmt, ...) printf("%s" fmt "%s", GREEN, ##__VA_ARGS__, RESET) -#define YELLOW_PRINT(fmt, ...) \ +#define YELLOW_PRINT(fmt, ...) \ printf("%s" fmt "%s", YELLOW, ##__VA_ARGS__, RESET) #endif \ No newline at end of file diff --git a/include/lnpkg/file.h b/include/lnpkg/file.h new file mode 100644 index 0000000..ca9b707 --- /dev/null +++ b/include/lnpkg/file.h @@ -0,0 +1,33 @@ +#ifndef __FILE_H__ +#define __FILE_H__ + +#include +#include + +#include + +#define LNPKG_FILE_LIST_DIR_INITIAL_CAPACITY 1 + +enum lnpkg_type { LNPKG_FILE, LNPKG_DIR }; + +struct lnpkg_file { + struct dirent* rawdir; + char* name; + enum lnpkg_type filetype; +}; + +struct lnpkg_listdir_data { + struct lnpkg_file* dirs; + size_t len; + size_t capacity; +}; + +int lnpkg_mkdir(char*); +int lnpkg_rmdir(char*); +int lnpkg_havedir(char*); +void lnpkg_rmr(char*); + +struct lnpkg_listdir_data* lnpkg_listdir(char*); +void lnpkg_listdir_close(struct lnpkg_listdir_data*); + +#endif \ No newline at end of file diff --git a/include/wnpkg/file.h b/include/wnpkg/file.h deleted file mode 100644 index f583042..0000000 --- a/include/wnpkg/file.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __FILE_H__ -#define __FILE_H__ - -int wnpkg_mkdir(char *); -int wnpkg_rmdir(char *); -int wnpkg_havedir(char *); - -#endif \ No newline at end of file diff --git a/include/wnpkg/plat.h b/include/wnpkg/plat.h deleted file mode 100644 index 2f02eec..0000000 --- a/include/wnpkg/plat.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __PLAT_H__ -#define __PLAT_H__ - -#if defined(_WIN32) || defined(_WIN64) -#define WNPKG_WINDOWS -#endif - -#endif \ No newline at end of file diff --git a/src/builder.c b/src/builder.c new file mode 100644 index 0000000..573dab6 --- /dev/null +++ b/src/builder.c @@ -0,0 +1,170 @@ +#include "lnpkg/builder.h" + +#include +#include + +#include "lnpkg/color.h" + +void lnpkg_write_main() { + const char* builder = + "#include \n" + "#include \n" + "#include \"lnpkg.h\"\n\n" + "int main(){\n" + " lnpkg_mkdir(\"lnpkg\");\n" + " lnpkg_writenode();\n" + " lnpkg_writeindexjs();\n" + " system(\"chmod +x lnpkg/node\");\n" + " system(\"./lnpkg/node lnpkg/index.js\");\n" + " lnpkg_cleanup();\n" + " return 0;\n" + "}"; + + FILE* app = fopen("lnpkg-build/source/app.c", "w"); + + if (app == NULL) { + RED_PRINT("[Error]: Writing app in lnpkg-build failed.\n"); + exit(1); + } + + fprintf(app, "%s", builder); + fclose(app); + YELLOW_PRINT("[Log]: Application created successfully.\n"); +} + +void lnpkg_write_node_s() { + char builder[2048] = + ".global _binary_node_start\n" + ".global _binary_node_end\n" + ".global _binary_indexjs_start\n" + ".global _binary_indexjs_end\n" + "\n" + ".section .rodata\n" + "_binary_node_start:\n" + " .incbin \"lnpkg-build/source/node\"\n" + "_binary_node_end:\n" + "_binary_indexjs_start:\n" + " .incbin \"lnpkg-build/source/index.js\"\n" + "_binary_indexjs_end:\n"; + FILE* node_s = fopen("lnpkg-build/source/node.s", "w"); + + if (node_s == NULL) { + RED_PRINT("[Error]: Writing node.s in lnpkg-build failed.\n"); + fclose(node_s); + exit(1); + } + + fprintf(node_s, "%s", builder); + fclose(node_s); +} + +void lnpkg_write_lnpkg_h() { + const char builder[] = + "#include \n" + "#include \n" + "\n" + "extern const unsigned char _binary_node_start[];\n" + "extern const unsigned char _binary_node_end[];\n" + "extern const unsigned char _binary_indexjs_start[];\n" + "extern const unsigned char _binary_indexjs_end[];\n" + "\n" + "#include \n" + "#include \n" + "\n" + "int lnpkg_mkdir(char* dir) {\n" + " return mkdir(dir, 0755);\n" + "}\n" + "\n" + "int lnpkg_rmdir(char* dir) {\n" + " return rmdir(dir);\n" + "}\n" + "\n" + "static int lnpkg_writenode() {\n" + " const unsigned char* node = _binary_node_start;\n" + " size_t node_len = (size_t)(_binary_node_end - _binary_node_start);\n" + "\n" + " FILE* node_f = fopen(\"lnpkg/node\", \"wb\");\n" + "\n" + " if (!node_f) {\n" + " perror(\"fopen\");\n" + " return 1;\n" + " }\n" + "\n" + " size_t written = fwrite(node, 1, node_len, node_f);\n" + " fclose(node_f);\n" + "\n" + " if (written != node_len) {\n" + " fprintf(stderr, \"Error: failed to write full node (wrote %zu of " + "%zu bytes)\\n\",\n" + " written, node_len);\n" + " return 1;\n" + " }\n" + "\n" + " return 0;\n" + "}\n" + "\n" + "static int lnpkg_writeindexjs() {\n" + " const unsigned char* indexjs = _binary_indexjs_start;\n" + " size_t indexjs_len = (size_t)(_binary_indexjs_end - " + "_binary_indexjs_start);\n" + "\n" + " FILE* indexjs_f = fopen(\"lnpkg/index.js\", \"wb\");\n" + "\n" + " if (!indexjs_f) {\n" + " perror(\"fopen\");\n" + " return 1;\n" + " }\n" + "\n" + " size_t written = fwrite(indexjs, 1, indexjs_len, indexjs_f);\n" + " fclose(indexjs_f);\n" + "\n" + " if (written != indexjs_len) {\n" + " fprintf(stderr, \"Error: failed to write full indexjs (wrote %zu of " + "%zu bytes)\\n\",\n" + " written, indexjs_len);\n" + " return 1;\n" + " }\n" + "\n" + " return 0;\n" + "}\n" + "\n" + "void lnpkg_cleanup() {\n" + " remove(\"lnpkg/index.js\");\n" + " remove(\"lnpkg/node\");\n" + " lnpkg_rmdir(\"lnpkg\");\n" + "}"; + + FILE* lnpkg_h; + + lnpkg_h = fopen("lnpkg-build/source/lnpkg.h", "w"); + + if (!lnpkg_h) { + RED_PRINT("[Error]: Writing lnpkg.h in lnpkg-build failed.\n"); + exit(1); + } + + fprintf(lnpkg_h, "%s", builder); + fclose(lnpkg_h); +} + +void lnpkg_write_node() { + char cmd[2048]; + int is_exe = 0; + + const char* PREFIX = getenv("PREFIX"); + if (PREFIX == NULL) { + RED_PRINT("[Error]: Failed to get NODE Bin\n"); + return; + } + sprintf(cmd, "cp %s/bin/node lnpkg-build/source/node", PREFIX); + + if (system(cmd) == 0) { + YELLOW_PRINT("[Log]: added %s in source folder.\n", + (is_exe) ? "node.exe" : "node"); + } else { + RED_PRINT( + "[Error]: Unable to move %s file in target folder, file may not " + "exist.\n", + (is_exe) ? "node.exe" : "node"); + } +} \ No newline at end of file diff --git a/src/file.c b/src/file.c index 8e8f1fa..633e045 100644 --- a/src/file.c +++ b/src/file.c @@ -1,40 +1,90 @@ -#include "wnpkg/file.h" +#include "lnpkg/file.h" -#ifdef WNPKG_WINDOWS -#include -#else +#include +#include +#include + +#include #include #include -#endif - -#include "wnpkg/plat.h" -int wnpkg_mkdir(char *dir) -{ -#ifdef WNPKG_WINDOWS - return _mkdir(dir); -#else +int lnpkg_mkdir(char* dir) { return mkdir(dir, 0755); -#endif } -int wnpkg_rmdir(char *dir) -{ -#ifdef WNPKG_WINDOWS - return _rmdir(dir); -#else +int lnpkg_rmdir(char* dir) { return rmdir(dir); -#endif } -int wnpkg_havedir(char *dir) -{ +int lnpkg_havedir(char* dir) { struct stat info; - if(stat(dir, &info) != 0) { + if (stat(dir, &info) != 0) { return 0; - } else if(info.st_mode & __S_IFDIR) { + } else if (S_ISDIR(info.st_mode)) { return 1; } else { return 0; } +} + +struct lnpkg_listdir_data* lnpkg_listdir(char* path) { + struct lnpkg_listdir_data* tld = + malloc(sizeof(struct lnpkg_listdir_data)); + tld->dirs = malloc(sizeof(struct lnpkg_file) * + LNPKG_FILE_LIST_DIR_INITIAL_CAPACITY); // 100 + tld->len = 0; + tld->capacity = LNPKG_FILE_LIST_DIR_INITIAL_CAPACITY; + + DIR* d; + struct dirent* dir; + + d = opendir(path); + + if (d) { + while ((dir = readdir(d)) != NULL) { + if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) + continue; + if (tld->len >= tld->capacity) { + tld->capacity = tld->len + 1; + tld->dirs = realloc(tld->dirs, sizeof(struct lnpkg_file) * tld->capacity); + } + size_t nlen = tld->len++; + tld->dirs[nlen].filetype = (dir->d_type == DT_DIR) ? LNPKG_DIR : LNPKG_FILE; + tld->dirs[nlen].name = strdup(dir->d_name); + tld->dirs[nlen].rawdir = dir; + } + closedir(d); + } + + return tld; +} + +void lnpkg_listdir_close(struct lnpkg_listdir_data* tld) { + for (size_t i = 0; i < tld->len; i++) { + free(tld->dirs[i].name); + } + free(tld->dirs); + free(tld); +} + +void lnpkg_rmr(char* path) { + if (lnpkg_havedir(path) == 1) { + struct lnpkg_listdir_data* build_files = lnpkg_listdir(path); + char fullpath[1024]; + for (size_t i = 0; i < build_files->len; ++i) { + struct lnpkg_file f = build_files->dirs[i]; + char* name = f.name; + if (f.filetype == LNPKG_FILE) { + memset(fullpath, 0, 1024); + snprintf(fullpath, sizeof(fullpath), "%s/%s", path, name); + remove(fullpath); + } else if (f.filetype == LNPKG_DIR) { + memset(fullpath, 0, 1024); + snprintf(fullpath, sizeof(fullpath), "%s/%s", path, name); + lnpkg_rmr(fullpath); + } + } + lnpkg_listdir_close(build_files); + lnpkg_rmdir(path); + } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 50d6b03..a6b47d3 100644 --- a/src/main.c +++ b/src/main.c @@ -2,120 +2,76 @@ #include #include -#include "wnpkg/color.h" -#include "wnpkg/file.h" -#include "wnpkg/plat.h" +#include "lnpkg/builder.h" +#include "lnpkg/color.h" +#include "lnpkg/file.h" -int main(int argc, char *argv[]) -{ - if (argc < 2) - { +#define LNPKG_BUILD_DIR "lnpkg-build" + +int main(int argc, char* argv[]) { + if (argc < 2) { printf("Please provide project folder.\n"); return 1; } - char path[1024]; - char icon[1024]; - char app_name[1024]; - char config_path[1024]; - int letter = 0; - char c; - int use_icon = 0; - - int is_exe = 0; -#ifdef WNPKG_WINDOWS - is_exe = 1; -#endif - - // C program that runs embedded Node.js app -#ifdef WNPKG_WINDOWS - char builder[2048] = "#include \n#include \n\nint " - "main(){\n\tsystem(\"source\\\\node.exe " - "source\\\\index.js\");\n\treturn 0;\n}"; -#else - char builder[2048] = - "#include \n#include \n\nint " - "main(){\n\tsystem(\"chmod +x source/node\");\n\tsystem(\"./source/node " - "source/index.js\");\n\treturn 0;\n}"; -#endif - YELLOW_PRINT("[Log]: Making Builder folder...\n"); - // Remove old build folder - if (wnpkg_rmdir("wnpkg-build") == 0) - { - YELLOW_PRINT( - "[Log]: Old builder folder (wnpkg-build) removed successfully.\n"); - } + lnpkg_rmr(LNPKG_BUILD_DIR); // Create build folder - if (wnpkg_mkdir("wnpkg-build") == 0) - { - YELLOW_PRINT("[Log]: Builder folder (wnpkg-build) created successfully.\n"); - } - else - { + if (lnpkg_mkdir(LNPKG_BUILD_DIR) == 0) { + YELLOW_PRINT("[Log]: Builder folder (lnpkg-build) created successfully.\n"); + } else { printf( - "[Error]: Builder folder (wnpkg-build) may already exist or failed to " + "[Error]: Builder folder (lnpkg-build) may already exist or failed to " "create.\n"); return 1; } // Create source subfolder YELLOW_PRINT("[Log]: Making Source folder...\n"); - if (wnpkg_mkdir("wnpkg-build/source") == 0) - { + if (lnpkg_mkdir("lnpkg-build/source") == 0) { YELLOW_PRINT( - "[Log]: Source folder (wnpkg-build/source) created successfully.\n"); - } - else - { + "[Log]: Source folder (lnpkg-build/source) created successfully.\n"); + } else { printf( - "[Error]: Source folder (wnpkg-build) may already exist or failed to " + "[Error]: Source folder (lnpkg-build) may already exist or failed to " "create.\n"); return 1; } + lnpkg_write_main(); + lnpkg_write_node(); + lnpkg_write_node_s(); + lnpkg_write_lnpkg_h(); + char cmd[2048]; #define RESET_CMD() memset(cmd, 0, sizeof(cmd)) // Copy index.js into build folder -#ifdef WNPKG_WINDOWS - sprintf(cmd, "copy %s\\index.js wnpkg-build\\source\\index.js", argv[1]); - sprintf(config_path, "%s\\wnpkg_config", argv[1]); -#else - sprintf(cmd, "cp %s/index.js wnpkg-build/source/index.js", argv[1]); - sprintf(config_path, "%s/wnpkg_config", argv[1]); -#endif + sprintf(cmd, "cp %s/index.js lnpkg-build/source/index.js", argv[1]); - if (system(cmd) == 0) - { - YELLOW_PRINT("[Log]: added index.js in source folder.\n"); - } - else - { + char config_path[1024]; + sprintf(config_path, "%s/lnpkg_config", argv[1]); + + if (system(cmd) == 0) { + YELLOW_PRINT("[Log]: Added index.js in source folder.\n"); + } else { printf( - "[Error]: Unable to move index.js file in target folder, file may not " + "[Error]: Unable to move index.js file in target folder, file may not " "exist.\n"); return 1; } // Copy package.json into build folder -#ifdef WNPKG_WINDOWS - sprintf(cmd, "copy %s\\package.json wnpkg-build\\source\\package.json", - argv[1]); -#else - sprintf(cmd, "cp %s/package.json wnpkg-build/source/package.json", argv[1]); -#endif - if (system(cmd) == 0) - { - YELLOW_PRINT("[Log]: added package.json in source folder.\n"); - } - else - { + sprintf(cmd, "cp %s/package.json lnpkg-build/source/package.json", argv[1]); + + if (system(cmd) == 0) { + YELLOW_PRINT("[Log]: Added package.json in source folder.\n"); + } else { printf( - "[Error]: Unable to move package.json file in target folder, file may " + "[Error]: Unable to move package.json file in target folder, file may " "not exist.\n"); return 1; } @@ -125,213 +81,52 @@ int main(int argc, char *argv[]) // Copy node_modules into build folder char folder[1024]; -#ifdef WNPKG_WINDOWS - snprintf(folder, sizeof(folder), "%s\\node_modules", argv[1]); - if (wnpkg_havedir(folder)) - { - sprintf( - cmd, - "xcopy /E /I /Q /Y %s wnpkg-build\\source\\node_modules", - folder); - } -#else - if (wnpkg_havedir(folder)) - { + if (lnpkg_havedir(folder)) { snprintf(folder, sizeof(folder), "%s/node_modules", argv[1]); - sprintf(cmd, "cp -rf %s/node_modules wnpkg-build/source/node_modules", argv[1]); + sprintf(cmd, "cp -rf %s/node_modules lnpkg-build/source/node_modules", + argv[1]); } -#endif - if (system(cmd) == 0) - { - YELLOW_PRINT("[Log]: added node modules in source folder.\n"); - } - else - { + if (system(cmd) == 0) { + YELLOW_PRINT("[Log]: Added node modules in source folder.\n"); + } else { printf( - "[Error]: Unable to move node modules file in target folder, file may " + "[Error]: Unable to move node modules file in target folder, file may " "not exist.\n"); return 1; } - // Read app name and icon from configuration file - FILE *fname = fopen(config_path, "r"); - if (fname == NULL) - { - RED_PRINT("[Error]: Config file (wnpkg_config) not found.\n"); + // Read app name from configuration file + FILE* fname = fopen(config_path, "r"); + if (fname == NULL) { + RED_PRINT("[Error]: Config file (lnpkg_config) not found.\n"); fclose(fname); exit(1); } + char app_name[1024]; fgets(app_name, sizeof(app_name), fname); app_name[strcspn(app_name, "\r\n")] = 0; // Parse app name until delimiter + int letter = 0; letter = 0; while (app_name[letter] != ';') letter++; app_name[letter] = '\0'; - fgets(icon, sizeof(icon), fname); - icon[strcspn(icon, "\r\n")] = 0; - - // Parse icon name until delimiter - letter = 0; - while (icon[letter] != ';') - letter++; - icon[letter] = '\0'; - - // Check if user wants default icon - if (strcmp(icon, "*") == 0) - { - YELLOW_PRINT("[Log]: Default icon defined successfully.\n"); - } - else - { - // Copy user-defined icon into source folder - RESET_CMD(); -#ifdef WNPKG_WINDOWS - sprintf(cmd, "copy %s\\%s wnpkg-build\\source\\%s", argv[1], icon, icon); -#else - sprintf(cmd, "cp %s/index.js wnpkg-build/source/index.js", argv[1]); -#endif - if (system(cmd) == 0) - { - YELLOW_PRINT("[Log]: added app icon in source folder.\n"); - } - else - { - printf( - "[Error]: Unable to move icon file in target folder, file may not " - "exist.\n"); - return 1; - } - - // Write resource file (.rc) for the icon - FILE *write_icon = fopen("wnpkg-build\\source\\icon.rc", "w"); - if (write_icon == NULL) - { - RED_PRINT("[Error]: Writing icon file config in wnpkg-build failed.\n"); - fclose(write_icon); - exit(1); - } - - // Generate .rc file content - char icon_data[] = "1 ICON \"wnpkg-build\\source\\"; - strcat(icon_data, icon); - strcat(icon_data, "\""); - - fprintf(write_icon, "%s", icon_data); - fclose(write_icon); - - // Compile icon.rc into icon.o - RESET_CMD(); - snprintf(cmd, sizeof(cmd), - "windres wnpkg-build\\source\\icon.rc -O coff -o " - "wnpkg-build\\source\\icon.o" /*, app_name*/); - if (system(cmd) == 0) - { - YELLOW_PRINT("[Log]: added icon config in source folder.\n"); - } - else - { - RED_PRINT("[Error]: Unable to add icon config file in target folder.\n"); - return 1; - } - use_icon = 1; - } - - // Write app.c source file with embedded launcher -#ifdef WNPKG_WINDOWS - FILE *writeApp = fopen("wnpkg-build\\source\\app.c", "w"); -#else - FILE *writeApp = fopen("wnpkg-build/source/app.c", "w"); -#endif - if (writeApp == NULL) - { - RED_PRINT("[Error]: Writing app in wnpkg-build failed.\n"); - fclose(writeApp); - exit(1); - } - - fprintf(writeApp, "%s", builder); - fclose(writeApp); - YELLOW_PRINT("[Log]: Application created successfully.\n"); - - // Build executable using GCC (with or without icon) + // Build executable using GCC RESET_CMD(); - if (use_icon == 0) - { -#ifdef WNPKG_WINDOWS - snprintf(cmd, sizeof(cmd), - "gcc wnpkg-build/source/app.c -o wnpkg-build/%s.exe " - "-Wl,--subsystem,console", - app_name); -#else - snprintf(cmd, sizeof(cmd), "gcc wnpkg-build/source/app.c -o wnpkg-build/%s", - app_name); -#endif - } - else - { -#ifdef WNPKG_WINDOWS - snprintf(cmd, sizeof(cmd), - "gcc wnpkg-build/source/app.c wnpkg-build/source/icon.o -mwindows " - "-o wnpkg-build/%s.exe -Wl,--subsystem,console", - app_name); -#else - snprintf(cmd, sizeof(cmd), - "gcc wnpkg-build/source/app.c wnpkg-build/source/icon.o -o " - "wnpkg-build/%s", - app_name); -#endif - } - - if (system(cmd) == 0) - { - YELLOW_PRINT("[Log]: added application executable in build folder.\n"); - } - else - { + snprintf(cmd, sizeof(cmd), + "gcc lnpkg-build/source/app.c lnpkg-build/source/node.s -o " + "lnpkg-build/%s", + app_name); + + if (system(cmd) == 0) { + YELLOW_PRINT("[Log]: Added application executable in build folder.\n"); + } else { RED_PRINT( - "[Error]: unable to add application executable in build folder.\n"); - } - - // Copy node.exe into the source folder - RESET_CMD(); -#ifdef WNPKG_WINDOWS - sprintf(cmd, "copy \"\\Program Files\\nodejs\\node.exe\" " - "wnpkg-build\\source\\node.exe"); -#else - char npath[1024]; - FILE *fp = popen("which node", "r"); - if(fp == NULL) { - printf("Error : failed to get node bin."); - exit(1); - } - - if(fgets(npath, sizeof(npath), fp) == NULL) { - printf("Error : node-js not installed, install it."); - pclose(fp); - exit(1); - } - pclose(fp); - npath[strcspn(npath, "\n")] = 0; - - sprintf(cmd, "cp %s wnpkg-build/source/node && chmod +x wnpkg-build/source/node", npath); -#endif - - if (system(cmd) == 0) - { - YELLOW_PRINT("[Log]: added %s in source folder.\n", - (is_exe) ? "node.exe" : "node"); - } - else - { - RED_PRINT("[Error]: Unable to move %s file in target folder, file may not " - "exist.\n", - (is_exe) ? "node.exe" : "node"); - return 1; + "[Error]: Unable to add application executable in build folder.\n"); } fclose(fname);