一键式项目打包运行工具
基于现有项目目录,无需任何改动,直接压缩并配置即可运行。
项目结构
```
ProjectPacker/
├── bin/
│ └── ProjectPacker # 主程序
├── templates/ # 配置模板
│ ├── default.json # 默认配置
│ ├── webapp.json # Web应用模板
│ └── desktop.json # 桌面应用模板
├── runtime/ # 运行时环境
│ ├── jre/ # Java运行时
│ ├── nodejs/ # Node.js运行时
│ └── python/ # Python环境
└── output/ # 输出目录
```
核心代码实现
1. 智能项目检测器 (src/ProjectDetector.cpp)
```cpp
class ProjectDetector {
public:
struct ProjectInfo {
std::string type; // 项目类型: nodejs, python, java, executable, etc.
std::string mainProgram; // 主程序路径
std::vector<std::string> dependencies; // 依赖文件
std::map<std::string, std::string> config; // 配置信息
std::vector<std::string> ignorePatterns; // 忽略模式
};
ProjectInfo detectProject(const std::string& projectDir) {
ProjectInfo info;
// 自动检测项目类型
if (fs::exists(projectDir + "/package.json")) {
info.type = "nodejs";
info.mainProgram = detectNodeMain(projectDir);
} else if (fs::exists(projectDir + "/requirements.txt")) {
info.type = "python";
info.mainProgram = detectPythonMain(projectDir);
} else if (fs::exists(projectDir + "/pom.xml") ||
fs::exists(projectDir + "/build.gradle")) {
info.type = "java";
info.mainProgram = detectJavaMain(projectDir);
} else {
info.type = "executable";
info.mainProgram = findExecutable(projectDir);
}
// 收集依赖文件
info.dependencies = collectDependencies(projectDir, info.type);
// 生成忽略模式
info.ignorePatterns = generateIgnorePatterns(info.type);
return info;
}
private:
std::string detectNodeMain(const std::string& dir) {
// 解析package.json找到main字段
auto packageJson = readJsonFile(dir + "/package.json");
return packageJson["main"].asString("index.js");
}
std::string detectPythonMain(const std::string& dir) {
// 查找__main__.py或包含if __name__ == "__main__"的文件
for (const auto& entry : fs::recursive_directory_iterator(dir)) {
if (entry.path().extension() == ".py") {
std::string content = readFile(entry.path().string());
if (content.find("if __name__ == \"__main__\"") != std::string::npos) {
return entry.path().filename().string();
}
}
}
return "app.py"; // 默认
}
std::vector<std::string> collectDependencies(const std::string& dir, const std::string& type) {
std::vector<std::string> deps;
if (type == "nodejs") {
deps.push_back("package.json");
deps.push_back("package-lock.json");
deps.push_back("node_modules/");
} else if (type == "python") {
deps.push_back("requirements.txt");
deps.push_back("*.py");
// 不包含虚拟环境
} else if (type == "java") {
deps.push_back("src/");
deps.push_back("pom.xml");
deps.push_back("build.gradle");
}
return deps;
}
};
```
2. 一键打包器 (src/ProjectPacker.cpp)
```cpp
class ProjectPacker {
public:
bool packProject(const std::string& projectDir,
const std::string& outputFile,
const std::string& password = "") {
// 1. 检测项目类型
ProjectInfo info = m_detector.detectProject(projectDir);
// 2. 创建临时目录结构
std::string tempDir = createTempDirectory();
setupProjectStructure(tempDir, info);
// 3. 复制项目文件(排除忽略的文件)
copyProjectFiles(projectDir, tempDir + "/project", info.ignorePatterns);
// 4. 生成运行配置
generateRunConfig(tempDir, info);
// 5. 包含运行时环境(如果需要)
includeRuntime(tempDir, info.type);
// 6. 打包为加密容器
return createSecurePackage(tempDir, outputFile, password, info);
}
private:
void generateRunConfig(const std::string& tempDir, const ProjectInfo& info) {
json config;
config["project_type"] = info.type;
config["main_program"] = info.mainProgram;
config["working_dir"] = "project";
config["dependencies"] = info.dependencies;
// 根据项目类型生成启动脚本
if (info.type == "nodejs") {
config["start_command"] = {
"./runtime/node/bin/node",
"project/" + info.mainProgram
};
} else if (info.type == "python") {
config["start_command"] = {
"./runtime/python/bin/python",
"project/" + info.mainProgram
};
} else if (info.type == "java") {
config["start_command"] = {
"./runtime/java/bin/java",
"-jar",
"project/target/*.jar"
};
}
writeJsonFile(tempDir + "/run_config.json", config);
}
void includeRuntime(const std::string& tempDir, const std::string& projectType) {
std::string runtimeSource;
if (projectType == "nodejs") {
runtimeSource = "./runtime/node/";
} else if (projectType == "python") {
runtimeSource = "./runtime/python/";
} else if (projectType == "java") {
runtimeSource = "./runtime/java/";
}
if (!runtimeSource.empty()) {
fs::copy(runtimeSource, tempDir + "/runtime",
fs::copy_options::recursive);
}
}
};
```
3. 通用运行器 (src/ProjectRunner.cpp)
```cpp
class ProjectRunner {
public:
int runPackage(const std::string& packageFile,
const std::string& password,
const std::vector<std::string>& args = {}) {
// 1. 解压到安全临时目录
std::string tempDir = extractPackage(packageFile, password);
// 2. 读取运行配置
auto config = readRunConfig(tempDir);
// 3. 设置环境变量
setupEnvironment(tempDir, config);
// 4. 执行项目
return executeProject(tempDir, config, args);
}
private:
int executeProject(const std::string& tempDir,
const json& config,
const std::vector<std::string>& args) {
std::string command;
std::vector<std::string> fullArgs;
// 构建执行命令
if (config.contains("start_command")) {
for (const auto& arg : config["start_command"]) {
fullArgs.push_back(arg.asString());
}
} else {
// 默认执行逻辑
std::string mainProgram = config["main_program"].asString();
std::string projectType = config["project_type"].asString();
if (projectType == "executable") {
command = tempDir + "/project/" + mainProgram;
} else {
command = tempDir + "/project/" + mainProgram;
}
fullArgs.push_back(command);
}
// 添加用户参数
for (const auto& arg : args) {
fullArgs.push_back(arg);
}
// 执行程序
return executeProcess(fullArgs, tempDir + "/project");
}
void setupEnvironment(const std::string& tempDir, const json& config) {
// 设置PATH包含运行时环境
std::string pathEnv = getEnv("PATH");
std::string newPath = tempDir + "/runtime/bin:" + pathEnv;
setEnv("PATH", newPath);
// 设置项目相关环境变量
setEnv("PROJECT_ROOT", tempDir + "/project");
setEnv("RUNTIME_ROOT", tempDir + "/runtime");
// 设置语言特定环境变量
std::string projectType = config["project_type"].asString();
if (projectType == "python") {
setEnv("PYTHONPATH", tempDir + "/project:" + tempDir + "/runtime/lib");
} else if (projectType == "java") {
setEnv("JAVA_HOME", tempDir + "/runtime");
}
}
};
```
配置文件模板
默认配置 (templates/default.json)
```json
{
"version": "1.0",
"compression": {
"algorithm": "lzma",
"level": 9,
"multithreading": true
},
"encryption": {
"algorithm": "AES-256-GCM",
"key_derivation_iterations": 100000
},
"security": {
"memory_protection": true,
"anti_debug": true,
"environment_check": true
},
"runtime": {
"include_runtime": true,
"auto_cleanup": true,
"temp_dir_lifetime": 3600
}
}
```
Web应用模板 (templates/webapp.json)
```json
{
"project_type": "nodejs",
"web_server": {
"port": 3000,
"host": "0.0.0.0",
"environment": "production"
},
"dependencies": {
"include_node_modules": true,
"include_package_json": true
},
"start_command": [
"./runtime/node/bin/node",
"project/server.js"
],
"environment_variables": {
"NODE_ENV": "production",
"PORT": "3000"
}
}
```
使用示例
1. 一键打包现有项目
```bash
# 打包Node.js项目
ProjectPacker pack ./my-node-app ./app.packed
# 打包Python项目(带密码)
ProjectPacker pack ./my-python-app ./app.secure -p "secret123"
# 打包Java项目使用特定模板
ProjectPacker pack ./my-java-app ./app.jar -t java-app
```
2. 运行打包的项目
```bash
# 运行打包的项目
ProjectPacker run ./app.packed
# 运行加密项目(需要密码)
ProjectPacker run ./app.secure -p "secret123"
# 带参数运行
ProjectPacker run ./app.packed -- --port 8080 --debug
```
3. 查看项目信息
```bash
# 查看包内信息
ProjectPacker info ./app.packed
# 列出包内文件
ProjectPacker list ./app.packed
# 验证包完整性
ProjectPacker verify ./app.packed
```
智能检测功能
支持的项目类型检测:
1. Node.js项目: 检测package.json, 找到main入口
2. Python项目: 检测requirements.txt, 找到main文件
3. Java项目: 检测pom.xml或build.gradle
4. Go项目: 检测go.mod, 找到main包
5. 可执行文件: 自动查找可执行文件
6. 静态网站: 检测index.html, 配置Web服务器
7. 二进制应用: 检测二进制文件及其依赖库
自动依赖收集:
· Node.js: node_modules, package.json
· Python: requirements.txt, *.py文件
· Java: JAR文
2431

被折叠的 条评论
为什么被折叠?



