Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions generate-assets/src/bin/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ fn main() -> anyhow::Result<()> {

let asset_dir = std::env::args().nth(1).unwrap();
let content_dir = std::env::args().nth(2).unwrap();
let no_update = std::env::args().nth(3) == Some(String::from("--no-update"));

let db = prepare_crates_db()?;
let db = prepare_crates_db(!no_update)?;

let github_client = {
// This should be configured in CI, but it's not mandatory if running locally
Expand Down Expand Up @@ -61,6 +62,7 @@ fn main() -> anyhow::Result<()> {
/// Sort the assets in the section so that:
/// - Assets that have been manually assigned an order in `bevy-assets` are first
/// - Assets that are semver compatible with Bevy are next
/// - Assets that have more stars are first
/// - If all else is equal, sort randomly
fn sort_section(nodes: &mut [AssetNode], latest_bevy_version: &semver::Version) {
for node in nodes.iter_mut() {
Expand All @@ -73,18 +75,26 @@ fn sort_section(nodes: &mut [AssetNode], latest_bevy_version: &semver::Version)
for node in nodes {
let is_semver_compat = node_semver_compat_with(node, latest_bevy_version);

let existing_order = match node {
AssetNode::Asset(asset) => asset.order.unwrap_or(usize::MAX),
let (existing_order, stars) = match node {
AssetNode::Asset(asset) => {
(asset.order.unwrap_or(usize::MAX), asset.stars.unwrap_or(0))
}
_ => continue,
};

let random: u32 = rand::random();
to_sort.push((node, existing_order, !is_semver_compat, random));
to_sort.push((
node,
existing_order,
!is_semver_compat,
-(stars as i32),
random,
));
}

to_sort.sort_by_key(|sorts| (sorts.1, sorts.2, sorts.3));
to_sort.sort_by_key(|sorts| (sorts.1, sorts.2, sorts.3, sorts.4));

for (i, (node, _, _, _)) in to_sort.into_iter().enumerate() {
for (i, (node, ..)) in to_sort.into_iter().enumerate() {
let AssetNode::Asset(asset) = node else {
continue;
};
Expand Down Expand Up @@ -128,6 +138,7 @@ struct FrontMatterAssetExtra {
licenses: Option<Vec<String>>,
bevy_versions: Option<Vec<String>>,
nsfw: Option<bool>,
stars: Option<u32>,
}

impl From<&Asset> for FrontMatterAsset {
Expand All @@ -142,6 +153,7 @@ impl From<&Asset> for FrontMatterAsset {
licenses: asset.licenses.clone(),
bevy_versions: asset.bevy_versions.clone(),
nsfw: asset.nsfw,
stars: asset.stars,
},
}
}
Expand Down
22 changes: 22 additions & 0 deletions generate-assets/src/github_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ struct GithubLicenseResponse {
license: GithubLicenseLicense,
}

#[derive(Deserialize)]
struct GithubStarsResponse {
#[serde(rename = "stargazers_count")]
stars: u32,
}

#[derive(Deserialize)]
struct GithubLicenseLicense {
spdx_id: String,
Expand Down Expand Up @@ -96,6 +102,22 @@ impl GithubClient {
}
}

/// Gets the star count from a github repo link
///
/// Note that this method requests the whole repository info
/// If any other fields from it are needed, this method may be modified as to not make unnecessary requests
pub fn get_stars_from_url(&self, link: &str) -> anyhow::Result<u32> {
let response: GithubStarsResponse = self
.agent
.get(&format!("{BASE_URL}/repos/{link}"))
.set("Accept", "application/json")
.set("Authorization", &format!("Bearer {}", self.token))
.call()?
.into_json()?;

Ok(response.stars)
}

/// Search file by name
pub fn search_file(
&self,
Expand Down
22 changes: 22 additions & 0 deletions generate-assets/src/gitlab_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ struct GitlabContentResponse {
content: String,
}

#[derive(Deserialize)]
struct GitlabStarsResponse {
#[serde(rename = "star_count")]
stars: u32,
}

pub struct GitlabClient {
agent: ureq::Agent,
// This is not currently used because we have so few assets using gitlab that we don't need it.
Expand Down Expand Up @@ -76,4 +82,20 @@ impl GitlabClient {
bail!("Content is not in base64");
}
}

/// Gets the star count of a gitlab project from the url
///
/// Note that this requests the whole project info
/// So if any more fields from it are needed, this method may be modified as to not make unnecessary requests
pub fn get_stars_from_url(&self, url: &str) -> anyhow::Result<u32> {
let response: GitlabStarsResponse = self
.agent
.get(&format!("{BASE_URL}/{url}"))
.set("Accept", "application/json")
// .set("Authorization", &format!("Bearer {}", self.token))
.call()?
.into_json()?;

Ok(response.stars)
}
}
Loading