Skip to content

Commit 7ef1c54

Browse files
authored
feat: copy files into crate_name directory (#31)
1 parent e343fa4 commit 7ef1c54

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2025 Open SASS
3+
Copyright (c) 2025 Open SASS Core Maintainers
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ cargo build --release
6060

6161
<video src="https://github.com/user-attachments/assets/add61376-cdf6-4a46-9d3b-067bc4d0c4bd"></video>
6262

63-
### Add an OpenSASS component to your project
63+
### Yew Usage
6464

6565
```sh
6666
os add i18nrs yew
@@ -70,23 +70,36 @@ This will:
7070

7171
- Download the `i18nrs` crate.
7272
- Extract files only related to the `yew` feature.
73-
- Copy `src/` files into your project.
74-
- Update your `Cargo.toml` dependencies and feature flags.
73+
- Copy `src/` files into your project under a new directory `crate_name`, in this case `i18nrs`.
74+
- Update your `Cargo.toml` dependencies and `lib.rs` file.
75+
76+
### Dioxus Usage
77+
78+
```sh
79+
os add i18nrs dio
80+
```
81+
82+
### Leptos Usage
83+
84+
```sh
85+
os add i18nrs lep
86+
```
7587

7688
## 🧃 Components
7789

7890
Open SASS offers the following components:
7991

8092
| 🧩 Component | 📦 GitHub Repository | 📝 Description |
8193
|------------------|--------------------------|---------------------|
82-
| `accordion` | [![GitHub](https://img.shields.io/github/stars/opensass/accordion-rs)](https://github.com/opensass/accordion-rs) | ↕️ A highly customizable accordion component for WASM frameworks. |
83-
| `alert` | [![GitHub](https://img.shields.io/github/stars/opensass/alert-rs)](https://github.com/opensass/alert-rs) | ⚠️ A highly customizable alert component for WASM frameworks. |
94+
| `accordion-rs` | [![GitHub](https://img.shields.io/github/stars/opensass/accordion-rs)](https://github.com/opensass/accordion-rs) | ↕️ A highly customizable accordion component for WASM frameworks. |
95+
| `alert-rs` | [![GitHub](https://img.shields.io/github/stars/opensass/alert-rs)](https://github.com/opensass/alert-rs) | ⚠️ A highly customizable alert component for WASM frameworks. |
8496
| `eld` | [![GitHub](https://img.shields.io/github/stars/opensass/eld)](https://github.com/opensass/eld) | 🚛 ELD Toolkit for WASM frameworks. |
85-
| `i18n` | [![GitHub](https://img.shields.io/github/stars/opensass/i18n-rs)](https://github.com/opensass/i18n-rs) | 🌐 Internationalization (i18n) component for WASM frameworks. |
86-
| `input` | [![GitHub](https://img.shields.io/github/stars/opensass/input-rs)](https://github.com/opensass/input-rs) | 🔤 A highly customizable input component for WASM frameworks. |
87-
| `radio` | [![GitHub](https://img.shields.io/github/stars/opensass/radio-rs)](https://github.com/opensass/radio-rs) | 🎛️ A highly customizable radio buttons component for WASM frameworks. |
88-
| `scroll` | [![GitHub](https://img.shields.io/github/stars/opensass/scroll-rs)](https://github.com/opensass/scroll-rs) | 🖱️ A highly customizable scroll-to-anywhere component for WASM frameworks. |
89-
| `select` | [![GitHub](https://img.shields.io/github/stars/opensass/select-rs)](https://github.com/opensass/select-rs) | 🔽 A highly customizable select group component for WASM frameworks. |
97+
| `i18nrs` | [![GitHub](https://img.shields.io/github/stars/opensass/i18n-rs)](https://github.com/opensass/i18n-rs) | 🌐 Internationalization (i18n) component for WASM frameworks. |
98+
| `image-rs` | [![GitHub](https://img.shields.io/github/stars/opensass/image-rs)](https://github.com/opensass/image-rs) | 🖼️ Image Component for WASM frameworks. |
99+
| `input-rs` | [![GitHub](https://img.shields.io/github/stars/opensass/input-rs)](https://github.com/opensass/input-rs) | 🔤 A highly customizable input component for WASM frameworks. |
100+
| `radiors` | [![GitHub](https://img.shields.io/github/stars/opensass/radio-rs)](https://github.com/opensass/radio-rs) | 🎛️ A highly customizable radio buttons component for WASM frameworks. |
101+
| `scroll-rs` | [![GitHub](https://img.shields.io/github/stars/opensass/scroll-rs)](https://github.com/opensass/scroll-rs) | 🖱️ A highly customizable scroll-to-anywhere component for WASM frameworks. |
102+
| `select-rs` | [![GitHub](https://img.shields.io/github/stars/opensass/select-rs)](https://github.com/opensass/select-rs) | 🔽 A highly customizable select group component for WASM frameworks. |
90103

91104
And much more coming over time...
92105

src/cmds/add.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::fs::{copy_relevant_files, update_cargo_toml, update_lib_rs};
1+
use crate::utils::fs::{copy_relevant_files, update_cargo_toml, update_pub_file};
22
use crates_io_api::SyncClient;
33
use flate2::read::GzDecoder;
44
use indicatif::{ProgressBar, ProgressStyle};
@@ -52,10 +52,13 @@ pub fn run_add(crate_name: &str, feature: &str) -> anyhow::Result<()> {
5252
let current_project_src = Path::new("src");
5353

5454
spinner.set_message("📁 Copying relevant source files...");
55-
let copied_files = copy_relevant_files(&source_path, current_project_src, feature)?;
55+
let _ = copy_relevant_files(&source_path, current_project_src, crate_name, feature)?;
5656

5757
spinner.set_message("🧩 Updating lib.rs...");
58-
update_lib_rs(current_project_src.join("lib.rs"), &copied_files)?;
58+
update_pub_file(
59+
current_project_src.join("lib.rs"),
60+
&[crate_name.to_string()],
61+
)?;
5962

6063
spinner.set_message("🛠️ Updating Cargo.toml...");
6164
update_cargo_toml(

src/utils/fs.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::{Context, Result};
12
use std::{
23
fs,
34
path::{Path, PathBuf},
@@ -8,42 +9,59 @@ use walkdir::WalkDir;
89
pub fn copy_relevant_files(
910
src_dir: &Path,
1011
dest_dir: &Path,
12+
crate_name: &str,
1113
feature: &str,
12-
) -> anyhow::Result<Vec<String>> {
14+
) -> Result<Vec<String>> {
1315
let mut copied = Vec::new();
16+
1417
let feature_file = match feature {
1518
"dio" => "dioxus",
1619
"lep" => "leptos",
1720
_ => feature,
1821
};
1922

23+
let crate_dir = dest_dir.join(crate_name);
24+
fs::create_dir_all(&crate_dir)
25+
.with_context(|| format!("Failed to create crate directory at {:?}", crate_dir))?;
26+
2027
for entry in WalkDir::new(src_dir).into_iter().flatten() {
2128
let path = entry.path();
2229
if path.is_file() {
2330
let file_name = path.file_name().unwrap().to_string_lossy();
24-
if [
31+
32+
let should_copy = [
2533
"common.rs",
2634
"config.rs",
27-
&format!("{feature_file}.rs"),
2835
"countries.rs",
2936
"chart.rs",
37+
&format!("{feature_file}.rs"),
3038
]
31-
.contains(&file_name.as_ref())
32-
{
33-
let dest_path = dest_dir.join(file_name.to_string());
34-
fs::copy(path, &dest_path)?;
35-
copied.push(file_name.to_string());
39+
.contains(&file_name.as_ref());
40+
41+
if should_copy {
42+
let dest_file_name = if file_name == format!("{feature_file}.rs") {
43+
format!("{crate_name}.rs")
44+
} else {
45+
file_name.to_string()
46+
};
47+
48+
let dest_path = crate_dir.join(&dest_file_name);
49+
50+
fs::copy(path, &dest_path)
51+
.with_context(|| format!("Failed to copy {:?} to {:?}", path, dest_path))?;
52+
53+
copied.push(dest_file_name.trim_end_matches(".rs").to_string());
3654
}
3755
}
3856
}
3957

40-
Ok(copied
41-
.into_iter()
42-
.map(|f| f.trim_end_matches(".rs").to_string())
43-
.collect())
58+
let crate_file = dest_dir.join(format!("{crate_name}.rs"));
59+
update_pub_file(crate_file, &copied)?;
60+
61+
Ok(copied)
4462
}
4563

46-
pub fn update_lib_rs(lib_path: PathBuf, modules: &[String]) -> anyhow::Result<()> {
64+
pub fn update_pub_file(lib_path: PathBuf, modules: &[String]) -> Result<()> {
4765
let mut content = if lib_path.exists() {
4866
fs::read_to_string(&lib_path)?
4967
} else {
@@ -61,7 +79,7 @@ pub fn update_lib_rs(lib_path: PathBuf, modules: &[String]) -> anyhow::Result<()
6179
Ok(())
6280
}
6381

64-
pub fn update_cargo_toml(from: PathBuf, to: &Path, _feature: &str) -> anyhow::Result<()> {
82+
pub fn update_cargo_toml(from: PathBuf, to: &Path, _feature: &str) -> Result<()> {
6583
let source_content = fs::read_to_string(&from)?;
6684
let from_doc: DocumentMut = source_content.parse()?;
6785

0 commit comments

Comments
 (0)