Last active
April 14, 2022 11:54
-
-
Save epage/7cf001dbf50b87d3479dce31a7fa0215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import pathlib | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--members", metavar="COUNT", type=int, default=1000) | |
parser.add_argument("--nest", metavar="COUNT", type=int, default=1) | |
parser.add_argument("--inherit", action="store_true") | |
parser.add_argument("--target", type=pathlib.Path, default=".") | |
args = parser.parse_args() | |
workspace_root = args.target | |
package_root = workspace_root / "crates" | |
members = [] | |
for index in range(args.members): | |
name = f"package_{index}" | |
current_root = package_root / name | |
current_root.mkdir(parents=True, exist_ok=True) | |
current_manifest_path = current_root / "Cargo.toml" | |
current_manifest_path.write_text(package_manifest(name, args.inherit)) | |
current_source = current_root / "src/lib.rs" | |
current_source.parent.mkdir(parents=True, exist_ok=True) | |
current_source.write_text("") | |
members.append(str(current_root.relative_to(workspace_root))) | |
if index % args.nest == 0: | |
package_root = workspace_root / "crates" | |
else: | |
package_root = current_root | |
workspace_path = workspace_root / "Cargo.toml" | |
workspace_path.write_text(workspace_manifest(members, args.inherit)) | |
def workspace_manifest(members, inherit): | |
members = '",\n "'.join(members) | |
if inherit: | |
return f""" | |
[workspace] | |
members = [ | |
"{members}" | |
] | |
version = "1.0.0" | |
authors = ["Nice Folks"] | |
description = "..." | |
documentation = "https://example.github.io/example" | |
readme = "README.md" | |
homepage = "https://example.com" | |
repository = "https://github.com/example/example" | |
license = "MIT" | |
keywords = ["cli"] | |
categories = ["development-tools"] | |
publish = false | |
edition = "2018" | |
""" | |
else: | |
return f""" | |
[workspace] | |
members = [ | |
"{members}" | |
] | |
""" | |
def package_manifest(name, inherit): | |
if inherit: | |
return f""" | |
[package] | |
name = "{name}" | |
version.workspace = true | |
authors.workspace = true | |
description.workspace = true | |
documentation.workspace = true | |
readme.workspace = true | |
homepage.workspace = true | |
repository.workspace = true | |
license.workspace = true | |
keywords.workspace = true | |
categories.workspace = true | |
publish.workspace = true | |
edition.workspace = true | |
""" | |
else: | |
return f""" | |
[package] | |
name = "{name}" | |
version = "1.0.0" | |
authors = ["Nice Folks"] | |
description = "..." | |
documentation = "https://example.github.io/example" | |
readme = "README.md" | |
homepage = "https://example.com" | |
repository = "https://github.com/example/example" | |
license = "MIT" | |
keywords = ["cli"] | |
categories = ["development-tools"] | |
publish = false | |
edition = "2018" | |
""" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment