Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Dec 18, 2024
1 parent 6171394 commit b30bd23
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 158 deletions.
106 changes: 43 additions & 63 deletions cli/Source/bin/code/legacy_args.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// ---------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// --------------------------------------------------------------------------------------------

use std::collections::HashMap;

use cli::commands::args::{
CliCore, Commands, DesktopCodeOptions, ExtensionArgs, ExtensionSubcommand,
InstallExtensionArgs, ListExtensionArgs, UninstallExtensionArgs,
CliCore,
Commands,
DesktopCodeOptions,
ExtensionArgs,
ExtensionSubcommand,
InstallExtensionArgs,
ListExtensionArgs,
UninstallExtensionArgs,
};

/// Tries to parse the argv using the legacy CLI interface, looking for its
/// flags and generating a CLI with subcommands if those don't exist.
pub fn try_parse_legacy(
iter: impl IntoIterator<Item = impl Into<std::ffi::OsString>>,
iter:impl IntoIterator<Item = impl Into<std::ffi::OsString>>,
) -> Option<CliCore> {
let raw = clap_lex::RawArgs::new(iter);

Expand All @@ -22,7 +29,7 @@ pub fn try_parse_legacy(
raw.next(&mut cursor); // Skip the bin

// First make a hashmap of all flags and capture positional arguments.
let mut args: HashMap<String, Vec<String>> = HashMap::new();
let mut args:HashMap<String, Vec<String>> = HashMap::new();

let mut last_arg = None;

Expand All @@ -36,15 +43,15 @@ pub fn try_parse_legacy(
if let Some(v) = value {
prev.push(v.to_string_lossy().to_string());
}
}
},

None => {
if let Some(v) = value {
args.insert(long.to_string(), vec![v.to_string_lossy().to_string()]);
} else {
args.insert(long.to_string(), vec![]);
}
}
},
}
}
} else if let Ok(value) = arg.to_value() {
Expand All @@ -61,12 +68,12 @@ pub fn try_parse_legacy(
}

let get_first_arg_value =
|key: &str| args.get(key).and_then(|v| v.first()).map(|s| s.to_string());
|key:&str| args.get(key).and_then(|v| v.first()).map(|s| s.to_string());

let desktop_code_options = DesktopCodeOptions {
extensions_dir: get_first_arg_value("extensions-dir"),
user_data_dir: get_first_arg_value("user-data-dir"),
use_version: None,
extensions_dir:get_first_arg_value("extensions-dir"),
user_data_dir:get_first_arg_value("user-data-dir"),
use_version:None,
};

// Now translate them to subcommands.
Expand All @@ -78,50 +85,47 @@ pub fn try_parse_legacy(

if args.contains_key("list-extensions") {
Some(CliCore {
subcommand: Some(Commands::Extension(ExtensionArgs {
subcommand: ExtensionSubcommand::List(ListExtensionArgs {
category: get_first_arg_value("category"),
show_versions: args.contains_key("show-versions"),
subcommand:Some(Commands::Extension(ExtensionArgs {
subcommand:ExtensionSubcommand::List(ListExtensionArgs {
category:get_first_arg_value("category"),
show_versions:args.contains_key("show-versions"),
}),
desktop_code_options,
})),
..Default::default()
})
} else if let Some(exts) = args.remove("install-extension") {
Some(CliCore {
subcommand: Some(Commands::Extension(ExtensionArgs {
subcommand: ExtensionSubcommand::Install(InstallExtensionArgs {
id_or_path: exts,
pre_release: args.contains_key("pre-release"),
donot_include_pack_and_dependencies: args
subcommand:Some(Commands::Extension(ExtensionArgs {
subcommand:ExtensionSubcommand::Install(InstallExtensionArgs {
id_or_path:exts,
pre_release:args.contains_key("pre-release"),
donot_include_pack_and_dependencies:args
.contains_key("do-not-include-pack-dependencies"),
force: args.contains_key("force"),
force:args.contains_key("force"),
}),
desktop_code_options,
})),
..Default::default()
})
} else if let Some(_exts) = args.remove("update-extensions") {
Some(CliCore {
subcommand: Some(Commands::Extension(ExtensionArgs {
subcommand: ExtensionSubcommand::Update,
subcommand:Some(Commands::Extension(ExtensionArgs {
subcommand:ExtensionSubcommand::Update,
desktop_code_options,
})),
..Default::default()
})
} else if let Some(exts) = args.remove("uninstall-extension") {
Some(CliCore {
subcommand: Some(Commands::Extension(ExtensionArgs {
subcommand: ExtensionSubcommand::Uninstall(UninstallExtensionArgs { id: exts }),
subcommand:Some(Commands::Extension(ExtensionArgs {
subcommand:ExtensionSubcommand::Uninstall(UninstallExtensionArgs { id:exts }),
desktop_code_options,
})),
..Default::default()
})
} else if args.contains_key("status") {
Some(CliCore {
subcommand: Some(Commands::Status),
..Default::default()
})
Some(CliCore { subcommand:Some(Commands::Status), ..Default::default() })
} else {
None
}
Expand All @@ -133,13 +137,7 @@ mod tests {

#[test]
fn test_parses_list_extensions() {
let args = vec![
"code",
"--list-extensions",
"--category",
"themes",
"--show-versions",
];
let args = vec!["code", "--list-extensions", "--category", "themes", "--show-versions"];

let cli = try_parse_legacy(args).unwrap();

Expand All @@ -149,10 +147,7 @@ mod tests {

assert!(list_args.show_versions);
} else {
panic!(
"Expected list subcommand, got {:?}",
extension_args.subcommand
);
panic!("Expected list subcommand, got {:?}", extension_args.subcommand);
}
} else {
panic!("Expected extension subcommand, got {:?}", cli.subcommand);
Expand Down Expand Up @@ -183,10 +178,7 @@ mod tests {

assert!(install_args.force);
} else {
panic!(
"Expected install subcommand, got {:?}",
extension_args.subcommand
);
panic!("Expected install subcommand, got {:?}", extension_args.subcommand);
}
} else {
panic!("Expected extension subcommand, got {:?}", cli.subcommand);
Expand All @@ -203,10 +195,7 @@ mod tests {
if let ExtensionSubcommand::Uninstall(uninstall_args) = extension_args.subcommand {
assert_eq!(uninstall_args.id, vec!["connor4312.codesong"]);
} else {
panic!(
"Expected uninstall subcommand, got {:?}",
extension_args.subcommand
);
panic!("Expected uninstall subcommand, got {:?}", extension_args.subcommand);
}
} else {
panic!("Expected extension subcommand, got {:?}", cli.subcommand);
Expand All @@ -228,23 +217,14 @@ mod tests {
let cli = try_parse_legacy(args).unwrap();

if let Some(Commands::Extension(extension_args)) = cli.subcommand {
assert_eq!(
extension_args.desktop_code_options.user_data_dir,
Some("foo".to_string())
);
assert_eq!(extension_args.desktop_code_options.user_data_dir, Some("foo".to_string()));

assert_eq!(
extension_args.desktop_code_options.extensions_dir,
Some("bar".to_string())
);
assert_eq!(extension_args.desktop_code_options.extensions_dir, Some("bar".to_string()));

if let ExtensionSubcommand::Uninstall(uninstall_args) = extension_args.subcommand {
assert_eq!(uninstall_args.id, vec!["connor4312.codesong"]);
} else {
panic!(
"Expected uninstall subcommand, got {:?}",
extension_args.subcommand
);
panic!("Expected uninstall subcommand, got {:?}", extension_args.subcommand);
}
} else {
panic!("Expected extension subcommand, got {:?}", cli.subcommand);
Expand Down
Loading

0 comments on commit b30bd23

Please sign in to comment.