Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ output/*
debug/
target/
tmp/
tmp_compress
tmp_*/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ refactoring guidance.
## Running through Coze OpenAPI
1. Set .env file for configuration on ABCoder's working directory. Taking Coze as an example:
```
REPO_DIR={YOU_REPO_PATH}
CACHE_DIR={YOUR_CACHE_PATH}
TOOLS_DIR={ABCODER_PATH}/toos
# Base directory for other XXX_DIRs, default to current directory
WORK_DIR=./

# DIRs below support both relative and absolute paths, and relative paths are relative to WORK_DIR
REPO_DIR=tmp_repos
CACHE_DIR=tmp_caches
TOOLS_DIR=tools
EXCLUDE_DIRS=target,gen-codes

# coze|ollama
Expand Down
1 change: 1 addition & 0 deletions src/bin/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ fn export_compress(repo_path: &String, cmp: &CompressAction) {
let repo = repo::get_repo(repo_path, !cmp.not_load_external_symbol);
if let Err(err) = repo {
println!("get repo error: {:?}", err);

process::exit(1);
}

Expand Down
51 changes: 46 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum Language {

#[derive(Debug)]
pub struct Config {
pub work_dir: String,
pub repo_dir: String,
pub cache_dir: String,
pub parser_dir: String,
Expand All @@ -44,16 +45,24 @@ pub struct Config {
pub exclude_dirs: Vec<String>,
}

fn default_work_dir() -> String {
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string()
}

fn default_repo_dir() -> String {
"tmp".to_string()
"tmp_repos".to_string()
}

fn default_tools_dir() -> String {
"tools".to_string()
}

fn default_cache_dir() -> String {
"tmp_compress".to_string()
"tmp_caches".to_string()
}

fn default_parser_dir() -> String {
Expand All @@ -71,6 +80,7 @@ fn default_maas_model_name() -> String {
impl Config {
pub fn new() -> Self {
Self {
work_dir: default_work_dir(),
repo_dir: default_repo_dir(),
cache_dir: default_cache_dir(),
parser_dir: default_parser_dir(),
Expand All @@ -86,8 +96,9 @@ impl Config {
}
}

pub fn from_env() -> Self {
Self {
pub fn parse_from_env() -> Self {
let mut s = Self {
work_dir: std::env::var("WORK_DIR").unwrap_or_else(|_| default_work_dir()),
repo_dir: std::env::var("REPO_DIR").unwrap_or_else(|_| default_repo_dir()),
cache_dir: std::env::var("CACHE_DIR").unwrap_or_else(|_| default_cache_dir()),
parser_dir: std::env::var("PARSER_DIR").unwrap_or_else(|_| default_parser_dir()),
Expand All @@ -109,14 +120,44 @@ impl Config {
_ => Language::Chinese,
})
.unwrap_or(Language::Chinese),
};

if !s.repo_dir.starts_with("/") {
s.repo_dir = Path::new(&s.work_dir)
.join(s.repo_dir)
.to_str()
.unwrap()
.to_string();
}
if !s.cache_dir.starts_with("/") {
s.cache_dir = Path::new(&s.work_dir)
.join(s.cache_dir)
.to_str()
.unwrap()
.to_string();
}
if !s.parser_dir.starts_with("/") {
s.parser_dir = Path::new(&s.work_dir)
.join(s.parser_dir)
.to_str()
.unwrap()
.to_string();
}
if !s.tools_dir.starts_with("/") {
s.tools_dir = Path::new(&s.work_dir)
.join(s.tools_dir)
.to_str()
.unwrap()
.to_string();
}
s
}
}

lazy_static! {
pub static ref CONFIG: Config = {
dotenv::dotenv().ok();
Config::from_env()
Config::parse_from_env()
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub fn get_repo(repo_path: &String, load_extern: bool) -> Result<Repository, Err
fn parse_repo(name: &str, path: &Path, load_extern: bool) -> Result<Vec<u8>, Error> {
let (parser, args) = config::parser_and_args(path.to_str().unwrap(), load_extern);
// parse the repo by parse
match cmd::run_command_bytes(&parser, args, path.to_str().unwrap()) {
match cmd::run_command_bytes(&parser, args) {
Ok(output) => {
cache::get_cache().put(&name, output.clone()).unwrap();
return Ok(output);
Expand Down
15 changes: 6 additions & 9 deletions src/utils/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2025 CloudWeGo Authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -19,10 +19,7 @@ use std::{env, os};
pub fn run_command(cmd: &str, args: Vec<&str>) -> Result<String, Error> {
println!("execute command: {} {:?}", cmd, args);
// get current directory
let output = Command::new(cmd)
.args(args)
.current_dir(env::current_dir()?.to_str().unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_dir为啥要拿掉?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 cwd 默认和父进程保持一致就行 & 执行命令的 path 统一在收敛在最外面计算(基于 CONFIG 中的字段),统一管理

.output()?;
let output = Command::new(cmd).args(args).output()?;

match output.status.success() {
true => Ok(String::from_utf8_lossy(&output.stdout).into_owned()),
Expand All @@ -34,9 +31,9 @@ pub fn run_command(cmd: &str, args: Vec<&str>) -> Result<String, Error> {
}
}

pub fn run_command_bytes(cmd: &str, args: Vec<String>, cwd: &str) -> Result<Vec<u8>, Error> {
pub fn run_command_bytes(cmd: &str, args: Vec<String>) -> Result<Vec<u8>, Error> {
println!("execute command: {} {:?}", cmd, args);
let output = Command::new(cmd).args(args).current_dir(cwd).output()?;
let output = Command::new(cmd).args(args).output()?;

match output.status.success() {
true => Ok(output.stdout),
Expand Down