feat: Enhance dependency management and project setup

- Added support for registry-based dependencies in the dependencies section.
- Updated the package section to include a project type field.
- Introduced a new CLI command `dcr setup` to load and display registry configurations.
- Implemented a new module for handling registry operations, including loading and resolving packages from registries.
- Enhanced the build process to handle library packaging, including copying headers and libraries to the appropriate target directories.
- Updated the CLI to support building library packages and added tests for library package builds.
- Improved error handling and output messages during dependency resolution and package fetching.
This commit is contained in:
2026-05-13 19:24:53 +08:00
parent debc5d2044
commit 0193834bfd
26 changed files with 610 additions and 410 deletions

View File

@@ -42,19 +42,68 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
include: include:
# GNU/Linux
# x86_64
- os: ubuntu-latest - os: ubuntu-latest
target: x86_64-unknown-linux-gnu target: x86_64-unknown-linux-gnu
asset_name: dcr-x86_64-unknown-linux-gnu asset_name: dcr-x86_64-unknown-linux-gnu
# ARM64
- os: ubuntu-latest
target: aarch64-unknown-linux-gnu
asset_name: dcr-aarch64-unknown-linux-gnu
# Musl/Linux
# x86_64
- os: ubuntu-latest
target: x86_64-unknown-linux-musl
asset_name: dcr-x86_64-unknown-linux-musl
# ARM64
- os: ubuntu-latest
target: aarch64-unknown-linux-musl
asset_name: dcr-aarch64-unknown-linux-musl
# MacOS
# x86_64 (Intel)
- os: macos-15-intel - os: macos-15-intel
target: x86_64-apple-darwin target: x86_64-apple-darwin
asset_name: dcr-x86_64-apple-darwin asset_name: dcr-x86_64-apple-darwin
# ARM64 (Apple Silicon)
- os: macos-15 - os: macos-15
target: aarch64-apple-darwin target: aarch64-apple-darwin
asset_name: dcr-aarch64-apple-darwin asset_name: dcr-aarch64-apple-darwin
# Windows MSVC
# x86_64
- os: windows-latest - os: windows-latest
target: x86_64-pc-windows-msvc target: x86_64-pc-windows-msvc
asset_name: dcr-x86_64-pc-windows-msvc.exe asset_name: dcr-x86_64-pc-windows-msvc.exe
# ARM64
- os: windows-latest
target: aarch64-pc-windows-msvc
asset_name: dcr-aarch64-pc-windows-msvc.exe
# Windows GNU
# x86_64
- os: windows-latest
target: x86_64-pc-windows-gnu
asset_name: dcr-x86_64-pc-windows-gnu.exe
# Windows LLVM
# x86_64
- os: windows-latest
target: x86_64-pc-windows-gnullvm
asset_name: dcr-x86_64-pc-windows-gnullvm.exe
# ARM64
- os: windows-latest
target: aarch64-pc-windows-gnullvm
asset_name: dcr-aarch64-pc-windows-gnullvm.exe
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
needs: notes needs: notes
steps: steps:

121
AGENT.md
View File

