Skip to content

Commit 8026e4e

Browse files
committed
add rust to cli
1 parent a6598b8 commit 8026e4e

5 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/somesy/cli/init.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def config():
8484
if mkdocs_file := typer.prompt("mkdocs.yml file path", default="mkdocs.yml"):
8585
options["mkdocs_file"] = mkdocs_file
8686

87+
options["no_sync_rust"] = not typer.confirm(
88+
"Do you want to sync to a Cargo.toml file?", default=True
89+
)
90+
if rust_file := typer.prompt("Cargo.toml file path", default="Cargo.toml"):
91+
options["rust_file"] = rust_file
92+
8793
# ----
8894

8995
options["show_info"] = typer.confirm(

src/somesy/cli/sync.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ def sync(
107107
help="Custom mkdocs.yml file path (default: mkdocs.yml)",
108108
**existing_file_arg_config,
109109
),
110+
no_sync_rust: bool = typer.Option(
111+
None,
112+
"--no-sync-rust",
113+
"-R",
114+
help="Do not sync Cargo.toml file (default: False)",
115+
),
116+
rust_file: Path = typer.Option(
117+
None,
118+
"--rust-file",
119+
"-r",
120+
help="Custom Cargo.toml file path (default: Cargo.toml)",
121+
**existing_file_arg_config,
122+
),
110123
no_sync_cff: bool = typer.Option(
111124
None,
112125
"--no-sync-cff",
@@ -153,6 +166,8 @@ def sync(
153166
pom_xml_file=pom_xml_file,
154167
no_sync_mkdocs=no_sync_mkdocs,
155168
mkdocs_file=mkdocs_file,
169+
no_sync_rust=no_sync_rust,
170+
rust_file=rust_file,
156171
)
157172
run_sync(somesy_input)
158173

@@ -186,6 +201,8 @@ def run_sync(somesy_input: SomesyInput):
186201
logger.info(
187202
f" - [italic]mkdocs.yml[/italic]:\t[grey]{conf.mkdocs_file}[/grey]"
188203
)
204+
if not conf.no_sync_rust:
205+
logger.info(f" - [italic]Cargo.toml[/italic]:\t[grey]{conf.rust_file}[/grey]")
189206

190207
if not conf.no_sync_cff:
191208
logger.info(f" - [italic]CITATION.cff[/italic]:\t[grey]{conf.cff_file}[/grey]")

src/somesy/commands/sync.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from somesy.package_json.writer import PackageJSON
1616
from somesy.pom_xml.writer import POM
1717
from somesy.pyproject.writer import Pyproject
18+
from somesy.rust import Rust
1819

1920
logger = logging.getLogger("somesy")
2021

@@ -58,6 +59,9 @@ def sync(somesy_input: SomesyInput):
5859
if conf.mkdocs_file.is_file() and not conf.no_sync_mkdocs:
5960
_sync_file(metadata, conf.mkdocs_file, MkDocs)
6061

62+
if conf.rust_file.is_file() and not conf.no_sync_rust:
63+
_sync_file(metadata, conf.rust_file, Rust)
64+
6165
# create these by default if they are missing:
6266
if not conf.no_sync_cff:
6367
_sync_file(metadata, conf.cff_file, CFF)

src/somesy/core/core.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"package.json",
1616
"Project.toml",
1717
"fpm.toml",
18+
"Cargo.toml",
1819
]
1920
"""Input files ordered by priority for discovery."""
2021

@@ -78,7 +79,7 @@ def get_input_content(path: Path, *, no_unwrap: bool = False) -> Dict[str, Any]:
7879
ret = tomlkit.load(f)
7980
return ret if no_unwrap else ret.unwrap()
8081

81-
# pyproject.toml
82+
# pyproject.toml or fpm.toml
8283
if (path.suffix == ".toml" and "pyproject" in path.name) or path.name in [
8384
"Project.toml",
8485
"fpm.toml",
@@ -92,6 +93,22 @@ def get_input_content(path: Path, *, no_unwrap: bool = False) -> Dict[str, Any]:
9293
"No tool.somesy section found in pyproject.toml file!"
9394
)
9495

96+
# Cargo.toml
97+
if path.name == "Cargo.toml":
98+
with open(path, "r") as f:
99+
input_content = tomlkit.load(f)
100+
if (
101+
"package" in input_content
102+
and "metadata" in input_content["package"]
103+
and "somesy" in input_content["package"]["metadata"]
104+
):
105+
return input_content["package"]["metadata"]["somesy"].unwrap()
106+
else:
107+
raise RuntimeError(
108+
"No package.somesy section found in Cargo.toml file!"
109+
)
110+
111+
# package.json
95112
if path.suffix == ".json" and "package" in path.name:
96113
with open(path, "r") as f:
97114
input_content = json.load(f)

src/somesy/core/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def model_dump_json(self, *args, **kwargs):
129129
"fortran",
130130
"pom_xml",
131131
"mkdocs",
132+
"rust",
132133
]
133134

134135

@@ -209,6 +210,13 @@ def at_least_one_target(cls, values):
209210
"mkdocs.yml"
210211
)
211212

213+
no_sync_rust: Annotated[
214+
bool, Field(description="Do not sync with Cargo.toml.")
215+
] = False
216+
rust_file: Annotated[Path, Field(description="Cargo.toml file path.")] = Path(
217+
"Cargo.toml"
218+
)
219+
212220
no_sync_cff: Annotated[bool, Field(description="Do not sync with CFF.")] = False
213221
cff_file: Annotated[Path, Field(description="CFF file path.")] = Path(
214222
"CITATION.cff"

0 commit comments

Comments
 (0)