Last active
March 18, 2023 17:50
-
-
Save fivethreeo/739bc7403f9147e4168acee69d92672d to your computer and use it in GitHub Desktop.
Get rust deps with all features from use statements (code by chatgpt)
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
cat << EOF | python3 | |
import re | |
import requests | |
import json | |
# Regular expression to match Rust use statements | |
regex = r"use ([^;]+);" | |
# Rust source code as a multiline string | |
source_code = ''' | |
use serde::{Deserialize, Serialize}; | |
use actix_web::{web, App, HttpServer, Responder}; | |
use dotenv::dotenv; | |
use std::env; | |
''' | |
# Extract module names from use statements | |
matches = re.findall(regex, source_code) | |
module_names = [match.split("::")[0] for match in matches] | |
# Replace underscores with hyphens in module names | |
for i in range(len(module_names)): | |
module_names[i] = module_names[i].replace("_", "-") | |
# Remove duplicates from module names | |
unique_module_names = list(set(module_names)) | |
# Create dependencies dictionary | |
dependencies = {} | |
for module_name in unique_module_names: | |
# Send GET request to crates.io API endpoint | |
api_url = f"https://crates.io/api/v1/crates/{module_name}" | |
response = requests.get(api_url) | |
# Check if request was successful | |
if response.status_code != 200: | |
print(f"Error: Failed to retrieve package info from {api_url}") | |
continue | |
# Parse JSON response and extract latest version and all features | |
json_response = response.json() | |
latest_version = json_response["crate"]["newest_version"] | |
all_features = json_response["versions"][0]["features"].keys() | |
# Add package name, latest version, and all features to dependencies dictionary | |
dependencies[module_name] = {"version": latest_version, "features": list(all_features)} | |
# Generate TOML string | |
toml = "[dependencies]\n" | |
for package_name, package_info in dependencies.items(): | |
version = package_info["version"] | |
features = package_info["features"] | |
toml += f"{package_name} = {{ version = \\"{version}\\", features = {features} }}\\n" | |
# Print TOML string to console | |
print(toml) | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment