-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Updates local dependency, opens editor (#25)
- Loading branch information
Showing
8 changed files
with
362 additions
and
141 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::constants::GITHUB_REPO_URL; | ||
|
||
pub fn update_dependency_if_exists(cargo_toml: &mut String) -> anyhow::Result<()> { | ||
let mut updated_content = cargo_toml | ||
.lines() | ||
.map(|line| { | ||
if line.contains("syntest") { | ||
format!("syntest = {{ git = \"{}\" }}", GITHUB_REPO_URL) | ||
} else { | ||
line.to_string() | ||
} | ||
}) | ||
.collect::<Vec<String>>() | ||
.join("\n"); | ||
|
||
updated_content.push_str("\n"); | ||
|
||
*cargo_toml = updated_content; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_update_dependency_if_exists() { | ||
let mut cargo_toml = r#" | ||
[package] | ||
name = "rustfinity" | ||
version = "0.1.0" | ||
[dependencies] | ||
syntest = "1.0" | ||
"# | ||
.to_string(); | ||
|
||
// update existing dependency | ||
update_dependency_if_exists(&mut cargo_toml).unwrap(); | ||
|
||
let expected = r#" | ||
[package] | ||
name = "rustfinity" | ||
version = "0.1.0" | ||
[dependencies] | ||
syntest = { git = "https://www.github.com/dcodesdev/rustfinity.com" } | ||
"#; | ||
|
||
assert_eq!(cargo_toml, expected); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub const GITHUB_CHALLENGES_BASE_URL: &'static str = | ||
"https://raw.githubusercontent.com/dcodesdev/rustfinity.com/main/challenges"; | ||
|
||
pub const GITHUB_REPO_URL: &'static str = "https://www.github.com/dcodesdev/rustfinity.com"; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::process::Command; | ||
|
||
/// Editors the user might be using | ||
#[derive(Debug, PartialEq)] | ||
pub enum Editor { | ||
VSCode, | ||
Zed, | ||
} | ||
|
||
impl Editor { | ||
pub fn find() -> Option<Self> { | ||
if Command::new("code").arg("--version").output().is_ok() { | ||
return Some(Editor::VSCode); | ||
} | ||
|
||
if Command::new("zed").arg("--version").output().is_ok() { | ||
return Some(Editor::Zed); | ||
} | ||
|
||
None | ||
} | ||
|
||
pub fn open(&self, path: &str) { | ||
Command::new(self.command()).arg(path).spawn().unwrap(); | ||
} | ||
|
||
fn command(&self) -> &str { | ||
match self { | ||
Editor::VSCode => "code", | ||
Editor::Zed => "zed", | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_find_editor() { | ||
let editor = Editor::find(); | ||
assert_eq!(editor, None); | ||
} | ||
} |
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