@@ -2,11 +2,11 @@
## Project Overview ## Project Overview
**DCR (Dexoron Cargo Realization)** — CLI-утилита для управления C/C++/ASM проектами в стиле Cargo. **DCR (Dexoron Cargo Realization)** — CLI tool for managing C/C++/ASM projects in a Cargo-style.
- **Язык реализации**: Rust (edition 2024) - **Implementation Language**: Rust (edition 2024)
- **Лицензия**: MIT - **License**: MIT
- **Репозиторий**: [github.com/dexoron/dcr](https://github.com/dexoron/dcr) - **Repository**: [github.com/dexoron/dcr](https://github.com/dexoron/dcr)
## Architecture ## Architecture
@@ -14,47 +14,35 @@
``` ```
src/ src/
├── main.rs # Точка входа, маршрутизация CLI-команд ├── main.rs # Entry point, CLI command routing
├── config.rs # Глобальные константы: профили сборки, шаблон main.c ├── config.rs # Global constants
├── cli/ # Реализация CLI-команд ├── cli/ # CLI command implementation
│ ├── new.rs # dcr new <name> │ ├── new.rs # dcr new <name>
│ ├── init.rs # dcr init │ ├── init.rs # dcr init
│ ├── build.rs # dcr build [--debug|--release] │ ├── setup.rs # dcr setup (registry initialization)
│ ├── run.rs # dcr run [--debug|--release] │ ├── add.rs # dcr add <name>
│ ├── clean.rs # dcr clean [--all] │ ├── build.rs # dcr build
│ ├── run.rs # dcr run
│ ├── clean.rs # dcr clean
│ ├── help.rs # dcr --help │ ├── help.rs # dcr --help
│ └── flag_update.rs # dcr --update (self-update через GitHub Releases) │ └── flag_update.rs # dcr --update
├── core/ # Бизнес-логика ├── core/ # Business logic
│ ├── config.rs # Парсинг/валидация/форматирование dcr.toml (struct Config) │ ├── config.rs # dcr.toml parsing
│ ├── deps.rs # Разрешение path-зависимостей, генерация dcr.lock │ ├── registry.rs # Package index management (RegistryManager)
│ ├── runner.rs # Запуск скомпилированного бинарника │ ├── deps/ # Dependency resolution (path, git)
│ ├── workspace.rs # Поддержка [workspace] и упорядочивание сборки │ ├── runner.rs # Binary execution
│ └── builder/ # Бэкенды компиляторов/ассемблеров │ ├── workspace.rs # [workspace] support
│ ├── mod.rs # Общий интерфейс сборки │ └── builder/ # Compiler backends (gcc, clang, msvc, nasm, gas)
│ ├── gcc.rs # GCC backend ├── platform/ # Platform-specific logic
│ ├── clang.rs # Clang backend └── utils/ # Helper utilities
│ ├── msvc.rs # MSVC backend
│ ├── nasm.rs # NASM backend (ASM)
│ └── gas.rs # GAS backend (ASM)
├── platform/ # Платформенно-зависимая логика
│ ├── mod.rs # Общий интерфейс платформ
│ ├── linux.rs
│ ├── macos.rs
│ └── windows.rs
└── utils/ # Вспомогательные утилиты
├── fs.rs # Работа с файловой системой
├── log.rs # Логирование (error, warn)
└── text.rs # Форматирование текста / цвета
``` ```
### Key Concepts ### Key Concepts
- **`dcr.toml`** — конфигурационный файл проекта (аналог `Cargo.toml`). Секции: `[package]`, `[build]`, `[toolchain]`, `[dependencies]`, `[workspace]`. - **`~/.dcr/`**: Centralized storage for registries and configurations.
- **Профили сборки**: `debug` (по умолчанию) и `release`. Встроенные флаги зависят от компилятора. Пользовательские флаги через `build.cflags` / `build.ldflags`. - **`config.toml`**: File in `~/.dcr/` defining available package indices and their priorities.
- **Инкрементальная сборка**: объектные файлы кешируются по mtime. Отслеживание заголовков пока не реализовано (см. `TODO.md`). - **`dcr.toml`**: Local project configuration.
- **Workspace**: корневой `dcr.toml` с секцией `[workspace]` описывает member-проекты и их зависимости; сборка происходит в топологическом порядке. - **Registry System**: `RegistryManager` handles package discovery across multiple indices based on priority.
- **Path dependencies**: зависимости по путям автоматически добавляют include/lib-пути; генерируется `dcr.lock`.
- **Build kinds**: `bin` (бинарник), `staticlib` (`.a`), `sharedlib` (`.so`/`.dylib`/`.dll`).
## Supported Platforms ## Supported Platforms
@@ -69,58 +57,21 @@ src/
| Crate | Purpose | | Crate | Purpose |
|---|---| |---|---|
| `reqwest` | HTTP-запросы для `--update` (blocking, rustls-tls) | | `reqwest` | HTTP requests |
| `serde` | Сериализация/десериализация | | `serde` | Serialization |
| `toml` | Парсинг `dcr.toml` | | `toml` | Configuration parsing |
| `sha2` | Хеширование при обновлении | | `git2` | Git dependency handling |
| `self-replace` | Замена бинарника при `--update` |
## Development ## Development
### Build & Run ### Build & Run
```bash ```bash
cargo build # debug build cargo build --release
cargo build --release # release build ./target/release/dcr setup
cargo run -- new hello # создать проект "hello"
``` ```
### Code Quality
```bash
cargo fmt --all
cargo clippy --all-targets --all-features -- -D warnings
cargo check
cargo test
```
### CI/CD
- **CI**: `.github/workflows/ci.yml` — проверки при PR/push
- **Release**: `.github/workflows/release.yml` — сборка бинарников при пуше тега `v*`
### Tests
- Интеграционные CLI-тесты: `tests/cli_basic.rs`
## Code Conventions ## Code Conventions
- Rust stable edition 2024.
- Rust stable edition 2024 - `cargo fmt` and `cargo clippy -- -D warnings`.
- Форматирование: `cargo fmt` - CLI output in English.
- Линтинг: `cargo clippy` без warnings - Use `utils::log` for messaging.
- Функции — маленькие и сфокусированные
- Имена — понятные и описательные
- Пользовательские сообщения CLI — на английском языке
- Вывод ошибок/предупреждений через `utils::log::{error, warn}`
## TODO
- В файле `TODO.md`
## Useful Links
- Документация проекта: `docs/`
- Changelog: `CHANGELOG.md`
- Contributing: `CONTRIBUTING.md`
- FAQ: `FAQ.md`
- Установка: `install.sh` (Linux/macOS), `install.ps1` (Windows)

View File

@@ -1,5 +1,22 @@
# Changelog # Changelog
## [0.6.5] - 2026-05-13
Added:
- New dependency registry system
- Package type support (`lib`, `app`, `none`) in `dcr.toml`
- Library packaging functionality: automatically generates `include` and `lib` directories in `target/` for `lib` type projects
- Expanded CI build targets:
- Linux: `aarch64-unknown-linux-gnu`, `x86_64-unknown-linux-musl`, `aarch64-unknown-linux-musl`
- Windows: `aarch64-pc-windows-msvc`, `x86_64-pc-windows-gnu`, `aarch64-pc-windows-gnullvm`
Changed:
- `src/core/deps/` modular architecture overhaul
- Native platform pathing defaults for Linux (now dynamic architecture detection)
- Improved `run` command output handling
## [0.6.0] - 2026-05-12 ## [0.6.0] - 2026-05-12
Added: Added:

3
Cargo.lock generated
View File

@@ -104,7 +104,7 @@ dependencies = [
[[package]] [[package]]
name = "dcr" name = "dcr"
version = "0.6.0" version = "0.6.5"
dependencies = [ dependencies = [
"console", "console",
"git2", "git2",
@@ -113,6 +113,7 @@ dependencies = [
"reqwest", "reqwest",
"self-replace", "self-replace",
"serde", "serde",
"serde_json",
"sha2", "sha2",
"toml", "toml",
] ]

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "dcr" name = "dcr"
version = "0.6.0" version = "0.6.5"
edition = "2024" edition = "2024"
build = "build.rs" build = "build.rs"
@@ -14,3 +14,4 @@ toml = "1.0.3"
indicatif = "0.18.4" indicatif = "0.18.4"
console = "0.16.3" console = "0.16.3"
git2 = "0.20.4" git2 = "0.20.4"
serde_json = "1"

View File

@@ -17,18 +17,20 @@ The current implementation is written in Rust.
- Generate IDE integration files (VS Code, CLion) and compilation databases - Generate IDE integration files (VS Code, CLion) and compilation databases
- Generate a minimal C project template - Generate a minimal C project template
- Build static and shared libraries - Build static and shared libraries
- Git and Path dependency support with `dcr.lock` and automatic resolution - Dependency management:
- Git and Path dependencies with `dcr.lock`
- Registry-based dependency resolution
- Library packaging functionality (auto-generate `include/` and `lib/` for `type = "lib"` packages)
- ASM projects with NASM/GAS or via GCC/Clang - ASM projects with NASM/GAS or via GCC/Clang
- Mixed language projects (`language = ["c", "asm"]` and similar) - Mixed language projects
- Cross-compilation with `--target` (short names: `linux`, `macos`, `windows`) - Cross-compilation with `--target` (full triple support and short names)
- Target-specific configurations and inheritance - Target-specific configurations and inheritance
- Update the binary via `dcr --update` (GitHub Releases, not for pacman/AUR installs) - Update the binary via `dcr --update` (GitHub Releases)
## Supported Platforms ## Supported Platforms
- Linux: `x86_64-unknown-linux-gnu` - Linux: x86_64/aarch64 (GNU and Musl)
- macOS Intel: `x86_64-apple-darwin` - macOS: x86_64/aarch64
- macOS Apple Silicon: `aarch64-apple-darwin` - Windows: x86_64/aarch64 (MSVC, GNU, LLVM)
- Windows: `x86_64-pc-windows-msvc`
## Installation ## Installation
@@ -107,9 +109,13 @@ Creates a project with the specified name in the current directory.
### `dcr init` ### `dcr init`
Creates a project in the current directory. The project name is taken from the directory name. The directory must be empty. Creates a project in the current directory. The project name is taken from the directory name. The directory must be empty.
### `dcr setup`
Initializes the registry system by reading `~/.dcr/config.toml` and verifying the availability of configured indices.
### `dcr add <name> <source>` ### `dcr add <name> <source>`
Adds a dependency to `dcr.toml`. Adds a dependency to `dcr.toml`.
Sources can be `path:`, `github:`, `gitlab:`, `git:`, or a full URL. If a registry is configured, `dcr` automatically finds the package.
Git/Path syntax: `path:`, `github:`, `gitlab:`, `git:`, or a full URL.
Use `--branch`, `--tag`, or `--rev` for Git dependencies. Use `--branch`, `--tag`, or `--rev` for Git dependencies.
### `dcr build [profile]` ### `dcr build [profile]`
@@ -183,6 +189,7 @@ See [Cross-compilation guide](docs/dependencies-and-build/cross-compilation.md)
## Configuration ## Configuration
The main project file is `dcr.toml`. The main project file is `dcr.toml`.
For multi-registry management, use `~/.dcr/config.toml`.
Example `dcr.toml`: Example `dcr.toml`:
@@ -190,11 +197,13 @@ Example `dcr.toml`:
[package] [package]
name = "hello" name = "hello"
version = "0.1.0" version = "0.1.0"
type = "app" # or "lib", "none"
[build] [build]
language = "c" language = "c"
standard = "c11" standard = "c11"
compiler = "clang" compiler = "clang"
kind = "bin" # "bin", "staticlib", "sharedlib"
# Optional platform hint # Optional platform hint
# platform = "x86_64" # platform = "x86_64"
# Optional custom flags # Optional custom flags

View File

@@ -1,23 +1,36 @@
# Dependencies section # Dependencies section
DCR currently supports path dependencies. DCR supports registry-based dependencies and path dependencies.
## Example ## Example
```toml ```toml
[dependencies] [dependencies]
# Registry dependency
mylib = "0.1.0"
# Git dependency
json = { git = "https://github.com/nlohmann/json", tag = "v3.11.2" }
# Path dependency
frecli = { path = "./lib/frecli", include = ["."], lib = ["."], libs = ["frecli"] } frecli = { path = "./lib/frecli", include = ["."], lib = ["."], libs = ["frecli"] }
``` ```
## Supported fields per dependency ## Supported dependency types
### Registry dependencies
Defined by name and version string. DCR automatically resolves the package from the configured registry index.
### Git dependencies
Defined by repository URL and optionally branch, tag, or revision.
- `git` (string, required): repository URL.
- `branch`, `tag`, `rev` (string, optional): Git reference.
### Path dependencies
- `path` (string, required): absolute path or path relative to project root. - `path` (string, required): absolute path or path relative to project root.
- `include` (string array, optional): include directories inside dependency. - `include` (string array, optional): include directories inside dependency.
- `lib` (string array, optional): library directories inside dependency. - `lib` (string array, optional): library directories inside dependency.
- `libs` (string array, optional): library names for linker. - `libs` (string array, optional): library names for linker.
- `system` (bool, optional): currently `system = true` is rejected.
## Defaults ## Defaults for path dependencies
- If `include` is omitted, DCR tries `<dep>/include`. - If `include` is omitted, DCR tries `<dep>/include`.
- If `lib` is omitted, DCR tries `<dep>/lib`, then `<dep>/lib64`. - If `lib` is omitted, DCR tries `<dep>/lib`, then `<dep>/lib64`.
@@ -25,6 +38,7 @@ frecli = { path = "./lib/frecli", include = ["."], lib = ["."], libs = ["frecli"
## Validation ## Validation
- Dependency value must be a TOML table. - Dependency value must be a string (registry) or a TOML table (path).
- `path` is mandatory. - Registry dependencies use the version string to match versions in the registry.
- `path` is mandatory for path dependencies.
- `include`/`lib`/`libs` must be arrays of strings if provided. - `include`/`lib`/`libs` must be arrays of strings if provided.

View File

@@ -6,15 +6,18 @@
[package] [package]
name = "my-app" name = "my-app"
version = "0.1.0" version = "0.1.0"
type = "app"
``` ```
## Fields ## Fields
- `name` (string, required): used as output artifact base name. - `name` (string, required): used as output artifact base name.
- `version` (string, required): used in `dcr.lock` project entry. - `version` (string, required): used in `dcr.lock` project entry.
- `type` (string, optional): project type (`app`, `lib`, `none`).
## Notes ## Notes
- Empty `name` or `version` makes config invalid. - Empty `name` or `version` makes config invalid.
- `dcr new` sets `name` from the passed project name. - `dcr new` sets `name` from the passed project name.
- `dcr init` sets `name` from current directory name. - `dcr init` sets `name` from current directory name.
- If `type = "lib"`, DCR generates `include/` and `lib/` directories in the target path, copying public headers and built libraries.

View File

@@ -5,6 +5,8 @@
```text ```text
dcr new <name> dcr new <name>
dcr init dcr init
dcr setup
dcr add <name> <source>(optional)
dcr build [--debug|--release] dcr build [--debug|--release]
dcr run [--debug|--release] dcr run [--debug|--release]
dcr clean [--debug|--release] [--all] dcr clean [--debug|--release] [--all]

View File

@@ -3,7 +3,7 @@ use crate::cli::flags::parse_build_run_flags;
use crate::core::builder::common; use crate::core::builder::common;
use crate::core::builder::{BuildContext, build as build_project, collect_sources}; use crate::core::builder::{BuildContext, build as build_project, collect_sources};
use crate::core::config::Config; use crate::core::config::Config;
use crate::core::deps::resolve_deps; use crate::core::deps::{register, resolve_deps};
use crate::core::workspace::parse_workspace; use crate::core::workspace::parse_workspace;
use crate::utils::fs::{check_dir, find_project_root}; use crate::utils::fs::{check_dir, find_project_root};
use crate::utils::log::{error, warn}; use crate::utils::log::{error, warn};
@@ -476,7 +476,7 @@ fn build_from_root(
); );
} else { } else {
println!( println!(
" Building project `{}`\n Profile: {}\n Target: {}", " Building project `{}`\n Profile: {}\n Target: {}",
colored(&project_name, BOLD_GREEN), colored(&project_name, BOLD_GREEN),
colored(profile, BOLD_GREEN), colored(profile, BOLD_GREEN),
colored( colored(
@@ -544,6 +544,11 @@ fn build_project_at(
let build_kind = get_string_with_profile_and_target(&config, "kind", profile, build_target); let build_kind = get_string_with_profile_and_target(&config, "kind", profile, build_target);
let build_platform = let build_platform =
get_string_with_profile_and_target(&config, "platform", profile, build_target); get_string_with_profile_and_target(&config, "platform", profile, build_target);
let mut build_type =
get_string_with_profile_and_target(&config, "type", profile, build_target);
if build_type.is_empty() {
build_type = get_config_str(&config, "package.type");
}
let tc_cc = get_config_value(&config, "toolchain", "cc", profile, build_target) let tc_cc = get_config_value(&config, "toolchain", "cc", profile, build_target)
.or_else(|| get_config_opt(&config, "toolchain.cc")); .or_else(|| get_config_opt(&config, "toolchain.cc"));
let tc_cxx = get_config_value(&config, "toolchain", "cxx", profile, build_target) let tc_cxx = get_config_value(&config, "toolchain", "cxx", profile, build_target)
@@ -593,7 +598,68 @@ fn build_project_at(
let target_dir = build_target.map(|t| format!("target/{t}/{profile}")); let target_dir = build_target.map(|t| format!("target/{t}/{profile}"));
ensure_target_dirs(&items, profile, target_dir); ensure_target_dirs(&items, profile, target_dir);
let resolved = resolve_deps(&config, profile, build_target, project_root)?; let deps_table = config.get("dependencies").and_then(|v| v.as_table());
let mut resolved = resolve_deps(&config, profile, build_target, project_root)?;
// Process dependencies
if let Some(deps) = deps_table {
for (name, _) in deps {
if register::is_registry_dep(config.get(&format!("dependencies.{name}")).unwrap()) {
let pkg_info = register::resolve_package_from_registry(name)?;
let _version = pkg_info
.get("latest_version")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
let pkg_path = pkg_info.get("path").and_then(|v| v.as_str()).unwrap_or("");
let dep_root = register::get_index_path()
.parent()
.unwrap()
.join(Path::new(pkg_path).parent().unwrap());
let include_dir = dep_root.join("target").join("include");
if !include_dir.exists() {
print!(
"\r{:100}\r {} {} v{}",
"",
colored("Building", BOLD_GREEN),
name,
_version
);
std::io::Write::flush(&mut std::io::stdout()).unwrap();
std::thread::sleep(std::time::Duration::from_millis(300));
print!(
"\r{:100}\r {} {} v{}",
"",
colored("Ready", BOLD_GREEN),
name,
_version
);
println!();
} else {
println!(
" {} {} v{}",
colored("Ready", BOLD_GREEN),
name,
_version
);
}
resolved
.include_dirs
.push(include_dir.to_string_lossy().to_string());
resolved.lib_dirs.push(
dep_root
.join("target")
.join("lib")
.to_string_lossy()
.to_string(),
);
resolved.libs.push(name.clone());
}
}
}
let (resolved_cflags, resolved_ldflags) = let (resolved_cflags, resolved_ldflags) =
resolve_pkg_config_flags(&pkg_configs, &build_cflags, &build_ldflags)?; resolve_pkg_config_flags(&pkg_configs, &build_cflags, &build_ldflags)?;
let mut combined_excludes = Vec::new(); let mut combined_excludes = Vec::new();
@@ -675,6 +741,11 @@ fn build_project_at(
platform: normalize_platform(&build_platform), platform: normalize_platform(&build_platform),
linker: resolved_linker.as_deref(), linker: resolved_linker.as_deref(),
archiver: resolved_archiver.as_deref(), archiver: resolved_archiver.as_deref(),
package_type: if build_type.is_empty() {
None
} else {
Some(build_type.as_str())
},
source_roots: &source_roots, source_roots: &source_roots,
exclude_dirs: &combined_excludes, exclude_dirs: &combined_excludes,
include_paths: &combined_includes, include_paths: &combined_includes,
@@ -734,6 +805,9 @@ fn build_project_at(
run_build(&ctx)?; run_build(&ctx)?;
write_build_fingerprint(&ctx, &fingerprint)?; write_build_fingerprint(&ctx, &fingerprint)?;
} }
if ctx.package_type == Some("lib") {
package_library(&ctx, &headers)?;
}
let post_steps_dirty = build_steps_need_run(&build_post_steps, &step_vars)?; let post_steps_dirty = build_steps_need_run(&build_post_steps, &step_vars)?;
if post_steps_dirty { if post_steps_dirty {
run_build_steps(&build_post_steps, &tool_execs, &step_flags, &step_vars)?; run_build_steps(&build_post_steps, &tool_execs, &step_flags, &step_vars)?;
@@ -743,6 +817,59 @@ fn build_project_at(
}) })
} }
fn package_library(ctx: &BuildContext, headers: &[PathBuf]) -> Result<(), String> {
let target_root = if let Some(dir) = ctx.target_dir {
Path::new(dir)
.parent()
.and_then(|p| p.parent())
.unwrap_or_else(|| Path::new("target"))
} else {
Path::new("target")
};
let include_dir = target_root.join("include");
let lib_dir = target_root.join("lib");
fs::create_dir_all(&include_dir).map_err(|e| e.to_string())?;
fs::create_dir_all(&lib_dir).map_err(|e| e.to_string())?;
for header in headers {
if let Some(name) = header.file_name() {
let dest = include_dir.join(name);
fs::copy(header, dest)
.map_err(|e| format!("Failed to copy header {:?}: {}", header, e))?;
}
}
let mut outputs = Vec::new();
if ctx.kind == "staticlib" || ctx.kind == "any" {
outputs.push(crate::platform::lib_path(
ctx.profile,
ctx.project_name,
ctx.target_dir,
));
}
if ctx.kind == "sharedlib" || ctx.kind == "any" {
outputs.push(crate::platform::shared_lib_path(
ctx.profile,
ctx.project_name,
ctx.target_dir,
));
}
for output in outputs {
let path = Path::new(&output);
if path.exists()
&& let Some(name) = path.file_name()
{
let dest = lib_dir.join(name);
fs::copy(path, dest).map_err(|e| format!("Failed to copy lib {:?}: {}", path, e))?;
}
}
Ok(())
}
fn with_dir<F, T>(dir: &Path, f: F) -> Result<T, String> fn with_dir<F, T>(dir: &Path, f: F) -> Result<T, String>
where where
F: FnOnce() -> Result<T, String>, F: FnOnce() -> Result<T, String>,

View File

@@ -541,6 +541,7 @@ fn collect_project_info_inner(root: &Path, profile: &str) -> Result<ProjectInfo,
platform: normalize_platform(&platform), platform: normalize_platform(&platform),
linker: resolved_linker.as_deref(), linker: resolved_linker.as_deref(),
archiver: resolved_archiver.as_deref(), archiver: resolved_archiver.as_deref(),
package_type: None,
source_roots: &source_roots, source_roots: &source_roots,
exclude_dirs: &combined_excludes, exclude_dirs: &combined_excludes,
include_paths: &combined_includes, include_paths: &combined_includes,

View File

@@ -8,4 +8,5 @@ pub mod help;
pub mod init; pub mod init;
pub mod new; pub mod new;
pub mod run; pub mod run;
pub mod setup;
pub mod test; pub mod test;

16
src/cli/setup.rs Normal file
View File

@@ -0,0 +1,16 @@
pub fn setup(_args: &[String]) -> i32 {
println!("Setting up DCR registries...");
match crate::core::registry::RegistryManager::load() {
Ok(manager) => {
println!("Loaded {} registries.", manager.config.registry.len());
for (name, reg) in manager.config.registry {
println!("- {}: {} (priority {})", name, reg.url, reg.priority);
}
0
}
Err(e) => {
println!("Error: {}", e);
1
}
}
}

View File

@@ -17,6 +17,7 @@ pub struct BuildContext<'a> {
pub platform: Option<&'a str>, pub platform: Option<&'a str>,
pub linker: Option<&'a str>, pub linker: Option<&'a str>,
pub archiver: Option<&'a str>, pub archiver: Option<&'a str>,
pub package_type: Option<&'a str>,
pub source_roots: &'a [std::path::PathBuf], pub source_roots: &'a [std::path::PathBuf],
pub exclude_dirs: &'a [std::path::PathBuf], pub exclude_dirs: &'a [std::path::PathBuf],
pub include_paths: &'a [String], pub include_paths: &'a [String],

View File

@@ -110,6 +110,16 @@ impl Config {
} }
} }
if let Some(pkg_type) = package.get("type").and_then(|v| v.as_str()) {
let pkg_type = pkg_type.trim();
if !pkg_type.is_empty() && pkg_type != "lib" && pkg_type != "app" && pkg_type != "none"
{
return Err(ConfigError::Invalid(
"package.type must be 'lib', 'app', or 'none'".into(),
));
}
}
let build = self let build = self
.get("build") .get("build")
.and_then(|v| v.as_table()) .and_then(|v| v.as_table())
@@ -398,6 +408,11 @@ fn default_toml() -> Result<Value, ConfigError> {
"version".to_string(), "version".to_string(),
Value::String(DEFAULT_VERSION.to_string()), Value::String(DEFAULT_VERSION.to_string()),
); );
package.insert("type".to_string(), Value::String("none".to_string()));
package.insert("description".to_string(), Value::String("".to_string()));
package.insert("author".to_string(), Value::String("".to_string()));
package.insert("license".to_string(), Value::String("MIT".to_string()));
let mut build = Map::new(); let mut build = Map::new();
build.insert( build.insert(
"language".to_string(), "language".to_string(),
@@ -458,7 +473,29 @@ fn format_toml(value: &Value) -> Result<String, ConfigError> {
let mut out = String::new(); let mut out = String::new();
out.push_str("[package]\n"); out.push_str("[package]\n");
out.push_str(&format!("name = \"{name}\"\n")); out.push_str(&format!("name = \"{name}\"\n"));
out.push_str(&format!("version = \"{version}\"\n\n")); out.push_str(&format!("version = \"{version}\"\n"));
for key in [
"type",
"description",
"author",
"authors",
"homepage",
"license",
"repository",
"readme",
"keywords",
"categories",
] {
if let Some(val) = package.get(key) {
match val {
Value::String(s) => out.push_str(&format!("{key} = \"{s}\"\n")),
Value::Array(_) => out.push_str(&format!("{key} = {}\n", format_string_array(val))),
_ => {}
}
}
}
out.push('\n');
out.push_str("[build]\n"); out.push_str("[build]\n");
if language.starts_with('[') { if language.starts_with('[') {
@@ -522,6 +559,9 @@ fn format_dep_value(value: &Value) -> String {
Value::String(s) => format!("\"{s}\""), Value::String(s) => format!("\"{s}\""),
Value::Table(tbl) => { Value::Table(tbl) => {
let mut parts = Vec::new(); let mut parts = Vec::new();
if let Some(v) = tbl.get("version").and_then(|v| v.as_str()) {
parts.push(format!("version = \"{v}\""));
}
if let Some(v) = tbl.get("path").and_then(|v| v.as_str()) { if let Some(v) = tbl.get("path").and_then(|v| v.as_str()) {
parts.push(format!("path = \"{v}\"")); parts.push(format!("path = \"{v}\""));
} }
@@ -537,18 +577,18 @@ fn format_dep_value(value: &Value) -> String {
if let Some(v) = tbl.get("rev").and_then(|v| v.as_str()) { if let Some(v) = tbl.get("rev").and_then(|v| v.as_str()) {
parts.push(format!("rev = \"{v}\"")); parts.push(format!("rev = \"{v}\""));
} }
if let Some(v) = tbl.get("default-features").and_then(|v| v.as_bool()) {
parts.push(format!(
"default-features = {}",
if v { "true" } else { "false" }
));
}
if let Some(v) = tbl.get("features") {
parts.push(format!("features = {}", format_string_array(v)));
}
if let Some(v) = tbl.get("system").and_then(|v| v.as_bool()) { if let Some(v) = tbl.get("system").and_then(|v| v.as_bool()) {
parts.push(format!("system = {}", if v { "true" } else { "false" })); parts.push(format!("system = {}", if v { "true" } else { "false" }));
} }
if let Some(v) = tbl.get("include") {
parts.push(format!("include = {}", format_string_array(v)));
}
if let Some(v) = tbl.get("lib") {
parts.push(format!("lib = {}", format_string_array(v)));
}
if let Some(v) = tbl.get("libs") {
parts.push(format!("libs = {}", format_string_array(v)));
}
format!("{{ {} }}", parts.join(", ")) format!("{{ {} }}", parts.join(", "))
} }
_ => "\"\"".to_string(), _ => "\"\"".to_string(),

View File

@@ -1,7 +1,9 @@
#[allow(dead_code)]
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum DepSource { pub enum DepSource {
Path(String), Path(String),
Git { Git {
@@ -10,8 +12,13 @@ pub enum DepSource {
tag: Option<String>, tag: Option<String>,
rev: Option<String>, rev: Option<String>,
}, },
Registry {
version: String,
features: Option<Vec<String>>,
},
} }
#[allow(dead_code)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DepSpec { pub struct DepSpec {
pub name: String, pub name: String,
@@ -21,13 +28,14 @@ pub struct DepSpec {
pub libs_raw: Option<Vec<String>>, pub libs_raw: Option<Vec<String>>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Default)]
pub struct ResolvedDeps { pub struct ResolvedDeps {
pub include_dirs: Vec<String>, pub include_dirs: Vec<String>,
pub lib_dirs: Vec<String>, pub lib_dirs: Vec<String>,
pub libs: Vec<String>, pub libs: Vec<String>,
} }
#[allow(dead_code)]
pub fn resolve_path(project_root: &Path, raw: &str) -> Result<PathBuf, String> { pub fn resolve_path(project_root: &Path, raw: &str) -> Result<PathBuf, String> {
let p = Path::new(raw); let p = Path::new(raw);
let full = if p.is_absolute() { let full = if p.is_absolute() {
@@ -38,6 +46,7 @@ pub fn resolve_path(project_root: &Path, raw: &str) -> Result<PathBuf, String> {
Ok(full) Ok(full)
} }
#[allow(dead_code)]
pub fn resolve_paths( pub fn resolve_paths(
base: &Path, base: &Path,
raw: Option<&[String]>, raw: Option<&[String]>,
@@ -71,10 +80,12 @@ pub fn resolve_paths(
Ok(out) Ok(out)
} }
#[allow(dead_code)]
pub fn expand_profile(raw: &str, profile: &str) -> String { pub fn expand_profile(raw: &str, profile: &str) -> String {
raw.replace("{profile}", profile) raw.replace("{profile}", profile)
} }
#[allow(dead_code)]
pub fn sync_dep_dir(src: &Path, dst: &Path) -> std::io::Result<()> { pub fn sync_dep_dir(src: &Path, dst: &Path) -> std::io::Result<()> {
if dst.exists() { if dst.exists() {
fs::remove_dir_all(dst)?; fs::remove_dir_all(dst)?;
@@ -82,6 +93,7 @@ pub fn sync_dep_dir(src: &Path, dst: &Path) -> std::io::Result<()> {
copy_dir_all(src, dst) copy_dir_all(src, dst)
} }
#[allow(dead_code)]
pub fn copy_dir_all(src: &Path, dst: &Path) -> std::io::Result<()> { pub fn copy_dir_all(src: &Path, dst: &Path) -> std::io::Result<()> {
fs::create_dir_all(dst)?; fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? { for entry in fs::read_dir(src)? {

View File

@@ -1,5 +1,6 @@
use std::path::Path; use std::path::Path;
#[allow(dead_code)]
pub fn fetch_git_dep( pub fn fetch_git_dep(
url: &str, url: &str,
target_dir: &Path, target_dir: &Path,
@@ -29,6 +30,7 @@ pub fn fetch_git_dep(
Ok(()) Ok(())
} }
#[allow(dead_code)]
fn checkout_ref(repo: &git2::Repository, reference_str: &str) -> Result<(), String> { fn checkout_ref(repo: &git2::Repository, reference_str: &str) -> Result<(), String> {
let (object, reference) = repo let (object, reference) = repo
.revparse_ext(reference_str) .revparse_ext(reference_str)

View File

@@ -18,18 +18,20 @@ pub fn write_lock(
packages: &[DepLock], packages: &[DepLock],
) -> Result<(), String> { ) -> Result<(), String> {
let mut out = String::new(); let mut out = String::new();
out.push_str("[[package]]\n");
out.push_str(&format!("name = \"{}\"\n", escape_value(project_name)));
out.push_str(&format!(
"version = \"{}\"\n",
escape_value(project_version)
));
if !packages.is_empty() { if !packages.is_empty() {
out.push_str("[[package]]\n");
out.push_str(&format!("name = \"{}\"\n", escape_value(project_name)));
out.push_str(&format!( out.push_str(&format!(
"version = \"{}\"\n", "dependencies = [{}]\n",
escape_value(project_version)
));
out.push_str(&format!(
"dependencies = [{}]\n\n",
quote_list(&packages.iter().map(|p| p.name.clone()).collect::<Vec<_>>()) quote_list(&packages.iter().map(|p| p.name.clone()).collect::<Vec<_>>())
)); ));
} }
out.push('\n');
for pkg in packages { for pkg in packages {
out.push_str("[[package]]\n"); out.push_str("[[package]]\n");
out.push_str(&format!("name = \"{}\"\n", escape_value(&pkg.name))); out.push_str(&format!("name = \"{}\"\n", escape_value(&pkg.name)));
@@ -55,6 +57,7 @@ fn escape_value(input: &str) -> String {
input.replace('\\', "\\\\").replace('"', "\\\"") input.replace('\\', "\\\\").replace('"', "\\\"")
} }
#[allow(dead_code)]
pub fn read_dep_version(dep_path: &Path) -> Option<String> { pub fn read_dep_version(dep_path: &Path) -> Option<String> {
let path = dep_path.join("dcr.toml"); let path = dep_path.join("dcr.toml");
let content = fs::read_to_string(path).ok()?; let content = fs::read_to_string(path).ok()?;
@@ -67,6 +70,7 @@ pub fn read_dep_version(dep_path: &Path) -> Option<String> {
.map(|s| s.to_string()) .map(|s| s.to_string())
} }
#[allow(dead_code)]
pub fn compute_checksum(root: &Path) -> Result<String, String> { pub fn compute_checksum(root: &Path) -> Result<String, String> {
let mut files = Vec::new(); let mut files = Vec::new();
collect_files(root, root, &mut files)?; collect_files(root, root, &mut files)?;
@@ -82,6 +86,7 @@ pub fn compute_checksum(root: &Path) -> Result<String, String> {
Ok(to_hex(&hash)) Ok(to_hex(&hash))
} }
#[allow(dead_code)]
fn collect_files(root: &Path, dir: &Path, out: &mut Vec<String>) -> Result<(), String> { fn collect_files(root: &Path, dir: &Path, out: &mut Vec<String>) -> Result<(), String> {
for entry in fs::read_dir(dir).map_err(|err| format!("read_dir failed: {err}"))? { for entry in fs::read_dir(dir).map_err(|err| format!("read_dir failed: {err}"))? {
let entry = entry.map_err(|err| format!("read_dir failed: {err}"))?; let entry = entry.map_err(|err| format!("read_dir failed: {err}"))?;
@@ -105,6 +110,7 @@ fn collect_files(root: &Path, dir: &Path, out: &mut Vec<String>) -> Result<(), S
Ok(()) Ok(())
} }
#[allow(dead_code)]
fn to_hex(bytes: &[u8]) -> String { fn to_hex(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len() * 2); let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes { for b in bytes {

View File

@@ -1,23 +1,19 @@
pub mod common; pub mod common;
pub mod git; pub mod git;
pub mod lock; pub mod lock;
pub mod register;
use crate::core::config::Config; use crate::core::config::Config;
use std::fs; use crate::core::deps::common::ResolvedDeps;
use std::path::Path; use std::path::Path;
use toml::Value;
pub use common::{DepSource, DepSpec, ResolvedDeps};
use common::{expand_profile, resolve_path, resolve_paths, sync_dep_dir};
use git::fetch_git_dep;
use lock::{DepLock, compute_checksum, read_dep_version, write_lock};
pub fn resolve_deps( pub fn resolve_deps(
config: &Config, config: &Config,
profile: &str, _profile: &str,
target: Option<&str>, _target: Option<&str>,
project_root: &Path, project_root: &Path,
) -> Result<ResolvedDeps, String> { ) -> Result<ResolvedDeps, String> {
let mut resolved = ResolvedDeps::default();
let project_name = config let project_name = config
.get("package.name") .get("package.name")
.and_then(|v| v.as_str()) .and_then(|v| v.as_str())
@@ -28,259 +24,70 @@ pub fn resolve_deps(
.and_then(|v| v.as_str()) .and_then(|v| v.as_str())
.unwrap_or("") .unwrap_or("")
.to_string(); .to_string();
let deps = parse_dependencies(config, profile, target)?;
if deps.is_empty() {
return Ok(ResolvedDeps {
include_dirs: Vec::new(),
lib_dirs: Vec::new(),
libs: Vec::new(),
});
}
let mut include_dirs = Vec::new(); let deps_table = config.get("dependencies").and_then(|v| v.as_table());
let mut lib_dirs = Vec::new(); let lock_packages = Vec::new();
let mut libs = Vec::new();
let mut lock_packages = Vec::new();
for dep in deps { if let Some(deps) = deps_table {
let (dep_path, is_git) = match &dep.source { for (name, value) in deps {
DepSource::Path(path_raw) => { if register::is_registry_dep(value) {
let expanded = expand_profile(path_raw, profile); let _pkg_info = register::resolve_package_from_registry(name)?;
(resolve_path(project_root, &expanded)?, false) // Используем путь относительно project_root или системный путь, если нужно
} let dep_root = project_root.join("dcr-index");
DepSource::Git {
url,
branch,
tag,
rev,
} => {
let git_deps_dir = project_root
.join("target")
.join(profile)
.join("deps")
.join("git");
fs::create_dir_all(&git_deps_dir)
.map_err(|e| format!("failed to create target/{}/deps/git: {}", profile, e))?;
let dep_dir = git_deps_dir.join(&dep.name);
if !dep_dir.exists() {
use crate::utils::text::{BOLD_GREEN, colored};
use std::io::{self, Write};
print!(
" {} dependency `{}` from {}...",
colored("Fetching", BOLD_GREEN),
dep.name,
url
);
io::stdout().flush().ok();
fetch_git_dep( resolved.include_dirs.push(
url, dep_root
&dep_dir, .join("target")
branch.as_deref(), .join("include")
tag.as_deref(), .to_string_lossy()
rev.as_deref(), .to_string(),
)?; );
resolved.lib_dirs.push(
print!( dep_root
"\r\x1B[2K {} dependency `{}`\n", .join("target")
colored("Fetched", BOLD_GREEN), .join("lib")
dep.name .to_string_lossy()
); .to_string(),
io::stdout().flush().ok(); );
} else { resolved.libs.push(name.clone());
fetch_git_dep( } else if let Some(table) = value.as_table()
url, && let Some(path) = table.get("path").and_then(|v| v.as_str())
&dep_dir, {
branch.as_deref(), let dep_root = project_root.join(path);
tag.as_deref(), if let Some(includes) = table.get("include").and_then(|v| v.as_array()) {
rev.as_deref(), for inc in includes {
)?; if let Some(inc_str) = inc.as_str() {
resolved
.include_dirs
.push(dep_root.join(inc_str).to_string_lossy().to_string());
}
}
}
let lib_path = dep_root.join("target").join("lib");
if lib_path.exists() {
resolved
.lib_dirs
.push(lib_path.to_string_lossy().to_string());
}
if let Some(libs) = table.get("libs").and_then(|v| v.as_array()) {
for lib in libs {
if let Some(lib_str) = lib.as_str() {
resolved.libs.push(lib_str.to_string());
}
}
} else {
resolved.libs.push(name.clone());
} }
(dep_dir, true)
} }
};
if !dep_path.is_dir() {
return Err(format!(
"Dependency '{}' path is not a directory: {}",
dep.name,
dep_path.display()
));
} }
let include = resolve_paths(&dep_path, dep.include_raw.as_deref(), &["include"], profile)?;
let lib = resolve_paths(
&dep_path,
dep.lib_raw.as_deref(),
&["lib", "lib64"],
profile,
)?;
let libs_list = dep
.libs_raw
.clone()
.unwrap_or_else(|| vec![dep.name.clone()]);
if include.is_empty() || lib.is_empty() {
return Err(format!(
"Dependency '{}' missing include/lib dirs. Add include/lib in dcr.toml.",
dep.name
));
}
include_dirs.extend(include.iter().map(|p| p.to_string_lossy().to_string()));
lib_dirs.extend(lib.iter().map(|p| p.to_string_lossy().to_string()));
libs.extend(libs_list.iter().cloned());
if !is_git {
let dep_cache = project_root
.join("target")
.join(profile)
.join("deps")
.join(&dep.name);
sync_dep_dir(&dep_path, &dep_cache)
.map_err(|err| format!("Failed to sync dep {}: {err}", dep.name))?;
}
let dep_version = read_dep_version(&dep_path).unwrap_or_else(|| "0.0.0".to_string());
let dep_checksum = compute_checksum(&dep_path)
.map_err(|err| format!("Failed to hash dep {}: {err}", dep.name))?;
let source_str = match &dep.source {
DepSource::Path(p) => format!("path+{}", p),
DepSource::Git { url, .. } => format!("git+{}", url),
};
lock_packages.push(DepLock {
name: dep.name.clone(),
version: dep_version,
checksum: dep_checksum,
source: source_str,
});
} }
write_lock( crate::core::deps::lock::write_lock(
project_root, project_root,
&project_name, &project_name,
&project_version, &project_version,
&lock_packages, &lock_packages,
)?; )?;
Ok(ResolvedDeps { Ok(resolved)
include_dirs,
lib_dirs,
libs,
})
}
fn parse_dependencies(
config: &Config,
profile: &str,
target: Option<&str>,
) -> Result<Vec<DepSpec>, String> {
let mut deps_table = None;
let combinations = if let Some(t) = target {
let normalized_t = crate::cli::build::normalize_target_os(t);
vec![
format!("dependencies.{}.{}", normalized_t, profile),
format!("dependencies.{}.{}", profile, normalized_t),
format!("dependencies.{}", normalized_t),
format!("dependencies.{}", profile),
"dependencies".to_string(),
]
} else {
vec![
format!("dependencies.{}", profile),
"dependencies".to_string(),
]
};
for key in combinations {
if let Some(val) = config.get(&key).and_then(|v| v.as_table()) {
deps_table = Some(val);
break;
}
}
let deps_table = match deps_table {
Some(t) => t,
None => return Ok(Vec::new()),
};
let mut deps = Vec::new();
for (name, value) in deps_table {
match value {
Value::String(path) => {
deps.push(DepSpec {
name: name.to_string(),
source: DepSource::Path(path.to_string()),
include_raw: None,
lib_raw: None,
libs_raw: None,
});
}
Value::Table(tbl) => {
if tbl.get("system").and_then(|v| v.as_bool()).unwrap_or(false) {
return Err(format!(
"Dependency '{}' uses system=true, which is not supported yet",
name
));
}
let source = if let Some(git_url) = tbl.get("git").and_then(|v| v.as_str()) {
DepSource::Git {
url: git_url.to_string(),
branch: tbl
.get("branch")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
tag: tbl
.get("tag")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
rev: tbl
.get("rev")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
}
} else if let Some(path) = tbl.get("path").and_then(|v| v.as_str()) {
DepSource::Path(path.to_string())
} else {
return Err(format!("Dependency '{}' missing path or git", name));
};
let include = parse_string_list(tbl.get("include"), name, "include")?;
let lib = parse_string_list(tbl.get("lib"), name, "lib")?;
let libs = parse_string_list(tbl.get("libs"), name, "libs")?;
deps.push(DepSpec {
name: name.to_string(),
source,
include_raw: include,
lib_raw: lib,
libs_raw: libs,
});
}
_ => {
return Err(format!("Dependency '{}' must be a string or a table", name));
}
}
}
deps.sort_by(|a, b| a.name.cmp(&b.name));
Ok(deps)
}
fn parse_string_list(
value: Option<&Value>,
dep_name: &str,
key: &str,
) -> Result<Option<Vec<String>>, String> {
let Some(value) = value else { return Ok(None) };
let list = value.as_array().ok_or_else(|| {
format!(
"Dependency '{}' field '{}' must be an array of strings",
dep_name, key
)
})?;
let mut out = Vec::new();
for item in list {
let s = item
.as_str()
.ok_or_else(|| format!("Dependency '{}' field '{}' must be strings", dep_name, key))?;
out.push(s.to_string());
}
Ok(Some(out))
} }

76
src/core/deps/register.rs Normal file
View File

@@ -0,0 +1,76 @@
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::fs;
use std::path::{Path, PathBuf};
use toml::Value as TomlValue;
#[derive(Debug, Serialize, Deserialize)]
pub struct Registry {
pub url: String,
pub priority: i32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DcrConfig {
pub registry: std::collections::HashMap<String, Registry>,
}
pub fn get_registry_config() -> Option<DcrConfig> {
let home = std::env::var("HOME").ok()?;
let config_path = Path::new(&home).join(".dcr/config.toml");
if !config_path.exists() {
return None;
}
let content = fs::read_to_string(config_path).ok()?;
toml::from_str(&content).ok()
}
pub fn get_index_path() -> PathBuf {
std::env::var("DCR_INDEX_PATH")
.map(PathBuf::from)
.unwrap_or_else(|_| {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
Path::new(&home).join(".dcr").join("index.json")
})
}
pub fn resolve_package_from_registry(name: &str) -> Result<JsonValue, String> {
let config = get_registry_config().ok_or("No registry config found")?;
let mut registries: Vec<(&String, &Registry)> = config.registry.iter().collect();
registries.sort_by(|a, b| b.1.priority.cmp(&a.1.priority));
for (_name_reg, _reg) in registries {
let index_path = get_index_path();
if index_path.exists() {
let index_content = fs::read_to_string(&index_path).map_err(|e| e.to_string())?;
let index: JsonValue =
serde_json::from_str(&index_content).map_err(|e| e.to_string())?;
if let Some(pkgs) = index.get("packages").and_then(|v| v.as_array()) {
for pkg in pkgs {
if pkg.get("name").and_then(|v| v.as_str()) == Some(name) {
return Ok(pkg.clone());
}
}
}
}
}
Err(format!(
"Package {} not found in registry (checked: {:?})",
name,
get_index_path()
))
}
pub fn is_registry_dep(value: &TomlValue) -> bool {
if value.is_str() {
return true;
}
if let Some(table) = value.as_table() {
return !table.contains_key("git")
&& !table.contains_key("path")
&& !table.contains_key("url")
&& !table.contains_key("version");
}
false
}

View File

@@ -1,5 +1,6 @@
pub mod builder; pub mod builder;
pub mod config; pub mod config;
pub mod deps; pub mod deps;
pub mod registry;
pub mod runner; pub mod runner;
pub mod workspace; pub mod workspace;

41
src/core/registry.rs Normal file
View File

@@ -0,0 +1,41 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize)]
pub struct Registry {
pub url: String,
pub priority: i32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub registry: HashMap<String, Registry>,
}
pub struct RegistryManager {
pub config: Config,
#[allow(dead_code)]
pub path: PathBuf,
}
impl RegistryManager {
pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
let home = std::env::var("HOME")?;
let dcr_dir = PathBuf::from(home).join(".dcr");
let config_path = dcr_dir.join("config.toml");
if !config_path.exists() {
return Err("Config file ~/.dcr/config.toml not found".into());
}
let content = fs::read_to_string(&config_path)?;
let config: Config = toml::from_str(&content)?;
Ok(Self {
config,
path: dcr_dir,
})
}
}

View File

@@ -1,44 +1,21 @@
use crate::platform; use crate::platform;
use crate::utils::fs::check_dir;
use crate::utils::log::warn;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
pub fn run_binary(project_name: &str, profile: &str, target_dir: Option<&str>) -> i32 { pub fn run_binary(project_name: &str, profile: &str, target_dir: Option<&str>) -> i32 {
let bin_path = platform::bin_path(profile, project_name, target_dir); let bin_path = platform::bin_path(profile, project_name, target_dir);
if Path::new(&bin_path).exists() { if Path::new(&bin_path).exists() {
let output = Command::new(&bin_path) let output = Command::new(&bin_path).output();
.output() match output {
.unwrap_or_else(|_| panic!("Failed to run {}", bin_path)); Ok(o) => {
print!("{}", String::from_utf8_lossy(&output.stdout)); print!("{}", String::from_utf8_lossy(&o.stdout));
eprint!("{}", String::from_utf8_lossy(&output.stderr)); eprint!("{}", String::from_utf8_lossy(&o.stderr));
return output.status.code().unwrap_or(1); return o.status.code().unwrap_or(0);
}
Err(_) => {
return 1;
}
}
} }
if target_dir.is_none()
&& check_dir(Some(&format!("target/{profile}")))
.unwrap_or_default()
.contains(&profile.to_string())
{
warn("Launch of the latest release");
let bin_path = platform::bin_path(profile, project_name, target_dir);
let output = Command::new(&bin_path)
.output()
.unwrap_or_else(|_| panic!("Failed to run {}", &bin_path));
print!("{}", String::from_utf8_lossy(&output.stdout));
eprint!("{}", String::from_utf8_lossy(&output.stderr));
return output.status.code().unwrap_or(1);
}
if target_dir.is_some() && Path::new(&bin_path).exists() {
warn("Launch of the latest release");
let output = Command::new(&bin_path)
.output()
.unwrap_or_else(|_| panic!("Failed to run {}", &bin_path));
print!("{}", String::from_utf8_lossy(&output.stdout));
eprint!("{}", String::from_utf8_lossy(&output.stderr));
return output.status.code().unwrap_or(1);
}
1 1
} }

View File

@@ -18,6 +18,7 @@ fn main() {
let code = match cmd { let code = match cmd {
"new" => cli::r#new::new(rest), "new" => cli::r#new::new(rest),
"init" => cli::init::init(rest), "init" => cli::init::init(rest),
"setup" => cli::setup::setup(rest),
"add" => cli::add::add(rest), "add" => cli::add::add(rest),
"build" => cli::build::build(rest), "build" => cli::build::build(rest),
"run" => cli::run::run(rest), "run" => cli::run::run(rest),

View File

@@ -1,7 +1,11 @@
pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String { pub fn bin_path(profile: &str, name: &str, target_dir: Option<&str>) -> String {
match target_dir { match target_dir {
Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name), Some(dir) => format!("{}/{}", dir.trim_end_matches('/'), name),
None => format!("./target/{profile}/{name}"), None => {
let arch = std::env::consts::ARCH;
let target = format!("{arch}-unknown-linux-gnu");
format!("./target/{target}/{profile}/{name}")
}
} }
} }
@@ -25,12 +29,16 @@ mod tests {
#[test] #[test]
fn bin_path_default() { fn bin_path_default() {
assert_eq!(bin_path("debug", "hello", None), "./target/debug/hello"); let arch = std::env::consts::ARCH;
let expected = format!("./target/{arch}-unknown-linux-gnu/debug/hello");
assert_eq!(bin_path("debug", "hello", None), expected);
} }
#[test] #[test]
fn bin_path_release() { fn bin_path_release() {
assert_eq!(bin_path("release", "hello", None), "./target/release/hello"); let arch = std::env::consts::ARCH;
let expected = format!("./target/{arch}-unknown-linux-gnu/release/hello");
assert_eq!(bin_path("release", "hello", None), expected);
} }
#[test] #[test]

View File

@@ -280,6 +280,42 @@ fn dcr_add_dependencies() {
assert!(!out.status.success(), "dcr add without prefix should fail"); assert!(!out.status.success(), "dcr add without prefix should fail");
} }
#[test]
fn dcr_builds_lib_package() {
let Some(compiler) = available_compiler() else {
eprintln!("no compiler found; skipping lib package test");
return;
};
let dir = unique_sandbox_dir("lib_package");
let out = run_dcr(&["init"], &dir);
assert!(out.status.success(), "dcr init should succeed");
let toml = std::fs::read_to_string(dir.join("dcr.toml")).unwrap();
let updated_toml = toml
.replace("kind = \"bin\"", "kind = \"staticlib\"")
.replace("type = \"none\"", "type = \"lib\"");
std::fs::write(dir.join("dcr.toml"), updated_toml).expect("failed to write toml");
std::fs::write(dir.join("src").join("my_lib.h"), "void hello();")
.expect("failed to write header");
let envs = [("DCR_COMPILER", compiler)];
let out = run_dcr_env(&["build"], &dir, &envs);
if !out.status.success() {
eprintln!("stdout: {}", String::from_utf8_lossy(&out.stdout));
eprintln!("stderr: {}", String::from_utf8_lossy(&out.stderr));
}
assert!(out.status.success(), "dcr build should succeed");
let target_dir = dir.join("target");
assert!(
target_dir.join("include").join("my_lib.h").is_file(),
"include/my_lib.h missing"
);
assert!(target_dir.join("lib").exists(), "lib directory missing");
}
#[test] #[test]
fn dcr_test_runs_without_sandbox_dependency() { fn dcr_test_runs_without_sandbox_dependency() {
let Some(compiler) = available_compiler() else { let Some(compiler) = available_compiler() else {