Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(crates/tuono): tuono new - add --latest flag to download latest template #292

Closed
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
7 changes: 6 additions & 1 deletion crates/tuono/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ enum Actions {
/// examples
#[arg(short, long)]
template: Option<String>,

/// Install the latest version of tuono
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Install the latest version of tuono
/// Load the main branch templates

#[arg(short, long)]
latest: bool,
},
}

Expand Down Expand Up @@ -167,8 +171,9 @@ pub fn app() -> std::io::Result<()> {
Actions::New {
folder_name,
template,
latest,
} => {
scaffold_project::create_new_project(folder_name, template);
scaffold_project::create_new_project(folder_name, template, latest);
}
}

Expand Down
49 changes: 36 additions & 13 deletions crates/tuono/src/scaffold_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const GITHUB_TUONO_TAGS_URL: &str = "https://api.github.com/repos/tuono-labs/tuo
const GITHUB_TUONO_TAG_COMMIT_TREES_URL: &str =
"https://api.github.com/repos/tuono-labs/tuono/git/trees/";

const GITHUB_TUONO_LATEST_TREE_URL: &str =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need this constant? Couldn't we just do GITHUB_TUONO_TAG_COMMIT_TREES_URL + main?recursive=1?

"https://api.github.com/repos/tuono-labs/tuono/git/trees/main?recursive=1";

const GITHUB_RAW_CONTENT_URL: &str = "https://raw.githubusercontent.com/tuono-labs/tuono";

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -50,7 +53,11 @@ fn create_file(path: PathBuf, content: String) -> std::io::Result<()> {
Ok(())
}

pub fn create_new_project(folder_name: Option<String>, template: Option<String>) {
pub fn create_new_project(
folder_name: Option<String>,
template: Option<String>,
flag_latest: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flag_latest: bool,
select_main_branch_template: bool,

) {
let folder = folder_name.unwrap_or(".".to_string());

// In case of missing select the tuono example
Expand All @@ -64,23 +71,34 @@ pub fn create_new_project(folder_name: Option<String>, template: Option<String>)
// This string does not include the "v" version prefix
let cli_version: &str = crate_version!();

let res_tag = client
.get(format!("{}v{}", GITHUB_TUONO_TAGS_URL, cli_version))
.send()
.unwrap_or_else(|_| panic!("Failed to call the tag github API for v{cli_version}"))
.json::<GithubTagResponse>()
.expect("Failed to parse the tag response");
let tree_url: String = if flag_latest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about turning this if statement into a standalone function? I think this would improve the code readability

GITHUB_TUONO_LATEST_TREE_URL.to_string()
} else {
// This string does not include the "v" version prefix
let res_tag = client
.get(format!("{}v{}", GITHUB_TUONO_TAGS_URL, cli_version))
.send()
.unwrap_or_else(|_| panic!("Failed to call the tag github API for v{cli_version}"))
.json::<GithubTagResponse>()
.expect("Failed to parse the tag response");

let sha_tagged_commit = res_tag.object.sha;
let sha_tagged_commit = res_tag.object.sha;

let res_tree = client
.get(format!(
format!(
"{}{}?recursive=1",
GITHUB_TUONO_TAG_COMMIT_TREES_URL, sha_tagged_commit
))
)
};

let res_tree = client
.get(tree_url)
.send()
.unwrap_or_else(|_| {
panic!("Failed to call the tagged commit tree github API for v{cli_version}")
if flag_latest {
panic!("Failed to call the tagged commit tree github API for v{cli_version}");
} else {
panic!("Failed to call the tagged commit tree github API for latest version");
}
})
.json::<GithubTreeResponse<GithubFile>>()
.expect("Failed to parse the tree structure");
Expand Down Expand Up @@ -119,8 +137,13 @@ pub fn create_new_project(folder_name: Option<String>, template: Option<String>)
} in new_project_files.iter()
{
if let GithubFileType::Blob = element_type {
let raw_content_url = if flag_latest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually {GITHUB_RAW_CONTENT_URL}/{path} and {GITHUB_RAW_CONTENT_URL}/main/{path} have the same output.
In order to ease the if statement why don't we just do something like:

let tag = if flag_latest { "main" } else { format!("v{cli_version}") };
let url = format!("{GITHUB_RAW_CONTENT_URL}/{tag/{path}")

format!("{GITHUB_RAW_CONTENT_URL}/v{cli_version}/{path}")
} else {
format!("{GITHUB_RAW_CONTENT_URL}/{path}")
};
let file_content = client
.get(format!("{GITHUB_RAW_CONTENT_URL}/v{cli_version}/{path}"))
.get(raw_content_url)
.send()
.expect("Failed to call the folder github API")
.text()
Expand Down
Loading