Skip to content

Commit

Permalink
cli: tauri new --latest
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitypowerful committed Jan 8, 2025
1 parent c75d83d commit d2505c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
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
#[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 =
"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,
) {
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 {
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 {
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

0 comments on commit d2505c4

Please sign in to comment.